Class: TokenValidation::V2::Client

Inherits:
Common::Client::Base show all
Defined in:
lib/token_validation/v2/client.rb

Instance Method Summary collapse

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initialize(api_key:) ⇒ Client

Returns a new instance of Client.



11
12
13
# File 'lib/token_validation/v2/client.rb', line 11

def initialize(api_key:)
  @api_key = api_key
end

Instance Method Details

#permitted_scopes(response:) ⇒ Object (private)



36
37
38
# File 'lib/token_validation/v2/client.rb', line 36

def permitted_scopes(response:)
  JSON.parse(response.body)['data']['attributes']['scp']
end

#token_permits_scope?(scope:, response:) ⇒ Boolean (private)

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/token_validation/v2/client.rb', line 31

def token_permits_scope?(scope:, response:)
  permitted_scopes = permitted_scopes(response:)
  permitted_scopes.include?(scope)
end

#token_valid?(audience:, token:, scope:) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/token_validation/v2/client.rb', line 15

def token_valid?(audience:, token:, scope:)
  json = URI.encode_www_form({ 'aud': audience })
  headers = {
    'apiKey': @api_key,
    'Authorization': "Bearer #{token}",
    'Content-Type': 'application/x-www-form-urlencoded'
  }
  response = perform(:post, 'v2/validation', json, headers)

  return false unless response.status == 200

  token_permits_scope?(scope:, response:)
end