class Nucleus::Adapters::TokenAuthClient

Attributes

api_token[R]

Public Class Methods

new(check_certificates = true, &token_parser) click to toggle source

Create a new instance of an {TokenAuthClient}. @param [Boolean] check_certificates true if SSL certificates are to be validated, false if they are to be ignored (e.g. when using self-signed certificates in development environments) @yield [verify_ssl, username, password] Auth credentials token parser block, must provide the API token, usually retrieved from an HTTP call to the endpoint. @yieldparam [Boolean] verify_ssl true if SSL certificates are to be validated, false if they are to be ignored (e.g. when using self-signed certificates in development environments) @yieldparam [String] username username to be used to retrieve the API token @yieldparam [String] password password to be used to retrieve the API token @yieldreturn [String] API token to be used for authenticated API requests, nil if authentication failed, e.g. due to bad credentials

Calls superclass method Nucleus::Adapters::AuthClient::new
# 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

Public Instance Methods

auth_header() click to toggle source
# File lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb, line 30
def auth_header
  raise Errors::EndpointAuthenticationError, 'Authentication client was not authenticated yet' unless @api_token
  { 'Authorization' => "Bearer #{api_token}" }
end
authenticate(username, password) click to toggle source
# 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)
  raise Errors::EndpointAuthenticationError, 'Authentication failed, credentials seem to be invalid' unless token
  # verification passed, credentials are valid
  @api_token = token
  self
end