What is it?
auth-hmac is a Ruby implementation of HMAC based authentication of HTTP requests.
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 = Base64.encode64(Digest::SHA2.new(512).digest(random)) 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:
which will return true if the request has been signed with one of the access id and secret key pairs provided in the constructor.
AuthHMAC supports authentication within Rails controllers and signing of requests generated by Active Resource. See AuthHMAC::Rails::ControllerFilter::ClassMethods and AuthHMAC::Rails::ActiveResourceExtension::BaseHmac::ClassMethods for details.
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.
sudo gem install auth-hmac
The source repository is accessible via GitHub or Ruby Forge:
git clone git://github.com/seangeo/auth-hmac.git git clone git://rubyforge.org/auth-hmac.git
The project page is at rubyforge.org/projects/auth-hmac. Please file any bugs or feedback using the trackers and forums there.
Authors and Contributors
rAtom was developed by Peerworks and written by Sean Geoghegan.
(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.