PageRenderTime 114ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/sdk-1.5.5/authentication/signature_v3query.class.php

https://bitbucket.org/ardydedase/web-build-optimizer
PHP | 192 lines | 102 code | 30 blank | 60 comment | 11 complexity | c59b6c0024de9f92d509239e8303943a MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright 2010-2012 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. /*%******************************************************************************************%*/
  17. // CLASS
  18. /**
  19. * Implements support for Signature v3 (AWS Query).
  20. *
  21. * @version 2011.11.22
  22. * @license See the included NOTICE.md file for more information.
  23. * @copyright See the included NOTICE.md file for more information.
  24. * @link http://aws.amazon.com/php/ PHP Developer Center
  25. */
  26. class AuthV3Query extends Signer implements Signable
  27. {
  28. /**
  29. * Constructs a new instance of the <AuthV3Query> class.
  30. *
  31. * @param string $endpoint (Required) The endpoint to direct the request to.
  32. * @param string $operation (Required) The operation to execute as a result of this request.
  33. * @param array $payload (Required) The options to use as part of the payload in the request.
  34. * @param CFCredential $credentials (Required) The credentials to use for signing and making requests.
  35. * @return void
  36. */
  37. public function __construct($endpoint, $operation, $payload, CFCredential $credentials)
  38. {
  39. parent::__construct($endpoint, $operation, $payload, $credentials);
  40. }
  41. /**
  42. * Generates a cURL handle with all of the required authentication bits set.
  43. *
  44. * @return resource A cURL handle ready for executing.
  45. */
  46. public function authenticate()
  47. {
  48. // Determine signing values
  49. $current_time = time();
  50. $date = gmdate(CFUtilities::DATE_FORMAT_RFC2616, $current_time);
  51. $timestamp = gmdate(CFUtilities::DATE_FORMAT_ISO8601, $current_time);
  52. $nonce = $this->util->generate_guid();
  53. $curlopts = array();
  54. $signed_headers = array();
  55. // Do we have an authentication token?
  56. if ($this->auth_token)
  57. {
  58. $headers['X-Amz-Security-Token'] = $this->auth_token;
  59. $query['SecurityToken'] = $this->auth_token;
  60. }
  61. $query['Action'] = $this->operation;
  62. $query['Version'] = $this->api_version;
  63. // Set custom CURLOPT settings
  64. if (is_array($this->payload) && isset($this->payload['curlopts']))
  65. {
  66. $curlopts = $this->payload['curlopts'];
  67. unset($this->payload['curlopts']);
  68. }
  69. // Merge in any options that were passed in
  70. if (is_array($this->payload))
  71. {
  72. $query = array_merge($query, $this->payload);
  73. }
  74. $return_curl_handle = isset($query['returnCurlHandle']) ? $query['returnCurlHandle'] : false;
  75. unset($query['returnCurlHandle']);
  76. // Do a case-sensitive, natural order sort on the array keys.
  77. uksort($query, 'strcmp');
  78. $canonical_query_string = $this->util->to_signable_string($query);
  79. // Remove the default scheme from the domain.
  80. $domain = str_replace(array('http://', 'https://'), '', $this->endpoint);
  81. // Parse our request.
  82. $parsed_url = parse_url('http://' . $domain);
  83. // Set the proper host header.
  84. if (isset($parsed_url['port']) && (integer) $parsed_url['port'] !== 80 && (integer) $parsed_url['port'] !== 443)
  85. {
  86. $host_header = strtolower($parsed_url['host']) . ':' . $parsed_url['port'];
  87. }
  88. else
  89. {
  90. $host_header = strtolower($parsed_url['host']);
  91. }
  92. // Set the proper request URI.
  93. $request_uri = isset($parsed_url['path']) ? $parsed_url['path'] : '/';
  94. // Generate the querystring from $query
  95. $this->querystring = $this->util->to_query_string($query);
  96. // Gather information to pass along to other classes.
  97. $helpers = array(
  98. 'utilities' => $this->utilities_class,
  99. 'request' => $this->request_class,
  100. 'response' => $this->response_class,
  101. );
  102. // Compose the request.
  103. $request_url = ($this->use_ssl ? 'https://' : 'http://') . $domain;
  104. $request_url .= !isset($parsed_url['path']) ? '/' : '';
  105. // Instantiate the request class
  106. $request = new $this->request_class($request_url, $this->proxy, $helpers, $this->credentials);
  107. $request->set_method('POST');
  108. $request->set_body($this->querystring);
  109. $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
  110. // Pass along registered stream callbacks
  111. if ($this->registered_streaming_read_callback)
  112. {
  113. $request->register_streaming_read_callback($this->registered_streaming_read_callback);
  114. }
  115. if ($this->registered_streaming_write_callback)
  116. {
  117. $request->register_streaming_write_callback($this->registered_streaming_write_callback);
  118. }
  119. // Add authentication headers
  120. $headers['X-Amz-Nonce'] = $nonce;
  121. $headers['Date'] = $date;
  122. $headers['Content-Length'] = strlen($this->querystring);
  123. $headers['Content-MD5'] = $this->util->hex_to_base64(md5($this->querystring));
  124. $headers['Host'] = $host_header;
  125. // Sort headers
  126. uksort($headers, 'strnatcasecmp');
  127. // Prepare the string to sign (HTTPS)
  128. $this->string_to_sign = $date . $nonce;
  129. // Add headers to request and compute the string to sign
  130. foreach ($headers as $header_key => $header_value)
  131. {
  132. // Strip linebreaks from header values as they're illegal and can allow for security issues
  133. $header_value = str_replace(array("\r", "\n"), '', $header_value);
  134. // Add the header if it has a value
  135. if ($header_value !== '')
  136. {
  137. $request->add_header($header_key, $header_value);
  138. }
  139. // Generate the string to sign
  140. if (
  141. substr(strtolower($header_key), 0, 8) === 'content-' ||
  142. strtolower($header_key) === 'date' ||
  143. strtolower($header_key) === 'expires' ||
  144. strtolower($header_key) === 'host' ||
  145. substr(strtolower($header_key), 0, 6) === 'x-amz-'
  146. )
  147. {
  148. $signed_headers[] = $header_key;
  149. }
  150. }
  151. // Hash the AWS secret key and generate a signature for the request.
  152. $signature = base64_encode(hash_hmac('sha256', $this->string_to_sign, $this->secret_key, true));
  153. $headers['X-Amzn-Authorization'] = 'AWS3-HTTPS'
  154. . ' AWSAccessKeyId=' . $this->key
  155. . ',Algorithm=HmacSHA256'
  156. . ',SignedHeaders=' . implode(';', $signed_headers)
  157. . ',Signature=' . $signature;
  158. $request->add_header('X-Amzn-Authorization', $headers['X-Amzn-Authorization']);
  159. $request->request_headers = $headers;
  160. return $request;
  161. }
  162. }