PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/system/application/libraries/extid/EasyOpenID.php

https://github.com/folkevil/ExtID
PHP | 450 lines | 251 code | 33 blank | 166 comment | 24 complexity | 226aefe9b048980f75c6bc20b781571c MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter EasyOpenID Class
  18. *
  19. * This class enables the easy use of OpenID authentication with auto-access
  20. * to several OpenID servers as well as a general OpenID URI entry ability.
  21. *
  22. * @package CodeIgniter
  23. * @subpackage EasyOpenID
  24. * @category Libraries
  25. * @author James Brumond
  26. * @link http://code.kbjrweb.com/project/easyopenid
  27. */
  28. require_once dirname(__FILE__).'/OpenID.php';
  29. /**
  30. * A class for abstracting seperate OpenID requests
  31. *
  32. * @class Eoid
  33. */
  34. class Eoid {
  35. public $provider = null;
  36. public $provider_data = null;
  37. public $return_to = null;
  38. public $required = array( );
  39. public $optional = array( );
  40. public $policies = array(
  41. PAPE_AUTH_MULTI_FACTOR_PHYSICAL => 0,
  42. PAPE_AUTH_MULTI_FACTOR => 0,
  43. PAPE_AUTH_PHISHING_RESISTANT => 0
  44. );
  45. }
  46. /**
  47. * The simplified active-use version of the OpenID class
  48. *
  49. * @class EasyOpenID
  50. * @parent OpenID
  51. */
  52. class EasyOpenID extends OpenID {
  53. protected $eoid = null;
  54. protected $data = false;
  55. /**
  56. * Constructor
  57. *
  58. * @access public
  59. * @return void
  60. */
  61. public function __construct()
  62. {
  63. parent::__construct();
  64. $this->eoid = new Eoid();
  65. }
  66. /**
  67. * Reset all active-use data (replace the Eoid
  68. * with a new one)
  69. *
  70. * @access public
  71. * @return self
  72. */
  73. public function reset()
  74. {
  75. $this->eoid = new Eoid();
  76. return $this;
  77. }
  78. /**
  79. * Set a PAPE policy as on or off
  80. *
  81. * @access public
  82. * @param int the policy to set
  83. * @param bool turn it on (TRUE) or off (FALSE)
  84. * @return self
  85. */
  86. public function set_policy($policy, $flag = true)
  87. {
  88. $flag = ($flag) ? 1 : 0;
  89. if (array_key_exists($policy, $this->eoid->policies))
  90. {
  91. $this->eoid->policies[$policy] = $flag;
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Set the multi-factor PAPE policy as on or off
  97. *
  98. * @access public
  99. * @param bool turn it on (TRUE) or off (FALSE)
  100. * @return self
  101. */
  102. public function multi_factor($flag = true)
  103. {
  104. $this->set_policy(PAPE_AUTH_MULTI_FACTOR, $flag);
  105. return $this;
  106. }
  107. /**
  108. * Set the multi-factor physical PAPE policy as on or off
  109. *
  110. * @access public
  111. * @param bool turn it on (TRUE) or off (FALSE)
  112. * @return self
  113. */
  114. public function multi_factor_physical($flag = true)
  115. {
  116. $this->set_policy(PAPE_AUTH_MULTI_FACTOR_PHYSICAL, $flag);
  117. return $this;
  118. }
  119. /**
  120. * Set the phishing resistant PAPE policy as on or off
  121. *
  122. * @access public
  123. * @param bool turn it on (TRUE) or off (FALSE)
  124. * @return self
  125. */
  126. public function phishing_resistant($flag = true)
  127. {
  128. $this->set_policy(PAPE_AUTH_PHISHING_RESISTANT, $flag);
  129. return $this;
  130. }
  131. /**
  132. * Set the OpenID provider
  133. *
  134. * @access public
  135. * @param string the provider to use
  136. * @param string any secondary data needed (currently
  137. * only used by the blogger function).
  138. * @return self
  139. */
  140. public function provider($provider, $provider_data = null)
  141. {
  142. if (is_string($provider))
  143. $this->eoid->provider = $provider;
  144. if (is_string($provider_data))
  145. $this->eoid->provider_data = $provider_data;
  146. return $this;
  147. }
  148. /**
  149. * Set the route to return to after authentication
  150. * (the "finish auth" page)
  151. *
  152. * @access public
  153. * @param string the route to redirect to
  154. * @return self
  155. */
  156. public function return_to($route)
  157. {
  158. if (is_string($route))
  159. $this->eoid->return_to = $route;
  160. return $this;
  161. }
  162. /**
  163. * Value or values requested as "required" from the provider
  164. *
  165. * @access public
  166. * @param mixed string value or array of values
  167. * @return self
  168. */
  169. public function required($value)
  170. {
  171. if (is_string($value))
  172. $this->eoid->required[] = $value;
  173. if (is_array($value))
  174. $this->eoid->required = array_merge($value, $this->eoid->required);
  175. $this->eoid->required = array_unique($this->eoid->required);
  176. return $this;
  177. }
  178. /**
  179. * Value or values requested as "optional" from the provider
  180. *
  181. * @access public
  182. * @param mixed string value or array of values
  183. * @return self
  184. */
  185. public function optional($value)
  186. {
  187. if (is_string($value))
  188. $this->eoid->optional[] = $value;
  189. if (is_array($value))
  190. $this->eoid->optional = array_merge($value, $this->eoid->optional);
  191. $this->eoid->optional = array_unique($this->eoid->optional);
  192. return $this;
  193. }
  194. protected $types = array(
  195. 'google' => 'ax',
  196. 'yahoo' => 'ax',
  197. 'myspace' => 'sreg',
  198. 'blogger' => 'sreg',
  199. 'aol' => 'sreg'
  200. );
  201. /**
  202. * Parse SREG parameters into proper format
  203. *
  204. * @access protected
  205. * @param array values to parse
  206. * @param &array where to put them when we're finished
  207. * @return self
  208. */
  209. protected function parse_sreg($from, &$to)
  210. {
  211. foreach ($from as $field)
  212. {
  213. switch ($field)
  214. {
  215. case 'fullname':
  216. case 'name':
  217. $to[] = 'fullname';
  218. break;
  219. case 'nickname':
  220. case 'username':
  221. $to[] = 'nickname';
  222. break;
  223. case 'email':
  224. $to[] = 'email';
  225. break;
  226. }
  227. }
  228. $to = array_unique($to);
  229. }
  230. /**
  231. * Parse AX parameters into proper format
  232. *
  233. * @access protected
  234. * @param array values to parse
  235. * @param &array where to put them when we're finished
  236. * @return void
  237. */
  238. protected function parse_ax($from, &$to)
  239. {
  240. foreach ($from as $field)
  241. {
  242. switch ($field)
  243. {
  244. case 'fname':
  245. case 'firstname':
  246. $to[] = 'fname';
  247. break;
  248. case 'lname':
  249. case 'lastname':
  250. $to[] = 'lname';
  251. break;
  252. case 'fullname':
  253. case 'name':
  254. $to[] = 'fname';
  255. $to[] = 'lname';
  256. break;
  257. case 'nickname':
  258. case 'username':
  259. $to[] = 'nickname';
  260. break;
  261. case 'email':
  262. $to[] = 'email';
  263. break;
  264. }
  265. }
  266. $to = array_unique($to);
  267. }
  268. /**
  269. * Parse all parameters for a request, SREG or AX
  270. *
  271. * @access protected
  272. * @param string "sreg" or "ax"
  273. * @return array
  274. */
  275. protected function parse_request($type)
  276. {
  277. $required = $optional = array( );
  278. if ($type == 'sreg')
  279. {
  280. $this->parse_sreg($this->eoid->required, $required);
  281. $this->parse_sreg($this->eoid->optional, $optional);
  282. }
  283. else
  284. {
  285. $this->parse_ax($this->eoid->required, $required);
  286. $this->parse_ax($this->eoid->optional, $optional);
  287. }
  288. return array($required, $optional);
  289. }
  290. /**
  291. * Parse the stored policies into a usable list
  292. *
  293. * @access protected
  294. * @return array
  295. */
  296. protected function parse_policies()
  297. {
  298. $policies = array( );
  299. foreach ($this->eoid->policies as $policy => $flag)
  300. {
  301. if ($flag) $policies[] = $policy;
  302. }
  303. return $policies;
  304. }
  305. /**
  306. * Make the request
  307. *
  308. * Note: Upon success, this function will redirect the page.
  309. *
  310. * @access public
  311. * @param string if not using a built-in provider, is it SREG or AX
  312. * @return int
  313. */
  314. public function make_request($type = 'sreg')
  315. {
  316. if (isset($this->types[$this->eoid->provider]))
  317. {
  318. $type = $this->types[$this->eoid->provider];
  319. }
  320. if (is_string($this->eoid->provider) && is_string($this->eoid->return_to))
  321. {
  322. $return = $this->eoid->return_to;
  323. $policies = $this->parse_policies();
  324. list($required, $optional) = $this->parse_request($type);
  325. switch ($this->eoid->provider)
  326. {
  327. case 'google':
  328. $result = $this->try_auth_google($return, $policies, $required, $optional);
  329. break;
  330. case 'yahoo':
  331. $result = $this->try_auth_yahoo($return, $policies, $required, $optional);
  332. break;
  333. case 'myspace':
  334. $result = $this->try_auth_myspace($return, $policies, $required, $optional);
  335. break;
  336. case 'blogger':
  337. $result = $this->try_auth_blogger($this->eoid->provider_data, $return, $policies, $required, $optional);
  338. break;
  339. case 'aol':
  340. $result = $this->try_auth_aol($return, $policies, $required, $optional);
  341. break;
  342. default:
  343. if (strtolower($type) == 'sreg')
  344. {
  345. $result = $this->try_auth_sreg($this->eoid->provider, $return, $policies, $required, $optional);
  346. }
  347. elseif (strtolower($type) == 'ax')
  348. {
  349. $result = $this->try_auth_ax($this->eoid->provider, $return, $policies, $required, $optional);
  350. }
  351. else
  352. {
  353. return $this->_throw_error('Invalid provider type "'.$type.'"');
  354. }
  355. break;
  356. }
  357. }
  358. else
  359. {
  360. return $this->_throw_error(OPENID_RETURN_NO_URL);
  361. }
  362. return $this->_throw_error($result);
  363. }
  364. /**
  365. * Fetch and store the OpenID provider response
  366. *
  367. * Note: when using the popup mode, this function will close the window.
  368. *
  369. * @access public
  370. * @return self
  371. */
  372. public function fetch_response()
  373. {
  374. $resp = $this->finish_auth();
  375. if (! is_int($resp))
  376. {
  377. $this->ci->session->set_userdata('_openid_data', $resp);
  378. $this->data = $resp;
  379. }
  380. else
  381. {
  382. $this->_error = $this->error_msg($resp);
  383. }
  384. return $this;
  385. }
  386. /**
  387. * Get the user data.
  388. *
  389. * @access public
  390. * @return mixed
  391. */
  392. public function result()
  393. {
  394. // get the result from the session
  395. $result = $this->ci->session->userdata('_openid_data');
  396. $this->ci->session->unset_userdata('_openid_data');
  397. $result = ($result !== false) ? $result : $this->data;
  398. if (is_array($result))
  399. {
  400. // fetch data out of nested structures
  401. foreach ($result as $i => $j)
  402. {
  403. if (is_array($j))
  404. {
  405. $result[$i] = ((array_key_exists(0, $j)) ? $j[0] : null);
  406. }
  407. }
  408. }
  409. else
  410. {
  411. return false;
  412. }
  413. return $result;
  414. }
  415. }
  416. /* End of file EasyOpenID.php */
  417. /* Location: ./system/libraries/EasyOpenID.php */