Class: Nucleus::Adapters::ExpiringTokenAuthClient

Inherits:
TokenAuthClient show all
Defined in:
lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb

Instance Attribute Summary

Attributes inherited from TokenAuthClient

#api_token

Attributes inherited from AuthClient

#verify_ssl

Instance Method Summary collapse

Methods inherited from AuthClient

#refresh

Constructor Details

#initialize(check_certificates = true) {|verify_ssl, username, password| ... } ⇒ ExpiringTokenAuthClient

Create a new instance of an Nucleus::Adapters::ExpiringTokenAuthClient. An expiring token knows when it starts to be invalid, saving requests to the endpoint that would fail anyways. false if they are to be ignored (e.g. when using self-signed certificates in development environments) must provide the API token and its expiration date, usually retrieved from an HTTP call to the endpoint. false if they are to be ignored (e.g. when using self-signed certificates in development environments)

0

API token to be used for authenticated API requests,

nil if authentication failed, e.g. due to bad credentials

1

Expiration time until the token is valid

Parameters:

  • check_certificates (Boolean) (defaults to: true)

    true if SSL certificates are to be validated,

Yields:

  • (verify_ssl, username, password)

    Auth credentials token parser block,

Yield Parameters:

  • verify_ssl (Boolean)

    true if SSL certificates are to be validated,

  • username (String)

    username to be used to retrieve the API token

  • password (String)

    password to be used to retrieve the API token

Yield Returns:

  • (Array<String>)

    Array with 2 contents:



18
19
20
21
# File 'lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb', line 18

def initialize(check_certificates = true, &token_expiration_parser)
  @token_expiration_parser = token_expiration_parser
  super(check_certificates)
end

Instance Method Details

#auth_headerObject



34
35
36
37
38
# File 'lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb', line 34

def auth_header
  raise Errors::EndpointAuthenticationError, 'Authentication client was not authenticated yet' unless @api_token
  raise Errors::EndpointAuthenticationError, 'Cached authentication token expired' if expired?
  { 'Authorization' => "Bearer #{api_token}" }
end

#authenticate(username, password) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb', line 24

def authenticate(username, password)
  token, expiration_time = @token_expiration_parser.call(verify_ssl, username, password)
  raise Errors::EndpointAuthenticationError, 'Authentication failed, credentials seem to be invalid' unless token
  # verification passed, credentials are valid
  @api_token = token
  @expires = expiration_time
  self
end