OpenId.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Auth
  17. * @subpackage Zend_Auth_Adapter
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Auth_Adapter_Interface
  24. */
  25. require_once 'Zend/Auth/Adapter/Interface.php';
  26. /**
  27. * @see Zend_OpenId_Consumer
  28. */
  29. require_once 'Zend/OpenId/Consumer.php';
  30. /**
  31. * A Zend_Auth Authentication Adapter allowing the use of OpenID protocol as an
  32. * authentication mechanism
  33. *
  34. * @category Zend
  35. * @package Zend_Auth
  36. * @subpackage Zend_Auth_Adapter
  37. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Auth_Adapter_OpenId implements Zend_Auth_Adapter_Interface
  41. {
  42. /**
  43. * The identity value being authenticated
  44. *
  45. * @var string
  46. */
  47. private $_id = null;
  48. /**
  49. * Reference to an implementation of a storage object
  50. *
  51. * @var Zend_OpenId_Consumer_Storage
  52. */
  53. private $_storage = null;
  54. /**
  55. * The URL to redirect response from server to
  56. *
  57. * @var string
  58. */
  59. private $_returnTo = null;
  60. /**
  61. * The HTTP URL to identify consumer on server
  62. *
  63. * @var string
  64. */
  65. private $_root = null;
  66. /**
  67. * Extension object or array of extensions objects
  68. *
  69. * @var string
  70. */
  71. private $_extensions = null;
  72. /**
  73. * The response object to perform HTTP or HTML form redirection
  74. *
  75. * @var Zend_Controller_Response_Abstract
  76. */
  77. private $_response = null;
  78. /**
  79. * Enables or disables interaction with user during authentication on
  80. * OpenID provider.
  81. *
  82. * @var bool
  83. */
  84. private $_check_immediate = false;
  85. /**
  86. * HTTP client to make HTTP requests
  87. *
  88. * @var Zend_Http_Client $_httpClient
  89. */
  90. private $_httpClient = null;
  91. /**
  92. * Constructor
  93. *
  94. * @param string $id the identity value
  95. * @param Zend_OpenId_Consumer_Storage $storage an optional implementation
  96. * of a storage object
  97. * @param string $returnTo HTTP URL to redirect response from server to
  98. * @param string $root HTTP URL to identify consumer on server
  99. * @param mixed $extensions extension object or array of extensions objects
  100. * @param Zend_Controller_Response_Abstract $response an optional response
  101. * object to perform HTTP or HTML form redirection
  102. */
  103. public function __construct($id = null,
  104. Zend_OpenId_Consumer_Storage $storage = null,
  105. $returnTo = null,
  106. $root = null,
  107. $extensions = null,
  108. Zend_Controller_Response_Abstract $response = null) {
  109. $this->_id = $id;
  110. $this->_storage = $storage;
  111. $this->_returnTo = $returnTo;
  112. $this->_root = $root;
  113. $this->_extensions = $extensions;
  114. $this->_response = $response;
  115. }
  116. /**
  117. * Sets the value to be used as the identity
  118. *
  119. * @param string $id the identity value
  120. * @return Zend_Auth_Adapter_OpenId Provides a fluent interface
  121. */
  122. public function setIdentity($id)
  123. {
  124. $this->_id = $id;
  125. return $this;
  126. }
  127. /**
  128. * Sets the storage implementation which will be use by OpenId
  129. *
  130. * @param Zend_OpenId_Consumer_Storage $storage
  131. * @return Zend_Auth_Adapter_OpenId Provides a fluent interface
  132. */
  133. public function setStorage(Zend_OpenId_Consumer_Storage $storage)
  134. {
  135. $this->_storage = $storage;
  136. return $this;
  137. }
  138. /**
  139. * Sets the HTTP URL to redirect response from server to
  140. *
  141. * @param string $returnTo
  142. * @return Zend_Auth_Adapter_OpenId Provides a fluent interface
  143. */
  144. public function setReturnTo($returnTo)
  145. {
  146. $this->_returnTo = $returnTo;
  147. return $this;
  148. }
  149. /**
  150. * Sets HTTP URL to identify consumer on server
  151. *
  152. * @param string $root
  153. * @return Zend_Auth_Adapter_OpenId Provides a fluent interface
  154. */
  155. public function setRoot($root)
  156. {
  157. $this->_root = $root;
  158. return $this;
  159. }
  160. /**
  161. * Sets OpenID extension(s)
  162. *
  163. * @param mixed $extensions
  164. * @return Zend_Auth_Adapter_OpenId Provides a fluent interface
  165. */
  166. public function setExtensions($extensions)
  167. {
  168. $this->_extensions = $extensions;
  169. return $this;
  170. }
  171. /**
  172. * Sets an optional response object to perform HTTP or HTML form redirection
  173. *
  174. * @param string $response
  175. * @return Zend_Auth_Adapter_OpenId Provides a fluent interface
  176. */
  177. public function setResponse($response)
  178. {
  179. $this->_response = $response;
  180. return $this;
  181. }
  182. /**
  183. * Enables or disables interaction with user during authentication on
  184. * OpenID provider.
  185. *
  186. * @param bool $check_immediate
  187. * @return Zend_Auth_Adapter_OpenId Provides a fluent interface
  188. */
  189. public function setCheckImmediate($check_immediate)
  190. {
  191. $this->_check_immediate = $check_immediate;
  192. return $this;
  193. }
  194. /**
  195. * Sets HTTP client object to make HTTP requests
  196. *
  197. * @param Zend_Http_Client $client HTTP client object to be used
  198. */
  199. public function setHttpClient($client) {
  200. $this->_httpClient = $client;
  201. }
  202. /**
  203. * Authenticates the given OpenId identity.
  204. * Defined by Zend_Auth_Adapter_Interface.
  205. *
  206. * @throws Zend_Auth_Adapter_Exception If answering the authentication query is impossible
  207. * @return Zend_Auth_Result
  208. */
  209. public function authenticate() {
  210. $id = $this->_id;
  211. if (!empty($id)) {
  212. $consumer = new Zend_OpenId_Consumer($this->_storage);
  213. $consumer->setHttpClient($this->_httpClient);
  214. /* login() is never returns on success */
  215. if (!$this->_check_immediate) {
  216. if (!$consumer->login($id,
  217. $this->_returnTo,
  218. $this->_root,
  219. $this->_extensions,
  220. $this->_response)) {
  221. return new Zend_Auth_Result(
  222. Zend_Auth_Result::FAILURE,
  223. $id,
  224. array("Authentication failed", $consumer->getError()));
  225. }
  226. } else {
  227. if (!$consumer->check($id,
  228. $this->_returnTo,
  229. $this->_root,
  230. $this->_extensions,
  231. $this->_response)) {
  232. return new Zend_Auth_Result(
  233. Zend_Auth_Result::FAILURE,
  234. $id,
  235. array("Authentication failed", $consumer->getError()));
  236. }
  237. }
  238. } else {
  239. $params = (isset($_SERVER['REQUEST_METHOD']) &&
  240. $_SERVER['REQUEST_METHOD']=='POST') ? $_POST: $_GET;
  241. $consumer = new Zend_OpenId_Consumer($this->_storage);
  242. $consumer->setHttpClient($this->_httpClient);
  243. if ($consumer->verify(
  244. $params,
  245. $id,
  246. $this->_extensions)) {
  247. return new Zend_Auth_Result(
  248. Zend_Auth_Result::SUCCESS,
  249. $id,
  250. array("Authentication successful"));
  251. } else {
  252. return new Zend_Auth_Result(
  253. Zend_Auth_Result::FAILURE,
  254. $id,
  255. array("Authentication failed", $consumer->getError()));
  256. }
  257. }
  258. }
  259. }