Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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