Class Merb::AuthenticationMixin::BasicAuthentication
In: merb-core/lib/merb-core/controller/mixins/authentication.rb
Parent: Object

Methods

Included Modules

Merb::ControllerExceptions

Public Class methods

:api: private

[Source]

    # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 88
88:     def initialize(controller, realm = "Application", &authenticator)
89:       @controller = controller
90:       @realm = realm
91:       @auth = Rack::Auth::Basic::Request.new(@controller.request.env)
92:       authenticate_or_request(&authenticator) if authenticator
93:     end

Public Instance methods

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

Parameters

&authenticator:A block that decides whether the provided username and password
  are valid.

Returns

Object:False if basic auth is not provided, otherwise the return value of the authenticator block.

@overridable :api: public

[Source]

     # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 107
107:     def authenticate(&authenticator)
108:       if @auth.provided? and @auth.basic?
109:         authenticator.call(*@auth.credentials)
110:       else
111:         false
112:       end
113:     end

Returns

String:The password provided in the request.

:api: public

[Source]

     # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 158
158:     def password
159:       provided? ? @auth.credentials.last : nil
160:     end

Returns

Boolean:Whether there has been any basic authentication credentials provided

:api: public

[Source]

     # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 142
142:     def provided?
143:       @auth.provided?
144:     end

Request basic authentication and halt the filter chain. This is for use in a before filter.

Throws

:halt with an "HTTP Basic: Access denied." message with no layout, and sets the status to Unauthorized.

:api: public

[Source]

     # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 121
121:     def request
122:       request!
123:       throw :halt, @controller.render("HTTP Basic: Access denied.\n", :status => Unauthorized.status, :layout => false)
124:     end

Sets headers to request basic auth.

Returns

String:Returns the empty string to provide a response body.

:api: public

[Source]

     # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 132
132:     def request!
133:       @controller.status = Unauthorized.status
134:       @controller.headers['WWW-Authenticate'] = 'Basic realm="%s"' % @realm
135:       ""
136:     end

Returns

String:The username provided in the request.

:api: public

[Source]

     # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 150
150:     def username
151:       provided? ? @auth.credentials.first : nil
152:     end

Protected Instance methods

:api: private

[Source]

     # File merb-core/lib/merb-core/controller/mixins/authentication.rb, line 165
165:     def authenticate_or_request(&authenticator)
166:       authenticate(&authenticator) || request
167:     end

[Validate]