-
Notifications
You must be signed in to change notification settings - Fork 12
/
Client.java
642 lines (603 loc) · 24.5 KB
/
Client.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.openapiutil;
import com.aliyun.tea.*;
import com.aliyun.tea.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.*;
public class Client {
public final static String SEPARATOR = "&";
public final static String URL_ENCODING = "UTF-8";
public static final String ALGORITHM_NAME = "HmacSHA1";
public static final String HASH_SHA256 = "SHA-256";
public static final String HASH_SM3 = "SM3";
public static final String HMAC_SHA256 = "ACS3-HMAC-SHA256";
public static final String RSA_SHA256 = "ACS3-RSA-SHA256";
public static final String HMAC_SM3 = "ACS3-HMAC-SM3";
public static final String UTF8 = "UTF-8";
public static final String PEM_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\n";
public static final String PEM_END = "\n-----END RSA PRIVATE KEY-----";
/**
* Convert all params of body other than type of readable into content
*
* @param body body Model
* @param content content Model
* @return void
*/
public static void convert(TeaModel body, TeaModel content) throws Exception {
if (body == null || content == null) {
return;
}
Map<String, Object> bodyMap = body.toMap();
bodyMap = (Map<String, Object>) exceptStream(bodyMap);
TeaModel.toModel(bodyMap, content);
}
private static Object exceptStream(Object object) throws Exception {
if (object instanceof List) {
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < ((List) object).size(); i++) {
Object item = ((List) object).get(i);
if (null != item) {
item = exceptStream(item);
if (null != item) {
list.add(item);
}
} else {
list.add(item);
}
}
return list;
} else if (object instanceof Map) {
Map<String, Object> map = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) object).entrySet()) {
Object item = entry.getValue();
if (null != item) {
item = exceptStream(item);
if (null != item) {
map.put(entry.getKey(), item);
}
} else {
map.put(entry.getKey(), item);
}
}
return map;
} else if (object instanceof InputStream || object instanceof OutputStream) {
return null;
}
return object;
}
/**
* Get the string to be signed according to request
*
* @param request which contains signed messages
* @return the signed string
*/
public static String getStringToSign(TeaRequest request) throws Exception {
if (request == null) {
return "";
}
String method = request.method;
String pathname = request.pathname;
Map<String, String> headers = request.headers;
Map<String, String> query = request.query;
String accept = headers.get("accept") == null ? "" : headers.get("accept");
String contentMD5 = headers.get("content-md5") == null ? "" : headers.get("content-md5");
String contentType = headers.get("content-type") == null ? "" : headers.get("content-type");
String date = headers.get("date") == null ? "" : headers.get("date");
String header = method + "\n" + accept + "\n" + contentMD5 + "\n" + contentType + "\n" + date + "\n";
String canonicalizedHeaders = getCanonicalizedHeaders(headers);
String canonicalizedResource = getCanonicalizedResource(pathname, query);
String stringToSign = header + canonicalizedHeaders + canonicalizedResource;
return stringToSign;
}
protected static String getCanonicalizedHeaders(Map<String, String> headers) {
if (headers == null) {
return "";
}
String prefix = "x-acs-";
Set<String> keys = headers.keySet();
List<String> canonicalizedKeys = new ArrayList<>();
for (String key : keys) {
if (key.startsWith(prefix)) {
canonicalizedKeys.add(key);
}
}
String[] canonicalizedKeysArray = canonicalizedKeys.toArray(new String[canonicalizedKeys.size()]);
Arrays.sort(canonicalizedKeysArray);
StringBuilder result = new StringBuilder();
for (int i = 0; i < canonicalizedKeysArray.length; i++) {
String key = canonicalizedKeysArray[i];
result.append(key);
result.append(":");
result.append(headers.get(key).trim());
result.append("\n");
}
return result.toString();
}
protected static Map<String, String> getCanonicalizedHeadersMap(Map<String, String> headers) {
Map<String, String> result = new HashMap<>();
if (headers == null) {
return result;
}
String prefix = "x-acs-";
Set<String> keys = headers.keySet();
List<String> canonicalizedKeys = new ArrayList<>();
Map<String, String> valueMap = new HashMap<>();
for (String key : keys) {
String lowerKey = key.toLowerCase();
if (lowerKey.startsWith(prefix) || lowerKey.equals("host")
|| lowerKey.equals("content-type")) {
if (!canonicalizedKeys.contains(lowerKey)) {
canonicalizedKeys.add(lowerKey);
}
valueMap.put(lowerKey, headers.get(key).trim());
}
}
String[] canonicalizedKeysArray = canonicalizedKeys.toArray(new String[canonicalizedKeys.size()]);
Arrays.sort(canonicalizedKeysArray);
String signedHeaders = StringUtils.join(";", Arrays.asList(canonicalizedKeysArray));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < canonicalizedKeysArray.length; i++) {
String key = canonicalizedKeysArray[i];
sb.append(key);
sb.append(":");
sb.append(valueMap.get(key));
sb.append("\n");
}
result.put("canonicalHeaders", sb.toString());
result.put("signedHeaders", signedHeaders);
return result;
}
protected static String getCanonicalizedQueryString(StringBuilder sb, Map<String, String> query, String[] keys) throws Exception {
if (query == null || query.size() == 0) {
return "";
}
if (keys == null || keys.length == 0) {
return "";
}
if (sb == null) {
sb = new StringBuilder();
}
Arrays.sort(keys);
String key;
String value;
for (int i = 0; i < keys.length; i++) {
key = keys[i];
sb.append(percentEncode(key));
value = query.get(key);
sb.append("=");
if (!StringUtils.isEmpty(value)) {
sb.append(percentEncode(value));
}
sb.append(SEPARATOR);
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
protected static String getCanonicalizedQueryStringForROA(StringBuilder sb, Map<String, String> query, String[] keys) throws Exception {
if (query == null || query.size() == 0) {
return "";
}
if (keys == null || keys.length == 0) {
return "";
}
if (sb == null) {
sb = new StringBuilder();
}
Arrays.sort(keys);
String key;
String value;
for (int i = 0; i < keys.length; i++) {
key = keys[i];
sb.append(key);
value = query.get(key);
if (!StringUtils.isEmpty(value)) {
sb.append("=");
sb.append(value);
}
sb.append(SEPARATOR);
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
protected static String getCanonicalizedResource(Map<String, String> query) throws Exception {
if (query == null || query.size() == 0) {
return "";
}
String[] keys = query.keySet().toArray(new String[query.size()]);
StringBuilder result = new StringBuilder();
return getCanonicalizedQueryString(result, query, keys);
}
protected static String getCanonicalizedResource(String pathname, Map<String, String> query) throws Exception {
if (query == null || query.size() == 0) {
return pathname;
}
String[] keys = query.keySet().toArray(new String[query.size()]);
if (pathname == null || keys.length <= 0) {
return pathname;
}
StringBuilder result = new StringBuilder(pathname);
result.append("?");
return getCanonicalizedQueryStringForROA(result, query, keys);
}
/**
* Get signature according to stringToSign, secret
*
* @param stringToSign the signed string
* @param secret accesskey secret
* @return the signature
*/
public static String getROASignature(String stringToSign, String secret) throws Exception {
if (StringUtils.isEmpty(secret)) {
return secret;
}
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(secret.getBytes(UTF8), "HmacSHA1"));
byte[] signData = mac.doFinal(stringToSign.getBytes(UTF8));
return Base64.encodeBase64String(signData);
}
/**
* Parse filter into a form string
*
* @param filter object
* @return the string
*/
public static String toForm(java.util.Map<String, ?> filter) throws Exception {
return toFormWithSymbol(filter, SEPARATOR);
}
private static String toFormWithSymbol(java.util.Map<String, ?> filter, String symbol) throws Exception {
Map<String, String> map = query(filter);
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, ?> entry : map.entrySet()) {
if (StringUtils.isEmpty(entry.getValue())) {
continue;
}
if (first) {
first = false;
} else {
result.append(symbol);
}
result.append(URLEncoder.encode(entry.getKey(), UTF8));
result.append("=");
result.append(URLEncoder.encode(String.valueOf(entry.getValue()), UTF8));
}
return result.toString();
}
/**
* Get timestamp
*
* @return the timestamp string
*/
public static String getTimestamp() throws Exception {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(new SimpleTimeZone(0, "UTC"));
return df.format(new Date());
}
/**
* Parse filter into a object which's type is map[string]string
*
* @param filter query param
* @return the object
*/
public static java.util.Map<String, String> query(java.util.Map<String, ?> filter) throws Exception {
Map<String, String> outMap = new HashMap<>();
if (null != filter) {
processObject(outMap, "", filter);
}
return outMap;
}
private static void processObject(Map<String, String> map, String key, Object value) throws UnsupportedEncodingException {
if (null == value) {
return;
}
if (value instanceof List) {
List list = (List) value;
for (int i = 0; i < list.size(); i++) {
processObject(map, key + "." + (i + 1), list.get(i));
}
} else if (value instanceof TeaModel) {
Map<String, Object> subMap = (Map<String, Object>) (((TeaModel) value).toMap());
for (Map.Entry<String, Object> entry : subMap.entrySet()) {
processObject(map, key + "." + (entry.getKey()), entry.getValue());
}
} else if (value instanceof Map) {
Map<String, Object> subMap = (Map<String, Object>) value;
for (Map.Entry<String, Object> entry : subMap.entrySet()) {
processObject(map, key + "." + (entry.getKey()), entry.getValue());
}
} else {
if (key.startsWith(".")) {
key = key.substring(1);
}
if (value instanceof byte[]) {
map.put(key, new String((byte[]) value, UTF8));
} else {
map.put(key, String.valueOf(value));
}
}
}
/**
* Get signature according to signedParams, method and secret
*
* @param signedParams params which need to be signed
* @param method http method e.g. GET
* @param secret AccessKeySecret
* @return the signature
*/
public static String getRPCSignature(java.util.Map<String, String> signedParams, String method, String secret) throws Exception {
if (signedParams == null || StringUtils.isEmpty(secret)) {
return secret;
}
Map<String, String> queries = signedParams;
String[] sortedKeys = queries.keySet().toArray(new String[]{});
Arrays.sort(sortedKeys);
StringBuilder canonicalizedQueryString = new StringBuilder();
for (String key : sortedKeys) {
if (StringUtils.isEmpty(queries.get(key))) {
continue;
}
canonicalizedQueryString.append(SEPARATOR)
.append(percentEncode(key)).append("=")
.append(percentEncode(queries.get(key)));
}
StringBuilder stringToSign = new StringBuilder();
stringToSign.append(method);
stringToSign.append(SEPARATOR);
stringToSign.append(percentEncode("/"));
stringToSign.append(SEPARATOR);
stringToSign.append(percentEncode(
canonicalizedQueryString.toString().substring(1)));
Mac mac = Mac.getInstance(ALGORITHM_NAME);
mac.init(new SecretKeySpec((secret + SEPARATOR).getBytes(URL_ENCODING), ALGORITHM_NAME));
byte[] signData = mac.doFinal(stringToSign.toString().getBytes(URL_ENCODING));
return Base64.encodeBase64String(signData);
}
public static String percentEncode(String value) throws UnsupportedEncodingException {
return value != null ? URLEncoder.encode(value, URL_ENCODING).replace("+", "%20")
.replace("*", "%2A").replace("%7E", "~") : null;
}
/**
* Parse array into a string with specified style
*
* @param object the object
* @param prefix the prefix string
* @return the string
* @style specified style e.g. repeatList
*/
public static String arrayToStringWithSpecifiedStyle(Object object, String prefix, String style) throws Exception {
if (null == object) {
return "";
}
switch (style) {
case "repeatList":
Map<String, Object> map = new HashMap<String, Object>();
map.put(prefix, object);
return toFormWithSymbol(map, "&&");
case "simple":
case "spaceDelimited":
case "pipeDelimited":
return flatArray((List) object, style);
case "json":
Object parsedObject = TeaModel.parseObject(object);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
return gson.toJson(parsedObject);
default:
return "";
}
}
/**
* Parse map with flat style
*
* @param object the object
* @return the object
*/
public static Object mapToFlatStyle(Object object) throws Exception {
if (null == object) {
return object;
}
if (object instanceof List) {
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < ((List) object).size(); i++) {
list.add(mapToFlatStyle(((List) object).get(i)));
}
return list;
} else if (object instanceof TeaModel) {
TeaModel result = (TeaModel) object;
for (Field field : result.getClass().getFields()) {
Object value = field.get(object);
if (value instanceof Map) {
Map<String, Object> flatMap = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) value).entrySet()) {
flatMap.put("#" + entry.getKey().length() + "#" + entry.getKey(), mapToFlatStyle(entry.getValue()));
}
field.set(object, flatMap);
} else {
field.set(object, mapToFlatStyle(value));
}
}
return result;
} else if (object instanceof Map) {
Map<String, Object> flatMap = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) object).entrySet()) {
flatMap.put("#" + entry.getKey().length() + "#" + entry.getKey(), mapToFlatStyle(entry.getValue()));
}
return flatMap;
}
return object;
}
private static String flatArray(List array, String sty) {
if (array == null || array.size() <= 0 || sty == null) {
return "";
}
String flag;
if ("simple".equalsIgnoreCase(sty)) {
flag = ",";
} else if ("spaceDelimited".equalsIgnoreCase(sty)) {
flag = " ";
} else {
flag = "|";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.size(); i++) {
sb.append(array.get(i));
sb.append(flag);
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
public static Map<String, Object> parseToMap(Object o) {
if (null == o) {
return null;
}
return (Map<String, Object>) TeaModel.parseObject(o);
}
public static String getEndpoint(String endpoint, boolean useAccelerate, String endpointType) {
if ("internal".equals(endpointType)) {
String[] strs = endpoint.split("\\.");
strs[0] += "-internal";
endpoint = StringUtils.join(".", Arrays.asList(strs));
}
if (useAccelerate && "accelerate".equals(endpointType)) {
return "oss-accelerate.aliyuncs.com";
}
return endpoint;
}
public static String hexEncode(byte[] raw) {
if (raw == null) {
return null;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < raw.length; i++) {
String hex = Integer.toHexString(raw[i] & 0xFF);
if (hex.length() < 2) {
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
public static byte[] hash(byte[] raw, String signAlgorithm) throws Exception {
if (signAlgorithm == null) {
return null;
}
if (signAlgorithm.equals(HMAC_SHA256) || signAlgorithm.equals(RSA_SHA256)) {
MessageDigest digest = MessageDigest.getInstance(HASH_SHA256);
return digest.digest(raw);
} else if (signAlgorithm.equals(HMAC_SM3)) {
BouncyCastleProvider provider = new BouncyCastleProvider();
MessageDigest digest = MessageDigest.getInstance(HASH_SM3, provider);
return digest.digest(raw);
}
return null;
}
public static String getAuthorization(TeaRequest request, String signAlgorithm, String payload, String accessKey, String secret) throws Exception {
if (request == null) {
return null;
}
if (secret == null) {
throw new Exception("Need secret!");
}
if (StringUtils.isEmpty(signAlgorithm)) {
throw new Exception("Need signAlgorithm!");
}
String canonicalURI = request.pathname;
if (canonicalURI == null || StringUtils.isEmpty(canonicalURI) || "".equals(canonicalURI.trim())) {
canonicalURI = "/";
}
String method = request.method;
Map<String, String> headers = request.headers;
Map<String, String> query = request.query;
Map<String, String> cannoicalHeaders = getCanonicalizedHeadersMap(headers);
String signedHeaders = cannoicalHeaders.get("signedHeaders");
String queryString = getCanonicalizedResource(query);
StringBuilder sb = new StringBuilder(method);
sb.append("\n").append(canonicalURI).append("\n").append(queryString).append("\n").append(cannoicalHeaders.get("canonicalHeaders"))
.append("\n").append(signedHeaders).append("\n").append(payload);
String hex = hexEncode(hash(sb.toString().getBytes(UTF8), signAlgorithm));
String stringToSign = signAlgorithm + "\n" + hex;
String signature = hexEncode(SignatureMethod(stringToSign, secret, signAlgorithm));
String auth = signAlgorithm + " Credential=" + accessKey + ",SignedHeaders=" + signedHeaders
+ ",Signature=" + signature;
return auth;
}
protected static String checkRSASecret(String secret) {
if (secret != null) {
if (secret.startsWith(PEM_BEGIN)) {
secret = secret.replace(PEM_BEGIN, "");
}
while (secret.endsWith("\n") || secret.endsWith("\r")) {
secret = secret.substring(0, secret.length() - 1);
}
if (secret.endsWith(PEM_END)) {
secret = secret.replace(PEM_END, "");
}
}
return secret;
}
public static byte[] SignatureMethod(String stringToSign, String secret, String signAlgorithm) throws Exception {
if (stringToSign == null || secret == null || "".equals(secret) || signAlgorithm == null) {
return null;
}
byte[] bytes = null;
if (signAlgorithm.equals(HMAC_SHA256)) {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
bytes = sha256_HMAC.doFinal(stringToSign.getBytes());
return bytes;
} else if (signAlgorithm.equals(RSA_SHA256)) {
secret = checkRSASecret(secret);
Signature rsaSign = Signature.getInstance("SHA256withRSA");
KeyFactory kf = KeyFactory.getInstance("RSA");
byte[] keySpec = Base64.decodeBase64(secret);
PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(keySpec));
rsaSign.initSign(privateKey);
rsaSign.update(stringToSign.getBytes(UTF8));
bytes = rsaSign.sign();
} else if (signAlgorithm.equals(HMAC_SM3)) {
SecretKey key = new SecretKeySpec((secret).getBytes(UTF8), "HMAC-SM3");
HMac mac = new HMac(new SM3Digest());
bytes = new byte[mac.getMacSize()];
byte[] inputBytes = stringToSign.getBytes(UTF8);
mac.init(new KeyParameter(key.getEncoded()));
mac.update(inputBytes, 0, inputBytes.length);
mac.doFinal(bytes, 0);
}
return bytes;
}
public static String getEncodePath(String path) throws UnsupportedEncodingException {
if (StringUtils.isEmpty(path) || "/".equals(path)) {
return path;
}
String[] strs = path.split("/");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
sb.append(percentEncode(strs[i]));
sb.append("/");
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
public static String getEncodeParam(String param) throws UnsupportedEncodingException {
if (StringUtils.isEmpty(param)) {
return param;
}
return percentEncode(param);
}
}