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.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 require_once 'Zend/Oauth.php';
26 require_once 'Zend/Uri.php';
28 /** Zend_Oauth_Config_Interface */
29 require_once 'Zend/Oauth/Config/ConfigInterface.php';
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 class Zend_Oauth_Config implements Zend_Oauth_Config_ConfigInterface
40 * Signature method used when signing all parameters for an HTTP request
44 protected $_signatureMethod = 'HMAC-SHA1';
47 * Three request schemes are defined by OAuth, of which passing
48 * all OAuth parameters by Header is preferred. The other two are
49 * POST Body and Query String.
53 protected $_requestScheme = Zend_Oauth::REQUEST_SCHEME_HEADER;
56 * Preferred request Method - one of GET or POST - which Zend_Oauth
57 * will enforce as standard throughout the library. Generally a default
58 * of POST works fine unless a Provider specifically requires otherwise.
62 protected $_requestMethod = Zend_Oauth::POST;
65 * OAuth Version; This defaults to 1.0 - Must not be changed!
69 protected $_version = '1.0';
72 * This optional value is used to define where the user is redirected to
73 * after authorizing a Request Token from an OAuth Providers website.
74 * It's optional since a Provider may ask for this to be defined in advance
75 * when registering a new application for a Consumer Key.
79 protected $_callbackUrl = null;
82 * The URL root to append default OAuth endpoint paths.
86 protected $_siteUrl = null;
89 * The URL to which requests for a Request Token should be directed.
90 * When absent, assumed siteUrl+'/request_token'
94 protected $_requestTokenUrl = null;
97 * The URL to which requests for an Access Token should be directed.
98 * When absent, assumed siteUrl+'/access_token'
102 protected $_accessTokenUrl = null;
105 * The URL to which users should be redirected to authorize a Request Token.
106 * When absent, assumed siteUrl+'/authorize'
110 protected $_authorizeUrl = null;
113 * An OAuth application's Consumer Key.
117 protected $_consumerKey = null;
120 * Every Consumer Key has a Consumer Secret unless you're in RSA-land.
124 protected $_consumerSecret = null;
127 * If relevant, a PEM encoded RSA private key encapsulated as a
130 * @var Zend_Crypt_Rsa_Key_Private
132 protected $_rsaPrivateKey = null;
135 * If relevant, a PEM encoded RSA public key encapsulated as a
138 * @var Zend_Crypt_Rsa_Key_Public
140 protected $_rsaPublicKey = null;
143 * Generally this will nearly always be an Access Token represented as a
144 * Zend_Oauth_Token_Access object.
146 * @var Zend_Oauth_Token
148 protected $_token = null;
151 * Constructor; create a new object with an optional array|Zend_Config
152 * instance containing initialising options.
154 * @param array|Zend_Config $options
157 public function __construct($options = null)
159 if ($options !== null) {
160 if ($options instanceof Zend_Config) {
161 $options = $options->toArray();
163 $this->setOptions($options);
168 * Parse option array or Zend_Config instance and setup options using their
171 * @param array|Zend_Config $options
172 * @return Zend_Oauth_Config
174 public function setOptions(array $options)
176 foreach ($options as $key => $value) {
179 $this->setConsumerKey($value);
181 case 'consumerSecret':
182 $this->setConsumerSecret($value);
184 case 'signatureMethod':
185 $this->setSignatureMethod($value);
188 $this->setVersion($value);
191 $this->setCallbackUrl($value);
194 $this->setSiteUrl($value);
196 case 'requestTokenUrl':
197 $this->setRequestTokenUrl($value);
199 case 'accessTokenUrl':
200 $this->setAccessTokenUrl($value);
202 case 'userAuthorizationUrl':
203 $this->setUserAuthorizationUrl($value);
206 $this->setAuthorizeUrl($value);
208 case 'requestMethod':
209 $this->setRequestMethod($value);
211 case 'rsaPrivateKey':
212 $this->setRsaPrivateKey($value);
215 $this->setRsaPublicKey($value);
219 if (isset($options['requestScheme'])) {
220 $this->setRequestScheme($options['requestScheme']);
230 * @return Zend_Oauth_Config
232 public function setConsumerKey($key)
234 $this->_consumerKey = $key;
243 public function getConsumerKey()
245 return $this->_consumerKey;
249 * Set consumer secret
251 * @param string $secret
252 * @return Zend_Oauth_Config
254 public function setConsumerSecret($secret)
256 $this->_consumerSecret = $secret;
261 * Get consumer secret
263 * Returns RSA private key if set; otherwise, returns any previously set
268 public function getConsumerSecret()
270 if ($this->_rsaPrivateKey !== null) {
271 return $this->_rsaPrivateKey;
273 return $this->_consumerSecret;
277 * Set signature method
279 * @param string $method
280 * @return Zend_Oauth_Config
281 * @throws Zend_Oauth_Exception if unsupported signature method specified
283 public function setSignatureMethod($method)
285 $method = strtoupper($method);
286 if (!in_array($method, array(
287 'HMAC-SHA1', 'HMAC-SHA256', 'RSA-SHA1', 'PLAINTEXT'
290 require_once 'Zend/Oauth/Exception.php';
291 throw new Zend_Oauth_Exception('Unsupported signature method: '
293 . '. Supported are HMAC-SHA1, RSA-SHA1, PLAINTEXT and HMAC-SHA256');
295 $this->_signatureMethod = $method;;
300 * Get signature method
304 public function getSignatureMethod()
306 return $this->_signatureMethod;
312 * @param string $scheme
313 * @return Zend_Oauth_Config
314 * @throws Zend_Oauth_Exception if invalid scheme specified, or if POSTBODY set when request method of GET is specified
316 public function setRequestScheme($scheme)
318 $scheme = strtolower($scheme);
319 if (!in_array($scheme, array(
320 Zend_Oauth::REQUEST_SCHEME_HEADER,
321 Zend_Oauth::REQUEST_SCHEME_POSTBODY,
322 Zend_Oauth::REQUEST_SCHEME_QUERYSTRING,
325 require_once 'Zend/Oauth/Exception.php';
326 throw new Zend_Oauth_Exception(
327 '\'' . $scheme . '\' is an unsupported request scheme'
330 if ($scheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY
331 && $this->getRequestMethod() == Zend_Oauth::GET
333 require_once 'Zend/Oauth/Exception.php';
334 throw new Zend_Oauth_Exception(
335 'Cannot set POSTBODY request method if HTTP method set to GET'
338 $this->_requestScheme = $scheme;
347 public function getRequestScheme()
349 return $this->_requestScheme;
355 * @param string $version
356 * @return Zend_Oauth_Config
358 public function setVersion($version)
360 $this->_version = $version;
369 public function getVersion()
371 return $this->_version;
378 * @return Zend_Oauth_Config
379 * @throws Zend_Oauth_Exception for invalid URLs
381 public function setCallbackUrl($url)
383 if (!Zend_Uri::check($url)) {
384 require_once 'Zend/Oauth/Exception.php';
385 throw new Zend_Oauth_Exception(
386 '\'' . $url . '\' is not a valid URI'
389 $this->_callbackUrl = $url;
398 public function getCallbackUrl()
400 return $this->_callbackUrl;
407 * @return Zend_Oauth_Config
408 * @throws Zend_Oauth_Exception for invalid URLs
410 public function setSiteUrl($url)
412 if (!Zend_Uri::check($url)) {
413 require_once 'Zend/Oauth/Exception.php';
414 throw new Zend_Oauth_Exception(
415 '\'' . $url . '\' is not a valid URI'
418 $this->_siteUrl = $url;
427 public function getSiteUrl()
429 return $this->_siteUrl;
433 * Set request token URL
436 * @return Zend_Oauth_Config
437 * @throws Zend_Oauth_Exception for invalid URLs
439 public function setRequestTokenUrl($url)
441 if (!Zend_Uri::check($url)) {
442 require_once 'Zend/Oauth/Exception.php';
443 throw new Zend_Oauth_Exception(
444 '\'' . $url . '\' is not a valid URI'
447 $this->_requestTokenUrl = rtrim($url, '/');
452 * Get request token URL
454 * If no request token URL has been set, but a site URL has, returns the
455 * site URL with the string "/request_token" appended.
459 public function getRequestTokenUrl()
461 if (!$this->_requestTokenUrl && $this->_siteUrl) {
462 return $this->_siteUrl . '/request_token';
464 return $this->_requestTokenUrl;
468 * Set access token URL
471 * @return Zend_Oauth_Config
472 * @throws Zend_Oauth_Exception for invalid URLs
474 public function setAccessTokenUrl($url)
476 if (!Zend_Uri::check($url)) {
477 require_once 'Zend/Oauth/Exception.php';
478 throw new Zend_Oauth_Exception(
479 '\'' . $url . '\' is not a valid URI'
482 $this->_accessTokenUrl = rtrim($url, '/');
487 * Get access token URL
489 * If no access token URL has been set, but a site URL has, returns the
490 * site URL with the string "/access_token" appended.
494 public function getAccessTokenUrl()
496 if (!$this->_accessTokenUrl && $this->_siteUrl) {
497 return $this->_siteUrl . '/access_token';
499 return $this->_accessTokenUrl;
503 * Set user authorization URL
506 * @return Zend_Oauth_Config
507 * @throws Zend_Oauth_Exception for invalid URLs
509 public function setUserAuthorizationUrl($url)
511 return $this->setAuthorizeUrl($url);
515 * Set authorization URL
518 * @return Zend_Oauth_Config
519 * @throws Zend_Oauth_Exception for invalid URLs
521 public function setAuthorizeUrl($url)
523 if (!Zend_Uri::check($url)) {
524 require_once 'Zend/Oauth/Exception.php';
525 throw new Zend_Oauth_Exception(
526 '\'' . $url . '\' is not a valid URI'
529 $this->_authorizeUrl = rtrim($url, '/');
534 * Get user authorization URL
538 public function getUserAuthorizationUrl()
540 return $this->getAuthorizeUrl();
544 * Get authorization URL
546 * If no authorization URL has been set, but a site URL has, returns the
547 * site URL with the string "/authorize" appended.
551 public function getAuthorizeUrl()
553 if (!$this->_authorizeUrl && $this->_siteUrl) {
554 return $this->_siteUrl . '/authorize';
556 return $this->_authorizeUrl;
562 * @param string $method
563 * @return Zend_Oauth_Config
564 * @throws Zend_Oauth_Exception for invalid request methods
566 public function setRequestMethod($method)
568 $method = strtoupper($method);
569 if (!in_array($method, array(
576 require_once 'Zend/Oauth/Exception.php';
577 throw new Zend_Oauth_Exception('Invalid method: ' . $method);
579 $this->_requestMethod = $method;
588 public function getRequestMethod()
590 return $this->_requestMethod;
596 * @param Zend_Crypt_Rsa_Key_Public $key
597 * @return Zend_Oauth_Config
599 public function setRsaPublicKey(Zend_Crypt_Rsa_Key_Public $key)
601 $this->_rsaPublicKey = $key;
608 * @return Zend_Crypt_Rsa_Key_Public
610 public function getRsaPublicKey()
612 return $this->_rsaPublicKey;
616 * Set RSA private key
618 * @param Zend_Crypt_Rsa_Key_Private $key
619 * @return Zend_Oauth_Config
621 public function setRsaPrivateKey(Zend_Crypt_Rsa_Key_Private $key)
623 $this->_rsaPrivateKey = $key;
628 * Get RSA private key
630 * @return Zend_Crypt_Rsa_Key_Private
632 public function getRsaPrivateKey()
634 return $this->_rsaPrivateKey;
640 * @param Zend_Oauth_Token $token
641 * @return Zend_Oauth_Config
643 public function setToken(Zend_Oauth_Token $token)
645 $this->_token = $token;
652 * @return Zend_Oauth_Token
654 public function getToken()
656 return $this->_token;