Created
January 26, 2012 14:40
-
-
Save ciaranarcher/1683063 to your computer and use it in GitHub Desktop.
Warden Example - Basic HTTP Auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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