123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Oauth
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /** Zend_Oauth */
- #require_once 'Zend/Oauth.php';
- /** Zend_Uri */
- #require_once 'Zend/Uri.php';
- /** Zend_Oauth_Config_Interface */
- #require_once 'Zend/Oauth/Config/ConfigInterface.php';
- /**
- * @category Zend
- * @package Zend_Oauth
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Oauth_Config implements Zend_Oauth_Config_ConfigInterface
- {
- /**
- * Signature method used when signing all parameters for an HTTP request
- *
- * @var string
- */
- protected $_signatureMethod = 'HMAC-SHA1';
- /**
- * Three request schemes are defined by OAuth, of which passing
- * all OAuth parameters by Header is preferred. The other two are
- * POST Body and Query String.
- *
- * @var string
- */
- protected $_requestScheme = Zend_Oauth::REQUEST_SCHEME_HEADER;
- /**
- * Preferred request Method - one of GET or POST - which Zend_Oauth
- * will enforce as standard throughout the library. Generally a default
- * of POST works fine unless a Provider specifically requires otherwise.
- *
- * @var string
- */
- protected $_requestMethod = Zend_Oauth::POST;
- /**
- * OAuth Version; This defaults to 1.0 - Must not be changed!
- *
- * @var string
- */
- protected $_version = '1.0';
- /**
- * This optional value is used to define where the user is redirected to
- * after authorizing a Request Token from an OAuth Providers website.
- * It's optional since a Provider may ask for this to be defined in advance
- * when registering a new application for a Consumer Key.
- *
- * @var string
- */
- protected $_callbackUrl = null;
- /**
- * The URL root to append default OAuth endpoint paths.
- *
- * @var string
- */
- protected $_siteUrl = null;
- /**
- * The URL to which requests for a Request Token should be directed.
- * When absent, assumed siteUrl+'/request_token'
- *
- * @var string
- */
- protected $_requestTokenUrl = null;
- /**
- * The URL to which requests for an Access Token should be directed.
- * When absent, assumed siteUrl+'/access_token'
- *
- * @var string
- */
- protected $_accessTokenUrl = null;
- /**
- * The URL to which users should be redirected to authorize a Request Token.
- * When absent, assumed siteUrl+'/authorize'
- *
- * @var string
- */
- protected $_authorizeUrl = null;
- /**
- * An OAuth application's Consumer Key.
- *
- * @var string
- */
- protected $_consumerKey = null;
- /**
- * Every Consumer Key has a Consumer Secret unless you're in RSA-land.
- *
- * @var string
- */
- protected $_consumerSecret = null;
- /**
- * If relevant, a PEM encoded RSA private key encapsulated as a
- * Zend_Crypt_Rsa Key
- *
- * @var Zend_Crypt_Rsa_Key_Private
- */
- protected $_rsaPrivateKey = null;
- /**
- * If relevant, a PEM encoded RSA public key encapsulated as a
- * Zend_Crypt_Rsa Key
- *
- * @var Zend_Crypt_Rsa_Key_Public
- */
- protected $_rsaPublicKey = null;
- /**
- * Generally this will nearly always be an Access Token represented as a
- * Zend_Oauth_Token_Access object.
- *
- * @var Zend_Oauth_Token
- */
- protected $_token = null;
- /**
- * Define the OAuth realm
- *
- * @var string
- */
- protected $_realm = null;
- /**
- * Constructor; create a new object with an optional array|Zend_Config
- * instance containing initialising options.
- *
- * @param array|Zend_Config $options
- * @return void
- */
- public function __construct($options = null)
- {
- if ($options !== null) {
- if ($options instanceof Zend_Config) {
- $options = $options->toArray();
- }
- $this->setOptions($options);
- }
- }
- /**
- * Parse option array or Zend_Config instance and setup options using their
- * relevant mutators.
- *
- * @param array|Zend_Config $options
- * @return Zend_Oauth_Config
- */
- public function setOptions(array $options)
- {
- foreach ($options as $key => $value) {
- switch ($key) {
- case 'consumerKey':
- $this->setConsumerKey($value);
- break;
- case 'consumerSecret':
- $this->setConsumerSecret($value);
- break;
- case 'signatureMethod':
- $this->setSignatureMethod($value);
- break;
- case 'version':
- $this->setVersion($value);
- break;
- case 'callbackUrl':
- $this->setCallbackUrl($value);
- break;
- case 'siteUrl':
- $this->setSiteUrl($value);
- break;
- case 'requestTokenUrl':
- $this->setRequestTokenUrl($value);
- break;
- case 'accessTokenUrl':
- $this->setAccessTokenUrl($value);
- break;
- case 'userAuthorizationUrl':
- $this->setUserAuthorizationUrl($value);
- break;
- case 'authorizeUrl':
- $this->setAuthorizeUrl($value);
- break;
- case 'requestMethod':
- $this->setRequestMethod($value);
- break;
- case 'rsaPrivateKey':
- $this->setRsaPrivateKey($value);
- break;
- case 'rsaPublicKey':
- $this->setRsaPublicKey($value);
- break;
- case 'realm':
- $this->setRealm($value);
- break;
- }
- }
- if (isset($options['requestScheme'])) {
- $this->setRequestScheme($options['requestScheme']);
- }
- return $this;
- }
- /**
- * Set consumer key
- *
- * @param string $key
- * @return Zend_Oauth_Config
- */
- public function setConsumerKey($key)
- {
- $this->_consumerKey = $key;
- return $this;
- }
- /**
- * Get consumer key
- *
- * @return string
- */
- public function getConsumerKey()
- {
- return $this->_consumerKey;
- }
- /**
- * Set consumer secret
- *
- * @param string $secret
- * @return Zend_Oauth_Config
- */
- public function setConsumerSecret($secret)
- {
- $this->_consumerSecret = $secret;
- return $this;
- }
- /**
- * Get consumer secret
- *
- * Returns RSA private key if set; otherwise, returns any previously set
- * consumer secret.
- *
- * @return string
- */
- public function getConsumerSecret()
- {
- if ($this->_rsaPrivateKey !== null) {
- return $this->_rsaPrivateKey;
- }
- return $this->_consumerSecret;
- }
- /**
- * Set signature method
- *
- * @param string $method
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception if unsupported signature method specified
- */
- public function setSignatureMethod($method)
- {
- $method = strtoupper($method);
- if (!in_array($method, array(
- 'HMAC-SHA1', 'HMAC-SHA256', 'RSA-SHA1', 'PLAINTEXT'
- ))
- ) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception('Unsupported signature method: '
- . $method
- . '. Supported are HMAC-SHA1, RSA-SHA1, PLAINTEXT and HMAC-SHA256');
- }
- $this->_signatureMethod = $method;;
- return $this;
- }
- /**
- * Get signature method
- *
- * @return string
- */
- public function getSignatureMethod()
- {
- return $this->_signatureMethod;
- }
- /**
- * Set request scheme
- *
- * @param string $scheme
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception if invalid scheme specified, or if POSTBODY set when request method of GET is specified
- */
- public function setRequestScheme($scheme)
- {
- $scheme = strtolower($scheme);
- if (!in_array($scheme, array(
- Zend_Oauth::REQUEST_SCHEME_HEADER,
- Zend_Oauth::REQUEST_SCHEME_POSTBODY,
- Zend_Oauth::REQUEST_SCHEME_QUERYSTRING,
- ))
- ) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception(
- '\'' . $scheme . '\' is an unsupported request scheme'
- );
- }
- if ($scheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY
- && $this->getRequestMethod() == Zend_Oauth::GET
- ) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception(
- 'Cannot set POSTBODY request method if HTTP method set to GET'
- );
- }
- $this->_requestScheme = $scheme;
- return $this;
- }
- /**
- * Get request scheme
- *
- * @return string
- */
- public function getRequestScheme()
- {
- return $this->_requestScheme;
- }
- /**
- * Set version
- *
- * @param string $version
- * @return Zend_Oauth_Config
- */
- public function setVersion($version)
- {
- $this->_version = $version;
- return $this;
- }
- /**
- * Get version
- *
- * @return string
- */
- public function getVersion()
- {
- return $this->_version;
- }
- /**
- * Set callback URL
- *
- * @param string $url
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception for invalid URLs
- */
- public function setCallbackUrl($url)
- {
- if (!Zend_Uri::check($url) && $url !== 'oob') {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception(
- '\'' . $url . '\' is not a valid URI'
- );
- }
- $this->_callbackUrl = $url;
- return $this;
- }
- /**
- * Get callback URL
- *
- * @return string
- */
- public function getCallbackUrl()
- {
- return $this->_callbackUrl;
- }
- /**
- * Set site URL
- *
- * @param string $url
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception for invalid URLs
- */
- public function setSiteUrl($url)
- {
- if (!Zend_Uri::check($url)) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception(
- '\'' . $url . '\' is not a valid URI'
- );
- }
- $this->_siteUrl = $url;
- return $this;
- }
- /**
- * Get site URL
- *
- * @return string
- */
- public function getSiteUrl()
- {
- return $this->_siteUrl;
- }
- /**
- * Set request token URL
- *
- * @param string $url
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception for invalid URLs
- */
- public function setRequestTokenUrl($url)
- {
- if (!Zend_Uri::check($url)) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception(
- '\'' . $url . '\' is not a valid URI'
- );
- }
- $this->_requestTokenUrl = rtrim($url, '/');
- return $this;
- }
- /**
- * Get request token URL
- *
- * If no request token URL has been set, but a site URL has, returns the
- * site URL with the string "/request_token" appended.
- *
- * @return string
- */
- public function getRequestTokenUrl()
- {
- if (!$this->_requestTokenUrl && $this->_siteUrl) {
- return $this->_siteUrl . '/request_token';
- }
- return $this->_requestTokenUrl;
- }
- /**
- * Set access token URL
- *
- * @param string $url
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception for invalid URLs
- */
- public function setAccessTokenUrl($url)
- {
- if (!Zend_Uri::check($url)) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception(
- '\'' . $url . '\' is not a valid URI'
- );
- }
- $this->_accessTokenUrl = rtrim($url, '/');
- return $this;
- }
- /**
- * Get access token URL
- *
- * If no access token URL has been set, but a site URL has, returns the
- * site URL with the string "/access_token" appended.
- *
- * @return string
- */
- public function getAccessTokenUrl()
- {
- if (!$this->_accessTokenUrl && $this->_siteUrl) {
- return $this->_siteUrl . '/access_token';
- }
- return $this->_accessTokenUrl;
- }
- /**
- * Set user authorization URL
- *
- * @param string $url
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception for invalid URLs
- */
- public function setUserAuthorizationUrl($url)
- {
- return $this->setAuthorizeUrl($url);
- }
- /**
- * Set authorization URL
- *
- * @param string $url
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception for invalid URLs
- */
- public function setAuthorizeUrl($url)
- {
- if (!Zend_Uri::check($url)) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception(
- '\'' . $url . '\' is not a valid URI'
- );
- }
- $this->_authorizeUrl = rtrim($url, '/');
- return $this;
- }
- /**
- * Get user authorization URL
- *
- * @return string
- */
- public function getUserAuthorizationUrl()
- {
- return $this->getAuthorizeUrl();
- }
- /**
- * Get authorization URL
- *
- * If no authorization URL has been set, but a site URL has, returns the
- * site URL with the string "/authorize" appended.
- *
- * @return string
- */
- public function getAuthorizeUrl()
- {
- if (!$this->_authorizeUrl && $this->_siteUrl) {
- return $this->_siteUrl . '/authorize';
- }
- return $this->_authorizeUrl;
- }
- /**
- * Set request method
- *
- * @param string $method
- * @return Zend_Oauth_Config
- * @throws Zend_Oauth_Exception for invalid request methods
- */
- public function setRequestMethod($method)
- {
- $method = strtoupper($method);
- if (!in_array($method, array(
- Zend_Oauth::GET,
- Zend_Oauth::POST,
- Zend_Oauth::PUT,
- Zend_Oauth::DELETE,
- Zend_Oauth::OPTIONS,
- ))
- ) {
- #require_once 'Zend/Oauth/Exception.php';
- throw new Zend_Oauth_Exception('Invalid method: ' . $method);
- }
- $this->_requestMethod = $method;
- return $this;
- }
- /**
- * Get request method
- *
- * @return string
- */
- public function getRequestMethod()
- {
- return $this->_requestMethod;
- }
- /**
- * Set RSA public key
- *
- * @param Zend_Crypt_Rsa_Key_Public $key
- * @return Zend_Oauth_Config
- */
- public function setRsaPublicKey(Zend_Crypt_Rsa_Key_Public $key)
- {
- $this->_rsaPublicKey = $key;
- return $this;
- }
- /**
- * Get RSA public key
- *
- * @return Zend_Crypt_Rsa_Key_Public
- */
- public function getRsaPublicKey()
- {
- return $this->_rsaPublicKey;
- }
- /**
- * Set RSA private key
- *
- * @param Zend_Crypt_Rsa_Key_Private $key
- * @return Zend_Oauth_Config
- */
- public function setRsaPrivateKey(Zend_Crypt_Rsa_Key_Private $key)
- {
- $this->_rsaPrivateKey = $key;
- return $this;
- }
- /**
- * Get RSA private key
- *
- * @return Zend_Crypt_Rsa_Key_Private
- */
- public function getRsaPrivateKey()
- {
- return $this->_rsaPrivateKey;
- }
- /**
- * Set OAuth token
- *
- * @param Zend_Oauth_Token $token
- * @return Zend_Oauth_Config
- */
- public function setToken(Zend_Oauth_Token $token)
- {
- $this->_token = $token;
- return $this;
- }
- /**
- * Get OAuth token
- *
- * @return Zend_Oauth_Token
- */
- public function getToken()
- {
- return $this->_token;
- }
- /**
- * Set OAuth realm
- *
- * @param string $realm
- * @return Zend_Oauth_Config
- */
- public function setRealm($realm)
- {
- $this->_realm = $realm;
- return $this;
- }
- /**
- * Get OAuth realm
- *
- * @return string
- */
- public function getRealm()
- {
- return $this->_realm;
- }
- }
|