Skip to content

Instantly share code, notes, and snippets.

@wilkerlucio
Created June 1, 2010 12:48
Show Gist options
  • Save wilkerlucio/420907 to your computer and use it in GitHub Desktop.
Save wilkerlucio/420907 to your computer and use it in GitHub Desktop.
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
before_filter :base_auth
protected
def base_auth
warden.authenticate! :scope => :base
end
end
require 'base64'
class FakeModel
def id
'fake_id'
end
def self.find(*args)
nil
end
end
Warden::Strategies.add(:required_basic_auth) do
def authenticate!
if scope != :base
return
end
auth = request.headers["Authorization"]
if auth and auth =~ /^Basic (.+)$/
credentials = Base64::decode64($1)
login, password = credentials.split(":", 2)
if login == 'foo' and password == 'bar' # change this for a login/password that you want
success!(FakeModel.new)
return
end
end
custom!([401, custom_headers, ["HTTP Basic: Access denied.\n"]])
end
protected
def custom_headers
{
"Content-Type" => "text/html",
"WWW-Authenticate" => %(Basic realm="Application")
}
end
end
Devise.setup do |config|
# ... other stuff on your devise configuration
# configure warden
config.warden do |manager|
manager.default_strategies.unshift :required_basic_auth
end
# ... maybe here has more config... just ensure the above line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment