web/lib/Zend/Oauth/Config.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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_Oauth
       
    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
       
    19  * @version    $Id: Config.php 22662 2010-07-24 17:37:36Z mabe $
       
    20  */
       
    21 
       
    22 /** Zend_Oauth */
       
    23 require_once 'Zend/Oauth.php';
       
    24 
       
    25 /** Zend_Uri */
       
    26 require_once 'Zend/Uri.php';
       
    27 
       
    28 /** Zend_Oauth_Config_Interface */
       
    29 require_once 'Zend/Oauth/Config/ConfigInterface.php';
       
    30 
       
    31 /**
       
    32  * @category   Zend
       
    33  * @package    Zend_Oauth
       
    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
       
    36  */
       
    37 class Zend_Oauth_Config implements Zend_Oauth_Config_ConfigInterface
       
    38 {
       
    39     /**
       
    40      * Signature method used when signing all parameters for an HTTP request
       
    41      *
       
    42      * @var string
       
    43      */
       
    44     protected $_signatureMethod = 'HMAC-SHA1';
       
    45 
       
    46     /**
       
    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.
       
    50      *
       
    51      * @var string
       
    52      */
       
    53     protected $_requestScheme = Zend_Oauth::REQUEST_SCHEME_HEADER;
       
    54 
       
    55     /**
       
    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.
       
    59      *
       
    60      * @var string
       
    61      */
       
    62     protected $_requestMethod = Zend_Oauth::POST;
       
    63 
       
    64     /**
       
    65      * OAuth Version; This defaults to 1.0 - Must not be changed!
       
    66      *
       
    67      * @var string
       
    68      */
       
    69     protected $_version = '1.0';
       
    70 
       
    71     /**
       
    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.
       
    76      *
       
    77      * @var string
       
    78      */
       
    79     protected $_callbackUrl = null;
       
    80 
       
    81     /**
       
    82      * The URL root to append default OAuth endpoint paths.
       
    83      *
       
    84      * @var string
       
    85      */
       
    86     protected $_siteUrl = null;
       
    87 
       
    88     /**
       
    89      * The URL to which requests for a Request Token should be directed.
       
    90      * When absent, assumed siteUrl+'/request_token'
       
    91      *
       
    92      * @var string
       
    93      */
       
    94     protected $_requestTokenUrl = null;
       
    95 
       
    96     /**
       
    97      * The URL to which requests for an Access Token should be directed.
       
    98      * When absent, assumed siteUrl+'/access_token'
       
    99      *
       
   100      * @var string
       
   101      */
       
   102     protected $_accessTokenUrl = null;
       
   103 
       
   104     /**
       
   105      * The URL to which users should be redirected to authorize a Request Token.
       
   106      * When absent, assumed siteUrl+'/authorize'
       
   107      *
       
   108      * @var string
       
   109      */
       
   110     protected $_authorizeUrl = null;
       
   111 
       
   112     /**
       
   113      * An OAuth application's Consumer Key.
       
   114      *
       
   115      * @var string
       
   116      */
       
   117     protected $_consumerKey = null;
       
   118 
       
   119     /**
       
   120      * Every Consumer Key has a Consumer Secret unless you're in RSA-land.
       
   121      *
       
   122      * @var string
       
   123      */
       
   124     protected $_consumerSecret = null;
       
   125 
       
   126     /**
       
   127      * If relevant, a PEM encoded RSA private key encapsulated as a
       
   128      * Zend_Crypt_Rsa Key
       
   129      *
       
   130      * @var Zend_Crypt_Rsa_Key_Private
       
   131      */
       
   132     protected $_rsaPrivateKey = null;
       
   133 
       
   134     /**
       
   135      * If relevant, a PEM encoded RSA public key encapsulated as a
       
   136      * Zend_Crypt_Rsa Key
       
   137      *
       
   138      * @var Zend_Crypt_Rsa_Key_Public
       
   139      */
       
   140     protected $_rsaPublicKey = null;
       
   141 
       
   142     /**
       
   143      * Generally this will nearly always be an Access Token represented as a
       
   144      * Zend_Oauth_Token_Access object.
       
   145      *
       
   146      * @var Zend_Oauth_Token
       
   147      */
       
   148     protected $_token = null;
       
   149 
       
   150     /**
       
   151      * Constructor; create a new object with an optional array|Zend_Config
       
   152      * instance containing initialising options.
       
   153      *
       
   154      * @param  array|Zend_Config $options
       
   155      * @return void
       
   156      */
       
   157     public function __construct($options = null)
       
   158     {
       
   159         if ($options !== null) {
       
   160             if ($options instanceof Zend_Config) {
       
   161                 $options = $options->toArray();
       
   162             }
       
   163             $this->setOptions($options);
       
   164         }
       
   165     }
       
   166 
       
   167     /**
       
   168      * Parse option array or Zend_Config instance and setup options using their
       
   169      * relevant mutators.
       
   170      *
       
   171      * @param  array|Zend_Config $options
       
   172      * @return Zend_Oauth_Config
       
   173      */
       
   174     public function setOptions(array $options)
       
   175     {
       
   176         foreach ($options as $key => $value) {
       
   177             switch ($key) {
       
   178                 case 'consumerKey':
       
   179                     $this->setConsumerKey($value);
       
   180                     break;
       
   181                 case 'consumerSecret':
       
   182                     $this->setConsumerSecret($value);
       
   183                     break;
       
   184                 case 'signatureMethod':
       
   185                     $this->setSignatureMethod($value);
       
   186                     break;
       
   187                 case 'version':
       
   188                     $this->setVersion($value);
       
   189                     break;
       
   190                 case 'callbackUrl':
       
   191                     $this->setCallbackUrl($value);
       
   192                     break;
       
   193                 case 'siteUrl':
       
   194                     $this->setSiteUrl($value);
       
   195                     break;
       
   196                 case 'requestTokenUrl':
       
   197                     $this->setRequestTokenUrl($value);
       
   198                     break;
       
   199                 case 'accessTokenUrl':
       
   200                     $this->setAccessTokenUrl($value);
       
   201                     break;
       
   202                 case 'userAuthorizationUrl':
       
   203                     $this->setUserAuthorizationUrl($value);
       
   204                     break;
       
   205                 case 'authorizeUrl':
       
   206                     $this->setAuthorizeUrl($value);
       
   207                     break;
       
   208                 case 'requestMethod':
       
   209                     $this->setRequestMethod($value);
       
   210                     break;
       
   211                 case 'rsaPrivateKey':
       
   212                     $this->setRsaPrivateKey($value);
       
   213                     break;
       
   214                 case 'rsaPublicKey':
       
   215                     $this->setRsaPublicKey($value);
       
   216                     break;
       
   217             }
       
   218         }
       
   219         if (isset($options['requestScheme'])) {
       
   220             $this->setRequestScheme($options['requestScheme']);
       
   221         }
       
   222 
       
   223         return $this;
       
   224     }
       
   225 
       
   226     /**
       
   227      * Set consumer key
       
   228      *
       
   229      * @param  string $key
       
   230      * @return Zend_Oauth_Config
       
   231      */
       
   232     public function setConsumerKey($key)
       
   233     {
       
   234         $this->_consumerKey = $key;
       
   235         return $this;
       
   236     }
       
   237 
       
   238     /**
       
   239      * Get consumer key
       
   240      *
       
   241      * @return string
       
   242      */
       
   243     public function getConsumerKey()
       
   244     {
       
   245         return $this->_consumerKey;
       
   246     }
       
   247 
       
   248     /**
       
   249      * Set consumer secret
       
   250      *
       
   251      * @param  string $secret
       
   252      * @return Zend_Oauth_Config
       
   253      */
       
   254     public function setConsumerSecret($secret)
       
   255     {
       
   256         $this->_consumerSecret = $secret;
       
   257         return $this;
       
   258     }
       
   259 
       
   260     /**
       
   261      * Get consumer secret
       
   262      *
       
   263      * Returns RSA private key if set; otherwise, returns any previously set 
       
   264      * consumer secret.
       
   265      *
       
   266      * @return string
       
   267      */
       
   268     public function getConsumerSecret()
       
   269     {
       
   270         if ($this->_rsaPrivateKey !== null) {
       
   271             return $this->_rsaPrivateKey;
       
   272         }
       
   273         return $this->_consumerSecret;
       
   274     }
       
   275 
       
   276     /**
       
   277      * Set signature method
       
   278      *
       
   279      * @param  string $method
       
   280      * @return Zend_Oauth_Config
       
   281      * @throws Zend_Oauth_Exception if unsupported signature method specified
       
   282      */
       
   283     public function setSignatureMethod($method)
       
   284     {
       
   285         $method = strtoupper($method);
       
   286         if (!in_array($method, array(
       
   287                 'HMAC-SHA1', 'HMAC-SHA256', 'RSA-SHA1', 'PLAINTEXT'
       
   288             ))
       
   289         ) {
       
   290             require_once 'Zend/Oauth/Exception.php';
       
   291             throw new Zend_Oauth_Exception('Unsupported signature method: '
       
   292                 . $method
       
   293                 . '. Supported are HMAC-SHA1, RSA-SHA1, PLAINTEXT and HMAC-SHA256');
       
   294         }
       
   295         $this->_signatureMethod = $method;;
       
   296         return $this;
       
   297     }
       
   298 
       
   299     /**
       
   300      * Get signature method
       
   301      *
       
   302      * @return string
       
   303      */
       
   304     public function getSignatureMethod()
       
   305     {
       
   306         return $this->_signatureMethod;
       
   307     }
       
   308 
       
   309     /**
       
   310      * Set request scheme
       
   311      *
       
   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
       
   315      */
       
   316     public function setRequestScheme($scheme)
       
   317     {
       
   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,
       
   323             ))
       
   324         ) {
       
   325             require_once 'Zend/Oauth/Exception.php';
       
   326             throw new Zend_Oauth_Exception(
       
   327                 '\'' . $scheme . '\' is an unsupported request scheme'
       
   328             );
       
   329         }
       
   330         if ($scheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY
       
   331             && $this->getRequestMethod() == Zend_Oauth::GET
       
   332         ) {
       
   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'
       
   336             );
       
   337         }
       
   338         $this->_requestScheme = $scheme;
       
   339         return $this;
       
   340     }
       
   341 
       
   342     /**
       
   343      * Get request scheme
       
   344      *
       
   345      * @return string
       
   346      */
       
   347     public function getRequestScheme()
       
   348     {
       
   349         return $this->_requestScheme;
       
   350     }
       
   351 
       
   352     /**
       
   353      * Set version
       
   354      *
       
   355      * @param  string $version
       
   356      * @return Zend_Oauth_Config
       
   357      */
       
   358     public function setVersion($version)
       
   359     {
       
   360         $this->_version = $version;
       
   361         return $this;
       
   362     }
       
   363 
       
   364     /**
       
   365      * Get version
       
   366      *
       
   367      * @return string
       
   368      */
       
   369     public function getVersion()
       
   370     {
       
   371         return $this->_version;
       
   372     }
       
   373 
       
   374     /**
       
   375      * Set callback URL
       
   376      *
       
   377      * @param  string $url
       
   378      * @return Zend_Oauth_Config
       
   379      * @throws Zend_Oauth_Exception for invalid URLs
       
   380      */
       
   381     public function setCallbackUrl($url)
       
   382     {
       
   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'
       
   387             );
       
   388         }
       
   389         $this->_callbackUrl = $url;
       
   390         return $this;
       
   391     }
       
   392 
       
   393     /**
       
   394      * Get callback URL
       
   395      *
       
   396      * @return string
       
   397      */
       
   398     public function getCallbackUrl()
       
   399     {
       
   400         return $this->_callbackUrl;
       
   401     }
       
   402 
       
   403     /**
       
   404      * Set site URL
       
   405      *
       
   406      * @param  string $url
       
   407      * @return Zend_Oauth_Config
       
   408      * @throws Zend_Oauth_Exception for invalid URLs
       
   409      */
       
   410     public function setSiteUrl($url)
       
   411     {
       
   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'
       
   416             );
       
   417         }
       
   418         $this->_siteUrl = $url;
       
   419         return $this;
       
   420     }
       
   421 
       
   422     /**
       
   423      * Get site URL
       
   424      *
       
   425      * @return string
       
   426      */
       
   427     public function getSiteUrl()
       
   428     {
       
   429         return $this->_siteUrl;
       
   430     }
       
   431 
       
   432     /**
       
   433      * Set request token URL
       
   434      *
       
   435      * @param  string $url
       
   436      * @return Zend_Oauth_Config
       
   437      * @throws Zend_Oauth_Exception for invalid URLs
       
   438      */
       
   439     public function setRequestTokenUrl($url)
       
   440     {
       
   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'
       
   445             );
       
   446         }
       
   447         $this->_requestTokenUrl = rtrim($url, '/');
       
   448         return $this;
       
   449     }
       
   450 
       
   451     /**
       
   452      * Get request token URL
       
   453      *
       
   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.
       
   456      *
       
   457      * @return string
       
   458      */
       
   459     public function getRequestTokenUrl()
       
   460     {
       
   461         if (!$this->_requestTokenUrl && $this->_siteUrl) {
       
   462             return $this->_siteUrl . '/request_token';
       
   463         }
       
   464         return $this->_requestTokenUrl;
       
   465     }
       
   466 
       
   467     /**
       
   468      * Set access token URL
       
   469      *
       
   470      * @param  string $url
       
   471      * @return Zend_Oauth_Config
       
   472      * @throws Zend_Oauth_Exception for invalid URLs
       
   473      */
       
   474     public function setAccessTokenUrl($url)
       
   475     {
       
   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'
       
   480             );
       
   481         }
       
   482         $this->_accessTokenUrl = rtrim($url, '/');
       
   483         return $this;
       
   484     }
       
   485 
       
   486     /**
       
   487      * Get access token URL
       
   488      *
       
   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.
       
   491      *
       
   492      * @return string
       
   493      */
       
   494     public function getAccessTokenUrl()
       
   495     {
       
   496         if (!$this->_accessTokenUrl && $this->_siteUrl) {
       
   497             return $this->_siteUrl . '/access_token';
       
   498         }
       
   499         return $this->_accessTokenUrl;
       
   500     }
       
   501 
       
   502     /**
       
   503      * Set user authorization URL
       
   504      *
       
   505      * @param  string $url
       
   506      * @return Zend_Oauth_Config
       
   507      * @throws Zend_Oauth_Exception for invalid URLs
       
   508      */
       
   509     public function setUserAuthorizationUrl($url)
       
   510     {
       
   511         return $this->setAuthorizeUrl($url);
       
   512     }
       
   513 
       
   514     /**
       
   515      * Set authorization URL
       
   516      *
       
   517      * @param  string $url
       
   518      * @return Zend_Oauth_Config
       
   519      * @throws Zend_Oauth_Exception for invalid URLs
       
   520      */
       
   521     public function setAuthorizeUrl($url)
       
   522     {
       
   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'
       
   527             );
       
   528         }
       
   529         $this->_authorizeUrl = rtrim($url, '/');
       
   530         return $this;
       
   531     }
       
   532 
       
   533     /**
       
   534      * Get user authorization URL
       
   535      *
       
   536      * @return string
       
   537      */
       
   538     public function getUserAuthorizationUrl()
       
   539     {
       
   540         return $this->getAuthorizeUrl();
       
   541     }
       
   542 
       
   543     /**
       
   544      * Get authorization URL
       
   545      *
       
   546      * If no authorization URL has been set, but a site URL has, returns the 
       
   547      * site URL with the string "/authorize" appended.
       
   548      *
       
   549      * @return string
       
   550      */
       
   551     public function getAuthorizeUrl()
       
   552     {
       
   553         if (!$this->_authorizeUrl && $this->_siteUrl) {
       
   554             return $this->_siteUrl . '/authorize';
       
   555         }
       
   556         return $this->_authorizeUrl;
       
   557     }
       
   558 
       
   559     /**
       
   560      * Set request method
       
   561      *
       
   562      * @param  string $method
       
   563      * @return Zend_Oauth_Config
       
   564      * @throws Zend_Oauth_Exception for invalid request methods
       
   565      */
       
   566     public function setRequestMethod($method)
       
   567     {
       
   568         $method = strtoupper($method);
       
   569         if (!in_array($method, array(
       
   570                 Zend_Oauth::GET, 
       
   571                 Zend_Oauth::POST, 
       
   572                 Zend_Oauth::PUT, 
       
   573                 Zend_Oauth::DELETE,
       
   574             ))
       
   575         ) {
       
   576             require_once 'Zend/Oauth/Exception.php';
       
   577             throw new Zend_Oauth_Exception('Invalid method: ' . $method);
       
   578         }
       
   579         $this->_requestMethod = $method;
       
   580         return $this;
       
   581     }
       
   582 
       
   583     /**
       
   584      * Get request method
       
   585      *
       
   586      * @return string
       
   587      */
       
   588     public function getRequestMethod()
       
   589     {
       
   590         return $this->_requestMethod;
       
   591     }
       
   592 
       
   593     /**
       
   594      * Set RSA public key
       
   595      *
       
   596      * @param  Zend_Crypt_Rsa_Key_Public $key
       
   597      * @return Zend_Oauth_Config
       
   598      */
       
   599     public function setRsaPublicKey(Zend_Crypt_Rsa_Key_Public $key)
       
   600     {
       
   601         $this->_rsaPublicKey = $key;
       
   602         return $this;
       
   603     }
       
   604 
       
   605     /**
       
   606      * Get RSA public key
       
   607      *
       
   608      * @return Zend_Crypt_Rsa_Key_Public
       
   609      */
       
   610     public function getRsaPublicKey()
       
   611     {
       
   612         return $this->_rsaPublicKey;
       
   613     }
       
   614 
       
   615     /**
       
   616      * Set RSA private key
       
   617      *
       
   618      * @param  Zend_Crypt_Rsa_Key_Private $key
       
   619      * @return Zend_Oauth_Config
       
   620      */
       
   621     public function setRsaPrivateKey(Zend_Crypt_Rsa_Key_Private $key)
       
   622     {
       
   623         $this->_rsaPrivateKey = $key;
       
   624         return $this;
       
   625     }
       
   626 
       
   627     /**
       
   628      * Get RSA private key
       
   629      *
       
   630      * @return Zend_Crypt_Rsa_Key_Private
       
   631      */
       
   632     public function getRsaPrivateKey()
       
   633     {
       
   634         return $this->_rsaPrivateKey;
       
   635     }
       
   636 
       
   637     /**
       
   638      * Set OAuth token
       
   639      *
       
   640      * @param  Zend_Oauth_Token $token
       
   641      * @return Zend_Oauth_Config
       
   642      */
       
   643     public function setToken(Zend_Oauth_Token $token)
       
   644     {
       
   645         $this->_token = $token;
       
   646         return $this;
       
   647     }
       
   648 
       
   649     /**
       
   650      * Get OAuth token
       
   651      *
       
   652      * @return Zend_Oauth_Token
       
   653      */
       
   654     public function getToken()
       
   655     {
       
   656         return $this->_token;
       
   657     }
       
   658 }