diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2011-03-28 18:09:50 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2011-03-28 18:10:08 -0700 |
commit | e2b07ee000439d0bd41f725ff9f7ad53e52a7e9b (patch) | |
tree | 0f49fd2489ad5e019d5bde163daefa0fe8babb6f /actionpack/lib/action_controller | |
parent | aea1477362b640ebe52cf991b915ad32e7bf2571 (diff) | |
download | rails-e2b07ee000439d0bd41f725ff9f7ad53e52a7e9b.tar.gz rails-e2b07ee000439d0bd41f725ff9f7ad53e52a7e9b.tar.bz2 rails-e2b07ee000439d0bd41f725ff9f7ad53e52a7e9b.zip |
Added Base.http_basic_authenticate_with to do simple http basic authentication with a single class method call [DHH]
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r-- | actionpack/lib/action_controller/metal/http_authentication.rb | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index 39c804d707..e28709d8cf 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -8,9 +8,7 @@ module ActionController # === Simple \Basic example # # class PostsController < ApplicationController - # USER_NAME, PASSWORD = "dhh", "secret" - # - # before_filter :authenticate, :except => [ :index ] + # http_basic_authenticate_with :name => "dhh", "secret", :except => :index # # def index # render :text => "Everyone can see me!" @@ -19,15 +17,7 @@ module ActionController # def edit # render :text => "I'm only accessible if you know the password" # end - # - # private - # def authenticate - # authenticate_or_request_with_http_basic do |user_name, password| - # user_name == USER_NAME && password == PASSWORD - # end - # end - # end - # + # end # # === Advanced \Basic example # @@ -115,6 +105,20 @@ module ActionController extend self module ControllerMethods + extend ActiveSupport::Concern + + module ClassMethods + def http_basic_authenticate_with(options = {}) + before_filter(options.except(:name, :password, :realm)) do + authenticate_or_request_with_http_basic(options[:realm] || "Application") do + authenticate_or_request_with_http_basic do |name, password| + name == options[:name] && password == options[:password] + end + end + end + end + end + def authenticate_or_request_with_http_basic(realm = "Application", &login_procedure) authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm) end @@ -378,7 +382,6 @@ module ActionController # # RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L] module Token - extend self module ControllerMethods @@ -458,6 +461,5 @@ module ActionController controller.__send__ :render, :text => "HTTP Token: Access denied.\n", :status => :unauthorized end end - end end |