SignatureV2.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Aws\Common\Signature;
  17. use Aws\Common\Credentials\CredentialsInterface;
  18. use Guzzle\Http\Message\RequestInterface;
  19. /**
  20. * Implementation of Signature Version 2
  21. * @link http://aws.amazon.com/articles/1928
  22. */
  23. class SignatureV2 extends AbstractSignature
  24. {
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
  29. {
  30. // refresh the cached timestamp
  31. $this->getTimestamp(true);
  32. // set values we need in CanonicalizedParameterString
  33. $this->addParameter($request, 'Timestamp', $this->getDateTime('c'));
  34. $this->addParameter($request, 'SignatureVersion', '2');
  35. $this->addParameter($request, 'SignatureMethod', 'HmacSHA256');
  36. $this->addParameter($request, 'AWSAccessKeyId', $credentials->getAccessKeyId());
  37. if ($token = $credentials->getSecurityToken()) {
  38. $this->addParameter($request, 'SecurityToken', $token);
  39. }
  40. // Get the path and ensure it's absolute
  41. $path = '/' . ltrim($request->getUrl(true)->normalizePath()->getPath(), '/');
  42. // build string to sign
  43. $sign = $request->getMethod() . "\n"
  44. . $request->getHost() . "\n"
  45. . $path . "\n"
  46. . $this->getCanonicalizedParameterString($request);
  47. // Add the string to sign to the request for debugging purposes
  48. $request->getParams()->set('aws.string_to_sign', $sign);
  49. $signature = base64_encode(
  50. hash_hmac(
  51. 'sha256',
  52. $sign,
  53. $credentials->getSecretKey(),
  54. true
  55. )
  56. );
  57. $this->addParameter($request, 'Signature', $signature);
  58. }
  59. /**
  60. * Add a parameter key and value to the request according to type
  61. *
  62. * @param RequestInterface $request The request
  63. * @param string $key The name of the parameter
  64. * @param string $value The value of the parameter
  65. */
  66. public function addParameter(RequestInterface $request, $key, $value)
  67. {
  68. if ($request->getMethod() == 'POST') {
  69. $request->setPostField($key, $value);
  70. } else {
  71. $request->getQuery()->set($key, $value);
  72. }
  73. }
  74. /**
  75. * Get the canonicalized query/parameter string for a request
  76. *
  77. * @param RequestInterface $request Request used to build canonicalized string
  78. *
  79. * @return string
  80. */
  81. public function getCanonicalizedParameterString(RequestInterface $request)
  82. {
  83. if ($request->getMethod() == 'POST') {
  84. $params = $request->getPostFields()->toArray();
  85. } else {
  86. $params = $request->getQuery()->toArray();
  87. }
  88. // Don't resign a previous signature value
  89. unset($params['Signature']);
  90. uksort($params, 'strcmp');
  91. $str = '';
  92. foreach ($params as $key => $val) {
  93. $str .= rawurlencode($key) . '=' . rawurlencode($val) . '&';
  94. }
  95. return substr($str, 0, -1);
  96. }
  97. }