Class: Merb::AuthenticationMixin::BasicAuthentication

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

Constant Summary

Constant Summary

Constants included from ControllerExceptions

ControllerExceptions::STATUS_CODES

Instance Method Summary (collapse)

Constructor Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BasicAuthentication



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

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

- (Object) authenticate(&authenticator)

Determines whether or not the user is authenticated using the criteria in the provided authenticator block.

Overridable:



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

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

- (String) password

Returns The password provided in the request.



153
154
155
# File 'merb-core/lib/merb-core/controller/mixins/authentication.rb', line 153

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

- (Boolean) provided?

Returns Whether there has been any basic authentication credentials provided



139
140
141
# File 'merb-core/lib/merb-core/controller/mixins/authentication.rb', line 139

def provided?
  @auth.provided?
end

- (Object) request

Request basic authentication and halt the filter chain.

This is for use in a before filter. Throws :halt to stop the filter chain and force authentication with an "HTTP Basic: Access denied." message, an Unauthorized status, and without a layout.



120
121
122
123
# File 'merb-core/lib/merb-core/controller/mixins/authentication.rb', line 120

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

- (String) request!

Sets headers to request basic auth.



130
131
132
133
134
# File 'merb-core/lib/merb-core/controller/mixins/authentication.rb', line 130

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

- (String) username

Returns The username provided in the request.



146
147
148
# File 'merb-core/lib/merb-core/controller/mixins/authentication.rb', line 146

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