Class: Tilia::Http::Auth::Bearer

Inherits:
AbstractAuth show all
Defined in:
lib/tilia/http/auth/bearer.rb

Overview

HTTP Bearer authentication utility.

This class helps you setup bearer auth. The process is fairly simple:

  1. Instantiate the class.

  2. Call getToken (this will return null or a token as string)

  3. If you didn’t get a valid token, 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

#require_loginvoid

This method returns an undefined value.

This method sends the needed HTTP header and statuscode (401) to force authentication.



30
31
32
33
# File 'lib/tilia/http/auth/bearer.rb', line 30

def 
  @response.add_header('WWW-Authenticate', "Bearer realm=\"#{@realm}\"")
  @response.status = 401
end

#tokenObject

This method returns a string with an access token.

If no token was found, this method returns null.

Returns:

  • null|string



17
18
19
20
21
22
23
24
# File 'lib/tilia/http/auth/bearer.rb', line 17

def token
  auth = @request.header('Authorization')

  return nil unless auth
  return nil unless auth[0..6].downcase == 'bearer '

  auth[7..-1]
end