Current file: /Users/bill/git/HTTP_OAuth/HTTP/OAuth/Consumer/Request.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00%0.00%
0.00% 0 / 1
72.73%72.73%
72.73% 8 / 11 CRAP
82.79%82.79%
82.79% 101 / 122
 
HTTP_OAuth_Consumer_Request
0.00%0.00%
0.00% 0 / 1
72.73%72.73%
72.73% 8 / 11
82.79%82.79%
82.79% 101 / 122
 __construct($url = null, array $secrets = array()
100.00%100.00%
100.00% 1 / 1 3
100.00%100.00%
100.00% 7 / 7
 accept($object)
100.00%100.00%
100.00% 1 / 1 4
100.00%100.00%
100.00% 17 / 17
 getHTTPRequest2()
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 4 / 4
 setSecrets(array $secrets = array()
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 5 / 5
 getSecrets()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 setAuthType($type)
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 6 / 6
 getAuthType()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 send()
0.00%0.00%
0.00% 0 / 1 5.93
66.67%66.67%
66.67% 10 / 15
 buildRequest()
0.00%0.00%
0.00% 0 / 1 13.90
71.15%71.15%
71.15% 37 / 52
 getAuthForHeader(array $params)
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 8 / 8
 __call($method, $args)
0.00%0.00%
0.00% 0 / 1 2.02
83.33%83.33%
83.33% 5 / 6


       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/Request2.php';                                                
      25                 : require_once 'HTTP/Request2/Observer/Log.php';                                   
      26                 : require_once 'HTTP/OAuth/Message.php';                                           
      27                 : require_once 'HTTP/OAuth/Consumer/Response.php';                                 
      28                 : require_once 'HTTP/OAuth/Signature.php';                                         
      29                 : require_once 'HTTP/OAuth/Exception.php';                                         
      30                 :                                                                                  
      31                 : /**                                                                              
      32                 :  * HTTP_OAuth_Consumer_Request                                                   
      33                 :  *                                                                               
      34                 :  * Class to make OAuth requests to a provider.  Given a url, consumer secret,    
      35                 :  * token secret, and HTTP method make and sign a request to send.                
      36                 :  *                                                                               
      37                 :  * @category  HTTP                                                               
      38                 :  * @package   HTTP_OAuth                                                         
      39                 :  * @author    Jeff Hodsdon <jeffhodsdon@gmail.com>                               
      40                 :  * @copyright 2009 Jeff Hodsdon <jeffhodsdon@gmail.com>                          
      41                 :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License 
      42                 :  * @link      http://pear.php.net/package/HTTP_OAuth                             
      43                 :  * @link      http://github.com/jeffhodsdon/HTTP_OAuth                           
      44                 :  */                                                                              
      45                 : class HTTP_OAuth_Consumer_Request extends HTTP_OAuth_Message                     
      46                 : {                                                                                
      47                 :                                                                                  
      48                 :     /**                                                                          
      49                 :      *  Auth type constants                                                      
      50                 :      */                                                                          
      51                 :     const AUTH_HEADER = 1;                                                       
      52                 :     const AUTH_POST   = 2;                                                       
      53                 :     const AUTH_GET    = 3;                                                       
      54                 :                                                                                  
      55                 :     /**                                                                          
      56                 :      * Auth type                                                                 
      57                 :      *                                                                           
      58                 :      * @var int $authType Authorization type                                     
      59                 :      */                                                                          
      60                 :     protected $authType = self::AUTH_HEADER;                                     
      61                 :                                                                                  
      62                 :     /**                                                                          
      63                 :      * Secrets                                                                   
      64                 :      *                                                                           
      65                 :      * Consumer and token secrets that will be used to sign                      
      66                 :      * the request                                                               
      67                 :      *                                                                           
      68                 :      * @var array $secrets Array of consumer and token secret                    
      69                 :      */                                                                          
      70                 :     protected $secrets = array('', '');                                          
      71                 :                                                                                  
      72                 :     /**                                                                          
      73                 :      * HTTP_Request2 instance                                                    
      74                 :      *                                                                           
      75                 :      * @var HTTP_Request2 $request Instance of HTTP_Request2                     
      76                 :      */                                                                          
      77                 :     protected $request = null;                                                   
      78                 :                                                                                  
      79                 :     /**                                                                          
      80                 :      * Construct                                                                 
      81                 :      *                                                                           
      82                 :      * Sets url, secrets, and http method                                        
      83                 :      *                                                                           
      84                 :      * @param string $url     Url to be requested                                
      85                 :      * @param array  $secrets Array of consumer and token secret                 
      86                 :      *                                                                           
      87                 :      * @return void                                                              
      88                 :      */                                                                          
      89                 :     public function __construct($url = null, array $secrets = array())           
      90                 :     {                                                                            
      91              15 :         if ($url !== null) {                                                     
      92              10 :             $this->setUrl($url);                                                 
      93              10 :         }                                                                        
      94                 :                                                                                  
      95              15 :         if (count($secrets)) {                                                   
      96               1 :             $this->setSecrets($secrets);                                         
      97               1 :         }                                                                        
      98              15 :     }                                                                            
      99                 :                                                                                  
     100                 :     /**                                                                          
     101                 :      * Accept                                                                    
     102                 :      *                                                                           
     103                 :      * @param mixed $object Object to accept                                     
     104                 :      *                                                                           
     105                 :      * @see getHTTPRequest2()                                                    
     106                 :      * @return void                                                              
     107                 :      */                                                                          
     108                 :     public function accept($object)                                              
     109                 :     {                                                                            
     110              13 :         switch (get_class($object)) {                                            
     111              13 :         case 'HTTP_Request2':                                                    
     112              13 :             $this->request = $object;                                            
     113              13 :             foreach (self::$logs as $log) {                                      
     114               1 :                 $this->request->attach(new HTTP_Request2_Observer_Log($log));    
     115              13 :             }                                                                    
     116              13 :             break;                                                               
     117               1 :         default:                                                                 
     118               1 :             if ($object instanceof Log) {                                        
     119               1 :                 HTTP_OAuth::attachLog($object);                                  
     120               1 :                 $this->getHTTPRequest2()->attach(                                
     121               1 :                     new HTTP_Request2_Observer_Log($object)                      
     122               1 :                 );                                                               
     123               1 :             }                                                                    
     124               1 :             break;                                                               
     125              13 :         }                                                                        
     126              13 :     }                                                                            
     127                 :                                                                                  
     128                 :     /**                                                                          
     129                 :      * Returns $this->request if it is an instance of HTTP_Request.  If not, it   
     130                 :      * creates one.                                                              
     131                 :      *                                                                            
     132                 :      * @return HTTP_Request2                                                     
     133                 :      */                                                                          
     134                 :     protected function getHTTPRequest2()                                         
     135                 :     {                                                                            
     136              13 :         if (!$this->request instanceof HTTP_Request2) {                          
     137              10 :             $this->accept(new HTTP_Request2);                                    
     138              10 :         }                                                                        
     139              13 :         return $this->request;                                                   
     140                 :     }                                                                            
     141                 :                                                                                  
     142                 :     /**                                                                          
     143                 :      * Sets consumer/token secrets array                                         
     144                 :      *                                                                           
     145                 :      * @param array $secrets Array of secrets to set                             
     146                 :      *                                                                           
     147                 :      * @return void                                                              
     148                 :      */                                                                          
     149                 :     public function setSecrets(array $secrets = array())                         
     150                 :     {                                                                            
     151               7 :         if (count($secrets) == 1) {                                              
     152               1 :             $secrets[1] = '';                                                    
     153               1 :         }                                                                        
     154                 :                                                                                  
     155               7 :         $this->secrets = $secrets;                                               
     156               7 :     }                                                                            
     157                 :                                                                                  
     158                 :     /**                                                                          
     159                 :      * Gets secrets                                                              
     160                 :      *                                                                           
     161                 :      * @return array Secrets array                                               
     162                 :      */                                                                          
     163                 :     public function getSecrets()                                                 
     164                 :     {                                                                            
     165               2 :         return $this->secrets;                                                   
     166                 :     }                                                                            
     167                 :                                                                                  
     168                 :     /**                                                                          
     169                 :      * Sets authentication type                                                  
     170                 :      *                                                                           
     171                 :      * Valid auth types are self::AUTH_HEADER, self::AUTH_POST,                  
     172                 :      * and self::AUTH_GET                                                        
     173                 :      *                                                                           
     174                 :      * @param int $type Auth type defined by this class constants                
     175                 :      *                                                                           
     176                 :      * @return void                                                              
     177                 :      */                                                                          
     178                 :     public function setAuthType($type)                                           
     179                 :     {                                                                            
     180                 :         static $valid = array(self::AUTH_HEADER, self::AUTH_POST,                
     181               2 :             self::AUTH_GET);                                                     
     182               2 :         if (!in_array($type, $valid)) {                                          
     183               1 :             throw new InvalidArgumentException('Invalid Auth Type, see class ' . 
     184               1 :                 'constants');                                                    
     185                 :         }                                                                        
     186                 :                                                                                  
     187               1 :         $this->authType = $type;                                                 
     188               1 :     }                                                                            
     189                 :                                                                                  
     190                 :     /**                                                                          
     191                 :      * Gets authentication type                                                  
     192                 :      *                                                                           
     193                 :      * @return int Set auth type                                                 
     194                 :      */                                                                          
     195                 :     public function getAuthType()                                                
     196                 :     {                                                                            
     197               4 :         return $this->authType;                                                  
     198                 :     }                                                                            
     199                 :                                                                                  
     200                 :     /**                                                                          
     201                 :      * Sends request                                                             
     202                 :      *                                                                           
     203                 :      * Builds and sends the request. This will sign the request with             
     204                 :      * the given secrets at self::$secrets.                                      
     205                 :      *                                                                           
     206                 :      * @return HTTP_OAuth_Consumer_Response Response instance                    
     207                 :      * @throws HTTP_OAuth_Exception when request fails                           
     208                 :      */                                                                          
     209                 :     public function send()                                                       
     210                 :     {                                                                            
     211               3 :         $this->buildRequest();                                                   
     212               3 :         $request = $this->getHTTPRequest2();                                     
     213                 :                                                                                  
     214                 :         // Hack for the OAuth's spec + => %20 and HTTP_Request2                  
     215                 :         // HTTP_Request2 uses http_build_query() which does spaces               
     216                 :         // as '+' and not '%20'                                                  
     217               3 :         $headers     = $request->getHeaders();                                   
     218               3 :         $contentType = isset($headers['content-type'])                           
     219               3 :                        ? $headers['content-type'] : '';                          
     220               3 :         if ($this->getMethod() == 'POST'                                         
     221               3 :             && $contentType == 'application/x-www-form-urlencoded') {            
     222                 :                                                                                  
     223               0 :             $body = $this->getHTTPRequest2()->getBody();                         
     224               0 :             $body = str_replace('+', '%20', $body);                              
     225               0 :             $this->getHTTPRequest2()->setBody($body);                            
     226               0 :         }                                                                        
     227                 :                                                                                  
     228                 :         try {                                                                    
     229               3 :             $response = $this->getHTTPRequest2()->send();                        
     230               3 :         } catch (Exception $e) {                                                 
     231               0 :             throw new HTTP_OAuth_Exception($e->getMessage(), $e->getCode());     
     232                 :         }                                                                        
     233                 :                                                                                  
     234               3 :         return new HTTP_OAuth_Consumer_Response($response);                      
     235                 :     }                                                                            
     236                 :                                                                                  
     237                 :     /**                                                                          
     238                 :      * Builds request for sending                                                
     239                 :      *                                                                           
     240                 :      * Adds timestamp, nonce, signs, and creates the HttpRequest object.         
     241                 :      *                                                                           
     242                 :      * @return HttpRequest Instance of the request object ready to send()        
     243                 :      */                                                                          
     244                 :     protected function buildRequest()                                            
     245                 :     {                                                                            
     246               3 :         $method = $this->getSignatureMethod();                                   
     247               3 :         $this->debug('signing request with: ' . $method);                        
     248               3 :         $sig = HTTP_OAuth_Signature::factory($this->getSignatureMethod());       
     249                 :                                                                                  
     250               3 :         $this->oauth_timestamp = time();                                         
     251               3 :         $this->oauth_nonce     = md5(microtime(true) . rand(1, 999));            
     252               3 :         $this->oauth_version   = '1.0';                                          
     253               3 :         $params                = array_merge(                                    
     254               3 :             $this->getParameters(),                                              
     255               3 :             $this->getUrl()->getQueryVariables()                                 
     256               3 :         );                                                                       
     257               3 :         $this->oauth_signature = $sig->build(                                    
     258               3 :             $this->getMethod(),                                                  
     259               3 :             $this->getUrl()->getURL(),                                           
     260               3 :             $params,                                                             
     261               3 :             $this->secrets[0],                                                   
     262               3 :             $this->secrets[1]                                                    
     263               3 :         );                                                                       
     264                 :                                                                                  
     265               3 :         $params = $this->getOAuthParameters();                                   
     266               3 :         switch ($this->getAuthType()) {                                          
     267               3 :         case self::AUTH_HEADER:                                                  
     268               3 :             $auth = $this->getAuthForHeader($params);                            
     269               3 :             $this->setHeader('Authorization', $auth);                            
     270               3 :             break;                                                               
     271               0 :         case self::AUTH_POST:                                                    
     272               0 :             foreach ($params as $name => $value) {                               
     273               0 :                 $this->addPostParameter($name, $value);                          
     274               0 :             }                                                                    
     275               0 :             break;                                                               
     276               0 :         case self::AUTH_GET:                                                     
     277               0 :             break;                                                               
     278               3 :         }                                                                        
     279                 :                                                                                  
     280               3 :         switch ($this->getMethod()) {                                            
     281               3 :         case 'POST':                                                             
     282               0 :             foreach ($this->getParameters() as $name => $value) {                
     283               0 :                 if (substr($name, 0, 6) == 'oauth_') {                           
     284               0 :                     continue;                                                    
     285                 :                 }                                                                
     286                 :                                                                                  
     287               0 :                 $this->addPostParameter($name, $value);                          
     288               0 :             }                                                                    
     289               0 :             break;                                                               
     290               3 :         case 'GET':                                                              
     291               3 :             $url = $this->getUrl();                                              
     292               3 :             foreach ($this->getParameters() as $name => $value) {                
     293               3 :                 if (substr($name, 0, 6) == 'oauth_') {                           
     294               3 :                     continue;                                                    
     295                 :                 }                                                                
     296                 :                                                                                  
     297               2 :                 $url->setQueryVariable($name, $value);                           
     298               3 :             }                                                                    
     299                 :                                                                                  
     300               3 :             $this->setUrl($url);                                                 
     301               3 :             break;                                                               
     302               0 :         default:                                                                 
     303               0 :             break;                                                               
     304               3 :         }                                                                        
     305               3 :     }                                                                            
     306                 :                                                                                  
     307                 :     /**                                                                          
     308                 :      * Creates OAuth header                                                      
     309                 :      *                                                                           
     310                 :      * Given the passed in OAuth parameters, put them together                   
     311                 :      * in a formated string for a Authorization header.                          
     312                 :      *                                                                           
     313                 :      * @param array $params OAuth parameters                                     
     314                 :      *                                                                           
     315                 :      * @return void                                                              
     316                 :      */                                                                          
     317                 :     protected function getAuthForHeader(array $params)                           
     318                 :     {                                                                            
     319               3 :         $url    = $this->getUrl();                                               
     320               3 :         $realm  = $url->getScheme() . '://' . $url->getHost() . '/';             
     321               3 :         $header = 'OAuth realm="' . $realm . '"';                                
     322               3 :         foreach ($params as $name => $value) {                                   
     323               3 :             $header .= ", " . HTTP_OAuth::urlencode($name) . '="' .              
     324               3 :                 HTTP_OAuth::urlencode($value) . '"';                             
     325               3 :         }                                                                        
     326                 :                                                                                  
     327               3 :         return $header;                                                          
     328                 :     }                                                                            
     329                 :                                                                                  
     330                 :     /**                                                                          
     331                 :      * Call                                                                      
     332                 :      *                                                                           
     333                 :      * If method exists on HTTP_Request2 pass to that, otherwise                 
     334                 :      * throw BadMethodCallException                                              
     335                 :      *                                                                           
     336                 :      * @param string $method Name of the method                                  
     337                 :      * @param array  $args   Arguments for the method                            
     338                 :      *                                                                           
     339                 :      * @return mixed Result from method                                          
     340                 :      * @throws BadMethodCallException When method does not exist on HTTP_Request2
     341                 :      */                                                                          
     342                 :     public function __call($method, $args)                                       
     343                 :     {                                                                            
     344              13 :         $httpRequest2 = $this->getHTTPRequest2();                                
     345                 :                                                                                  
     346              13 :         if (is_callable(array($httpRequest2, $method))) {                        
     347              13 :             return call_user_func_array(                                         
     348              13 :                 array($httpRequest2, $method),                                   
     349                 :                 $args                                                            
     350              13 :             );                                                                   
     351                 :         }                                                                        
     352                 :                                                                                  
     353               0 :         throw new BadMethodCallException($method);                               
     354                 :     }                                                                            
     355                 :                                                                                  
     356                 : }                                                                                
     357                 :                                                                                  
     358                 : ?>                                                                               

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.