/vendor/magento/zendframework1/library/Zend/Service/Amazon/Authentication/V2.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 138 lines · 43 code · 17 blank · 78 comment · 2 complexity · b91365a27a1019d793f3821d50cf0209 MD5 · raw file

  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_V2 extends Zend_Service_Amazon_Authentication
  37. {
  38. /**
  39. * Signature Version
  40. */
  41. protected $_signatureVersion = '2';
  42. /**
  43. * Signature Encoding Method
  44. */
  45. protected $_signatureMethod = 'HmacSHA256';
  46. /**
  47. * Type of http request
  48. * @var string
  49. */
  50. protected $_httpMethod = "POST";
  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. $parameters['SignatureMethod'] = $this->_signatureMethod;
  63. if(!isset($parameters['Timestamp'])) {
  64. $parameters['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z', time()+10);
  65. }
  66. $data = $this->_signParameters($url, $parameters);
  67. return $data;
  68. }
  69. /**
  70. * Set http request type to POST or GET
  71. * @param string $method
  72. */
  73. public function setHttpMethod($method = "POST") {
  74. $this->_httpMethod = strtoupper($method);
  75. }
  76. /**
  77. * Get the current http request type
  78. * @return string
  79. */
  80. public function getHttpMethod()
  81. {
  82. return $this->_httpMethod;
  83. }
  84. /**
  85. * Computes the RFC 2104-compliant HMAC signature for request parameters
  86. *
  87. * This implements the Amazon Web Services signature, as per the following
  88. * specification:
  89. *
  90. * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
  91. * excluding <tt>Signature</tt>, the value of which is being created),
  92. * ignoring case.
  93. *
  94. * 2. Iterate over the sorted list and append the parameter name (in its
  95. * original case) and then its value. Do not URL-encode the parameter
  96. * values before constructing this string. Do not use any separator
  97. * characters when appending strings.
  98. *
  99. * @param string $queue_url Queue URL
  100. * @param array $parameters the parameters for which to get the signature.
  101. *
  102. * @return string the signed data.
  103. */
  104. protected function _signParameters($url, array &$paramaters)
  105. {
  106. $data = $this->_httpMethod . "\n";
  107. $data .= parse_url($url, PHP_URL_HOST) . "\n";
  108. $data .= ('' == $path = parse_url($url, PHP_URL_PATH)) ? '/' : $path;
  109. $data .= "\n";
  110. uksort($paramaters, 'strcmp');
  111. unset($paramaters['Signature']);
  112. $arrData = array();
  113. foreach($paramaters as $key => $value) {
  114. $arrData[] = $key . '=' . str_replace('%7E', '~', rawurlencode($value));
  115. }
  116. $data .= implode('&', $arrData);
  117. $hmac = Zend_Crypt_Hmac::compute($this->_secretKey, 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
  118. $paramaters['Signature'] = base64_encode($hmac);
  119. return $data;
  120. }
  121. }