Class: Authenticator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dvla/dataverse/support/authenticator.rb

Instance Method Summary collapse

Instance Method Details

#authenticator_configObject



57
58
59
# File 'lib/dvla/dataverse/support/authenticator.rb', line 57

def authenticator_config
  @authenticator_config ||= Struct::AuthenticatorConfig.new
end

#get_tokenString

Note:

This function will setup the token using the client credentials only if the token is null or expired. Otherwise, the same token will be returned.

Be sure to call setup_authentication_configs to setup the configuration before calling get_token

Examples:

authenticator.setup_authentication_configs
authenticator.get_token

Returns:

  • (String)

    access token



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dvla/dataverse/support/authenticator.rb', line 29

def get_token
  client = setup_client
  token = authenticator_config.latest_token
  time_now = Time.now.to_i

  if token.nil? || token.expires_at <= time_now
    client
      .client_credentials
      .get_token(params = { :scope => authenticator_config.scope })
      .then { |token| authenticator_config.latest_token = token }
  end

  authenticator_config.latest_token.token
end

#setup_authentication_configsObject

Note:

Setup the configuration for AuthenticatorConfig. call this function before using get_token.

This function is void

Examples:

authenticator.setup_authentication_configs
authenticator.get_token


53
54
55
# File 'lib/dvla/dataverse/support/authenticator.rb', line 53

def setup_authentication_configs
  [config_auth_url, config_token_url, config_client_id, config_client_secret, config_scope].each(&:call)
end