Class: G5AuthenticatableApi::TokenValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/g5_authenticatable_api/token_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, headers = {}, warden = nil) ⇒ TokenValidator

Returns a new instance of TokenValidator.



5
6
7
8
9
# File 'lib/g5_authenticatable_api/token_validator.rb', line 5

def initialize(params={},headers={},warden=nil)
  @params = params || {}
  @headers = headers || {}
  @warden = warden
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



3
4
5
# File 'lib/g5_authenticatable_api/token_validator.rb', line 3

def error
  @error
end

Instance Method Details

#access_tokenObject



28
29
30
31
32
# File 'lib/g5_authenticatable_api/token_validator.rb', line 28

def access_token
  @access_token ||= (extract_token_from_header ||
                     @params['access_token'] ||
                     @warden.try(:user).try(:g5_access_token))
end

#auth_clientObject



47
48
49
50
# File 'lib/g5_authenticatable_api/token_validator.rb', line 47

def auth_client
  @auth_client ||= G5AuthenticationClient::Client.new(allow_password_credentials: 'false',
                                                      access_token: access_token)
end

#auth_response_headerObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/g5_authenticatable_api/token_validator.rb', line 34

def auth_response_header
  if error
    auth_header = "Bearer"

    if access_token
      auth_header << " error=\"#{error_code}\""
      auth_header << ",error_description=\"#{error_description}\"" if error_description
    end

    auth_header
  end
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
# File 'lib/g5_authenticatable_api/token_validator.rb', line 19

def valid?
  begin
    validate!
    true
  rescue StandardError => e
    false
  end
end

#validate!Object



11
12
13
14
15
16
17
# File 'lib/g5_authenticatable_api/token_validator.rb', line 11

def validate!
  begin
    auth_client.token_info unless skip_validation?
  rescue StandardError => @error
    raise error
  end
end