Class: Tilia::Http::Auth::Basic

Inherits:
AbstractAuth show all
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:

  1. Instantiate the class.

  2. Call getCredentials (this will return null or a user/pass pair)

  3. If you didn’t get valid credentials, call ‘requireLogin’

Instance Attribute Summary

Attributes inherited from AbstractAuth

#realm

Instance Method Summary collapse

Methods inherited from AbstractAuth

#initialize

Constructor Details

This class inherits a constructor from Tilia::Http::Auth::AbstractAuth

Instance Method Details

#credentialsObject

This method returns a numeric array with a username and password as the only elements.

If no credentials were found, this method returns null.

Returns:

  • null|array



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_loginvoid

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 
  @response.add_header('WWW-Authenticate', "Basic realm=\"#{@realm}\"")
  @response.status = 401
end