@Test public void testSignature() throws SignatureException, MalformedURLException { AuthorizationHeader authorizationHeader = new AmazonSignatureService().signRequestHeaders(getWrapper(), null, null); assertEquals(EXPECTED_SIGNATURE, authorizationHeader.getSignature()); assertTrue(authorizationHeader.getAuthorizationHeader().contains("Authorization:AWS4-HMAC-SHA256 " + "Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, " + "SignedHeaders=host;range;x-amz-content-sha256;x-amz-date, " + "Signature=f0c1f1706f0378773c039781a41807b53d135bca4a0e1d5c824f9e14ae514078")); assertTrue(authorizationHeader.getAuthorizationHeader().contains("x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); assertTrue(authorizationHeader.getAuthorizationHeader().contains("x-amz-date:20130524T000000Z")); assertTrue(authorizationHeader.getAuthorizationHeader().contains("host:s3.amazonaws.com")); assertTrue(authorizationHeader.getAuthorizationHeader().contains("range:bytes=0-9")); }
public AuthorizationHeader signRequestHeaders(InputsWrapper wrapper, Map<String, String> headersMap, Map<String, String> queryParamsMap) throws SignatureException, MalformedURLException { AwsSignatureHelper signatureUtils = new AwsSignatureHelper(); String amazonDate = isBlank(wrapper.getDate()) ? signatureUtils.getAmazonDateString(new Date()) : wrapper.getDate(); String dateStamp = amazonDate.split(T_REGEX_STRING)[0]; String prefix = wrapper.getStorageInputs() == null ? EMPTY : wrapper.getStorageInputs().getBucketName(); String requestEndpoint = getRequestEndpoint(getUrlFromApiService(wrapper.getCommonInputs().getEndpoint(), wrapper.getCommonInputs().getApiService(), prefix)); String region = signatureUtils.getAmazonRegion(requestEndpoint); String apiService = getDefaultStringInput(wrapper.getApiService(), EC2_API); String credentialScope = signatureUtils.getAmazonCredentialScope(dateStamp, region, apiService); String amzCredential = wrapper.getCommonInputs().getIdentity() + SCOPE_SEPARATOR + credentialScope; Map<String, String> requestHeaders = getRequestHeadersMap(headersMap, wrapper.getHeaders(), requestEndpoint, wrapper.getSecurityToken(), amazonDate); String signedHeadersString = signatureUtils.getSignedHeadersString(requestHeaders); queryParamsMap = getPopulatedQueryParamsMap(queryParamsMap, wrapper); String canonicalRequest = awsSignatureV4.getCanonicalRequest(wrapper.getHttpVerb(), wrapper.getRequestUri(), signatureUtils.canonicalizedQueryString(queryParamsMap), signatureUtils.canonicalizedHeadersString(requestHeaders), signedHeadersString, wrapper.getRequestPayload()); String stringToSign = awsSignatureV4.getStringToSign(amazonDate, credentialScope, canonicalRequest); byte[] key = awsSignatureV4.getDerivedSigningKey(wrapper.getCommonInputs().getCredential(), dateStamp, region, apiService); String signature = awsSignatureV4.getSignature(stringToSign, key); String authorizationHeader = AWS4_SIGNING_ALGORITHM + " Credential=" + amzCredential + ", SignedHeaders=" + signedHeadersString + ", Signature=" + signature; requestHeaders.put(AUTHORIZATION, authorizationHeader); return new AuthorizationHeader(getHeadersOrParamsString(requestHeaders, COLON, LINE_SEPARATOR, false), signature); }
AmazonSignatureService { public AuthorizationHeader signRequestHeaders(InputsWrapper wrapper, Map<String, String> headersMap, Map<String, String> queryParamsMap) throws SignatureException, MalformedURLException { AwsSignatureHelper signatureUtils = new AwsSignatureHelper(); String amazonDate = isBlank(wrapper.getDate()) ? signatureUtils.getAmazonDateString(new Date()) : wrapper.getDate(); String dateStamp = amazonDate.split(T_REGEX_STRING)[0]; String prefix = wrapper.getStorageInputs() == null ? EMPTY : wrapper.getStorageInputs().getBucketName(); String requestEndpoint = getRequestEndpoint(getUrlFromApiService(wrapper.getCommonInputs().getEndpoint(), wrapper.getCommonInputs().getApiService(), prefix)); String region = signatureUtils.getAmazonRegion(requestEndpoint); String apiService = getDefaultStringInput(wrapper.getApiService(), EC2_API); String credentialScope = signatureUtils.getAmazonCredentialScope(dateStamp, region, apiService); String amzCredential = wrapper.getCommonInputs().getIdentity() + SCOPE_SEPARATOR + credentialScope; Map<String, String> requestHeaders = getRequestHeadersMap(headersMap, wrapper.getHeaders(), requestEndpoint, wrapper.getSecurityToken(), amazonDate); String signedHeadersString = signatureUtils.getSignedHeadersString(requestHeaders); queryParamsMap = getPopulatedQueryParamsMap(queryParamsMap, wrapper); String canonicalRequest = awsSignatureV4.getCanonicalRequest(wrapper.getHttpVerb(), wrapper.getRequestUri(), signatureUtils.canonicalizedQueryString(queryParamsMap), signatureUtils.canonicalizedHeadersString(requestHeaders), signedHeadersString, wrapper.getRequestPayload()); String stringToSign = awsSignatureV4.getStringToSign(amazonDate, credentialScope, canonicalRequest); byte[] key = awsSignatureV4.getDerivedSigningKey(wrapper.getCommonInputs().getCredential(), dateStamp, region, apiService); String signature = awsSignatureV4.getSignature(stringToSign, key); String authorizationHeader = AWS4_SIGNING_ALGORITHM + " Credential=" + amzCredential + ", SignedHeaders=" + signedHeadersString + ", Signature=" + signature; requestHeaders.put(AUTHORIZATION, authorizationHeader); return new AuthorizationHeader(getHeadersOrParamsString(requestHeaders, COLON, LINE_SEPARATOR, false), signature); } }
AmazonSignatureService { public AuthorizationHeader signRequestHeaders(InputsWrapper wrapper, Map<String, String> headersMap, Map<String, String> queryParamsMap) throws SignatureException, MalformedURLException { AwsSignatureHelper signatureUtils = new AwsSignatureHelper(); String amazonDate = isBlank(wrapper.getDate()) ? signatureUtils.getAmazonDateString(new Date()) : wrapper.getDate(); String dateStamp = amazonDate.split(T_REGEX_STRING)[0]; String prefix = wrapper.getStorageInputs() == null ? EMPTY : wrapper.getStorageInputs().getBucketName(); String requestEndpoint = getRequestEndpoint(getUrlFromApiService(wrapper.getCommonInputs().getEndpoint(), wrapper.getCommonInputs().getApiService(), prefix)); String region = signatureUtils.getAmazonRegion(requestEndpoint); String apiService = getDefaultStringInput(wrapper.getApiService(), EC2_API); String credentialScope = signatureUtils.getAmazonCredentialScope(dateStamp, region, apiService); String amzCredential = wrapper.getCommonInputs().getIdentity() + SCOPE_SEPARATOR + credentialScope; Map<String, String> requestHeaders = getRequestHeadersMap(headersMap, wrapper.getHeaders(), requestEndpoint, wrapper.getSecurityToken(), amazonDate); String signedHeadersString = signatureUtils.getSignedHeadersString(requestHeaders); queryParamsMap = getPopulatedQueryParamsMap(queryParamsMap, wrapper); String canonicalRequest = awsSignatureV4.getCanonicalRequest(wrapper.getHttpVerb(), wrapper.getRequestUri(), signatureUtils.canonicalizedQueryString(queryParamsMap), signatureUtils.canonicalizedHeadersString(requestHeaders), signedHeadersString, wrapper.getRequestPayload()); String stringToSign = awsSignatureV4.getStringToSign(amazonDate, credentialScope, canonicalRequest); byte[] key = awsSignatureV4.getDerivedSigningKey(wrapper.getCommonInputs().getCredential(), dateStamp, region, apiService); String signature = awsSignatureV4.getSignature(stringToSign, key); String authorizationHeader = AWS4_SIGNING_ALGORITHM + " Credential=" + amzCredential + ", SignedHeaders=" + signedHeadersString + ", Signature=" + signature; requestHeaders.put(AUTHORIZATION, authorizationHeader); return new AuthorizationHeader(getHeadersOrParamsString(requestHeaders, COLON, LINE_SEPARATOR, false), signature); }  }
AmazonSignatureService { public AuthorizationHeader signRequestHeaders(InputsWrapper wrapper, Map<String, String> headersMap, Map<String, String> queryParamsMap) throws SignatureException, MalformedURLException { AwsSignatureHelper signatureUtils = new AwsSignatureHelper(); String amazonDate = isBlank(wrapper.getDate()) ? signatureUtils.getAmazonDateString(new Date()) : wrapper.getDate(); String dateStamp = amazonDate.split(T_REGEX_STRING)[0]; String prefix = wrapper.getStorageInputs() == null ? EMPTY : wrapper.getStorageInputs().getBucketName(); String requestEndpoint = getRequestEndpoint(getUrlFromApiService(wrapper.getCommonInputs().getEndpoint(), wrapper.getCommonInputs().getApiService(), prefix)); String region = signatureUtils.getAmazonRegion(requestEndpoint); String apiService = getDefaultStringInput(wrapper.getApiService(), EC2_API); String credentialScope = signatureUtils.getAmazonCredentialScope(dateStamp, region, apiService); String amzCredential = wrapper.getCommonInputs().getIdentity() + SCOPE_SEPARATOR + credentialScope; Map<String, String> requestHeaders = getRequestHeadersMap(headersMap, wrapper.getHeaders(), requestEndpoint, wrapper.getSecurityToken(), amazonDate); String signedHeadersString = signatureUtils.getSignedHeadersString(requestHeaders); queryParamsMap = getPopulatedQueryParamsMap(queryParamsMap, wrapper); String canonicalRequest = awsSignatureV4.getCanonicalRequest(wrapper.getHttpVerb(), wrapper.getRequestUri(), signatureUtils.canonicalizedQueryString(queryParamsMap), signatureUtils.canonicalizedHeadersString(requestHeaders), signedHeadersString, wrapper.getRequestPayload()); String stringToSign = awsSignatureV4.getStringToSign(amazonDate, credentialScope, canonicalRequest); byte[] key = awsSignatureV4.getDerivedSigningKey(wrapper.getCommonInputs().getCredential(), dateStamp, region, apiService); String signature = awsSignatureV4.getSignature(stringToSign, key); String authorizationHeader = AWS4_SIGNING_ALGORITHM + " Credential=" + amzCredential + ", SignedHeaders=" + signedHeadersString + ", Signature=" + signature; requestHeaders.put(AUTHORIZATION, authorizationHeader); return new AuthorizationHeader(getHeadersOrParamsString(requestHeaders, COLON, LINE_SEPARATOR, false), signature); }  AuthorizationHeader signRequestHeaders(InputsWrapper wrapper, Map<String, String> headersMap,
                                                  Map<String, String> queryParamsMap); }
AmazonSignatureService { public AuthorizationHeader signRequestHeaders(InputsWrapper wrapper, Map<String, String> headersMap, Map<String, String> queryParamsMap) throws SignatureException, MalformedURLException { AwsSignatureHelper signatureUtils = new AwsSignatureHelper(); String amazonDate = isBlank(wrapper.getDate()) ? signatureUtils.getAmazonDateString(new Date()) : wrapper.getDate(); String dateStamp = amazonDate.split(T_REGEX_STRING)[0]; String prefix = wrapper.getStorageInputs() == null ? EMPTY : wrapper.getStorageInputs().getBucketName(); String requestEndpoint = getRequestEndpoint(getUrlFromApiService(wrapper.getCommonInputs().getEndpoint(), wrapper.getCommonInputs().getApiService(), prefix)); String region = signatureUtils.getAmazonRegion(requestEndpoint); String apiService = getDefaultStringInput(wrapper.getApiService(), EC2_API); String credentialScope = signatureUtils.getAmazonCredentialScope(dateStamp, region, apiService); String amzCredential = wrapper.getCommonInputs().getIdentity() + SCOPE_SEPARATOR + credentialScope; Map<String, String> requestHeaders = getRequestHeadersMap(headersMap, wrapper.getHeaders(), requestEndpoint, wrapper.getSecurityToken(), amazonDate); String signedHeadersString = signatureUtils.getSignedHeadersString(requestHeaders); queryParamsMap = getPopulatedQueryParamsMap(queryParamsMap, wrapper); String canonicalRequest = awsSignatureV4.getCanonicalRequest(wrapper.getHttpVerb(), wrapper.getRequestUri(), signatureUtils.canonicalizedQueryString(queryParamsMap), signatureUtils.canonicalizedHeadersString(requestHeaders), signedHeadersString, wrapper.getRequestPayload()); String stringToSign = awsSignatureV4.getStringToSign(amazonDate, credentialScope, canonicalRequest); byte[] key = awsSignatureV4.getDerivedSigningKey(wrapper.getCommonInputs().getCredential(), dateStamp, region, apiService); String signature = awsSignatureV4.getSignature(stringToSign, key); String authorizationHeader = AWS4_SIGNING_ALGORITHM + " Credential=" + amzCredential + ", SignedHeaders=" + signedHeadersString + ", Signature=" + signature; requestHeaders.put(AUTHORIZATION, authorizationHeader); return new AuthorizationHeader(getHeadersOrParamsString(requestHeaders, COLON, LINE_SEPARATOR, false), signature); }  AuthorizationHeader signRequestHeaders(InputsWrapper wrapper, Map<String, String> headersMap,
                                                  Map<String, String> queryParamsMap);  }