Created
November 25, 2009 09:29
-
-
Save dekart/242597 to your computer and use it in GitHub Desktop.
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
| module Rack | |
| module Middleware | |
| class BasicAuth | |
| def initialize(app, options = {}) | |
| @app = app | |
| @options = options | |
| end | |
| def call(env, options = {}) | |
| unless env['REMOTE_USER'] | |
| auth = Rack::Auth::Basic::Request.new(env) | |
| return unauthorized! unless auth.provided? | |
| return bad_request! unless auth.basic? | |
| return unauthorized! unless authorize(*auth.credentials) | |
| env['REMOTE_USER'] = auth.username | |
| end | |
| @app.call(env) | |
| end | |
| def authorize(username, password) | |
| @options[:username] == username && @options[:password] == password | |
| end | |
| def unauthorized! | |
| [ 401, {'WWW-Authenticate' => %(Basic realm="#{@options[:domain]}")}, 'Authorization Required' ] | |
| end | |
| def bad_request! | |
| [ 400, 'Bad Request' ] | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment