File indexing completed on 2025-01-26 05:29:43
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_Oauth 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** Zend_Oauth */ 0023 // require_once 'Zend/Oauth.php'; 0024 0025 /** Zend_Uri */ 0026 // require_once 'Zend/Uri.php'; 0027 0028 /** Zend_Oauth_Config_Interface */ 0029 // require_once 'Zend/Oauth/Config/ConfigInterface.php'; 0030 0031 /** 0032 * @category Zend 0033 * @package Zend_Oauth 0034 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0035 * @license http://framework.zend.com/license/new-bsd New BSD License 0036 */ 0037 class Zend_Oauth_Config implements Zend_Oauth_Config_ConfigInterface 0038 { 0039 /** 0040 * Signature method used when signing all parameters for an HTTP request 0041 * 0042 * @var string 0043 */ 0044 protected $_signatureMethod = 'HMAC-SHA1'; 0045 0046 /** 0047 * Three request schemes are defined by OAuth, of which passing 0048 * all OAuth parameters by Header is preferred. The other two are 0049 * POST Body and Query String. 0050 * 0051 * @var string 0052 */ 0053 protected $_requestScheme = Zend_Oauth::REQUEST_SCHEME_HEADER; 0054 0055 /** 0056 * Preferred request Method - one of GET or POST - which Zend_Oauth 0057 * will enforce as standard throughout the library. Generally a default 0058 * of POST works fine unless a Provider specifically requires otherwise. 0059 * 0060 * @var string 0061 */ 0062 protected $_requestMethod = Zend_Oauth::POST; 0063 0064 /** 0065 * OAuth Version; This defaults to 1.0 - Must not be changed! 0066 * 0067 * @var string 0068 */ 0069 protected $_version = '1.0'; 0070 0071 /** 0072 * This optional value is used to define where the user is redirected to 0073 * after authorizing a Request Token from an OAuth Providers website. 0074 * It's optional since a Provider may ask for this to be defined in advance 0075 * when registering a new application for a Consumer Key. 0076 * 0077 * @var string 0078 */ 0079 protected $_callbackUrl = null; 0080 0081 /** 0082 * The URL root to append default OAuth endpoint paths. 0083 * 0084 * @var string 0085 */ 0086 protected $_siteUrl = null; 0087 0088 /** 0089 * The URL to which requests for a Request Token should be directed. 0090 * When absent, assumed siteUrl+'/request_token' 0091 * 0092 * @var string 0093 */ 0094 protected $_requestTokenUrl = null; 0095 0096 /** 0097 * The URL to which requests for an Access Token should be directed. 0098 * When absent, assumed siteUrl+'/access_token' 0099 * 0100 * @var string 0101 */ 0102 protected $_accessTokenUrl = null; 0103 0104 /** 0105 * The URL to which users should be redirected to authorize a Request Token. 0106 * When absent, assumed siteUrl+'/authorize' 0107 * 0108 * @var string 0109 */ 0110 protected $_authorizeUrl = null; 0111 0112 /** 0113 * An OAuth application's Consumer Key. 0114 * 0115 * @var string 0116 */ 0117 protected $_consumerKey = null; 0118 0119 /** 0120 * Every Consumer Key has a Consumer Secret unless you're in RSA-land. 0121 * 0122 * @var string 0123 */ 0124 protected $_consumerSecret = null; 0125 0126 /** 0127 * If relevant, a PEM encoded RSA private key encapsulated as a 0128 * Zend_Crypt_Rsa Key 0129 * 0130 * @var Zend_Crypt_Rsa_Key_Private 0131 */ 0132 protected $_rsaPrivateKey = null; 0133 0134 /** 0135 * If relevant, a PEM encoded RSA public key encapsulated as a 0136 * Zend_Crypt_Rsa Key 0137 * 0138 * @var Zend_Crypt_Rsa_Key_Public 0139 */ 0140 protected $_rsaPublicKey = null; 0141 0142 /** 0143 * Generally this will nearly always be an Access Token represented as a 0144 * Zend_Oauth_Token_Access object. 0145 * 0146 * @var Zend_Oauth_Token 0147 */ 0148 protected $_token = null; 0149 0150 /** 0151 * Define the OAuth realm 0152 * 0153 * @var string 0154 */ 0155 protected $_realm = null; 0156 0157 /** 0158 * Constructor; create a new object with an optional array|Zend_Config 0159 * instance containing initialising options. 0160 * 0161 * @param array|Zend_Config $options 0162 * @return void 0163 */ 0164 public function __construct($options = null) 0165 { 0166 if ($options !== null) { 0167 if ($options instanceof Zend_Config) { 0168 $options = $options->toArray(); 0169 } 0170 $this->setOptions($options); 0171 } 0172 } 0173 0174 /** 0175 * Parse option array or Zend_Config instance and setup options using their 0176 * relevant mutators. 0177 * 0178 * @param array|Zend_Config $options 0179 * @return Zend_Oauth_Config 0180 */ 0181 public function setOptions(array $options) 0182 { 0183 foreach ($options as $key => $value) { 0184 switch ($key) { 0185 case 'consumerKey': 0186 $this->setConsumerKey($value); 0187 break; 0188 case 'consumerSecret': 0189 $this->setConsumerSecret($value); 0190 break; 0191 case 'signatureMethod': 0192 $this->setSignatureMethod($value); 0193 break; 0194 case 'version': 0195 $this->setVersion($value); 0196 break; 0197 case 'callbackUrl': 0198 $this->setCallbackUrl($value); 0199 break; 0200 case 'siteUrl': 0201 $this->setSiteUrl($value); 0202 break; 0203 case 'requestTokenUrl': 0204 $this->setRequestTokenUrl($value); 0205 break; 0206 case 'accessTokenUrl': 0207 $this->setAccessTokenUrl($value); 0208 break; 0209 case 'userAuthorizationUrl': 0210 $this->setUserAuthorizationUrl($value); 0211 break; 0212 case 'authorizeUrl': 0213 $this->setAuthorizeUrl($value); 0214 break; 0215 case 'requestMethod': 0216 $this->setRequestMethod($value); 0217 break; 0218 case 'rsaPrivateKey': 0219 $this->setRsaPrivateKey($value); 0220 break; 0221 case 'rsaPublicKey': 0222 $this->setRsaPublicKey($value); 0223 break; 0224 case 'realm': 0225 $this->setRealm($value); 0226 break; 0227 } 0228 } 0229 if (isset($options['requestScheme'])) { 0230 $this->setRequestScheme($options['requestScheme']); 0231 } 0232 0233 return $this; 0234 } 0235 0236 /** 0237 * Set consumer key 0238 * 0239 * @param string $key 0240 * @return Zend_Oauth_Config 0241 */ 0242 public function setConsumerKey($key) 0243 { 0244 $this->_consumerKey = $key; 0245 return $this; 0246 } 0247 0248 /** 0249 * Get consumer key 0250 * 0251 * @return string 0252 */ 0253 public function getConsumerKey() 0254 { 0255 return $this->_consumerKey; 0256 } 0257 0258 /** 0259 * Set consumer secret 0260 * 0261 * @param string $secret 0262 * @return Zend_Oauth_Config 0263 */ 0264 public function setConsumerSecret($secret) 0265 { 0266 $this->_consumerSecret = $secret; 0267 return $this; 0268 } 0269 0270 /** 0271 * Get consumer secret 0272 * 0273 * Returns RSA private key if set; otherwise, returns any previously set 0274 * consumer secret. 0275 * 0276 * @return string 0277 */ 0278 public function getConsumerSecret() 0279 { 0280 if ($this->_rsaPrivateKey !== null) { 0281 return $this->_rsaPrivateKey; 0282 } 0283 return $this->_consumerSecret; 0284 } 0285 0286 /** 0287 * Set signature method 0288 * 0289 * @param string $method 0290 * @return Zend_Oauth_Config 0291 * @throws Zend_Oauth_Exception if unsupported signature method specified 0292 */ 0293 public function setSignatureMethod($method) 0294 { 0295 $method = strtoupper($method); 0296 if (!in_array($method, array( 0297 'HMAC-SHA1', 'HMAC-SHA256', 'RSA-SHA1', 'PLAINTEXT' 0298 )) 0299 ) { 0300 // require_once 'Zend/Oauth/Exception.php'; 0301 throw new Zend_Oauth_Exception('Unsupported signature method: ' 0302 . $method 0303 . '. Supported are HMAC-SHA1, RSA-SHA1, PLAINTEXT and HMAC-SHA256'); 0304 } 0305 $this->_signatureMethod = $method;; 0306 return $this; 0307 } 0308 0309 /** 0310 * Get signature method 0311 * 0312 * @return string 0313 */ 0314 public function getSignatureMethod() 0315 { 0316 return $this->_signatureMethod; 0317 } 0318 0319 /** 0320 * Set request scheme 0321 * 0322 * @param string $scheme 0323 * @return Zend_Oauth_Config 0324 * @throws Zend_Oauth_Exception if invalid scheme specified, or if POSTBODY set when request method of GET is specified 0325 */ 0326 public function setRequestScheme($scheme) 0327 { 0328 $scheme = strtolower($scheme); 0329 if (!in_array($scheme, array( 0330 Zend_Oauth::REQUEST_SCHEME_HEADER, 0331 Zend_Oauth::REQUEST_SCHEME_POSTBODY, 0332 Zend_Oauth::REQUEST_SCHEME_QUERYSTRING, 0333 )) 0334 ) { 0335 // require_once 'Zend/Oauth/Exception.php'; 0336 throw new Zend_Oauth_Exception( 0337 '\'' . $scheme . '\' is an unsupported request scheme' 0338 ); 0339 } 0340 if ($scheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY 0341 && $this->getRequestMethod() == Zend_Oauth::GET 0342 ) { 0343 // require_once 'Zend/Oauth/Exception.php'; 0344 throw new Zend_Oauth_Exception( 0345 'Cannot set POSTBODY request method if HTTP method set to GET' 0346 ); 0347 } 0348 $this->_requestScheme = $scheme; 0349 return $this; 0350 } 0351 0352 /** 0353 * Get request scheme 0354 * 0355 * @return string 0356 */ 0357 public function getRequestScheme() 0358 { 0359 return $this->_requestScheme; 0360 } 0361 0362 /** 0363 * Set version 0364 * 0365 * @param string $version 0366 * @return Zend_Oauth_Config 0367 */ 0368 public function setVersion($version) 0369 { 0370 $this->_version = $version; 0371 return $this; 0372 } 0373 0374 /** 0375 * Get version 0376 * 0377 * @return string 0378 */ 0379 public function getVersion() 0380 { 0381 return $this->_version; 0382 } 0383 0384 /** 0385 * Set callback URL 0386 * 0387 * @param string $url 0388 * @return Zend_Oauth_Config 0389 * @throws Zend_Oauth_Exception for invalid URLs 0390 */ 0391 public function setCallbackUrl($url) 0392 { 0393 if (!Zend_Uri::check($url) && $url !== 'oob') { 0394 // require_once 'Zend/Oauth/Exception.php'; 0395 throw new Zend_Oauth_Exception( 0396 '\'' . $url . '\' is not a valid URI' 0397 ); 0398 } 0399 $this->_callbackUrl = $url; 0400 return $this; 0401 } 0402 0403 /** 0404 * Get callback URL 0405 * 0406 * @return string 0407 */ 0408 public function getCallbackUrl() 0409 { 0410 return $this->_callbackUrl; 0411 } 0412 0413 /** 0414 * Set site URL 0415 * 0416 * @param string $url 0417 * @return Zend_Oauth_Config 0418 * @throws Zend_Oauth_Exception for invalid URLs 0419 */ 0420 public function setSiteUrl($url) 0421 { 0422 if (!Zend_Uri::check($url)) { 0423 // require_once 'Zend/Oauth/Exception.php'; 0424 throw new Zend_Oauth_Exception( 0425 '\'' . $url . '\' is not a valid URI' 0426 ); 0427 } 0428 $this->_siteUrl = $url; 0429 return $this; 0430 } 0431 0432 /** 0433 * Get site URL 0434 * 0435 * @return string 0436 */ 0437 public function getSiteUrl() 0438 { 0439 return $this->_siteUrl; 0440 } 0441 0442 /** 0443 * Set request token URL 0444 * 0445 * @param string $url 0446 * @return Zend_Oauth_Config 0447 * @throws Zend_Oauth_Exception for invalid URLs 0448 */ 0449 public function setRequestTokenUrl($url) 0450 { 0451 if (!Zend_Uri::check($url)) { 0452 // require_once 'Zend/Oauth/Exception.php'; 0453 throw new Zend_Oauth_Exception( 0454 '\'' . $url . '\' is not a valid URI' 0455 ); 0456 } 0457 $this->_requestTokenUrl = rtrim($url, '/'); 0458 return $this; 0459 } 0460 0461 /** 0462 * Get request token URL 0463 * 0464 * If no request token URL has been set, but a site URL has, returns the 0465 * site URL with the string "/request_token" appended. 0466 * 0467 * @return string 0468 */ 0469 public function getRequestTokenUrl() 0470 { 0471 if (!$this->_requestTokenUrl && $this->_siteUrl) { 0472 return $this->_siteUrl . '/request_token'; 0473 } 0474 return $this->_requestTokenUrl; 0475 } 0476 0477 /** 0478 * Set access token URL 0479 * 0480 * @param string $url 0481 * @return Zend_Oauth_Config 0482 * @throws Zend_Oauth_Exception for invalid URLs 0483 */ 0484 public function setAccessTokenUrl($url) 0485 { 0486 if (!Zend_Uri::check($url)) { 0487 // require_once 'Zend/Oauth/Exception.php'; 0488 throw new Zend_Oauth_Exception( 0489 '\'' . $url . '\' is not a valid URI' 0490 ); 0491 } 0492 $this->_accessTokenUrl = rtrim($url, '/'); 0493 return $this; 0494 } 0495 0496 /** 0497 * Get access token URL 0498 * 0499 * If no access token URL has been set, but a site URL has, returns the 0500 * site URL with the string "/access_token" appended. 0501 * 0502 * @return string 0503 */ 0504 public function getAccessTokenUrl() 0505 { 0506 if (!$this->_accessTokenUrl && $this->_siteUrl) { 0507 return $this->_siteUrl . '/access_token'; 0508 } 0509 return $this->_accessTokenUrl; 0510 } 0511 0512 /** 0513 * Set user authorization URL 0514 * 0515 * @param string $url 0516 * @return Zend_Oauth_Config 0517 * @throws Zend_Oauth_Exception for invalid URLs 0518 */ 0519 public function setUserAuthorizationUrl($url) 0520 { 0521 return $this->setAuthorizeUrl($url); 0522 } 0523 0524 /** 0525 * Set authorization URL 0526 * 0527 * @param string $url 0528 * @return Zend_Oauth_Config 0529 * @throws Zend_Oauth_Exception for invalid URLs 0530 */ 0531 public function setAuthorizeUrl($url) 0532 { 0533 if (!Zend_Uri::check($url)) { 0534 // require_once 'Zend/Oauth/Exception.php'; 0535 throw new Zend_Oauth_Exception( 0536 '\'' . $url . '\' is not a valid URI' 0537 ); 0538 } 0539 $this->_authorizeUrl = rtrim($url, '/'); 0540 return $this; 0541 } 0542 0543 /** 0544 * Get user authorization URL 0545 * 0546 * @return string 0547 */ 0548 public function getUserAuthorizationUrl() 0549 { 0550 return $this->getAuthorizeUrl(); 0551 } 0552 0553 /** 0554 * Get authorization URL 0555 * 0556 * If no authorization URL has been set, but a site URL has, returns the 0557 * site URL with the string "/authorize" appended. 0558 * 0559 * @return string 0560 */ 0561 public function getAuthorizeUrl() 0562 { 0563 if (!$this->_authorizeUrl && $this->_siteUrl) { 0564 return $this->_siteUrl . '/authorize'; 0565 } 0566 return $this->_authorizeUrl; 0567 } 0568 0569 /** 0570 * Set request method 0571 * 0572 * @param string $method 0573 * @return Zend_Oauth_Config 0574 * @throws Zend_Oauth_Exception for invalid request methods 0575 */ 0576 public function setRequestMethod($method) 0577 { 0578 $method = strtoupper($method); 0579 if (!in_array($method, array( 0580 Zend_Oauth::GET, 0581 Zend_Oauth::POST, 0582 Zend_Oauth::PUT, 0583 Zend_Oauth::DELETE, 0584 Zend_Oauth::OPTIONS, 0585 )) 0586 ) { 0587 // require_once 'Zend/Oauth/Exception.php'; 0588 throw new Zend_Oauth_Exception('Invalid method: ' . $method); 0589 } 0590 $this->_requestMethod = $method; 0591 return $this; 0592 } 0593 0594 /** 0595 * Get request method 0596 * 0597 * @return string 0598 */ 0599 public function getRequestMethod() 0600 { 0601 return $this->_requestMethod; 0602 } 0603 0604 /** 0605 * Set RSA public key 0606 * 0607 * @param Zend_Crypt_Rsa_Key_Public $key 0608 * @return Zend_Oauth_Config 0609 */ 0610 public function setRsaPublicKey(Zend_Crypt_Rsa_Key_Public $key) 0611 { 0612 $this->_rsaPublicKey = $key; 0613 return $this; 0614 } 0615 0616 /** 0617 * Get RSA public key 0618 * 0619 * @return Zend_Crypt_Rsa_Key_Public 0620 */ 0621 public function getRsaPublicKey() 0622 { 0623 return $this->_rsaPublicKey; 0624 } 0625 0626 /** 0627 * Set RSA private key 0628 * 0629 * @param Zend_Crypt_Rsa_Key_Private $key 0630 * @return Zend_Oauth_Config 0631 */ 0632 public function setRsaPrivateKey(Zend_Crypt_Rsa_Key_Private $key) 0633 { 0634 $this->_rsaPrivateKey = $key; 0635 return $this; 0636 } 0637 0638 /** 0639 * Get RSA private key 0640 * 0641 * @return Zend_Crypt_Rsa_Key_Private 0642 */ 0643 public function getRsaPrivateKey() 0644 { 0645 return $this->_rsaPrivateKey; 0646 } 0647 0648 /** 0649 * Set OAuth token 0650 * 0651 * @param Zend_Oauth_Token $token 0652 * @return Zend_Oauth_Config 0653 */ 0654 public function setToken(Zend_Oauth_Token $token) 0655 { 0656 $this->_token = $token; 0657 return $this; 0658 } 0659 0660 /** 0661 * Get OAuth token 0662 * 0663 * @return Zend_Oauth_Token 0664 */ 0665 public function getToken() 0666 { 0667 return $this->_token; 0668 } 0669 0670 /** 0671 * Set OAuth realm 0672 * 0673 * @param string $realm 0674 * @return Zend_Oauth_Config 0675 */ 0676 public function setRealm($realm) 0677 { 0678 $this->_realm = $realm; 0679 return $this; 0680 } 0681 0682 /** 0683 * Get OAuth realm 0684 * 0685 * @return string 0686 */ 0687 public function getRealm() 0688 { 0689 return $this->_realm; 0690 } 0691 }