File indexing completed on 2024-12-22 05:36:28
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Auth 0017 * @subpackage Zend_Auth_Adapter 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 0024 /** 0025 * @see Zend_Auth_Adapter_Interface 0026 */ 0027 // require_once 'Zend/Auth/Adapter/Interface.php'; 0028 0029 0030 /** 0031 * @see Zend_OpenId_Consumer 0032 */ 0033 // require_once 'Zend/OpenId/Consumer.php'; 0034 0035 0036 /** 0037 * A Zend_Auth Authentication Adapter allowing the use of OpenID protocol as an 0038 * authentication mechanism 0039 * 0040 * @category Zend 0041 * @package Zend_Auth 0042 * @subpackage Zend_Auth_Adapter 0043 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0044 * @license http://framework.zend.com/license/new-bsd New BSD License 0045 */ 0046 class Zend_Auth_Adapter_OpenId implements Zend_Auth_Adapter_Interface 0047 { 0048 /** 0049 * The identity value being authenticated 0050 * 0051 * @var string 0052 */ 0053 private $_id = null; 0054 0055 /** 0056 * Reference to an implementation of a storage object 0057 * 0058 * @var Zend_OpenId_Consumer_Storage 0059 */ 0060 private $_storage = null; 0061 0062 /** 0063 * The URL to redirect response from server to 0064 * 0065 * @var string 0066 */ 0067 private $_returnTo = null; 0068 0069 /** 0070 * The HTTP URL to identify consumer on server 0071 * 0072 * @var string 0073 */ 0074 private $_root = null; 0075 0076 /** 0077 * Extension object or array of extensions objects 0078 * 0079 * @var string 0080 */ 0081 private $_extensions = null; 0082 0083 /** 0084 * The response object to perform HTTP or HTML form redirection 0085 * 0086 * @var Zend_Controller_Response_Abstract 0087 */ 0088 private $_response = null; 0089 0090 /** 0091 * Enables or disables interaction with user during authentication on 0092 * OpenID provider. 0093 * 0094 * @var bool 0095 */ 0096 private $_check_immediate = false; 0097 0098 /** 0099 * HTTP client to make HTTP requests 0100 * 0101 * @var Zend_Http_Client $_httpClient 0102 */ 0103 private $_httpClient = null; 0104 0105 /** 0106 * Constructor 0107 * 0108 * @param string $id the identity value 0109 * @param Zend_OpenId_Consumer_Storage $storage an optional implementation 0110 * of a storage object 0111 * @param string $returnTo HTTP URL to redirect response from server to 0112 * @param string $root HTTP URL to identify consumer on server 0113 * @param mixed $extensions extension object or array of extensions objects 0114 * @param Zend_Controller_Response_Abstract $response an optional response 0115 * object to perform HTTP or HTML form redirection 0116 */ 0117 public function __construct($id = null, 0118 Zend_OpenId_Consumer_Storage $storage = null, 0119 $returnTo = null, 0120 $root = null, 0121 $extensions = null, 0122 Zend_Controller_Response_Abstract $response = null) { 0123 $this->_id = $id; 0124 $this->_storage = $storage; 0125 $this->_returnTo = $returnTo; 0126 $this->_root = $root; 0127 $this->_extensions = $extensions; 0128 $this->_response = $response; 0129 } 0130 0131 /** 0132 * Sets the value to be used as the identity 0133 * 0134 * @param string $id the identity value 0135 * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 0136 */ 0137 public function setIdentity($id) 0138 { 0139 $this->_id = $id; 0140 return $this; 0141 } 0142 0143 /** 0144 * Sets the storage implementation which will be use by OpenId 0145 * 0146 * @param Zend_OpenId_Consumer_Storage $storage 0147 * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 0148 */ 0149 public function setStorage(Zend_OpenId_Consumer_Storage $storage) 0150 { 0151 $this->_storage = $storage; 0152 return $this; 0153 } 0154 0155 /** 0156 * Sets the HTTP URL to redirect response from server to 0157 * 0158 * @param string $returnTo 0159 * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 0160 */ 0161 public function setReturnTo($returnTo) 0162 { 0163 $this->_returnTo = $returnTo; 0164 return $this; 0165 } 0166 0167 /** 0168 * Sets HTTP URL to identify consumer on server 0169 * 0170 * @param string $root 0171 * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 0172 */ 0173 public function setRoot($root) 0174 { 0175 $this->_root = $root; 0176 return $this; 0177 } 0178 0179 /** 0180 * Sets OpenID extension(s) 0181 * 0182 * @param mixed $extensions 0183 * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 0184 */ 0185 public function setExtensions($extensions) 0186 { 0187 $this->_extensions = $extensions; 0188 return $this; 0189 } 0190 0191 /** 0192 * Sets an optional response object to perform HTTP or HTML form redirection 0193 * 0194 * @param string $response 0195 * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 0196 */ 0197 public function setResponse($response) 0198 { 0199 $this->_response = $response; 0200 return $this; 0201 } 0202 0203 /** 0204 * Enables or disables interaction with user during authentication on 0205 * OpenID provider. 0206 * 0207 * @param bool $check_immediate 0208 * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 0209 */ 0210 public function setCheckImmediate($check_immediate) 0211 { 0212 $this->_check_immediate = $check_immediate; 0213 return $this; 0214 } 0215 0216 /** 0217 * Sets HTTP client object to make HTTP requests 0218 * 0219 * @param Zend_Http_Client $client HTTP client object to be used 0220 */ 0221 public function setHttpClient($client) { 0222 $this->_httpClient = $client; 0223 } 0224 0225 /** 0226 * Authenticates the given OpenId identity. 0227 * Defined by Zend_Auth_Adapter_Interface. 0228 * 0229 * @throws Zend_Auth_Adapter_Exception If answering the authentication query is impossible 0230 * @return Zend_Auth_Result 0231 */ 0232 public function authenticate() { 0233 $id = $this->_id; 0234 if (!empty($id)) { 0235 $consumer = new Zend_OpenId_Consumer($this->_storage); 0236 $consumer->setHttpClient($this->_httpClient); 0237 /* login() is never returns on success */ 0238 if (!$this->_check_immediate) { 0239 if (!$consumer->login($id, 0240 $this->_returnTo, 0241 $this->_root, 0242 $this->_extensions, 0243 $this->_response)) { 0244 return new Zend_Auth_Result( 0245 Zend_Auth_Result::FAILURE, 0246 $id, 0247 array("Authentication failed", $consumer->getError())); 0248 } 0249 } else { 0250 if (!$consumer->check($id, 0251 $this->_returnTo, 0252 $this->_root, 0253 $this->_extensions, 0254 $this->_response)) { 0255 return new Zend_Auth_Result( 0256 Zend_Auth_Result::FAILURE, 0257 $id, 0258 array("Authentication failed", $consumer->getError())); 0259 } 0260 } 0261 } else { 0262 $params = (isset($_SERVER['REQUEST_METHOD']) && 0263 $_SERVER['REQUEST_METHOD']=='POST') ? $_POST: $_GET; 0264 $consumer = new Zend_OpenId_Consumer($this->_storage); 0265 $consumer->setHttpClient($this->_httpClient); 0266 if ($consumer->verify( 0267 $params, 0268 $id, 0269 $this->_extensions)) { 0270 return new Zend_Auth_Result( 0271 Zend_Auth_Result::SUCCESS, 0272 $id, 0273 array("Authentication successful")); 0274 } else { 0275 return new Zend_Auth_Result( 0276 Zend_Auth_Result::FAILURE, 0277 $id, 0278 array("Authentication failed", $consumer->getError())); 0279 } 0280 } 0281 } 0282 0283 }