Class: Nucleus::Adapters::HttpBasicAuthClient

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

Overview

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

Instance Attribute Summary

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

Create a new instance of an Nucleus::Adapters::HttpBasicAuthClient. false if they are to be ignored (e.g. when using self-signed certificates in development environments) must check if the combination of username and password is accepted by the endpoint. including the authentication header to be tested false if an error occurred, e.g. with bad credentials

Parameters:

  • check_certificates (Boolean) (defaults to: true)

    true if SSL certificates are to be validated,

Yields:

  • (verify_ssl, username, password)

    Auth credentials verification block,

Yield Parameters:

  • headers (Hash<String,String>)

    headers for an HTTP request,

Yield Returns:

  • (Boolean)

    true if the authentication was verified to be ok,



14
15
16
17
# 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

Instance Method Details

#auth_headerObject



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

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

#authenticate(username, password) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/nucleus/core/adapter_extensions/auth/http_basic_auth_client.rb', line 20

def authenticate(username, password)
  packed_credentials = ["#{username}:#{password}"].pack('m*').gsub(/\n/, '')
  valid = @verification.call(verify_ssl, 'Authorization' => "Basic #{packed_credentials}")
  fail Errors::EndpointAuthenticationError, 'Authentication failed, credentials seem to be invalid' unless valid
  # verification passed, credentials are valid

  @packed_credentials = packed_credentials
  self
end