Skip to content

Instantly share code, notes, and snippets.

@nbibler
Created November 9, 2009 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbibler/230043 to your computer and use it in GitHub Desktop.
Save nbibler/230043 to your computer and use it in GitHub Desktop.
Warden::Strategies.add(:site_basic) do
def valid?
authorization.present?
end
def authenticate!
if authorization.present?
site = Site.find_by_identifier_and_token(*user_name_and_password)
site ? success!(site) : fail!("Unauthorized")
else
pass
end
end
protected
def authorization
request.env['HTTP_AUTHORIZATION'] ||
request.env['X-HTTP_AUTHORIZATION'] ||
request.env['X_HTTP_AUTHORIZATION'] ||
request.env['REDIRECT_X_HTTP_AUTHORIZATION']
end
def user_name_and_password
decode_credentials.split(/:/, 2)
end
def decode_credentials
Base64.decode64(authorization.split(' ', 2).last || '')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment