Skip to content

Instantly share code, notes, and snippets.

@ggstroligo
Forked from ciaranarcher/warden_example.rb
Created November 18, 2022 00:03
Show Gist options
  • Select an option

  • Save ggstroligo/d39f82082a69a87372e22c081407e4a6 to your computer and use it in GitHub Desktop.

Select an option

Save ggstroligo/d39f82082a69a87372e22c081407e4a6 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