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
22 /** Zend_Oauth_Http */
23 require_once 'Zend/Oauth/Http.php';
25 /** Zend_Oauth_Token_Access */
26 require_once 'Zend/Oauth/Token/Access.php';
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
34 class Zend_Oauth_Http_AccessToken extends Zend_Oauth_Http
37 * Singleton instance if required of the HTTP client
39 * @var Zend_Http_Client
41 protected $_httpClient = null;
44 * Initiate a HTTP request to retrieve an Access Token.
46 * @return Zend_Oauth_Token_Access
48 public function execute()
50 $params = $this->assembleParams();
51 $response = $this->startRequestCycle($params);
52 $return = new Zend_Oauth_Token_Access($response);
57 * Assemble all parameters for an OAuth Access Token request.
61 public function assembleParams()
64 'oauth_consumer_key' => $this->_consumer->getConsumerKey(),
65 'oauth_nonce' => $this->_httpUtility->generateNonce(),
66 'oauth_signature_method' => $this->_consumer->getSignatureMethod(),
67 'oauth_timestamp' => $this->_httpUtility->generateTimestamp(),
68 'oauth_token' => $this->_consumer->getLastRequestToken()->getToken(),
69 'oauth_version' => $this->_consumer->getVersion(),
72 if (!empty($this->_parameters)) {
73 $params = array_merge($params, $this->_parameters);
76 $params['oauth_signature'] = $this->_httpUtility->sign(
78 $this->_consumer->getSignatureMethod(),
79 $this->_consumer->getConsumerSecret(),
80 $this->_consumer->getLastRequestToken()->getTokenSecret(),
81 $this->_preferredRequestMethod,
82 $this->_consumer->getAccessTokenUrl()
89 * Generate and return a HTTP Client configured for the Header Request Scheme
90 * specified by OAuth, for use in requesting an Access Token.
92 * @param array $params
93 * @return Zend_Http_Client
95 public function getRequestSchemeHeaderClient(array $params)
97 $params = $this->_cleanParamsOfIllegalCustomParameters($params);
98 $headerValue = $this->_toAuthorizationHeader($params);
99 $client = Zend_Oauth::getHttpClient();
101 $client->setUri($this->_consumer->getAccessTokenUrl());
102 $client->setHeaders('Authorization', $headerValue);
103 $client->setMethod($this->_preferredRequestMethod);
109 * Generate and return a HTTP Client configured for the POST Body Request
110 * Scheme specified by OAuth, for use in requesting an Access Token.
112 * @param array $params
113 * @return Zend_Http_Client
115 public function getRequestSchemePostBodyClient(array $params)
117 $params = $this->_cleanParamsOfIllegalCustomParameters($params);
118 $client = Zend_Oauth::getHttpClient();
119 $client->setUri($this->_consumer->getAccessTokenUrl());
120 $client->setMethod($this->_preferredRequestMethod);
122 $this->_httpUtility->toEncodedQueryString($params)
125 Zend_Http_Client::CONTENT_TYPE,
126 Zend_Http_Client::ENC_URLENCODED
132 * Generate and return a HTTP Client configured for the Query String Request
133 * Scheme specified by OAuth, for use in requesting an Access Token.
135 * @param array $params
137 * @return Zend_Http_Client
139 public function getRequestSchemeQueryStringClient(array $params, $url)
141 $params = $this->_cleanParamsOfIllegalCustomParameters($params);
142 return parent::getRequestSchemeQueryStringClient($params, $url);
146 * Attempt a request based on the current configured OAuth Request Scheme and
147 * return the resulting HTTP Response.
149 * @param array $params
150 * @return Zend_Http_Response
152 protected function _attemptRequest(array $params)
154 switch ($this->_preferredRequestScheme) {
155 case Zend_Oauth::REQUEST_SCHEME_HEADER:
156 $httpClient = $this->getRequestSchemeHeaderClient($params);
158 case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
159 $httpClient = $this->getRequestSchemePostBodyClient($params);
161 case Zend_Oauth::REQUEST_SCHEME_QUERYSTRING:
162 $httpClient = $this->getRequestSchemeQueryStringClient($params,
163 $this->_consumer->getAccessTokenUrl());
166 return $httpClient->request();
170 * Access Token requests specifically may not contain non-OAuth parameters.
171 * So these should be striped out and excluded. Detection is easy since
172 * specified OAuth parameters start with "oauth_", Extension params start
173 * with "xouth_", and no other parameters should use these prefixes.
175 * xouth params are not currently allowable.
177 * @param array $params
180 protected function _cleanParamsOfIllegalCustomParameters(array $params)
182 foreach ($params as $key=>$value) {
183 if (!preg_match("/^oauth_/", $key)) {
184 unset($params[$key]);