Skip to content

Instantly share code, notes, and snippets.

@coffeeaddict
Forked from ipoval/http_authentication
Created April 16, 2012 18:09
Show Gist options
  • Save coffeeaddict/2400427 to your computer and use it in GitHub Desktop.
Save coffeeaddict/2400427 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(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