Class: X::BearerTokenAuthenticator

Inherits:
Authenticator show all
Defined in:
x-core/lib/x/core/bearer_token_authenticator.rb

Overview

Authenticator for Bearer token authentication

Instance Method Summary collapse

Constructor Details

#initialize(bearer_token:) ⇒ BearerTokenAuthenticator

Initialize a new BearerTokenAuthenticator

Examples:

Create a new bearer token authenticator

authenticator = X::BearerTokenAuthenticator.new(bearer_token: "token")

Parameters:

  • bearer_token (String) —

    the bearer token for authentication

Raises:

  • (ArgumentError) —

    if the bearer token is nil or empty



19
20
21
22
# File 'x-core/lib/x/core/bearer_token_authenticator.rb', line 19

def initialize(bearer_token:)
  CredentialValidator.validate_required!({bearer_token:})
  @bearer_token = bearer_token
end

Instance Method Details

#headers(_request) ⇒ Hash{String => String}

Generate the authentication headers for a request

Examples:

Generate a bearer authentication header

authenticator.headers(request)

Parameters:

  • _request (#http_method, #uri, #body, #[], nil) —

    the request, which a bearer token does not sign

Returns:

  • (Hash{String => String}) —

    the authentication header with bearer token



31
32
33
# File 'x-core/lib/x/core/bearer_token_authenticator.rb', line 31

def headers(_request)
  {AUTHENTICATION_HEADER => "Bearer #{bearer_token}"}
end