class Nucleus::Adapters::HttpBasicAuthClient

Implementation of the AuthClient that works with the HTTP basic authentication.

Public Class Methods

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

Create a new instance of an {HttpBasicAuthClient}. @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 verification block, must check if the combination of username and password is accepted by the endpoint. @yieldparam [Hash<String,String>] headers headers for an HTTP request, including the authentication header to be tested @yieldreturn [Boolean] true if the authentication was verified to be ok, false if an error occurred, e.g. with bad credentials

Calls superclass method Nucleus::Adapters::AuthClient::new
# File lib/nucleus/core/adapter_extensions/auth/http_basic_auth_client.rb, line 14
def initialize(check_certificates = true, &verification)
  @verification = verification
  super(check_certificates)
end

Public Instance Methods

auth_header() click to toggle source

@see AuthClient#auth_header

# File lib/nucleus/core/adapter_extensions/auth/http_basic_auth_client.rb, line 30
def auth_header
  raise Errors::EndpointAuthenticationError,
        'Authentication client was not authenticated yet' unless @packed_credentials
  { 'Authorization' => "Basic #{@packed_credentials}" }
end
authenticate(username, password) click to toggle source

@see AuthClient#authenticate

# File lib/nucleus/core/adapter_extensions/auth/http_basic_auth_client.rb, line 20
def authenticate(username, password)
  packed_credentials = ["#{username}:#{password}"].pack('m*').delete("\n")
  valid = @verification.call(verify_ssl, 'Authorization' => "Basic #{packed_credentials}")
  raise Errors::EndpointAuthenticationError, 'Authentication failed, credentials seem to be invalid' unless valid
  # verification passed, credentials are valid
  @packed_credentials = packed_credentials
  self
end