Sha256: dab1835cf4bfe412cc70a539468850f1d60322062c6420a3d524dd40e452fb21

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module Nucleus
  module Adapters
    class TokenAuthClient < AuthClient
      attr_reader :api_token

      # 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
      def initialize(check_certificates = true, &token_parser)
        @token_parser = token_parser
        super(check_certificates)
      end

      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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nucleus-0.3.1 lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb