in src/main/java/com/amazonaws/neptune/auth/NeptuneSigV4SignerBase.java [186:242]
protected abstract void attachSignature(final T nativeRequest, final NeptuneSigV4Signature signature)
throws NeptuneSigV4SignerException;
/**
* Main logics to sign the request. The scheme is to convert the request into a
* signable request using toSignableRequest, then sign it using the AWS SDK, and
* finally attach the signature headers to the original request using attachSignature.
* <p>
* Note that toSignableRequest and attachSignature are abstract classes in
* this base class, they require dedicated implementations depending on the type of
* the native HTTP request.
*
* @param request the request to be signed
* @throws NeptuneSigV4SignerException in case something goes wrong during signing
*/
@Override
public void signRequest(final T request) throws NeptuneSigV4SignerException {
try {
// 1. Convert the Apache Http request into an AWS SDK signable request
// => to be implemented in subclass
final SdkHttpFullRequest awsSignableRequest = toSignableRequest(request);
// 2. Sign the AWS SDK signable request (which internally adds some HTTP headers)
// => generic, using the AWS SDK signer
final AwsCredentials credentials = awsCredentialsProvider.resolveCredentials();
final Aws4SignerParams awsSignerParams = Aws4SignerParams.builder().
awsCredentials(credentials).
signingName(serviceName).
signingRegion(awsRegion).
build();
final SdkHttpFullRequest awsSignedRequest = aws4Signer.sign(awsSignableRequest, awsSignerParams);
// extract session token if temporary credentials are provided
String sessionToken = "";
if ((credentials instanceof AwsSessionCredentials)) {
sessionToken = ((AwsSessionCredentials) credentials).sessionToken();
}
final NeptuneSigV4Signature signature =
new NeptuneSigV4Signature(
awsSignedRequest.matchingHeaders(HOST).get(0),
awsSignedRequest.matchingHeaders(X_AMZ_DATE).get(0),
awsSignedRequest.matchingHeaders(AUTHORIZATION).get(0),
sessionToken);
// 3. Copy over the Signature V4 headers to the original request
// => to be implemented in subclass
attachSignature(request, signature);
} catch (final Throwable t) {
throw new NeptuneSigV4SignerException(t);
}
}