Class: X::BearerTokenAuthenticator

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

Overview

Authenticator for Bearer token authentication

Constant Summary

Constants inherited from Authenticator

Authenticator::AUTHENTICATION_HEADER

Instance Attribute Summary collapse

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



21
22
23
# File 'lib/x/bearer_token_authenticator.rb', line 21

def initialize(bearer_token:)
  @bearer_token = bearer_token
end

Instance Attribute Details

#bearer_tokenString

The bearer token for authentication

Examples:

Get the bearer token

authenticator.bearer_token

Returns:

  • (String)

    the bearer token



12
13
14
# File 'lib/x/bearer_token_authenticator.rb', line 12

def bearer_token
  @bearer_token
end

Instance Method Details

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

Generate the authentication header for a request

Examples:

Generate a bearer authentication header

authenticator.header(request)

Parameters:

  • _request (Net::HTTPRequest)

    the HTTP request

Returns:

  • (Hash{String => String})

    the authentication header with bearer token



32
33
34
# File 'lib/x/bearer_token_authenticator.rb', line 32

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