in src/main/java/com/aliyun/credentials/provider/RsaKeyPairCredentialProvider.java [111:156]
public RefreshResult<CredentialModel> getNewSessionCredentials(CompatibleUrlConnClient client) {
if (StringUtils.isEmpty(this.privateKey)) {
throw new IllegalArgumentException("PrivateKey must not be empty.");
}
ParameterHelper parameterHelper = new ParameterHelper();
HttpRequest httpRequest = new HttpRequest();
httpRequest.setUrlParameter("Action", "GenerateSessionAccessKey");
httpRequest.setUrlParameter("Format", "JSON");
httpRequest.setUrlParameter("Version", "2015-04-01");
httpRequest.setUrlParameter("DurationSeconds", String.valueOf(durationSeconds));
httpRequest.setUrlParameter("AccessKeyId", this.publicKeyId);
String strToSign = parameterHelper.composeStringToSign(MethodType.GET, httpRequest.getUrlParameters());
String signature = parameterHelper.signString(strToSign, this.privateKey + "&");
httpRequest.setUrlParameter("Signature", signature);
httpRequest.setSysMethod(MethodType.GET);
httpRequest.setSysConnectTimeout(this.connectTimeout);
httpRequest.setSysReadTimeout(this.readTimeout);
httpRequest.setSysUrl(parameterHelper.composeUrl(this.STSEndpoint, httpRequest.getUrlParameters(),
"https"));
HttpResponse httpResponse;
try {
httpResponse = client.syncInvoke(httpRequest);
} catch (Exception e) {
throw new CredentialException("Failed to connect RsaKeyPair Service: " + e);
}
if (httpResponse.getResponseCode() != 200) {
throw new CredentialException(String.format("Error refreshing credentials from RsaKeyPair, HttpCode: %s, result: %s.", httpResponse.getResponseCode(), httpResponse.getHttpContentString()));
}
Gson gson = new Gson();
Map<String, Object> map = gson.fromJson(httpResponse.getHttpContentString(), Map.class);
if (null == map || !map.containsKey("SessionAccessKey")) {
throw new CredentialException(String.format("Error retrieving credentials from RsaKeyPair result: %s.", httpResponse.getHttpContentString()));
}
Map<String, String> result = (Map<String, String>) map.get("SessionAccessKey");
long expiration = ParameterHelper.getUTCDate(result.get("Expiration")).getTime();
CredentialModel credential = CredentialModel.builder()
.accessKeyId(result.get("SessionAccessKeyId"))
.accessKeySecret(result.get("SessionAccessKeySecret"))
.type(AuthConstant.RSA_KEY_PAIR)
.providerName(this.getProviderName())
.expiration(expiration)
.build();
return RefreshResult.builder(credential)
.staleTime(getStaleTime(expiration))
.build();
}