Skip to content

Instantly share code, notes, and snippets.

@wolfeidau
Created August 5, 2012 11:07
Show Gist options
  • Save wolfeidau/3263920 to your computer and use it in GitHub Desktop.
Save wolfeidau/3263920 to your computer and use it in GitHub Desktop.

AWS Example

For example, imagine that you want to sign the following request:

PUT /quotes/nelson HTTP/1.0
Content-Md5: c8fdb181845a4ca6b8fec737b3581d76
Content-Type: text/html
Date: Thu, 17 Nov 2005 18:49:58 GMT
X-Amz-Meta-Author: [email protected]
X-Amz-Magic: abracadabra

The canonical string to be signed is:

PUT\nc8fdb181845a4ca6b8fec737b3581d76\ntext/html\nThu, 17 Nov 2005 18:49:58 GMT\nx-amz-magic:abracadabra\nx-amz-meta-author:[email protected]\n/quotes/nelson

Suppose your AWS Access Key ID is "44CF9590006BF252F707" and your AWS Secret Access Key is "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV".

The resulting signature would be "jZNOcbfWmD/A/f3hSvVzXZjM2HU=", and, you would add the Authorization header to your request to come up with the following result:

PUT /quotes/nelson HTTP/1.0
Authorization: AWS 44CF9590006BF252F707:jZNOcbfWmD/A/f3hSvVzXZjM2HU=
Content-Md5: c8fdb181845a4ca6b8fec737b3581d76
Content-Type: text/html
Date: Thu, 17 Nov 2005 18:49:58 GMT
X-Amz-Meta-Author: [email protected]
X-Amz-Magic: abracadabra

My Hmac Filter for the Jersey API

Client code testing my

AuthHmacSecret authHmacSecret = new AuthHmacSecret()
    .accessId("44CF9590006BF252F707")
    .secret("OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV");

AuthHmacClientFilter authHmacClientFilter = new AuthHmacClientFilter(authHmacSecret);

Client client = Client.create();
client.addFilter(new LoggingFilter());

WebResource resource;
String response;
resource = client.resource("http://localhost/quotes/nelson");
resource.addFilter(authHmacClientFilter);
response = resource.getRequestBuilder()
    .header("Date", "Thu, 17 Nov 2005 18:49:58 GMT")
    .header("X-Amz-Magic", "abracadabra")
    .header("X-Amz-Meta-Author", "[email protected]")
    .header("Content-Md5", "c8fdb181845a4ca6b8fec737b3581d76")
    .type(MediaType.TEXT_HTML_TYPE)
    .put(String.class);

Log statement from my library.

Aug 05, 2012 8:55:29 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client out-bound request
1 > PUT http://localhost/quotes/nelson
1 > Date: Thu, 17 Nov 2005 18:49:58 GMT
1 > X-Amz-Magic: abracadabra
1 > X-Amz-Meta-Author: [email protected]
1 > Content-Md5: c8fdb181845a4ca6b8fec737b3581d76
1 > Content-Type: text/html
1 > Authorization: AWS jZNOcbfWmD/A/f3hSvVzXZjM2HU=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment