web/enmi/Zend/Service/Amazon/Authentication/V1.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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_V1 extends Zend_Service_Amazon_Authentication
       
    40 {
       
    41     /**
       
    42      * Signature Version
       
    43      */
       
    44     protected $_signatureVersion = '1';
       
    45 
       
    46     /**
       
    47      * Signature Encoding Method
       
    48      */
       
    49     protected $_signatureMethod = 'HmacSHA256';
       
    50     
       
    51     /**
       
    52      * Generate the required attributes for the signature
       
    53      * @param string $url
       
    54      * @param array $parameters
       
    55      * @return string
       
    56      */
       
    57     public function generateSignature($url, array &$parameters)
       
    58     {
       
    59         $parameters['AWSAccessKeyId']   = $this->_accessKey;
       
    60         $parameters['SignatureVersion'] = $this->_signatureVersion;
       
    61         $parameters['Version']          = $this->_apiVersion;
       
    62         if(!isset($parameters['Timestamp'])) {
       
    63             $parameters['Timestamp']    = gmdate('Y-m-d\TH:i:s\Z', time()+10);
       
    64         }
       
    65 
       
    66         $data = $this->_signParameters($url, $parameters);
       
    67         
       
    68         return $data;
       
    69     }
       
    70 
       
    71     /**
       
    72      * Computes the RFC 2104-compliant HMAC signature for request parameters
       
    73      *
       
    74      * This implements the Amazon Web Services signature, as per the following
       
    75      * specification:
       
    76      *
       
    77      * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
       
    78      *    excluding <tt>Signature</tt>, the value of which is being created),
       
    79      *    ignoring case.
       
    80      *
       
    81      * 2. Iterate over the sorted list and append the parameter name (in its
       
    82      *    original case) and then its value. Do not URL-encode the parameter
       
    83      *    values before constructing this string. Do not use any separator
       
    84      *    characters when appending strings.
       
    85      *
       
    86      * @param  string $queue_url  Queue URL
       
    87      * @param  array  $parameters the parameters for which to get the signature.
       
    88      *
       
    89      * @return string the signed data.
       
    90      */
       
    91     protected function _signParameters($url, array &$paramaters)
       
    92     {
       
    93         $data = '';
       
    94 
       
    95         uksort($paramaters, 'strcasecmp');
       
    96         unset($paramaters['Signature']);
       
    97 
       
    98         foreach($paramaters as $key => $value) {
       
    99             $data .= $key . $value;
       
   100         }
       
   101 
       
   102         $hmac = Zend_Crypt_Hmac::compute($this->_secretKey, 'SHA1', $data, Zend_Crypt_Hmac::BINARY);
       
   103 
       
   104         $paramaters['Signature'] = base64_encode($hmac);
       
   105         
       
   106         return $data;
       
   107     }
       
   108 }