Class: Tilia::Http::Auth::Basic
- Inherits:
-
AbstractAuth
- Object
- AbstractAuth
- Tilia::Http::Auth::Basic
- Defined in:
- lib/tilia/http/auth/basic.rb
Overview
HTTP Basic authentication utility.
This class helps you setup basic auth. The process is fairly simple:
-
Instantiate the class.
-
Call getCredentials (this will return null or a user/pass pair)
-
If you didn’t get valid credentials, call ‘requireLogin’
Instance Attribute Summary
Attributes inherited from AbstractAuth
Instance Method Summary collapse
-
#credentials ⇒ Object
This method returns a numeric array with a username and password as the only elements.
-
#require_login ⇒ void
This method sends the needed HTTP header and statuscode (401) to force the user to login.
Methods inherited from AbstractAuth
Constructor Details
This class inherits a constructor from Tilia::Http::Auth::AbstractAuth
Instance Method Details
#credentials ⇒ Object
This method returns a numeric array with a username and password as the only elements.
If no credentials were found, this method returns null.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tilia/http/auth/basic.rb', line 19 def credentials auth = @request.header('Authorization') return nil unless auth return nil unless auth[0..5].downcase == 'basic ' credentials = Base64.decode64(auth[6..-1]).split(':', 2) return nil unless credentials.size == 2 credentials end |
#require_login ⇒ void
This method returns an undefined value.
This method sends the needed HTTP header and statuscode (401) to force the user to login.
36 37 38 39 |
# File 'lib/tilia/http/auth/basic.rb', line 36 def require_login @response.add_header('WWW-Authenticate', "Basic realm=\"#{@realm}\"") @response.status = 401 end |