V1.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Service_Amazon_Authentication
  23. */
  24. require_once 'Zend/Service/Amazon/Authentication.php';
  25. /**
  26. * @see Zend_Crypt_Hmac
  27. */
  28. require_once 'Zend/Crypt/Hmac.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Service_Amazon
  32. * @subpackage Authentication
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Service_Amazon_Authentication_V1 extends Zend_Service_Amazon_Authentication
  37. {
  38. /**
  39. * Signature Version
  40. */
  41. protected $_signatureVersion = '1';
  42. /**
  43. * Signature Encoding Method
  44. */
  45. protected $_signatureMethod = 'HmacSHA256';
  46. /**
  47. * Generate the required attributes for the signature
  48. * @param string $url
  49. * @param array $parameters
  50. * @return string
  51. */
  52. public function generateSignature($url, array &$parameters)
  53. {
  54. $parameters['AWSAccessKeyId'] = $this->_accessKey;
  55. $parameters['SignatureVersion'] = $this->_signatureVersion;
  56. $parameters['Version'] = $this->_apiVersion;
  57. if(!isset($parameters['Timestamp'])) {
  58. $parameters['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z', time()+10);
  59. }
  60. $data = $this->_signParameters($url, $parameters);
  61. return $data;
  62. }
  63. /**
  64. * Computes the RFC 2104-compliant HMAC signature for request parameters
  65. *
  66. * This implements the Amazon Web Services signature, as per the following
  67. * specification:
  68. *
  69. * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
  70. * excluding <tt>Signature</tt>, the value of which is being created),
  71. * ignoring case.
  72. *
  73. * 2. Iterate over the sorted list and append the parameter name (in its
  74. * original case) and then its value. Do not URL-encode the parameter
  75. * values before constructing this string. Do not use any separator
  76. * characters when appending strings.
  77. *
  78. * @param string $queue_url Queue URL
  79. * @param array $parameters the parameters for which to get the signature.
  80. *
  81. * @return string the signed data.
  82. */
  83. protected function _signParameters($url, array &$paramaters)
  84. {
  85. $data = '';
  86. uksort($paramaters, 'strcasecmp');
  87. unset($paramaters['Signature']);
  88. foreach($paramaters as $key => $value) {
  89. $data .= $key . $value;
  90. }
  91. $hmac = Zend_Crypt_Hmac::compute($this->_secretKey, 'SHA1', $data, Zend_Crypt_Hmac::BINARY);
  92. $paramaters['Signature'] = base64_encode($hmac);
  93. return $data;
  94. }
  95. }