Class: Legate::Auth::Credential

Inherits:
Object
  • Object
show all
Defined in:
lib/legate/auth/credential.rb

Overview

Represents authentication credentials required by different schemes. Handles different types of credentials such as API keys, OAuth2 client credentials, service account keys, and more.

Examples:

API Key credential

credential = Legate::Auth::Credential.new(
  auth_type: :api_key,
  api_key: 'my-api-key'
)

OAuth2 credential with environment variable

credential = Legate::Auth::Credential.new(
  auth_type: :oauth2,
  client_id: 'my-client-id',
  client_secret: 'ENV:MY_CLIENT_SECRET'
)

Constant Summary collapse

VALID_TYPES =

Valid credential types

%i[api_key oauth2 oidc service_account google_service_account http_bearer basic].freeze
ENV_PREFIX =

Prefix for environment variable references

'ENV:'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_type:, **kwargs) ⇒ Credential

Initialize a new credential

Parameters:

  • auth_type (Symbol)

    The type of authentication (:api_key, :oauth2, :oidc, :service_account, :http_bearer)

  • kwargs (Hash)

    Additional attributes for the specific auth type

Raises:



36
37
38
39
40
41
42
# File 'lib/legate/auth/credential.rb', line 36

def initialize(auth_type:, **kwargs)
  @auth_type = auth_type.to_sym
  @attributes = kwargs

  validate_auth_type!
  validate_required_attributes!
end

Instance Attribute Details

#auth_typeSymbol (readonly)

Returns The type of authentication.

Returns:

  • (Symbol)

    The type of authentication



30
31
32
# File 'lib/legate/auth/credential.rb', line 30

def auth_type
  @auth_type
end

Instance Method Details

#[](name, resolve_env: true) ⇒ Object?

Get an attribute value

Parameters:

  • name (Symbol, String)

    The attribute name

  • resolve_env (Boolean) (defaults to: true)

    Whether to resolve environment variables

Returns:

  • (Object, nil)

    The attribute value, or nil if not present

Raises:



49
50
51
52
53
54
55
56
57
58
# File 'lib/legate/auth/credential.rb', line 49

def [](name, resolve_env: true)
  attr_name = name.to_sym
  value = @attributes[attr_name]

  if resolve_env && value.is_a?(String) && value.start_with?(ENV_PREFIX)
    resolve_environment_variable(value)
  else
    value
  end
end

#[]=(name, value) ⇒ Object

Set an attribute value

Parameters:

  • name (Symbol, String)

    The attribute name

  • value (Object)

    The attribute value



63
64
65
# File 'lib/legate/auth/credential.rb', line 63

def []=(name, value)
  @attributes[name.to_sym] = value
end

#has_attribute?(name) ⇒ Boolean

Check if the credential has an attribute

Parameters:

  • name (Symbol, String)

    The attribute name

Returns:

  • (Boolean)

    True if the attribute exists



87
88
89
# File 'lib/legate/auth/credential.rb', line 87

def has_attribute?(name)
  @attributes.key?(name.to_sym)
end

#to_h(resolve_env: false) ⇒ Hash

Convert to a hash

Parameters:

  • resolve_env (Boolean) (defaults to: false)

    Whether to resolve environment variables

Returns:

  • (Hash)

    A hash representation of the credential



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/legate/auth/credential.rb', line 70

def to_h(resolve_env: false)
  result = { auth_type: @auth_type }

  @attributes.each do |key, value|
    result[key] = if resolve_env && value.is_a?(String) && value.start_with?(ENV_PREFIX)
                    resolve_environment_variable(value)
                  else
                    value
                  end
  end

  result
end