Skip to content

Instantly share code, notes, and snippets.

@ciaranarcher
Created January 26, 2012 14:40
Show Gist options
  • Select an option

  • Save ciaranarcher/1683063 to your computer and use it in GitHub Desktop.

Select an option

Save ciaranarcher/1683063 to your computer and use it in GitHub Desktop.
Warden Example - Basic HTTP Auth
Warden::Manager.before_failure do |env, opts|
# Sinatra/Padrino is very sensitive to the request method and
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block.
env['REQUEST_METHOD'] = "POST"
end
Warden::Strategies.add(:basic_http) do
def valid?
# Check if valid and store an instance var
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials
end
def authenticate!
# We presume that valid? has been passed and @auth is instance of
# Rack::Auth::Basic::Request so we'll suck out the credentials here.
username = @auth.credentials[0]
password = @auth.credentials[1]
if username == "Aladdin" && password == "open sesame"
success! 1 # @todo Replace with user ID
else
fail!("Could not log in")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment