Skip to content

Instantly share code, notes, and snippets.

@ipoval
Created February 23, 2011 17:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ipoval/840808 to your computer and use it in GitHub Desktop.
Save ipoval/840808 to your computer and use it in GitHub Desktop.
Http basic authentication for padrino
module HttpAuthentication
module Basic
def authenticate_or_request_with_http_basic(realm = 'Application')
authenticate_with_http_basic || request_http_basic_authentication(realm)
end
def authenticate_with_http_basic
if auth_str = request.env['HTTP_AUTHORIZATION']
return 'login:password' == ActiveSupport::Base64.decode64(auth_str.sub(/^Basic\s+/, ''))
end
end
def request_http_basic_authentication(realm = 'Application')
response.headers["WWW-Authenticate"] = %(Basic realm="Application")
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
end
end
@CaDs
Copy link

CaDs commented Feb 4, 2013

Thanks, that worked like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment