-
-
Save coffeeaddict/2400427 to your computer and use it in GitHub Desktop.
Http basic authentication for padrino
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 HttpAuthentication | |
module Basic | |
def authenticate_or_request_with_http_basic(user, pass, realm = 'Application') | |
authenticate_with_http_basic(user, pass) || request_http_basic_authentication(realm) | |
end | |
def authenticate_with_http_basic(user, pass) | |
if auth_str = request.env['HTTP_AUTHORIZATION'] | |
return ActiveSupport::Base64.decode64(auth_str.sub(/^Basic\s+/, '')) == [ user, pass ].join(":") | |
end | |
end | |
def request_http_basic_authentication(realm = 'Application') | |
response.headers["WWW-Authenticate"] = %Q(Basic realm="#{realm}") | |
response.body = "HTTP Basic: Access denied.\n" | |
response.status = 401 | |
return false | |
end | |
end | |
end | |
# Add inside the Padtino App. | |
App.controllers :admin do | |
layout 'admin' | |
before do | |
halt(401, 'Not Authorized') unless authenticate_or_request_with_http_basic('admin', 'SuperSecret') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment