forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryStringSigner.java
More file actions
196 lines (169 loc) · 6.73 KB
/
QueryStringSigner.java
File metadata and controls
196 lines (169 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.auth;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TimeZone;
import java.util.TreeMap;
import com.amazonaws.AmazonClientException;
import com.amazonaws.Request;
/**
* Signer implementation responsible for signing an AWS query string request
* according to the various signature versions and hashing algorithms.
*/
public class QueryStringSigner extends AbstractAWSSigner implements Signer {
/** Date override for testing only */
private Date overriddenDate;
/**
* This signer will add "Signature" parameter to the request. Default
* signature version is "2" and default signing algorithm is "HmacSHA256".
*
* AWSAccessKeyId SignatureVersion SignatureMethod Timestamp Signature
*
* @param request
* request to be signed.
* @param credentials
* The credentials used to use to sign the request.
*/
public void sign(Request<?> request, AWSCredentials credentials) throws AmazonClientException {
sign(request, SignatureVersion.V2, SigningAlgorithm.HmacSHA256, credentials);
}
/**
* This signer will add following authentication parameters to the request:
*
* AWSAccessKeyId SignatureVersion SignatureMethod Timestamp Signature
*
* @param request
* request to be signed.
*
* @param version
* signature version. "2" is recommended.
*
* @param algorithm
* signature algorithm. "HmacSHA256" is recommended.
*/
public void sign(Request<?> request, SignatureVersion version, SigningAlgorithm algorithm, AWSCredentials credentials) throws AmazonClientException {
// annonymous credentials, don't sign
if ( credentials instanceof AnonymousAWSCredentials ) {
return;
}
AWSCredentials sanitizedCredentials = sanitizeCredentials(credentials);
request.addParameter("AWSAccessKeyId", sanitizedCredentials.getAWSAccessKeyId());
request.addParameter("SignatureVersion", version.toString());
int timeOffset = getTimeOffset(request);
request.addParameter("Timestamp", getFormattedTimestamp(timeOffset));
if ( sanitizedCredentials instanceof AWSSessionCredentials ) {
addSessionCredentials(request, (AWSSessionCredentials) sanitizedCredentials);
}
String stringToSign = null;
if ( version.equals( SignatureVersion.V1 ) ) {
stringToSign = calculateStringToSignV1(request.getParameters());
} else if ( version.equals( SignatureVersion.V2 ) ) {
request.addParameter("SignatureMethod", algorithm.toString());
stringToSign = calculateStringToSignV2(request);
} else {
throw new AmazonClientException("Invalid Signature Version specified");
}
String signatureValue = signAndBase64Encode(stringToSign, sanitizedCredentials.getAWSSecretKey(), algorithm);
request.addParameter("Signature", signatureValue);
}
/**
* Calculates string to sign for signature version 1.
*
* @param parameters
* request parameters
*
* @return String to sign
*/
private String calculateStringToSignV1(Map<String, String> parameters) {
StringBuilder data = new StringBuilder();
SortedMap<String, String> sorted =
new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
sorted.putAll(parameters);
for (Map.Entry<String, String> entry : sorted.entrySet()) {
data.append(entry.getKey());
data.append(entry.getValue());
}
return data.toString();
}
/**
* Calculate string to sign for signature version 2.
*
* @param request
* The request being signed.
*
* @return String to sign
*
* @throws AmazonClientException
* If the string to sign cannot be calculated.
*/
private String calculateStringToSignV2(Request<?> request) throws AmazonClientException {
URI endpoint = request.getEndpoint();
Map<String, String> parameters = request.getParameters();
StringBuilder data = new StringBuilder();
data.append("POST").append("\n");
data.append(getCanonicalizedEndpoint(endpoint)).append("\n");
data.append(getCanonicalizedResourcePath(request)).append("\n");
data.append(getCanonicalizedQueryString(parameters));
return data.toString();
}
private String getCanonicalizedResourcePath(Request<?> request) {
String resourcePath = "";
if (request.getEndpoint().getPath() != null) {
resourcePath += request.getEndpoint().getPath();
}
if (request.getResourcePath() != null) {
if (resourcePath.length() > 0 &&
!resourcePath.endsWith("/") &&
!request.getResourcePath().startsWith("/")) {
resourcePath += "/";
}
resourcePath += request.getResourcePath();
} else if (!resourcePath.endsWith("/")) {
resourcePath += "/";
}
if (!resourcePath.startsWith("/")) {
resourcePath = "/" + resourcePath;
}
if (resourcePath.startsWith("//")) {
resourcePath = resourcePath.substring(1);
}
return resourcePath;
}
/**
* Formats date as ISO 8601 timestamp
*/
private String getFormattedTimestamp(int offset) {
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
if (overriddenDate != null) {
return df.format(overriddenDate);
} else {
return df.format(getSignatureDate(offset));
}
}
/** For testing purposes only, to control the date used in signing. */
void overrideDate(Date date) {
this.overriddenDate = date;
}
@Override
protected void addSessionCredentials(Request<?> request, AWSSessionCredentials credentials) {
request.addParameter("SecurityToken", credentials.getSessionToken());
}
}