2009-05-13 04:10:37 -04:00
|
|
|
require 'active_support/base64'
|
2010-03-28 08:15:02 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2009-05-13 04:10:37 -04:00
|
|
|
|
2007-05-08 01:48:18 -04:00
|
|
|
module ActionController
|
|
|
|
module HttpAuthentication
|
|
|
|
# Makes it dead easy to do HTTP Basic authentication.
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-05-08 01:48:18 -04:00
|
|
|
# Simple Basic example:
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# class PostsController < ApplicationController
|
|
|
|
# USER_NAME, PASSWORD = "dhh", "secret"
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# before_filter :authenticate, :except => [ :index ]
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# def index
|
|
|
|
# render :text => "Everyone can see me!"
|
|
|
|
# end
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# def edit
|
|
|
|
# render :text => "I'm only accessible if you know the password"
|
2007-05-08 01:48:18 -04:00
|
|
|
# end
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# private
|
|
|
|
# def authenticate
|
2009-01-29 11:00:07 -05:00
|
|
|
# authenticate_or_request_with_http_basic do |user_name, password|
|
2007-06-23 12:43:08 -04:00
|
|
|
# user_name == USER_NAME && password == PASSWORD
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# Here is a more advanced Basic example where only Atom feeds and the XML API is protected by HTTP authentication,
|
2007-05-08 01:48:18 -04:00
|
|
|
# the regular HTML interface is protected by a session approach:
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# class ApplicationController < ActionController::Base
|
|
|
|
# before_filter :set_account, :authenticate
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# protected
|
|
|
|
# def set_account
|
|
|
|
# @account = Account.find_by_url_name(request.subdomains.first)
|
|
|
|
# end
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# def authenticate
|
|
|
|
# case request.format
|
|
|
|
# when Mime::XML, Mime::ATOM
|
|
|
|
# if user = authenticate_with_http_basic { |u, p| @account.users.authenticate(u, p) }
|
|
|
|
# @current_user = user
|
|
|
|
# else
|
|
|
|
# request_http_basic_authentication
|
|
|
|
# end
|
2007-05-08 01:48:18 -04:00
|
|
|
# else
|
2007-06-23 12:43:08 -04:00
|
|
|
# if session_authenticated?
|
|
|
|
# @current_user = @account.users.find(session[:authenticated][:user_id])
|
|
|
|
# else
|
|
|
|
# redirect_to(login_url) and return false
|
|
|
|
# end
|
2007-05-08 01:48:18 -04:00
|
|
|
# end
|
|
|
|
# end
|
2007-06-23 12:43:08 -04:00
|
|
|
# end
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-05-08 01:48:18 -04:00
|
|
|
# In your integration tests, you can do something like this:
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-05-08 01:48:18 -04:00
|
|
|
# def test_access_granted_from_xml
|
|
|
|
# get(
|
2009-01-29 11:00:07 -05:00
|
|
|
# "/notes/1.xml", nil,
|
2007-05-08 01:48:18 -04:00
|
|
|
# :authorization => ActionController::HttpAuthentication::Basic.encode_credentials(users(:dhh).name, users(:dhh).password)
|
|
|
|
# )
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-05-08 01:48:18 -04:00
|
|
|
# assert_equal 200, status
|
|
|
|
# end
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
|
|
|
# Simple Digest example:
|
|
|
|
#
|
2009-03-12 09:24:54 -04:00
|
|
|
# require 'digest/md5'
|
2009-01-29 11:00:07 -05:00
|
|
|
# class PostsController < ApplicationController
|
2009-03-12 09:24:54 -04:00
|
|
|
# REALM = "SuperSecret"
|
|
|
|
# USERS = {"dhh" => "secret", #plain text password
|
|
|
|
# "dap" => Digest:MD5::hexdigest(["dap",REALM,"secret"].join(":")) #ha1 digest password
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
|
|
|
# before_filter :authenticate, :except => [:index]
|
|
|
|
#
|
|
|
|
# def index
|
|
|
|
# render :text => "Everyone can see me!"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def edit
|
|
|
|
# render :text => "I'm only accessible if you know the password"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# private
|
|
|
|
# def authenticate
|
2009-03-12 09:24:54 -04:00
|
|
|
# authenticate_or_request_with_http_digest(REALM) do |username|
|
2009-01-29 11:00:07 -05:00
|
|
|
# USERS[username]
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2009-03-12 09:24:54 -04:00
|
|
|
# NOTE: The +authenticate_or_request_with_http_digest+ block must return the user's password or the ha1 digest hash so the framework can appropriately
|
|
|
|
# hash to check the user's credentials. Returning +nil+ will cause authentication to fail.
|
|
|
|
# Storing the ha1 hash: MD5(username:realm:password), is better than storing a plain password. If
|
|
|
|
# the password file or database is compromised, the attacker would be able to use the ha1 hash to
|
|
|
|
# authenticate as the user at this +realm+, but would not have the user's password to try using at
|
|
|
|
# other sites.
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# On shared hosts, Apache sometimes doesn't pass authentication headers to
|
|
|
|
# FCGI instances. If your environment matches this description and you cannot
|
2008-03-29 14:45:39 -04:00
|
|
|
# authenticate, try this rule in your Apache setup:
|
2009-01-29 11:00:07 -05:00
|
|
|
#
|
2007-06-23 12:43:08 -04:00
|
|
|
# RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
|
2007-05-08 01:48:18 -04:00
|
|
|
module Basic
|
|
|
|
extend self
|
|
|
|
|
|
|
|
module ControllerMethods
|
|
|
|
def authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)
|
|
|
|
authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm)
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_with_http_basic(&login_procedure)
|
2009-08-25 18:05:23 -04:00
|
|
|
HttpAuthentication::Basic.authenticate(request, &login_procedure)
|
2007-05-08 01:48:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_http_basic_authentication(realm = "Application")
|
|
|
|
HttpAuthentication::Basic.authentication_request(self, realm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-25 18:05:23 -04:00
|
|
|
def authenticate(request, &login_procedure)
|
2010-03-10 10:44:24 -05:00
|
|
|
unless request.authorization.blank?
|
2009-08-25 18:05:23 -04:00
|
|
|
login_procedure.call(*user_name_and_password(request))
|
2007-05-08 01:48:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_name_and_password(request)
|
|
|
|
decode_credentials(request).split(/:/, 2)
|
|
|
|
end
|
2009-01-29 11:00:07 -05:00
|
|
|
|
2007-05-08 01:48:18 -04:00
|
|
|
def decode_credentials(request)
|
2010-03-10 10:44:24 -05:00
|
|
|
ActiveSupport::Base64.decode64(request.authorization.split(' ', 2).last || '')
|
2007-05-08 01:48:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def encode_credentials(user_name, password)
|
2007-12-18 16:14:07 -05:00
|
|
|
"Basic #{ActiveSupport::Base64.encode64("#{user_name}:#{password}")}"
|
2007-05-08 01:48:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authentication_request(controller, realm)
|
|
|
|
controller.headers["WWW-Authenticate"] = %(Basic realm="#{realm.gsub(/"/, "")}")
|
2009-08-25 20:34:07 -04:00
|
|
|
controller.response_body = "HTTP Basic: Access denied.\n"
|
|
|
|
controller.status = 401
|
2007-05-08 01:48:18 -04:00
|
|
|
end
|
|
|
|
end
|
2009-01-29 11:00:07 -05:00
|
|
|
|
|
|
|
module Digest
|
|
|
|
extend self
|
|
|
|
|
|
|
|
module ControllerMethods
|
|
|
|
def authenticate_or_request_with_http_digest(realm = "Application", &password_procedure)
|
|
|
|
authenticate_with_http_digest(realm, &password_procedure) || request_http_digest_authentication(realm)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticate with HTTP Digest, returns true or false
|
|
|
|
def authenticate_with_http_digest(realm = "Application", &password_procedure)
|
2010-04-05 04:52:47 -04:00
|
|
|
HttpAuthentication::Digest.authenticate(request, realm, &password_procedure)
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Render output including the HTTP Digest authentication header
|
|
|
|
def request_http_digest_authentication(realm = "Application", message = nil)
|
|
|
|
HttpAuthentication::Digest.authentication_request(self, realm, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns false on a valid response, true otherwise
|
2010-04-05 04:52:47 -04:00
|
|
|
def authenticate(request, realm, &password_procedure)
|
|
|
|
request.authorization && validate_digest_response(request, realm, &password_procedure)
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
|
2009-05-26 16:06:09 -04:00
|
|
|
# Returns false unless the request credentials response value matches the expected value.
|
2009-03-12 09:24:54 -04:00
|
|
|
# First try the password as a ha1 digest password. If this fails, then try it as a plain
|
|
|
|
# text password.
|
2010-04-05 04:52:47 -04:00
|
|
|
def validate_digest_response(request, realm, &password_procedure)
|
|
|
|
secret_key = secret_token(request)
|
2009-01-29 17:06:03 -05:00
|
|
|
credentials = decode_credentials_header(request)
|
2010-03-02 19:22:53 -05:00
|
|
|
valid_nonce = validate_nonce(secret_key, request, credentials[:nonce])
|
2009-01-29 11:00:07 -05:00
|
|
|
|
2010-03-02 19:22:53 -05:00
|
|
|
if valid_nonce && realm == credentials[:realm] && opaque(secret_key) == credentials[:opaque]
|
2009-01-29 11:00:07 -05:00
|
|
|
password = password_procedure.call(credentials[:username])
|
2009-05-26 16:06:09 -04:00
|
|
|
return false unless password
|
|
|
|
|
2009-05-18 10:59:37 -04:00
|
|
|
method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']
|
2010-03-03 03:31:55 -05:00
|
|
|
uri = credentials[:uri][0,1] == '/' ? request.fullpath : request.url
|
2009-03-12 09:24:54 -04:00
|
|
|
|
|
|
|
[true, false].any? do |password_is_ha1|
|
2009-08-09 05:40:43 -04:00
|
|
|
expected = expected_response(method, uri, credentials, password, password_is_ha1)
|
2009-03-12 09:24:54 -04:00
|
|
|
expected == credentials[:response]
|
|
|
|
end
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the expected response for a request of +http_method+ to +uri+ with the decoded +credentials+ and the expected +password+
|
2009-03-12 09:24:54 -04:00
|
|
|
# Optional parameter +password_is_ha1+ is set to +true+ by default, since best practice is to store ha1 digest instead
|
|
|
|
# of a plain-text password.
|
|
|
|
def expected_response(http_method, uri, credentials, password, password_is_ha1=true)
|
|
|
|
ha1 = password_is_ha1 ? password : ha1(credentials, password)
|
2009-01-29 11:00:07 -05:00
|
|
|
ha2 = ::Digest::MD5.hexdigest([http_method.to_s.upcase, uri].join(':'))
|
|
|
|
::Digest::MD5.hexdigest([ha1, credentials[:nonce], credentials[:nc], credentials[:cnonce], credentials[:qop], ha2].join(':'))
|
|
|
|
end
|
|
|
|
|
2009-03-12 09:24:54 -04:00
|
|
|
def ha1(credentials, password)
|
|
|
|
::Digest::MD5.hexdigest([credentials[:username], credentials[:realm], password].join(':'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_credentials(http_method, credentials, password, password_is_ha1)
|
|
|
|
credentials[:response] = expected_response(http_method, credentials[:uri], credentials, password, password_is_ha1)
|
2009-01-29 11:00:07 -05:00
|
|
|
"Digest " + credentials.sort_by {|x| x[0].to_s }.inject([]) {|a, v| a << "#{v[0]}='#{v[1]}'" }.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def decode_credentials_header(request)
|
2010-03-10 10:44:24 -05:00
|
|
|
decode_credentials(request.authorization)
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def decode_credentials(header)
|
|
|
|
header.to_s.gsub(/^Digest\s+/,'').split(',').inject({}) do |hash, pair|
|
|
|
|
key, value = pair.split('=', 2)
|
|
|
|
hash[key.strip.to_sym] = value.to_s.gsub(/^"|"$/,'').gsub(/'/, '')
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authentication_header(controller, realm)
|
2010-04-05 04:52:47 -04:00
|
|
|
secret_key = secret_token(controller.request)
|
2010-03-02 19:22:53 -05:00
|
|
|
nonce = self.nonce(secret_key)
|
|
|
|
opaque = opaque(secret_key)
|
2009-03-12 09:24:54 -04:00
|
|
|
controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}")
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def authentication_request(controller, realm, message = nil)
|
|
|
|
message ||= "HTTP Digest: Access denied.\n"
|
|
|
|
authentication_header(controller, realm)
|
2009-08-25 20:29:48 -04:00
|
|
|
controller.response_body = message
|
|
|
|
controller.status = 401
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
def secret_token(request)
|
|
|
|
secret = request.env["action_dispatch.secret_token"]
|
|
|
|
raise "You must set config.secret_token in your app's config" if secret.blank?
|
|
|
|
secret
|
|
|
|
end
|
|
|
|
|
2009-01-29 11:00:07 -05:00
|
|
|
# Uses an MD5 digest based on time to generate a value to be used only once.
|
|
|
|
#
|
|
|
|
# A server-specified data string which should be uniquely generated each time a 401 response is made.
|
|
|
|
# It is recommended that this string be base64 or hexadecimal data.
|
|
|
|
# Specifically, since the string is passed in the header lines as a quoted string, the double-quote character is not allowed.
|
|
|
|
#
|
|
|
|
# The contents of the nonce are implementation dependent.
|
|
|
|
# The quality of the implementation depends on a good choice.
|
|
|
|
# A nonce might, for example, be constructed as the base 64 encoding of
|
|
|
|
#
|
|
|
|
# => time-stamp H(time-stamp ":" ETag ":" private-key)
|
|
|
|
#
|
|
|
|
# where time-stamp is a server-generated time or other non-repeating value,
|
|
|
|
# ETag is the value of the HTTP ETag header associated with the requested entity,
|
|
|
|
# and private-key is data known only to the server.
|
|
|
|
# With a nonce of this form a server would recalculate the hash portion after receiving the client authentication header and
|
|
|
|
# reject the request if it did not match the nonce from that header or
|
|
|
|
# if the time-stamp value is not recent enough. In this way the server can limit the time of the nonce's validity.
|
|
|
|
# The inclusion of the ETag prevents a replay request for an updated version of the resource.
|
|
|
|
# (Note: including the IP address of the client in the nonce would appear to offer the server the ability
|
|
|
|
# to limit the reuse of the nonce to the same client that originally got it.
|
|
|
|
# However, that would break proxy farms, where requests from a single user often go through different proxies in the farm.
|
|
|
|
# Also, IP address spoofing is not that hard.)
|
|
|
|
#
|
|
|
|
# An implementation might choose not to accept a previously used nonce or a previously used digest, in order to
|
|
|
|
# protect against a replay attack. Or, an implementation might choose to use one-time nonces or digests for
|
|
|
|
# POST or PUT requests and a time-stamp for GET requests. For more details on the issues involved see Section 4
|
|
|
|
# of this document.
|
|
|
|
#
|
2009-03-12 09:24:54 -04:00
|
|
|
# The nonce is opaque to the client. Composed of Time, and hash of Time with secret
|
|
|
|
# key from the Rails session secret generated upon creation of project. Ensures
|
2009-07-25 11:03:58 -04:00
|
|
|
# the time cannot be modified by client.
|
2010-03-02 19:22:53 -05:00
|
|
|
def nonce(secret_key, time = Time.now)
|
2009-01-29 11:00:07 -05:00
|
|
|
t = time.to_i
|
2009-03-12 09:24:54 -04:00
|
|
|
hashed = [t, secret_key]
|
2009-01-29 11:00:07 -05:00
|
|
|
digest = ::Digest::MD5.hexdigest(hashed.join(":"))
|
2009-05-13 04:10:37 -04:00
|
|
|
ActiveSupport::Base64.encode64("#{t}:#{digest}").gsub("\n", '')
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
|
2009-03-12 09:24:54 -04:00
|
|
|
# Might want a shorter timeout depending on whether the request
|
|
|
|
# is a PUT or POST, and if client is browser or web service.
|
|
|
|
# Can be much shorter if the Stale directive is implemented. This would
|
|
|
|
# allow a user to use new nonce without prompting user again for their
|
|
|
|
# username and password.
|
2010-03-02 19:22:53 -05:00
|
|
|
def validate_nonce(secret_key, request, value, seconds_to_timeout=5*60)
|
2009-05-13 04:10:37 -04:00
|
|
|
t = ActiveSupport::Base64.decode64(value).split(":").first.to_i
|
2010-03-02 19:22:53 -05:00
|
|
|
nonce(secret_key, t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
|
|
|
|
2009-03-12 09:24:54 -04:00
|
|
|
# Opaque based on random generation - but changing each request?
|
2010-03-02 19:22:53 -05:00
|
|
|
def opaque(secret_key)
|
2009-03-12 09:24:54 -04:00
|
|
|
::Digest::MD5.hexdigest(secret_key)
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
2009-03-12 09:24:54 -04:00
|
|
|
|
2009-01-29 11:00:07 -05:00
|
|
|
end
|
2010-04-30 10:46:30 -04:00
|
|
|
|
|
|
|
# Makes it dead easy to do HTTP Token authentication.
|
|
|
|
#
|
|
|
|
# Simple Token example:
|
|
|
|
#
|
|
|
|
# class PostsController < ApplicationController
|
|
|
|
# TOKEN = "secret"
|
|
|
|
#
|
|
|
|
# before_filter :authenticate, :except => [ :index ]
|
|
|
|
#
|
|
|
|
# def index
|
|
|
|
# render :text => "Everyone can see me!"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def edit
|
|
|
|
# render :text => "I'm only accessible if you know the password"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# private
|
|
|
|
# def authenticate
|
|
|
|
# authenticate_or_request_with_http_token do |token, options|
|
|
|
|
# token == TOKEN
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Here is a more advanced Token example where only Atom feeds and the XML API is protected by HTTP token authentication,
|
|
|
|
# the regular HTML interface is protected by a session approach:
|
|
|
|
#
|
|
|
|
# class ApplicationController < ActionController::Base
|
|
|
|
# before_filter :set_account, :authenticate
|
|
|
|
#
|
|
|
|
# protected
|
|
|
|
# def set_account
|
|
|
|
# @account = Account.find_by_url_name(request.subdomains.first)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def authenticate
|
|
|
|
# case request.format
|
|
|
|
# when Mime::XML, Mime::ATOM
|
|
|
|
# if user = authenticate_with_http_token { |t, o| @account.users.authenticate(t, o) }
|
|
|
|
# @current_user = user
|
|
|
|
# else
|
|
|
|
# request_http_token_authentication
|
|
|
|
# end
|
|
|
|
# else
|
|
|
|
# if session_authenticated?
|
|
|
|
# @current_user = @account.users.find(session[:authenticated][:user_id])
|
|
|
|
# else
|
|
|
|
# redirect_to(login_url) and return false
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# In your integration tests, you can do something like this:
|
|
|
|
#
|
|
|
|
# def test_access_granted_from_xml
|
|
|
|
# get(
|
|
|
|
# "/notes/1.xml", nil,
|
|
|
|
# :authorization => ActionController::HttpAuthentication::Token.encode_credentials(users(:dhh).token)
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# assert_equal 200, status
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# On shared hosts, Apache sometimes doesn't pass authentication headers to
|
|
|
|
# FCGI instances. If your environment matches this description and you cannot
|
|
|
|
# authenticate, try this rule in your Apache setup:
|
|
|
|
#
|
|
|
|
# RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
|
|
|
|
module Token
|
|
|
|
|
|
|
|
extend self
|
|
|
|
|
|
|
|
module ControllerMethods
|
|
|
|
def authenticate_or_request_with_http_token(realm = "Application", &login_procedure)
|
|
|
|
authenticate_with_http_token(&login_procedure) || request_http_token_authentication(realm)
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_with_http_token(&login_procedure)
|
|
|
|
Token.authenticate(self, &login_procedure)
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_http_token_authentication(realm = "Application")
|
|
|
|
Token.authentication_request(self, realm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If token Authorization header is present, call the login procedure with
|
|
|
|
# the present token and options.
|
|
|
|
#
|
|
|
|
# controller - ActionController::Base instance for the current request.
|
|
|
|
# login_procedure - Proc to call if a token is present. The Proc should
|
|
|
|
# take 2 arguments:
|
|
|
|
# authenticate(controller) { |token, options| ... }
|
|
|
|
#
|
|
|
|
# Returns the return value of `&login_procedure` if a token is found.
|
|
|
|
# Returns nil if no token is found.
|
|
|
|
def authenticate(controller, &login_procedure)
|
|
|
|
token, options = token_and_options(controller.request)
|
|
|
|
if !token.blank?
|
|
|
|
login_procedure.call(token, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parses the token and options out of the token authorization header. If
|
|
|
|
# the header looks like this:
|
|
|
|
# Authorization: Token token="abc", nonce="def"
|
|
|
|
# Then the returned token is "abc", and the options is {:nonce => "def"}
|
|
|
|
#
|
|
|
|
# request - ActionController::Request instance with the current headers.
|
|
|
|
#
|
|
|
|
# Returns an Array of [String, Hash] if a token is present.
|
|
|
|
# Returns nil if no token is found.
|
|
|
|
def token_and_options(request)
|
|
|
|
if header = request.authorization.to_s[/^Token (.*)/]
|
|
|
|
values = $1.split(',').
|
|
|
|
inject({}) do |memo, value|
|
|
|
|
value.strip! # remove any spaces between commas and values
|
|
|
|
key, value = value.split(/\=\"?/) # split key=value pairs
|
|
|
|
value.chomp!('"') # chomp trailing " in value
|
|
|
|
value.gsub!(/\\\"/, '"') # unescape remaining quotes
|
|
|
|
memo.update(key => value)
|
|
|
|
end
|
|
|
|
[values.delete("token"), values.with_indifferent_access]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Encodes the given token and options into an Authorization header value.
|
|
|
|
#
|
|
|
|
# token - String token.
|
|
|
|
# options - optional Hash of the options.
|
|
|
|
#
|
|
|
|
# Returns String.
|
|
|
|
def encode_credentials(token, options = {})
|
|
|
|
values = ["token=#{token.to_s.inspect}"]
|
|
|
|
options.each do |key, value|
|
|
|
|
values << "#{key}=#{value.to_s.inspect}"
|
|
|
|
end
|
|
|
|
"Token #{values * ", "}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets a WWW-Authenticate to let the client know a token is desired.
|
|
|
|
#
|
|
|
|
# controller - ActionController::Base instance for the outgoing response.
|
|
|
|
# realm - String realm to use in the header.
|
|
|
|
#
|
|
|
|
# Returns nothing.
|
|
|
|
def authentication_request(controller, realm)
|
|
|
|
controller.headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
|
|
|
|
controller.__send__ :render, :text => "HTTP Token: Access denied.\n", :status => :unauthorized
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-05-08 01:48:18 -04:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|