Class: Legate::Auth::Schemes::HTTPBearer

Inherits:
Legate::Auth::Scheme show all
Defined in:
lib/legate/auth/schemes/http_bearer.rb

Overview

HTTP Bearer authentication scheme. This scheme applies a bearer token to requests via the Authorization header.

Instance Method Summary collapse

Methods inherited from Legate::Auth::Scheme

#authentication_error?, #build_authorization_uri, #refresh_token, #revoke_token, #supports_refresh?, #to_s, #validate!

Instance Method Details

#apply_to_request(request, credential) ⇒ Hash

Apply authentication to a request

Parameters:

Returns:

  • (Hash)

    The modified request with authentication applied

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legate/auth/schemes/http_bearer.rb', line 26

def apply_to_request(request, credential)
  # Create a deep copy of the request to avoid modifying the original
  request_copy = Marshal.load(Marshal.dump(request))

  # Handle the case where we get a stack object from Excon
  if request_copy.is_a?(Hash)
    if request_copy[:stack]
      # Extract the data from stack (Excon middleware format)
      %i[scheme method path host port query].each do |key|
        request_copy[key] = request_copy[:stack][key] if request_copy[:stack][key] && !request_copy[key]
      end
    end

    # Ensure headers hash exists
    request_copy[:headers] ||= {}
  end

  # Extract the bearer token from the credential
  bearer_token = extract_bearer_token(credential)
  raise Legate::Auth::Error, 'Bearer token not found in credential' unless bearer_token

  # Apply the bearer token to the Authorization header
  validate_header_value!(bearer_token, 'Bearer token')
  request_copy[:headers]['Authorization'] = "Bearer #{bearer_token}"
  request_copy
end

#exchange_token(credential) ⇒ Legate::Auth::ExchangedCredential

Exchange a credential for a token

Parameters:

Returns:

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legate/auth/schemes/http_bearer.rb', line 56

def exchange_token(credential)
  # For bearer tokens, we simply create a "token" that wraps the bearer token
  # This is useful for token management consistency
  bearer_token = extract_bearer_token(credential)
  raise Legate::Auth::TokenExchangeError, 'Bearer token not found in credential' unless bearer_token

  # Create a simple exchanged credential that never expires
  Legate::Auth::ExchangedCredential.new(
    auth_type: :http_bearer,
    access_token: bearer_token
  )
end

#scheme_typeSymbol

Get the type of authentication scheme

Returns:

  • (Symbol)

    The scheme type identifier



17
18
19
# File 'lib/legate/auth/schemes/http_bearer.rb', line 17

def scheme_type
  :http_bearer
end

#to_hHash

Get hash representation of the scheme

Returns:

  • (Hash)

    Scheme configuration as a hash



71
72
73
74
75
# File 'lib/legate/auth/schemes/http_bearer.rb', line 71

def to_h
  {
    type: scheme_type
  }
end