File indexing completed on 2025-01-26 05:25:25

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_Amazon
0017  * @subpackage Authentication
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /**
0023  * @see Zend_Service_Amazon_Authentication
0024  */
0025 // require_once 'Zend/Service/Amazon/Authentication.php';
0026 
0027 /**
0028  * @see Zend_Crypt_Hmac
0029  */
0030 // require_once 'Zend/Crypt/Hmac.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Service_Amazon
0035  * @subpackage Authentication
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Service_Amazon_Authentication_V2 extends Zend_Service_Amazon_Authentication
0040 {
0041     /**
0042      * Signature Version
0043      */
0044     protected $_signatureVersion = '2';
0045 
0046     /**
0047      * Signature Encoding Method
0048      */
0049     protected $_signatureMethod = 'HmacSHA256';
0050 
0051     /**
0052      * Type of http request
0053      * @var string
0054      */
0055     protected $_httpMethod = "POST";
0056 
0057     /**
0058      * Generate the required attributes for the signature
0059      * @param string $url
0060      * @param array $parameters
0061      * @return string
0062      */
0063     public function generateSignature($url, array &$parameters)
0064     {
0065         $parameters['AWSAccessKeyId']   = $this->_accessKey;
0066         $parameters['SignatureVersion'] = $this->_signatureVersion;
0067         $parameters['Version']          = $this->_apiVersion;
0068         $parameters['SignatureMethod']  = $this->_signatureMethod;
0069         if(!isset($parameters['Timestamp'])) {
0070             $parameters['Timestamp']    = gmdate('Y-m-d\TH:i:s\Z', time()+10);
0071         }
0072 
0073         $data = $this->_signParameters($url, $parameters);
0074 
0075         return $data;
0076     }
0077 
0078     /**
0079      * Set http request type to POST or GET
0080      * @param string $method
0081      */
0082     public function setHttpMethod($method = "POST") {
0083         $this->_httpMethod = strtoupper($method);
0084     }
0085 
0086     /**
0087      * Get the current http request type
0088      * @return string
0089      */
0090     public function getHttpMethod()
0091     {
0092         return $this->_httpMethod;
0093     }
0094 
0095     /**
0096      * Computes the RFC 2104-compliant HMAC signature for request parameters
0097      *
0098      * This implements the Amazon Web Services signature, as per the following
0099      * specification:
0100      *
0101      * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
0102      *    excluding <tt>Signature</tt>, the value of which is being created),
0103      *    ignoring case.
0104      *
0105      * 2. Iterate over the sorted list and append the parameter name (in its
0106      *    original case) and then its value. Do not URL-encode the parameter
0107      *    values before constructing this string. Do not use any separator
0108      *    characters when appending strings.
0109      *
0110      * @param  string $queue_url  Queue URL
0111      * @param  array  $parameters the parameters for which to get the signature.
0112      *
0113      * @return string the signed data.
0114      */
0115     protected function _signParameters($url, array &$paramaters)
0116     {
0117         $data = $this->_httpMethod . "\n";
0118         $data .= parse_url($url, PHP_URL_HOST) . "\n";
0119         $data .= ('' == $path = parse_url($url, PHP_URL_PATH)) ? '/' : $path;
0120         $data .= "\n";
0121 
0122         uksort($paramaters, 'strcmp');
0123         unset($paramaters['Signature']);
0124 
0125         $arrData = array();
0126         foreach($paramaters as $key => $value) {
0127             $arrData[] = $key . '=' . str_replace('%7E', '~', rawurlencode($value));
0128         }
0129 
0130         $data .= implode('&', $arrData);
0131 
0132         $hmac = Zend_Crypt_Hmac::compute($this->_secretKey, 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
0133 
0134         $paramaters['Signature'] = base64_encode($hmac);
0135 
0136         return $data;
0137     }
0138 }