web/lib/Zend/Service/Amazon/Authentication/V2.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_Service_Amazon
       
    17  * @subpackage Authentication
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Service_Amazon_Authentication
       
    24  */
       
    25 require_once 'Zend/Service/Amazon/Authentication.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Crypt_Hmac
       
    29  */
       
    30 require_once 'Zend/Crypt/Hmac.php';
       
    31 
       
    32 /**
       
    33  * @category   Zend
       
    34  * @package    Zend_Service_Amazon
       
    35  * @subpackage Authentication
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_Service_Amazon_Authentication_V2 extends Zend_Service_Amazon_Authentication
       
    40 {
       
    41     /**
       
    42      * Signature Version
       
    43      */
       
    44     protected $_signatureVersion = '2';
       
    45 
       
    46     /**
       
    47      * Signature Encoding Method
       
    48      */
       
    49     protected $_signatureMethod = 'HmacSHA256';
       
    50     
       
    51     /**
       
    52      * Type of http request
       
    53      * @var string
       
    54      */
       
    55     protected $_httpMethod = "POST";
       
    56 
       
    57     /**
       
    58      * Generate the required attributes for the signature
       
    59      * @param string $url
       
    60      * @param array $parameters
       
    61      * @return string
       
    62      */
       
    63     public function generateSignature($url, array &$parameters)
       
    64     {
       
    65         $parameters['AWSAccessKeyId']   = $this->_accessKey;
       
    66         $parameters['SignatureVersion'] = $this->_signatureVersion;
       
    67         $parameters['Version']          = $this->_apiVersion;
       
    68         $parameters['SignatureMethod']  = $this->_signatureMethod;
       
    69         if(!isset($parameters['Timestamp'])) {
       
    70             $parameters['Timestamp']    = gmdate('Y-m-d\TH:i:s\Z', time()+10);
       
    71         }
       
    72 
       
    73         $data = $this->_signParameters($url, $parameters);
       
    74         
       
    75         return $data;
       
    76     }
       
    77     
       
    78     /**
       
    79      * Set http request type to POST or GET
       
    80      * @param $method string
       
    81      */
       
    82     public function setHttpMethod($method = "POST") {
       
    83         $this->_httpMethod = strtoupper($method);
       
    84     }
       
    85     
       
    86     /**
       
    87      * Get the current http request type
       
    88      * @return string
       
    89      */
       
    90     public function getHttpMethod()
       
    91     {
       
    92         return $this->_httpMethod;
       
    93     }
       
    94 
       
    95     /**
       
    96      * Computes the RFC 2104-compliant HMAC signature for request parameters
       
    97      *
       
    98      * This implements the Amazon Web Services signature, as per the following
       
    99      * specification:
       
   100      *
       
   101      * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
       
   102      *    excluding <tt>Signature</tt>, the value of which is being created),
       
   103      *    ignoring case.
       
   104      *
       
   105      * 2. Iterate over the sorted list and append the parameter name (in its
       
   106      *    original case) and then its value. Do not URL-encode the parameter
       
   107      *    values before constructing this string. Do not use any separator
       
   108      *    characters when appending strings.
       
   109      *
       
   110      * @param  string $queue_url  Queue URL
       
   111      * @param  array  $parameters the parameters for which to get the signature.
       
   112      *
       
   113      * @return string the signed data.
       
   114      */
       
   115     protected function _signParameters($url, array &$paramaters)
       
   116     {
       
   117         $data = $this->_httpMethod . "\n";
       
   118         $data .= parse_url($url, PHP_URL_HOST) . "\n";
       
   119         $data .= ('' == $path = parse_url($url, PHP_URL_PATH)) ? '/' : $path;
       
   120         $data .= "\n";
       
   121 
       
   122         uksort($paramaters, 'strcmp');
       
   123         unset($paramaters['Signature']);
       
   124 
       
   125         $arrData = array();
       
   126         foreach($paramaters as $key => $value) {
       
   127             $arrData[] = $key . '=' . str_replace('%7E', '~', rawurlencode($value));
       
   128         }
       
   129 
       
   130         $data .= implode('&', $arrData);
       
   131 
       
   132         $hmac = Zend_Crypt_Hmac::compute($this->_secretKey, 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
       
   133 
       
   134         $paramaters['Signature'] = base64_encode($hmac);
       
   135 
       
   136         return $data;
       
   137     }
       
   138 }