HTTP_OAuth
[ class tree: HTTP_OAuth ] [ index: HTTP_OAuth ] [ all elements ]

Source for file Consumer.php

Documentation is available at Consumer.php

  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->getAuthorizationUrl('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. {
  67.  
  68.     /**
  69.      * Consumer key
  70.      *
  71.      * @var string $key Consumer key
  72.      */
  73.     protected $key = null;
  74.  
  75.     /**
  76.      * secret
  77.      *
  78.      * @var string $secret Consumer secret
  79.      */
  80.     protected $secret = null;
  81.  
  82.     /**
  83.      * Token
  84.      *
  85.      * @var string Access/Request token
  86.      */
  87.     protected $token = null;
  88.  
  89.     /**
  90.      * Token secret
  91.      *
  92.      * @var string $tokenSecret Access/Request token secret
  93.      */
  94.     protected $tokenSecret = null;
  95.  
  96.     /**
  97.      * Signature method
  98.      *
  99.      * @var string $signatureMethod Signature method
  100.      */
  101.     protected $signatureMethod = 'HMAC-SHA1';
  102.  
  103.     /**
  104.      * Instance of HTTP_OAuth_Consumer_Request
  105.      * 
  106.      * @see accept()
  107.      * @see getOAuthConsumerRequest()
  108.      * @var HTTP_OAuth_Consumer_Request 
  109.      */
  110.     protected $consumerRequest = null;
  111.  
  112.     /**
  113.      * Instance of the last request made
  114.      *
  115.      * @var HTTP_OAuth_Consumer_Request $lastRequest The last request made
  116.      */
  117.     protected $lastRequest = null;
  118.  
  119.     /**
  120.      * Construct
  121.      *
  122.      * @param string $key         Consumer key
  123.      * @param string $secret      Consumer secret
  124.      * @param string $token       Access/Reqest token
  125.      * @param string $tokenSecret Access/Reqest token secret
  126.      *
  127.      * @return void 
  128.      */
  129.     public function __construct($key$secret$token = null$tokenSecret = null)
  130.     {
  131.         $this->key    = $key;
  132.         $this->secret = $secret;
  133.         $this->setToken($token);
  134.         $this->setTokenSecret($tokenSecret);
  135.     }
  136.  
  137.     /**
  138.      * Get request token
  139.      *
  140.      * @param string $url        Request token url
  141.      * @param string $callback   Callback url
  142.      * @param array  $additional Additional parameters to be in the request
  143.      *                            recommended in the spec.
  144.      * @param string $method     HTTP method to use for the request
  145.      *
  146.      * @return void 
  147.      * @throws HTTP_OAuth_Consumer_Exception_InvalidResponse Missing token/secret
  148.      */
  149.     public function getRequestToken($url$callback 'oob',
  150.         array $additional = array()$method 'POST'
  151.     )
  152.     {
  153.         $this->debug('Getting request token from ' $url);
  154.         $additional['oauth_callback'$callback;
  155.  
  156.         $this->debug('callback: ' $callback);
  157.         $response $this->sendRequest($url$additional$method);
  158.         $data     $response->getDataFromBody();
  159.         if (empty($data['oauth_token']|| empty($data['oauth_token_secret'])) {
  160.             throw new HTTP_OAuth_Consumer_Exception_InvalidResponse(
  161.                 'Failed getting token and token secret from response'$response
  162.             );
  163.         }
  164.  
  165.         $this->setToken($data['oauth_token']);
  166.         $this->setTokenSecret($data['oauth_token_secret']);
  167.     }
  168.  
  169.     /**
  170.      * Get access token
  171.      *
  172.      * @param string $url        Access token url
  173.      * @param string $verifier   OAuth verifier from the provider
  174.      * @param array  $additional Additional parameters to be in the request
  175.      *                            recommended in the spec.
  176.      * @param string $method     HTTP method to use for the request
  177.      *
  178.      * @return array Token and token secret
  179.      * @throws HTTP_OAuth_Consumer_Exception_InvalidResponse Mising token/secret
  180.      */
  181.     public function getAccessToken($url$verifier '',
  182.         array $additional = array()$method 'POST'
  183.     )
  184.     {
  185.         if ($this->getToken(=== null || $this->getTokenSecret(=== null{
  186.             throw new HTTP_OAuth_Exception('No token or token_secret');
  187.         }
  188.  
  189.         $this->debug('Getting access token from ' $url);
  190.         $additional['oauth_verifier'$verifier;
  191.  
  192.         $this->debug('verifier: ' $verifier);
  193.         $response $this->sendRequest($url$additional$method);
  194.         $data     $response->getDataFromBody();
  195.         if (empty($data['oauth_token']|| empty($data['oauth_token_secret'])) {
  196.             throw new HTTP_OAuth_Consumer_Exception_InvalidResponse(
  197.                 'Failed getting token and token secret from response'$response
  198.             );
  199.         }
  200.  
  201.         $this->setToken($data['oauth_token']);
  202.         $this->setTokenSecret($data['oauth_token_secret']);
  203.     }
  204.  
  205.     /**
  206.      * Get authorize url
  207.      *
  208.      * @param string $url        Authorization url
  209.      * @param array  $additional Additional parameters for the auth url
  210.      *
  211.      * @return string Authorization url
  212.      */
  213.     public function getAuthorizeUrl($urlarray $additional = array())
  214.     {
  215.         $params = array('oauth_token' => $this->getToken());
  216.         $params array_merge($additional$params);
  217.  
  218.         return sprintf('%s?%s'$urlHTTP_OAuth::buildHTTPQuery($params));
  219.     }
  220.  
  221.     /**
  222.      * Send request
  223.      *
  224.      * @param string $url        URL of the protected resource
  225.      * @param array  $additional Additional parameters
  226.      * @param string $method     HTTP method to use
  227.      *
  228.      * @return HTTP_OAuth_Consumer_Response Instance of a response class
  229.      */
  230.     public function sendRequest($urlarray $additional = array()$method 'POST')
  231.     {
  232.         $params = array(
  233.             'oauth_consumer_key'     => $this->key,
  234.             'oauth_signature_method' => $this->getSignatureMethod()
  235.         );
  236.  
  237.         if ($this->getToken()) {
  238.             $params['oauth_token'$this->getToken();
  239.         }
  240.  
  241.         $params array_merge($additional$params);
  242.  
  243.         $req = clone $this->getOAuthConsumerRequest();
  244.  
  245.         $req->setUrl($url);
  246.         $req->setMethod($method);
  247.         $req->setSecrets($this->getSecrets());
  248.         $req->setParameters($params);
  249.         $res $req->send();
  250.  
  251.         $this->lastRequest = $req;
  252.  
  253.         return $res;
  254.     }
  255.  
  256.     /**
  257.      * Get key
  258.      *
  259.      * @return string Consumer key
  260.      */
  261.     public function getKey()
  262.     {
  263.         return $this->key;
  264.     }
  265.  
  266.     /**
  267.      * Get secret
  268.      *
  269.      * @return string Consumer secret
  270.      */
  271.     public function getSecret()
  272.     {
  273.         return $this->secret;
  274.     }
  275.  
  276.     /**
  277.      * Get token
  278.      *
  279.      * @return string Token
  280.      */
  281.     public function getToken()
  282.     {
  283.         return $this->token;
  284.     }
  285.  
  286.     /**
  287.      * Set token
  288.      *
  289.      * @param string $token Request/Access token
  290.      *
  291.      * @return void 
  292.      */
  293.     public function setToken($token)
  294.     {
  295.         $this->debug('token is now: ' $token);
  296.         $this->token = $token;
  297.     }
  298.  
  299.     /**
  300.      * Get token secret
  301.      *
  302.      * @return string Accessoken secret
  303.      */
  304.     public function getTokenSecret()
  305.     {
  306.         return $this->tokenSecret;
  307.     }
  308.  
  309.     /**
  310.      * Set token secret
  311.      *
  312.      * @param string $secret Token secret
  313.      *
  314.      * @return void 
  315.      */
  316.     public function setTokenSecret($secret)
  317.     {
  318.         $this->debug('token_secret is now: ' $secret);
  319.         $this->tokenSecret = $secret;
  320.     }
  321.  
  322.     /**
  323.      * Get signature method
  324.      *
  325.      * @return string Signature method
  326.      */
  327.     public function getSignatureMethod()
  328.     {
  329.         return $this->signatureMethod;
  330.     }
  331.  
  332.     /**
  333.      * Set signature method
  334.      *
  335.      * @param string $method Signature method to use
  336.      *
  337.      * @return void 
  338.      */
  339.     public function setSignatureMethod($method)
  340.     {
  341.         $this->signatureMethod = $method;
  342.     }
  343.  
  344.     /**
  345.      * Get secrets
  346.      *
  347.      * @return array Array possible secrets
  348.      */
  349.     protected function getSecrets()
  350.     {
  351.         return array($this->secret(string) $this->tokenSecret);
  352.     }
  353.  
  354.     /**
  355.      * Accepts a custom instance of HTTP_OAuth_Consumer_Request.
  356.      * 
  357.      * @param HTTP_OAuth_Consumer_Request $object Custom instance
  358.      * 
  359.      * @see getOAuthConsumerRequest()
  360.      * @return void 
  361.      */
  362.     public function accept($object)
  363.     {
  364.         $class get_class($object);
  365.         switch ($class)
  366.         {
  367.         case 'HTTP_OAuth_Consumer_Request':
  368.             $this->consumerRequest = $object;
  369.             break;
  370.         case 'HTTP_Request2':
  371.             $this->getOAuthConsumerRequest()->accept($object);
  372.             break;
  373.         default:
  374.             throw new HTTP_OAuth_Exception('Could not accept: ' $class);
  375.             break;
  376.         }
  377.     }
  378.  
  379.     /**
  380.      * Gets instance of HTTP_OAuth_Consumer_Request
  381.      *
  382.      * @see accept()
  383.      * @return HTTP_OAuth_Consumer_Request 
  384.      */
  385.     public function getOAuthConsumerRequest()
  386.     {
  387.         if (!$this->consumerRequest instanceof HTTP_OAuth_Consumer_Request{
  388.             $this->consumerRequest = new HTTP_OAuth_Consumer_Request;
  389.         
  390.         return $this->consumerRequest;
  391.     }
  392.  
  393.     /**
  394.      * Gets the last request
  395.      *
  396.      * @return null|HTTP_OAuth_Consumer_RequestInstance of the last request
  397.      * @see self::sendRequest()
  398.      */
  399.     public function getLastRequest()
  400.     {
  401.         return $this->lastRequest;
  402.     }
  403. }
  404.  
  405. ?>

Documentation generated on Mon, 30 Nov 2009 23:00:04 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.