Aws::Signature::V4

Executes AWS Signature Version 4 Signing Process as explained at http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html.

The gem calculates AWS signature token and provides HTTP headers ready with authentication data.

Installation

Add this line to your application's Gemfile:

gem 'aws-signature-v4'

And then execute:

$ bundle

Or install it yourself as:

$ gem install aws-signature-v4

Usage

This example shows how to generate signature for DynamoDB Query API call:

payload = {
    :TableName => 'my_table',
    :IndexName => 'role-created_at-index',
    :select => 'ALL_ATTRIBUTES',
    'KeyConditionExpression': '#user_role = :user_role',
    'ExpressionAttributeValues': {
        ':user_role': {'S': 'ADMIN'}
    },
    :ExpressionAttributeNames => { '#user_role' => 'role'},
    :scan_index_forward => true,
    :ReturnConsumedCapacity => 'TOTAL'
}

aws_region = 'ap-southeast-1'
aws_access_key_id = 'AKIAJVB3LAMUASP5JTUQ'
aws_secret_access_key = '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'
aws_dynamodb_endpoint = 'https://dynamodb.ap-southeast-1.amazonaws.com'

# generate AWS Authorization header values
aws_signature = Aws::Signature::V4::Signature.new(aws_region, aws_access_key_id, aws_secret_access_key)
# parameters: service_name, amz_target, http_method, payload, host, uri
aws_signature.generate_signature('dynamodb', 'DynamoDB_20120810.Query', 'POST', payload, aws_dynamodb_endpoint, '/')
# Create HTTP object
uri = URI.parse(aws_dynamodb_endpoint)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# Alternatively use aws_signature.signature to get signature token value and construct headers yourself
request = Net::HTTP::Post.new(uri.request_uri, aws_signature.headers)
request.body = payload.to_json
# Send the request
res = http.request(request)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/khaled83/aws-signature-v4. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.