in tablestore/src/main/java/com/alicloud/openservices/tablestore/core/http/OTSValidationResponseHandler.java [37:89]
public void handle(ResponseMessage responseData) throws ClientException {
Map<String, String> headers = responseData.getLowerCaseHeadersMap();
// Verify the integrity of the header information
if (!headers.containsKey(OTS_HEADER_OTS_CONTENT_MD5)) {
throw new ClientException("MissingHeader: " + OTS_HEADER_OTS_CONTENT_MD5);
}
if (!headers.containsKey(OTS_HEADER_OTS_CONTENT_TYPE)) {
throw new ClientException("MissingHeader: " + OTS_HEADER_OTS_CONTENT_TYPE);
}
if (!headers.containsKey(OTS_HEADER_AUTHORIZATION)) {
throw new ClientException("MissingHeader: " + OTS_HEADER_AUTHORIZATION);
}
// Verify authorization information
StringBuilder strToSign = new StringBuilder(1000);
Map<String, String> sortedMap = new TreeMap<String, String>();
sortedMap.putAll(headers);
for(Map.Entry<String, String> entry : sortedMap.entrySet()) {
String key = entry.getKey();
String val = entry.getValue();
if (key.startsWith(OTS_HEADER_PREFIX)){
strToSign.append(key);
strToSign.append(':');
strToSign.append(val);
strToSign.append('\n');
}
}
strToSign.append('/');
strToSign.append(uri.getAction());
HmacSHA1Signature signer = new HmacSHA1Signature(Bytes.toBytes(credentials.getAccessKeySecret()));
signer.updateUTF8String(strToSign.toString());
String actualSign = signer.computeSignature();
String authHeader = headers.get(OTS_HEADER_AUTHORIZATION);
int posSign = authHeader.indexOf(actualSign);
if (posSign < 0) {
// cannot find signature
LOG.error("Validate response authorization failed, cannot find signature. headers:{}, accessKeyId:{}, computedSign:{}", headers, credentials.getAccessKeyId(), actualSign);
throw new ClientException("Validate response authorization failed, cannot find signature.");
}
if (posSign == 0 || authHeader.charAt(posSign - 1) != ':') {
// cannot find separator ':'
LOG.error("Validate response authorization failed, cannot find separator ':'. headers:{}, accessKeyId:{}, computedSign:{}", headers, credentials.getAccessKeyId(), actualSign);
throw new ClientException("Validate response authorization failed, cannot find separator ':'.");
}
if (posSign + actualSign.length() != authHeader.length()) {
// signature is not the last part of authHeader
LOG.error("Validate response authorization failed, signature is not the last part of authHeader. headers:{}, accessKeyId:{}, computedSign:{}", headers, credentials.getAccessKeyId(), actualSign);
throw new ClientException("Validate response authorization failed, signature is not the last part of authHeader.");
}
}