This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
Original Post: What's New in Edge Rails: HTTP Digest Authentication
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Long ago, in your mother’s version of rails, we got a http basic authentication plugin. That functionality has since been rolled into Rails core, but it was always lacking HTTP digest authentication. Until this commit, that is.
For those that may now know the difference, basic authentication only base 64 encodes the authenticating username and password (making it easily decoded) whereas digest authentication sends an MD5 hash of your username and password. To simplify, digest is more secure than basic.
To request digest authentication in Rails, you’ll need to be able to retrieve the cleartext password for a given user (so the framework can hash and compare it using the nonce it created specifically for that request). This is the downfall of this particular implementation – that you need to have access to the cleartext version of a user’s password. But… having made that choice, here’s how you do this:
12345678910111213
classArticlesController < ApplicationController before_filter :digest_authenticatedefdigest_authenticate# Given this username, return the cleartext password (or nil if not found) authenticate_or_request_with_http_digest("Articles Administration") do |username|User.find_by_username(username).try(cleartext_password)endendend
Most of us will want to do something with the result of the authentication and can do so with the boolean return value of authenticate_or_request_with_http_digest:
1234567891011121314151617181920
classArticlesController < ApplicationController before_filter :digest_authenticatedefdigest_authenticate success = authenticate_or_request_with_http_digest("Admin") do |username| (@user = User.find_by_username(username)).try(cleartext_password)end# If authentication succeeds, log the user in. If not, kick back out a failure# message as the response bodyif success session[:user_id] = @user.idelse request_http_digest_authentication("Admin", "Authentication failed")endendend
So there you have it, digest authentication in edge Rails.