Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created November 3, 2011 21:45
Show Gist options
  • Save jamiehodge/1337882 to your computer and use it in GitHub Desktop.
Save jamiehodge/1337882 to your computer and use it in GitHub Desktop.
Warden basic http authentication
Warden::Strategies.add(:basic) do
def auth
@auth ||= Rack::Auth::Basic::Request.new(env)
end
def valid?
auth.provided? && auth.basic? && auth.credentials
end
def authenticate!
user = User.authenticate(
auth.credentials.first,
auth.credentials.last
)
user.nil? ? custom!(unauthorized) : success!(user)
end
def store?
false
end
def unauthorized
[
401,
{
'Content-Type' => 'text/plain',
'Content-Length' => '0',
'WWW-Authenticate' => %(Basic realm="realm")
},
[]
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment