Class: Nucleus::Adapters::TokenAuthClient

Inherits:
AuthClient
  • Object
show all
Defined in:
lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb

Direct Known Subclasses

ExpiringTokenAuthClient

Instance Attribute Summary collapse

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| ... } ⇒ TokenAuthClient

Create a new instance of an Nucleus::Adapters::TokenAuthClient. false if they are to be ignored (e.g. when using self-signed certificates in development environments) must provide the API token, 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) nil if authentication failed, e.g. due to bad credentials

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:

  • (String)

    API token to be used for authenticated API requests,



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

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

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



4
5
6
# File 'lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb', line 4

def api_token
  @api_token
end

Instance Method Details

#auth_headerObject



30
31
32
33
# File 'lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb', line 30

def auth_header
  fail Errors::EndpointAuthenticationError, 'Authentication client was not authenticated yet' unless @api_token
  { 'Authorization' => "Bearer #{api_token}" }
end

#authenticate(username, password) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb', line 22

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