PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/Aws_v1/authentication/signature_v2query.class.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 163 lines | 78 code | 26 blank | 59 comment | 9 complexity | 012042f31704e4595a06d3ede66fb442 MD5 | raw file
  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. /*%******************************************************************************************%*/
  17. // CLASS
  18. /**
  19. * Implements support for Signature v2 (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 AuthV2Query extends Signer implements Signable
  27. {
  28. /**
  29. * Constructs a new instance of the <AuthV2Query> 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. $query = array();
  53. // Do we have an authentication token?
  54. if ($this->auth_token)
  55. {
  56. $headers['X-Amz-Security-Token'] = $this->auth_token;
  57. $query['SecurityToken'] = $this->auth_token;
  58. }
  59. // Only add it if it exists.
  60. if ($this->api_version)
  61. {
  62. $query['Version'] = $this->api_version;
  63. }
  64. $query['Action'] = $this->operation;
  65. $query['AWSAccessKeyId'] = $this->key;
  66. $query['SignatureMethod'] = 'HmacSHA256';
  67. $query['SignatureVersion'] = 2;
  68. $query['Timestamp'] = $timestamp;
  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. // Do a case-sensitive, natural order sort on the array keys.
  75. uksort($query, 'strcmp');
  76. // Create the string that needs to be hashed.
  77. $canonical_query_string = $this->util->to_signable_string($query);
  78. // Remove the default scheme from the domain.
  79. $domain = str_replace(array('http://', 'https://'), '', $this->endpoint);
  80. // Parse our request.
  81. $parsed_url = parse_url('http://' . $domain);
  82. // Set the proper host header.
  83. if (isset($parsed_url['port']) && (integer) $parsed_url['port'] !== 80 && (integer) $parsed_url['port'] !== 443)
  84. {
  85. $host_header = strtolower($parsed_url['host']) . ':' . $parsed_url['port'];
  86. }
  87. else
  88. {
  89. $host_header = strtolower($parsed_url['host']);
  90. }
  91. // Set the proper request URI.
  92. $request_uri = isset($parsed_url['path']) ? $parsed_url['path'] : '/';
  93. // Prepare the string to sign
  94. $this->string_to_sign = "POST\n$host_header\n$request_uri\n$canonical_query_string";
  95. // Hash the AWS secret key and generate a signature for the request.
  96. $query['Signature'] = base64_encode(hash_hmac('sha256', $this->string_to_sign, $this->secret_key, true));
  97. // Generate the querystring from $query
  98. $this->querystring = $this->util->to_query_string($query);
  99. // Gather information to pass along to other classes.
  100. $helpers = array(
  101. 'utilities' => $this->utilities_class,
  102. 'request' => $this->request_class,
  103. 'response' => $this->response_class,
  104. );
  105. // Compose the request.
  106. $request_url = ($this->use_ssl ? 'https://' : 'http://') . $domain;
  107. $request_url .= !isset($parsed_url['path']) ? '/' : '';
  108. // Instantiate the request class
  109. $request = new $this->request_class($request_url, $this->proxy, $helpers, $this->credentials);
  110. $request->set_method('POST');
  111. $request->set_body($this->querystring);
  112. $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
  113. // Pass along registered stream callbacks
  114. if ($this->registered_streaming_read_callback)
  115. {
  116. $request->register_streaming_read_callback($this->registered_streaming_read_callback);
  117. }
  118. if ($this->registered_streaming_write_callback)
  119. {
  120. $request->register_streaming_write_callback($this->registered_streaming_write_callback);
  121. }
  122. // Sort headers
  123. uksort($headers, 'strnatcasecmp');
  124. // Add headers to request and compute the string to sign
  125. foreach ($headers as $header_key => $header_value)
  126. {
  127. // Strip linebreaks from header values as they're illegal and can allow for security issues
  128. $header_value = str_replace(array("\r", "\n"), '', $header_value);
  129. // Add the header if it has a value
  130. if ($header_value !== '')
  131. {
  132. $request->add_header($header_key, $header_value);
  133. }
  134. }
  135. return $request;
  136. }
  137. }