-
Notifications
You must be signed in to change notification settings - Fork 142
/
COSSigner.java
275 lines (237 loc) · 11.8 KB
/
COSSigner.java
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright 2010-2019 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.
* According to cos feature, we modify some class,comment, field name, etc.
*/
package com.qcloud.cos.auth;
import static com.qcloud.cos.auth.COSSignerConstants.LINE_SEPARATOR;
import static com.qcloud.cos.auth.COSSignerConstants.Q_AK;
import static com.qcloud.cos.auth.COSSignerConstants.Q_HEADER_LIST;
import static com.qcloud.cos.auth.COSSignerConstants.Q_KEY_TIME;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGNATURE;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGN_ALGORITHM_KEY;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGN_ALGORITHM_VALUE;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGN_TIME;
import static com.qcloud.cos.auth.COSSignerConstants.Q_URL_PARAM_LIST;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import com.qcloud.cos.Headers;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.http.CosHttpRequest;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.internal.CosServiceRequest;
import com.qcloud.cos.utils.UrlEncoderUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacUtils;
public class COSSigner {
private static Set<String> needSignedHeaderSet = new HashSet<>();
private Boolean isCIWorkflowRequest = false;
// Time offset between local and server
private int localTimeDelta = 0;
static {
needSignedHeaderSet.add("cache-control");
needSignedHeaderSet.add("content-disposition");
needSignedHeaderSet.add("content-encoding");
needSignedHeaderSet.add("content-length");
needSignedHeaderSet.add("content-md5");
needSignedHeaderSet.add("content-type");
needSignedHeaderSet.add("expect");
needSignedHeaderSet.add("expires");
needSignedHeaderSet.add("host");
needSignedHeaderSet.add("if-match");
needSignedHeaderSet.add("if-modified-since");
needSignedHeaderSet.add("if-none-match");
needSignedHeaderSet.add("if-unmodified-since");
needSignedHeaderSet.add("origin");
needSignedHeaderSet.add("range");
needSignedHeaderSet.add("transfer-encoding");
needSignedHeaderSet.add(Headers.PIC_OPERATIONS.toLowerCase());
}
private boolean isAnonymous(COSCredentials cred) {
return cred instanceof AnonymousCOSCredentials;
}
public <X extends CosServiceRequest> void sign(CosHttpRequest<X> request, COSCredentials cred, Date expiredTime) {
if (isAnonymous(cred)) {
return;
}
String authoriationStr =
buildAuthorizationStr(request.getHttpMethod(), request.getResourcePath(),
request.getHeaders(), request.getParameters(), cred, expiredTime, true);
request.addHeader(Headers.COS_AUTHORIZATION, authoriationStr);
if (cred instanceof COSSessionCredentials) {
request.addHeader(Headers.SECURITY_TOKEN,
((COSSessionCredentials) cred).getSessionToken());
}
}
public String buildPostObjectSignature(String secretKey, String keyTime, String policy) {
String signKey = HmacUtils.hmacSha1Hex(secretKey, keyTime);
String stringToSign = DigestUtils.sha1Hex(policy);
return HmacUtils.hmacSha1Hex(signKey, stringToSign);
}
public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,
COSCredentials cred,
Date expiredTime) {
Date startTime = new Date();
return buildAuthorizationStr(methodName, resouce_path, new HashMap<>(), new HashMap<>(), cred, startTime, expiredTime, true);
}
public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,
Map<String, String> headerMap, Map<String, String> paramMap, COSCredentials cred,
Date expiredTime) {
Date startTime = new Date();
return buildAuthorizationStr(methodName, resouce_path, headerMap, paramMap,
cred, startTime, expiredTime,true);
}
public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,
Map<String, String> headerMap, Map<String, String> paramMap, COSCredentials cred,
Date expiredTime, Boolean signHost) {
Date startTime = new Date();
return buildAuthorizationStr(methodName, resouce_path, headerMap, paramMap,
cred, startTime, expiredTime, signHost);
}
public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,
Map<String, String> headerMap, Map<String, String> paramMap, COSCredentials cred,
Date startTime, Date expiredTime, Boolean signHost) {
if (isAnonymous(cred)) {
return null;
}
//万象工作流接口会出现uri带问号的情况 例如 /workflow/xxxxxx?active 这种情况?后面的参数不参与鉴权
if (isCIWorkflowRequest){
resouce_path = resouce_path.split("\\?")[0];
}
Map<String, String> signHeaders = buildSignHeaders(headerMap, signHost);
// 签名中的参数和http 头部 都要进行字符串排序
//对请求中的参数和http头部进行处理:对key先urlencode再小写处理,对value进行urlencode处理;
//生成 key 到 value 的映射 Map,根据key按照字典序排序
TreeMap<String, String> encodedSortedSignHeaders = buildEncodeSortedMemberMap(signHeaders);
TreeMap<String, String> encodedSortedParams = buildEncodeSortedMemberMap(paramMap);
//生成keylist
String qHeaderListStr = buildSignMemberStr(encodedSortedSignHeaders);
String qUrlParamListStr = buildSignMemberStr(encodedSortedParams);
String qKeyTimeStr, qSignTimeStr;
qKeyTimeStr = qSignTimeStr = buildTimeStr(startTime, expiredTime);
String signKey = HmacUtils.hmacSha1Hex(cred.getCOSSecretKey(), qKeyTimeStr);
String formatMethod = methodName.toString().toLowerCase();
String formatUri = resouce_path;
String formatParameters = formatMapToStr(encodedSortedParams);
String formatHeaders = formatMapToStr(encodedSortedSignHeaders);
String formatStr = new StringBuilder().append(formatMethod).append(LINE_SEPARATOR)
.append(formatUri).append(LINE_SEPARATOR).append(formatParameters)
.append(LINE_SEPARATOR).append(formatHeaders).append(LINE_SEPARATOR).toString();
String hashFormatStr = DigestUtils.sha1Hex(formatStr);
String stringToSign = new StringBuilder().append(Q_SIGN_ALGORITHM_VALUE)
.append(LINE_SEPARATOR).append(qSignTimeStr).append(LINE_SEPARATOR)
.append(hashFormatStr).append(LINE_SEPARATOR).toString();
String signature = HmacUtils.hmacSha1Hex(signKey, stringToSign);
String authoriationStr = new StringBuilder().append(Q_SIGN_ALGORITHM_KEY).append("=")
.append(Q_SIGN_ALGORITHM_VALUE).append("&").append(Q_AK).append("=")
.append(cred.getCOSAccessKeyId()).append("&").append(Q_SIGN_TIME).append("=")
.append(qSignTimeStr).append("&").append(Q_KEY_TIME).append("=").append(qKeyTimeStr)
.append("&").append(Q_HEADER_LIST).append("=").append(qHeaderListStr).append("&")
.append(Q_URL_PARAM_LIST).append("=").append(qUrlParamListStr).append("&")
.append(Q_SIGNATURE).append("=").append(signature).toString();
return authoriationStr;
}
public boolean needSignedHeader(String header) {
return needSignedHeaderSet.contains(header) || header.startsWith("x-cos-");
}
private Map<String, String> buildSignHeaders(Map<String, String> originHeaders, Boolean signHost) {
Boolean hasHost = false;
Map<String, String> signHeaders = new HashMap<>();
for (Entry<String, String> headerEntry : originHeaders.entrySet()) {
String key = headerEntry.getKey().toLowerCase();
if (key.equals("host")) {
hasHost = true;
}
if(needSignedHeader(key)) {
String value = headerEntry.getValue();
signHeaders.put(key, value);
}
}
if (!hasHost && signHost) {
String msg = String.format("buildAuthorization missing header: host. %s", originHeaders);
throw new CosClientException(msg);
}
return signHeaders;
}
private TreeMap<String, String> buildEncodeSortedMemberMap(Map<String, String> signElements){
TreeMap<String, String> encodeSortedSignElements = new TreeMap<>();
for (Entry<String, String> header : signElements.entrySet()) {
if (header.getKey() == null) {
continue;
}
String encodeLowerKey = UrlEncoderUtils.encode(header.getKey().trim()).toLowerCase();
String value = "";
if (header.getValue()!=null){
value = header.getValue().trim();
}
String encodeValue = UrlEncoderUtils.encode(value);
encodeSortedSignElements.put(encodeLowerKey, encodeValue);
}
return encodeSortedSignElements;
}
private String buildSignMemberStr(Map<String, String> signHeaders) {
StringBuilder strBuilder = new StringBuilder();
boolean seenOne = false;
for (String key : signHeaders.keySet()) {
if (!seenOne) {
seenOne = true;
} else {
strBuilder.append(";");
}
strBuilder.append(key);
}
return strBuilder.toString();
}
private String formatMapToStr(Map<String, String> kVMap) {
StringBuilder strBuilder = new StringBuilder();
boolean seeOne = false;
for (Entry<String, String> entry : kVMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (!seeOne) {
seeOne = true;
} else {
strBuilder.append("&");
}
strBuilder.append(key).append("=").append(value);
}
return strBuilder.toString();
}
private String buildTimeStr(Date startTime, Date endTime) {
StringBuilder strBuilder = new StringBuilder();
long startTimestamp = startTime.getTime() / 1000 + localTimeDelta;
long endTimestamp = endTime.getTime() / 1000 + localTimeDelta;
strBuilder.append(startTimestamp).append(";").append(endTimestamp);
return strBuilder.toString();
}
public static Set<String> getNeedSignedHeaderSet() {
return needSignedHeaderSet;
}
public static void setNeedSignedHeaderSet(Set<String> needSignedHeaderSet) {
COSSigner.needSignedHeaderSet = needSignedHeaderSet;
}
public void setCIWorkflowRequest(Boolean CIRequest) {
isCIWorkflowRequest = CIRequest;
}
public int getLocalTimeDelta() {
return localTimeDelta;
}
public void setLocalTimeDelta(int localTimeDelta) {
this.localTimeDelta = localTimeDelta;
}
}