Repository is archived
No commit activity in last 3 years
No release in over 3 years
A gem providing HMAC based authentication for HTTP. This is the DNC Labs fork.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.8.2
= 2.3.8
= 1.3.1
 Project Readme

auth-hmac¶ ↑

What is it?¶ ↑

auth-hmac is a Ruby implementation of HMAC based authentication of HTTP requests.

This is the DNC Innovation Lab fork of the project. We added Rack support and some other more obscure stuff for HMAC proxying that you probably won’t need unless you’re doing something weird like we are. :) Github user rjackson contributed Ruby 1.9 support, which is pretty awesome.

HMAC authentication involves a client and server having a shared secret key. When sending the request the client, signs the request using the secret key. This involves building a canonical representation of the request and then generating a HMAC of the request using the secret. The generated HMAC is then sent as part of the request.

When the server receives the request it builds the same canonical representation and generates a HMAC using it’s copy of the secret key, if the HMAC produced by the server matches the HMAC sent by the client, the server can be assured that the client also possesses the shared secret key.

HMAC based authentication also provides message integrity checking because the HMAC is based on a combination of the shared secret and the content of the request. So if any part of the request that is used to build the canonical representation is modified by a malicious party or in transit the authentication will then fail.

AuthHMAC was built to support authentication between various applications build by Peerworks.

AuthHMAC is loosely based on the Amazon Web Services authentication scheme but without the Amazon specific components, i.e. it is HMAC for the rest of us.

What does it require?¶ ↑

AuthHMAC requires Ruby’s OpenSSL support. This should be standard in most Ruby builds.

When to use it?¶ ↑

HMAC Authentication is best used as authentication for communication between applications such as web services. It provides better security than HTTP Basic authentication without the need to set up SSL. Of course if you need to protect the confidentiality of the data then you need SSL, but if you just want to authenticate requests without sending credentials in the clear AuthHMAC is a good choice.

How to use it?¶ ↑

The simplest way to use AuthHMAC is with the AuthHMAC.sign! and AuthHMAC#authenticate? methods.

AuthHMAC.sign! takes a HTTP request object, an access id and a secret key and signs the request with the access_id and secret key.

  • The HTTP request object can be a Net::HTTP::HTTPRequest object, a CGI::Request object or a Webrick HTTP request object. AuthHMAC will do its best to figure out which type it is an handle it accordingly.

  • The access_id is used to identify the secret key that was used to sign the request. Think of it as like a user name, it allows you to hand out different keys to different clients and authenticate each of them individually. The access_id is sent in the clear so you should avoid making it an important string.

  • The secret key is the shared secret between the client and the server. You should make this sufficiently random so that is can’t be guessed or exposed to dictionary attacks. The follow code will give you a pretty good secret key:

random = File.read(‘/dev/random’, 512) secret_key = [Digest::SHA2.new(512).digest(random)].pack(‘m’) On the server side you can then authenticate these requests using the AuthHMAC.authenticated? method. This takes the same arguments as the sign! method but returns true if the request has been signed with the access id and secret or false if it hasn’t.

If you have more than one set of credentials you might find it useful to create an instance of the AuthHMAC class, passing your credentials as a Hash of access id => secret keys, like so:

@authhmac = AuthHMAC.new(‘access_id1’ => ‘secret1’, ‘access_id2’ => ‘secret2’) You can then use the instance methods of the @authhmac object to sign and authenticate requests, for example:

@authhmac.sign!(request, “access_id1”) will sign request with “access_id1” and it’s corresponding secret key. Similarly authentication is done like so:

@authhmac.authenticated?(request)

which will return true if the request has been signed with one of the access id and secret key pairs provided in the constructor.

How does it work?¶ ↑

When creating a signature for a HTTP request AuthHMAC first generates a canonical representation of the request.

This canonical string is created like so:

canonical_string = HTTP-Verb    + "\n" +
                 Content-Type + "\n" +
                 Content-MD5  + "\n" +
                 Date         + "\n" +
                 request-uri;

Where Content-Type, Content-MD5 and Date are all taken from the headers of the request. If Content-Type or Content-MD5 are not present, they are substituted with an empty string. If Date is not present it is added to the request headers with the value Time.now.httpdate. request-uri is the path component of the request, without any query string, i.e. everything up to the ?.

This string is then used with the secret to generate a SHA1 HMAC using the following:

OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key, canonical_string)

The result is then Base64 encoded and added to the headers of the request as the Authorization header in the format:

Authorization: AuthHMAC <access_id>:<base64 encoded hmac>

When authenaticating a request, AuthHMAC looks for the Authorization header in the above format, parses out the components, regenerates a HMAC for the request, using the secret key identified by the access id and then compares the generated HMAC with the one provided by the client. If they match the request is authenticated.

Using these details it is possible to build code that will sign and authenticate AuthHMAC style requests in other languages.

INSTALL:¶ ↑

  • sudo gem install dnclabs-auth-hmac

Source Code¶ ↑

The source repository is accessible via GitHub:

git clone git://github.com/dnclabs/auth-hmac.git

Contact Information¶ ↑

Please file any bugs or feedback on github.com/dnclabs/auth-hmac/

Authors and Contributors¶ ↑

Upstream credits: rAtom was developed by Peerworks and written by Sean Geoghegan.

This fork: Maintained by the Democratic National Committee Innovation Labs team.

LICENSE:¶ ↑

(The MIT License)

Copyright © 2008 The Kaphan Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.