Class: Merb::AuthenticationMixin::BasicAuthentication

Inherits:
Object
  • Object
show all
Includes:
ControllerExceptions
Defined in:
lib/merb-core/controller/mixins/authentication.rb

Constant Summary

Constants included from ControllerExceptions

ControllerExceptions::STATUS_CODES

Instance Method Summary collapse

Constructor Details

#initialize(controller, realm = "Application", &authenticator) ⇒ BasicAuthentication

Returns a new instance of BasicAuthentication.



75
76
77
78
79
80
# File 'lib/merb-core/controller/mixins/authentication.rb', line 75

def initialize(controller, realm = "Application", &authenticator)
  @controller = controller
  @realm = realm
  @auth = Rack::Auth::Basic::Request.new(@controller.request.env)
  authenticate_or_request(&authenticator) if authenticator
end

Instance Method Details

#authenticate(&authenticator) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/merb-core/controller/mixins/authentication.rb', line 82

def authenticate(&authenticator)
  if @auth.provided? and @auth.basic?
    authenticator.call(*@auth.credentials)
  else
    false
  end
end

#passwordObject



111
112
113
# File 'lib/merb-core/controller/mixins/authentication.rb', line 111

def password
  provided? ? @auth.credentials.last : nil
end

#provided?Boolean

Checks to see if there has been any basic authentication credentials provided

Returns:

  • (Boolean)


103
104
105
# File 'lib/merb-core/controller/mixins/authentication.rb', line 103

def provided?
  @auth.provided?
end

#requestObject



90
91
92
93
# File 'lib/merb-core/controller/mixins/authentication.rb', line 90

def request
  request!
  throw :halt, @controller.render("HTTP Basic: Access denied.\n", :status => Unauthorized.status, :layout => false)
end

#request!Object

This is a special case for use outside a before filter. Use this if you need to request basic authenticaiton as part of an action



97
98
99
100
# File 'lib/merb-core/controller/mixins/authentication.rb', line 97

def request!
  @controller.status = Unauthorized.status
  @controller.headers['WWW-Authenticate'] = 'Basic realm="%s"' % @realm
end

#usernameObject



107
108
109
# File 'lib/merb-core/controller/mixins/authentication.rb', line 107

def username
  provided? ? @auth.credentials.first : nil
end