Sha256: dcf15d6e4daaa8e6391efa074456edeae905871831d7b0637a352cd38a91e22a

Contents?: true

Size: 1.99 KB

Versions: 11

Compression:

Stored size: 1.99 KB

Contents

require 'action_controller'

# :nodoc: adds authenticates_using_http_basic
class ActionController::Base
  # Keeps track of the currently authenticated user via the session. 
  #
  # Assumes the existence of a User model. A bare ActiveModel model will do the
  # trick. Model instances must implement id, and the model class must implement
  # find_by_id.
  def self.authenticates_using_http_basic(options = {})
    include Authpwn::HttpBasicControllerInstanceMethods
    before_filter :authenticate_using_http_basic, options   
  end
end

# :nodoc: namespace
module Authpwn

# Included in controllers that call authenticates_using_http_basic.
module HttpBasicControllerInstanceMethods
  include Authpwn::CurrentUser

  # Filter that implements authenticates_using_http_basic.
  #
  # If your ApplicationController contains authenticates_using_http_basic, you
  # can opt out in individual controllers using skip_before_filter.
  #
  #     skip_before_filter :authenticate_using_http_filter
  def authenticate_using_http_basic
    return if current_user
    authenticate_with_http_basic do |email, password|
      auth = User.authenticate_signin email, password
      self.current_user = auth unless auth.kind_of? Symbol
    end
  end
  private :authenticate_using_http_basic
  
  # Inform the user that their request is forbidden.
  #
  # If a user is logged on, this renders the session/forbidden view with a HTTP
  # 403 code.
  # 
  # If no user is logged in, a HTTP 403 code is returned, together with an
  # HTTP Authentication header causing the user-agent (browser) to initiate
  # http basic authentication.
  def bounce_to_http_basic()
    unless current_user
      request_http_basic_authentication
      return
    end

    respond_to do |format|
      format.html do
        render 'session/forbidden', status: :forbidden
      end
      format.json do
        render json: { error: "You're not allowed to access that" }
      end
    end
  end
end  # module Authpwn::HttpBasicControllerInstanceMethods

end  # namespace Authpwn

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
authpwn_rails-0.16.2 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.16.1 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.16.0 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.15.3 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.15.2 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.15.1 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.15.0 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.14.3 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.14.2 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.14.1 lib/authpwn_rails/http_basic.rb
authpwn_rails-0.14.0 lib/authpwn_rails/http_basic.rb