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