web/lib/Zend/Oauth/Http/RequestToken.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: RequestToken.php 23076 2010-10-10 21:37:20Z padraic $
       
    20  */
       
    21 
       
    22 /** Zend_Oauth_Http */
       
    23 require_once 'Zend/Oauth/Http.php';
       
    24 
       
    25 /** Zend_Oauth_Token_Request */
       
    26 require_once 'Zend/Oauth/Token/Request.php';
       
    27 
       
    28 /**
       
    29  * @category   Zend
       
    30  * @package    Zend_Oauth
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  */
       
    34 class Zend_Oauth_Http_RequestToken extends Zend_Oauth_Http
       
    35 {
       
    36     /**
       
    37      * Singleton instance if required of the HTTP client
       
    38      *
       
    39      * @var Zend_Http_Client
       
    40      */
       
    41     protected $_httpClient = null;
       
    42 
       
    43     /**
       
    44      * Initiate a HTTP request to retrieve a Request Token.
       
    45      *
       
    46      * @return Zend_Oauth_Token_Request
       
    47      */
       
    48     public function execute()
       
    49     {
       
    50         $params   = $this->assembleParams();
       
    51         $response = $this->startRequestCycle($params);
       
    52         $return   = new Zend_Oauth_Token_Request($response);
       
    53         return $return;
       
    54     }
       
    55 
       
    56     /**
       
    57      * Assemble all parameters for an OAuth Request Token request.
       
    58      *
       
    59      * @return array
       
    60      */
       
    61     public function assembleParams()
       
    62     {
       
    63         $params = array(
       
    64             'oauth_consumer_key'     => $this->_consumer->getConsumerKey(),
       
    65             'oauth_nonce'            => $this->_httpUtility->generateNonce(),
       
    66             'oauth_timestamp'        => $this->_httpUtility->generateTimestamp(),
       
    67             'oauth_signature_method' => $this->_consumer->getSignatureMethod(),
       
    68             'oauth_version'          => $this->_consumer->getVersion(),
       
    69         );
       
    70 
       
    71         // indicates we support 1.0a
       
    72         if ($this->_consumer->getCallbackUrl()) {
       
    73             $params['oauth_callback'] = $this->_consumer->getCallbackUrl();
       
    74         } else {
       
    75             $params['oauth_callback'] = 'oob';
       
    76         }
       
    77 
       
    78         if (!empty($this->_parameters)) {
       
    79             $params = array_merge($params, $this->_parameters);
       
    80         }
       
    81 
       
    82         $params['oauth_signature'] = $this->_httpUtility->sign(
       
    83             $params,
       
    84             $this->_consumer->getSignatureMethod(),
       
    85             $this->_consumer->getConsumerSecret(),
       
    86             null,
       
    87             $this->_preferredRequestMethod,
       
    88             $this->_consumer->getRequestTokenUrl()
       
    89         );
       
    90 
       
    91         return $params;
       
    92     }
       
    93 
       
    94     /**
       
    95      * Generate and return a HTTP Client configured for the Header Request Scheme
       
    96      * specified by OAuth, for use in requesting a Request Token.
       
    97      *
       
    98      * @param array $params
       
    99      * @return Zend_Http_Client
       
   100      */
       
   101     public function getRequestSchemeHeaderClient(array $params)
       
   102     {
       
   103         $headerValue = $this->_httpUtility->toAuthorizationHeader(
       
   104             $params
       
   105         );
       
   106         $client = Zend_Oauth::getHttpClient();
       
   107         $client->setUri($this->_consumer->getRequestTokenUrl());
       
   108         $client->setHeaders('Authorization', $headerValue);
       
   109         $rawdata = $this->_httpUtility->toEncodedQueryString($params, true);
       
   110         if (!empty($rawdata)) {
       
   111             $client->setRawData($rawdata, 'application/x-www-form-urlencoded');
       
   112         }
       
   113         $client->setMethod($this->_preferredRequestMethod);
       
   114         return $client;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Generate and return a HTTP Client configured for the POST Body Request
       
   119      * Scheme specified by OAuth, for use in requesting a Request Token.
       
   120      *
       
   121      * @param  array $params
       
   122      * @return Zend_Http_Client
       
   123      */
       
   124     public function getRequestSchemePostBodyClient(array $params)
       
   125     {
       
   126         $client = Zend_Oauth::getHttpClient();
       
   127         $client->setUri($this->_consumer->getRequestTokenUrl());
       
   128         $client->setMethod($this->_preferredRequestMethod);
       
   129         $client->setRawData(
       
   130             $this->_httpUtility->toEncodedQueryString($params)
       
   131         );
       
   132         $client->setHeaders(
       
   133             Zend_Http_Client::CONTENT_TYPE,
       
   134             Zend_Http_Client::ENC_URLENCODED
       
   135         );
       
   136         return $client;
       
   137     }
       
   138 
       
   139     /**
       
   140      * Attempt a request based on the current configured OAuth Request Scheme and
       
   141      * return the resulting HTTP Response.
       
   142      *
       
   143      * @param  array $params
       
   144      * @return Zend_Http_Response
       
   145      */
       
   146     protected function _attemptRequest(array $params)
       
   147     {
       
   148         switch ($this->_preferredRequestScheme) {
       
   149             case Zend_Oauth::REQUEST_SCHEME_HEADER:
       
   150                 $httpClient = $this->getRequestSchemeHeaderClient($params);
       
   151                 break;
       
   152             case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
       
   153                 $httpClient = $this->getRequestSchemePostBodyClient($params);
       
   154                 break;
       
   155             case Zend_Oauth::REQUEST_SCHEME_QUERYSTRING:
       
   156                 $httpClient = $this->getRequestSchemeQueryStringClient($params,
       
   157                     $this->_consumer->getRequestTokenUrl());
       
   158                 break;
       
   159         }
       
   160         return $httpClient->request();
       
   161     }
       
   162 }