|
||||
| Coverage | |||||||||||||
| Classes | Functions / Methods | Lines | |||||||||||
| Total |
|
100.00% | 1 / 1 |
|
100.00% | 18 / 18 | CRAP |
|
100.00% | 82 / 82 | |||
| HTTP_OAuth_Consumer |
|
100.00% | 1 / 1 |
|
100.00% | 18 / 18 |
|
100.00% | 82 / 82 | ||||
| __construct($key, $secret, $token = null, $tokenSecret = null) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 5 / 5 | ||||||
| getRequestToken($url, $callback = 'oob', array $additional = array() |
|
100.00% | 1 / 1 | 3 |
|
100.00% | 12 / 12 | ||||||
| getAccessToken($url, $verifier = '', array $additional = array() |
|
100.00% | 1 / 1 | 6 |
|
100.00% | 16 / 16 | ||||||
| getAuthorizeUrl($url, array $additional = array() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 3 / 3 | ||||||
| sendRequest($url, array $additional = array() |
|
100.00% | 1 / 1 | 2 |
|
100.00% | 15 / 15 | ||||||
| getKey() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
| getSecret() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
| getToken() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
| setToken($token) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 3 / 3 | ||||||
| getTokenSecret() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
| setTokenSecret($secret) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 3 / 3 | ||||||
| getSignatureMethod() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
| setSignatureMethod($method) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 2 / 2 | ||||||
| getSecrets() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
| accept($object) |
|
100.00% | 1 / 1 | 3 |
|
100.00% | 11 / 11 | ||||||
| getOAuthConsumerRequest() |
|
100.00% | 1 / 1 | 2 |
|
100.00% | 4 / 4 | ||||||
| getLastRequest() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
| getLastResponse() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 | ||||||
1 : <?php 2 : /** 3 : * HTTP_OAuth 4 : * 5 : * Implementation of the OAuth specification 6 : * 7 : * PHP version 5.2.0+ 8 : * 9 : * LICENSE: This source file is subject to the New BSD license that is 10 : * available through the world-wide-web at the following URI: 11 : * http://www.opensource.org/licenses/bsd-license.php. If you did not receive 12 : * a copy of the New BSD License and are unable to obtain it through the web, 13 : * please send a note to license@php.net so we can mail you a copy immediately. 14 : * 15 : * @category HTTP 16 : * @package HTTP_OAuth 17 : * @author Jeff Hodsdon <jeffhodsdon@gmail.com> 18 : * @copyright 2009 Jeff Hodsdon <jeffhodsdon@gmail.com> 19 : * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 20 : * @link http://pear.php.net/package/HTTP_OAuth 21 : * @link http://github.com/jeffhodsdon/HTTP_OAuth 22 : */ 23 : 24 : require_once 'HTTP/OAuth.php'; 25 : require_once 'HTTP/OAuth/Consumer/Request.php'; 26 : require_once 'HTTP/OAuth/Consumer/Exception/InvalidResponse.php'; 27 : 28 : /** 29 : * HTTP_OAuth_Consumer 30 : * 31 : * Main consumer class that assists consumers in establishing OAuth 32 : * creditials and making OAuth requests. 33 : * 34 : * <code> 35 : * $consumer = new HTTP_OAuth_Consumer('key', 'secret'); 36 : * $consumer->getRequestToken('http://example.com/oauth/request_token', $callback); 37 : * 38 : * // Store tokens 39 : * $_SESSION['token'] = $consumer->getToken(); 40 : * $_SESSION['token_secret'] = $consumer->getTokenSecret(); 41 : * 42 : * $url = $consumer->getAuthorizeUrl('http://example.com/oauth/authorize'); 43 : * http_redirect($url); // function from pecl_http 44 : * 45 : * // When they come back via the $callback url 46 : * $consumer = new HTTP_OAuth_Consumer('key', 'secret', $_SESSION['token'], 47 : * $_SESSION['token_secret']); 48 : * $consumer->getAccessToken('http://example.com/oauth/access_token'); 49 : * 50 : * // Store tokens 51 : * $_SESSION['token'] = $consumer->getToken(); 52 : * $_SESSION['token_secret'] = $consumer->getTokenSecret(); 53 : * 54 : * // $response is an instance of HTTP_OAuth_Consumer_Response 55 : * $response = $consumer->sendRequest('http://example.com/oauth/protected_resource'); 56 : * </code> 57 : * 58 : * @category HTTP 59 : * @package HTTP_OAuth 60 : * @author Jeff Hodsdon <jeffhodsdon@gmail.com> 61 : * @copyright 2009 Jeff Hodsdon <jeffhodsdon@gmail.com> 62 : * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 63 : * @link http://pear.php.net/package/HTTP_OAuth 64 : * @link http://github.com/jeffhodsdon/HTTP_OAuth 65 : */ 66 : class HTTP_OAuth_Consumer extends HTTP_OAuth 67 : { 68 : 69 : /** 70 : * Consumer key 71 : * 72 : * @var string $key Consumer key 73 : */ 74 : protected $key = null; 75 : 76 : /** 77 : * secret 78 : * 79 : * @var string $secret Consumer secret 80 : */ 81 : protected $secret = null; 82 : 83 : /** 84 : * Token 85 : * 86 : * @var string Access/Request token 87 : */ 88 : protected $token = null; 89 : 90 : /** 91 : * Token secret 92 : * 93 : * @var string $tokenSecret Access/Request token secret 94 : */ 95 : protected $tokenSecret = null; 96 : 97 : /** 98 : * Signature method 99 : * 100 : * @var string $signatureMethod Signature method 101 : */ 102 : protected $signatureMethod = 'HMAC-SHA1'; 103 : 104 : /** 105 : * Instance of HTTP_OAuth_Consumer_Request 106 : * 107 : * @see accept() 108 : * @see getOAuthConsumerRequest() 109 : * @var HTTP_OAuth_Consumer_Request 110 : */ 111 : protected $consumerRequest = null; 112 : 113 : /** 114 : * Instance of the last request made 115 : * 116 : * @var HTTP_OAuth_Consumer_Request $lastRequest The last request made 117 : */ 118 : protected $lastRequest = null; 119 : 120 : /** 121 : * Instance of the last response received 122 : * 123 : * @var HTTP_OAuth_Consumer_Response 124 : */ 125 : protected $lastResponse =null; 126 : 127 : /** 128 : * Construct 129 : * 130 : * @param string $key Consumer key 131 : * @param string $secret Consumer secret 132 : * @param string $token Access/Reqest token 133 : * @param string $tokenSecret Access/Reqest token secret 134 : * 135 : * @return void 136 : */ 137 : public function __construct($key, $secret, $token = null, $tokenSecret = null) 138 : { 139 12 : $this->key = $key; 140 12 : $this->secret = $secret; 141 12 : $this->setToken($token); 142 12 : $this->setTokenSecret($tokenSecret); 143 12 : } 144 : 145 : /** 146 : * Get request token 147 : * 148 : * @param string $url Request token url 149 : * @param string $callback Callback url 150 : * @param array $additional Additional parameters to be in the request 151 : * recommended in the spec. 152 : * @param string $method HTTP method to use for the request 153 : * 154 : * @return void 155 : * @throws HTTP_OAuth_Consumer_Exception_InvalidResponse Missing token/secret 156 : */ 157 : public function getRequestToken($url, $callback = 'oob', 158 : array $additional = array(), $method = 'POST' 159 : ) { 160 2 : $this->debug('Getting request token from ' . $url); 161 2 : $additional['oauth_callback'] = $callback; 162 : 163 2 : $this->debug('callback: ' . $callback); 164 2 : $response = $this->sendRequest($url, $additional, $method); 165 2 : $data = $response->getDataFromBody(); 166 2 : if (empty($data['oauth_token']) || empty($data['oauth_token_secret'])) { 167 1 : throw new HTTP_OAuth_Consumer_Exception_InvalidResponse( 168 1 : 'Failed getting token and token secret from response', $response 169 1 : ); 170 : } 171 : 172 1 : $this->setToken($data['oauth_token']); 173 1 : $this->setTokenSecret($data['oauth_token_secret']); 174 1 : } 175 : 176 : /** 177 : * Get access token 178 : * 179 : * @param string $url Access token url 180 : * @param string $verifier OAuth verifier from the provider 181 : * @param array $additional Additional parameters to be in the request 182 : * recommended in the spec. 183 : * @param string $method HTTP method to use for the request 184 : * 185 : * @return array Token and token secret 186 : * @throws HTTP_OAuth_Consumer_Exception_InvalidResponse Mising token/secret 187 : */ 188 : public function getAccessToken($url, $verifier = '', 189 : array $additional = array(), $method = 'POST' 190 : ) { 191 3 : if ($this->getToken() === null || $this->getTokenSecret() === null) { 192 1 : throw new HTTP_OAuth_Exception('No token or token_secret'); 193 : } 194 : 195 2 : $this->debug('Getting access token from ' . $url); 196 2 : if($verifier !== null) { 197 2 : $additional['oauth_verifier'] = $verifier; 198 2 : } 199 : 200 2 : $this->debug('verifier: ' . $verifier); 201 2 : $response = $this->sendRequest($url, $additional, $method); 202 2 : $data = $response->getDataFromBody(); 203 2 : if (empty($data['oauth_token']) || empty($data['oauth_token_secret'])) { 204 1 : throw new HTTP_OAuth_Consumer_Exception_InvalidResponse( 205 1 : 'Failed getting token and token secret from response', $response 206 1 : ); 207 : } 208 : 209 1 : $this->setToken($data['oauth_token']); 210 1 : $this->setTokenSecret($data['oauth_token_secret']); 211 1 : } 212 : 213 : /** 214 : * Get authorize url 215 : * 216 : * @param string $url Authorize url 217 : * @param array $additional Additional parameters for the auth url 218 : * 219 : * @return string Authorize url 220 : */ 221 : public function getAuthorizeUrl($url, array $additional = array()) 222 : { 223 1 : $params = array('oauth_token' => $this->getToken()); 224 1 : $params = array_merge($additional, $params); 225 : 226 1 : return sprintf('%s?%s', $url, HTTP_OAuth::buildHTTPQuery($params)); 227 : } 228 : 229 : /** 230 : * Send request 231 : * 232 : * @param string $url URL of the protected resource 233 : * @param array $additional Additional parameters 234 : * @param string $method HTTP method to use 235 : * 236 : * @return HTTP_OAuth_Consumer_Response Instance of a response class 237 : */ 238 : public function sendRequest($url, array $additional = array(), $method = 'POST') 239 : { 240 : $params = array( 241 5 : 'oauth_consumer_key' => $this->key, 242 5 : 'oauth_signature_method' => $this->getSignatureMethod() 243 5 : ); 244 : 245 5 : if ($this->getToken()) { 246 5 : $params['oauth_token'] = $this->getToken(); 247 5 : } 248 : 249 5 : $params = array_merge($additional, $params); 250 : 251 5 : $req = clone $this->getOAuthConsumerRequest(); 252 : 253 5 : $req->setUrl($url); 254 5 : $req->setMethod($method); 255 5 : $req->setSecrets($this->getSecrets()); 256 5 : $req->setParameters($params); 257 5 : $this->lastResponse = $req->send(); 258 5 : $this->lastRequest = $req; 259 : 260 5 : return $this->lastResponse; 261 : } 262 : 263 : /** 264 : * Get key 265 : * 266 : * @return string Consumer key 267 : */ 268 : public function getKey() 269 : { 270 1 : return $this->key; 271 : } 272 : 273 : /** 274 : * Get secret 275 : * 276 : * @return string Consumer secret 277 : */ 278 : public function getSecret() 279 : { 280 1 : return $this->secret; 281 : } 282 : 283 : /** 284 : * Get token 285 : * 286 : * @return string Token 287 : */ 288 : public function getToken() 289 : { 290 8 : return $this->token; 291 : } 292 : 293 : /** 294 : * Set token 295 : * 296 : * @param string $token Request/Access token 297 : * 298 : * @return void 299 : */ 300 : public function setToken($token) 301 : { 302 12 : $this->debug('token is now: ' . $token); 303 12 : $this->token = $token; 304 12 : } 305 : 306 : /** 307 : * Get token secret 308 : * 309 : * @return string Accessoken secret 310 : */ 311 : public function getTokenSecret() 312 : { 313 4 : return $this->tokenSecret; 314 : } 315 : 316 : /** 317 : * Set token secret 318 : * 319 : * @param string $secret Token secret 320 : * 321 : * @return void 322 : */ 323 : public function setTokenSecret($secret) 324 : { 325 12 : $this->debug('token_secret is now: ' . $secret); 326 12 : $this->tokenSecret = $secret; 327 12 : } 328 : 329 : /** 330 : * Get signature method 331 : * 332 : * @return string Signature method 333 : */ 334 : public function getSignatureMethod() 335 : { 336 6 : return $this->signatureMethod; 337 : } 338 : 339 : /** 340 : * Set signature method 341 : * 342 : * @param string $method Signature method to use 343 : * 344 : * @return void 345 : */ 346 : public function setSignatureMethod($method) 347 : { 348 1 : $this->signatureMethod = $method; 349 1 : } 350 : 351 : /** 352 : * Get secrets 353 : * 354 : * @return array Array possible secrets 355 : */ 356 : public function getSecrets() 357 : { 358 5 : return array($this->secret, (string) $this->tokenSecret); 359 : } 360 : 361 : /** 362 : * Accepts a custom instance of HTTP_OAuth_Consumer_Request. 363 : * 364 : * @param HTTP_OAuth_Consumer_Request $object Custom instance 365 : * 366 : * @see getOAuthConsumerRequest() 367 : * @return void 368 : */ 369 : public function accept($object) 370 : { 371 3 : $class = get_class($object); 372 : switch ($class) 373 : { 374 3 : case 'HTTP_OAuth_Consumer_Request': 375 1 : $this->consumerRequest = $object; 376 1 : break; 377 2 : case 'HTTP_Request2': 378 1 : $this->getOAuthConsumerRequest()->accept($object); 379 1 : break; 380 1 : default: 381 1 : throw new HTTP_OAuth_Exception('Could not accept: ' . $class); 382 : break; 383 1 : } 384 2 : } 385 : 386 : /** 387 : * Gets instance of HTTP_OAuth_Consumer_Request 388 : * 389 : * @see accept() 390 : * @return HTTP_OAuth_Consumer_Request 391 : */ 392 : public function getOAuthConsumerRequest() 393 : { 394 3 : if (!$this->consumerRequest instanceof HTTP_OAuth_Consumer_Request) { 395 3 : $this->consumerRequest = new HTTP_OAuth_Consumer_Request; 396 3 : } 397 3 : return $this->consumerRequest; 398 : } 399 : 400 : /** 401 : * Gets the last request 402 : * 403 : * @return null|HTTP_OAuth_Consumer_Request Instance of the last request 404 : * @see self::sendRequest() 405 : */ 406 : public function getLastRequest() 407 : { 408 1 : return $this->lastRequest; 409 : } 410 : 411 : /** 412 : * Gets the most recent HTTP_OAuth_Consumer_Response object 413 : * 414 : * @return HTTP_OAuth_Consumer_Response|null 415 : */ 416 : public function getLastResponse() 417 : { 418 1 : return $this->lastResponse; 419 : } 420 : } 421 : 422 : ?> |
| Generated by PHP_CodeCoverage 1.0.2 using PHP 5.3.3 and PHPUnit 3.5.6 at Tue Dec 28 21:42:13 PST 2010. |