Rails 2.x http basic authentication

Posted by randombits on Stack Overflow See other posts from Stack Overflow or by randombits
Published on 2010-06-03T02:45:27Z Indexed on 2010/06/03 2:54 UTC
Read the original article Hit count: 466

I'm trying to get basic http authentication working on my Rails app. I'm offering a simple REST interface served by a Rails server, only xml/json output.

Every method needs authentication, so I put the authenticate filter in ApplicationController:

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  before_filter :authenticate

protected
  def authenticate
    authenticate_or_request_with_http_basic do |u, p|
      true
    end
  end
end

Even with having the method return true, I'm receiving a 401 from the server:

$ curl http://127.0.0.1:3000/myresource/1.xml -i
HTTP/1.1 401 Unauthorized 
Cache-Control: no-cache
WWW-Authenticate: Basic realm="Application"
X-Runtime: 1
Content-Type: text/html; charset=utf-8
Content-Length: 27
Server: WEBrick/1.3.1 (Ruby/1.9.1/2010-01-10)
Date: Thu, 03 Jun 2010 02:43:55 GMT
Connection: Keep-Alive

HTTP Basic: Access denied.

If I'm explicitly returning true, yet getting served a 401.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby