Class: Anthropic::Credentials::CredentialsFile

Inherits:
ConfigProvider show all
Defined in:
lib/anthropic/credentials/credentials_file.rb,
sig/anthropic/credentials.rbs

Overview

An access token provider backed by a named profile.

A profile is a pair of files under the config directory (+~/.config/anthropic/+ by default; override with ANTHROPIC_CONFIG_DIR):

  • configs/<profile>.json -- non-secret. Holds the nested authentication object (discriminated by its type field), plus top-level organization_id, workspace_id, and base_url. The authentication object may contain a credentials_path field overriding the credentials file location.
  • credentials/<profile>.json -- secret (0600). Holds access_token, expires_at, and (for user_oauth with a client_id) refresh_token.

Dispatches on the authentication.type discriminator:

[+oidc_federation+] OIDC workload identity federation. Lazily constructs a WorkloadIdentity delegate from the nested auth fields plus the top-level organization_id and calls it to perform the jwt-bearer exchange.

[+user_oauth+] Output of an interactive PKCE login. The credentials file contains the access_token (and optionally refresh_token). Currently refresh is not implemented in Ruby; the credentials file is treated as externally rotated.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConfigProvider

#auth_block, #bind_base_url, #build_workload_delegate, #call, #call_oidc_federation, #call_user_oauth, #coerce_expires_at, #extra_headers, #fill, #fill_missing_from_env, #read_credentials, #read_credentials_if_exists, #resolve_base_url, #resolve_identity_token_provider, #resolved_base_url, #write_credentials

Constructor Details

#initialize(profile = nil) ⇒ CredentialsFile

Returns a new instance of CredentialsFile.

Parameters:

  • profile (String, nil) (defaults to: nil)

    profile name; if nil, resolves via ANTHROPIC_PROFILE env -> active_config pointer file -> "default"



38
39
40
41
42
43
# File 'lib/anthropic/credentials/credentials_file.rb', line 38

def initialize(profile = nil)
  super()
  @profile = profile || resolve_profile
  Anthropic::Config.validate_profile_name!(@profile)
  @config_path = config_file_path(@profile)
end

Instance Attribute Details

#config_pathPathname (readonly)

Returns path to the config file.

Returns:

  • (Pathname)

    path to the config file



34
35
36
# File 'lib/anthropic/credentials/credentials_file.rb', line 34

def config_path
  @config_path
end

#profileString (readonly)

Returns the profile name.

Returns:

  • (String)

    the profile name



31
32
33
# File 'lib/anthropic/credentials/credentials_file.rb', line 31

def profile
  @profile
end

Instance Method Details

#config_dirPathname

Returns:

  • (Pathname)


110
111
112
# File 'lib/anthropic/credentials/credentials_file.rb', line 110

def config_dir
  Anthropic::Credentials.config_dir
end

#config_file_path(profile) ⇒ Pathname

Parameters:

  • profile (String)

Returns:

  • (Pathname)


114
115
116
# File 'lib/anthropic/credentials/credentials_file.rb', line 114

def config_file_path(profile)
  Anthropic::Credentials.config_file_path(profile)
end

#credentials_file_path(profile) ⇒ Pathname

Parameters:

  • profile (String)

Returns:

  • (Pathname)


118
119
120
# File 'lib/anthropic/credentials/credentials_file.rb', line 118

def credentials_file_path(profile)
  Anthropic::Credentials.config_dir.join("credentials", "#{profile}.json")
end

#resolve_profileString

Returns:

  • (String)


106
107
108
# File 'lib/anthropic/credentials/credentials_file.rb', line 106

def resolve_profile
  Anthropic::Credentials.active_profile
end