H.Credentials (H v0.3.0)

Copy Markdown View Source

In-memory credential resolver for LLM providers.

Credentials are resolved on demand from explicit options, environment variables, H provider settings, or the Google Cloud CLI for ADC, and optionally cached in memory with a TTL safety margin. Secrets are never persisted to disk or included in inspected/sanitized error values.

Summary

Functions

Returns a specification to start this module under a supervisor.

Clears all cached tokens from a running cache process.

Returns true if provider has credentials available in opts or the environment without executing external token helpers.

Returns the list of providers that currently have credentials configured in opts or the environment, ordered by @provider_priority.

Resolves in-memory credentials for provider.

Redacts secret values (access_token, api_key, Bearer ..., etc.) from maps, lists, tuples, exceptions, and strings so credentials never leak into logs or errors.

Starts an in-memory credential cache process.

Types

anonymous_credentials()

@type anonymous_credentials() :: %{auth_mode: :none}

api_key_credentials()

@type api_key_credentials() :: %{api_key: String.t()}

codex_credentials()

@type codex_credentials() :: %{access_token: String.t(), account_id: String.t() | nil}

credentials()

vertex_credentials()

@type vertex_credentials() :: %{
  access_token: String.t(),
  project_id: String.t(),
  region: String.t()
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear_cache(cache \\ __MODULE__)

@spec clear_cache(GenServer.server()) :: :ok

Clears all cached tokens from a running cache process.

configured?(provider, opts \\ [])

@spec configured?(atom() | String.t(), keyword()) :: boolean()

Returns true if provider has credentials available in opts or the environment without executing external token helpers.

configured_providers(opts \\ [])

@spec configured_providers(keyword()) :: [atom()]

Returns the list of providers that currently have credentials configured in opts or the environment, ordered by @provider_priority.

resolve(provider, opts \\ [])

@spec resolve(atom() | String.t(), keyword()) ::
  {:ok, credentials()} | {:error, term()}

Resolves in-memory credentials for provider.

Supported providers:

  • :google_vertex / "google_vertex" / "google-vertex"
  • :openai_codex / "openai-codex" (OAuth access token)
  • :anthropic / "anthropic" (ANTHROPIC_API_KEY)
  • :openai / "openai" (OPENAI_API_KEY)
  • :openrouter / "openrouter" (OPENROUTER_API_KEY)
  • :deepseek / "deepseek" (DEEPSEEK_API_KEY)
  • :google / "google" (GOOGLE_API_KEY)
  • :ollama / "ollama" (no key required; returns %{auth_mode: :none})

Options:

  • :access_token - Explicit bearer token for Vertex or Codex
  • :settings - H-owned provider settings map (populated by the adapter)
  • :account_id - Explicit Codex account ID; otherwise derived from the token
  • :project_id - Override GOOGLE_CLOUD_PROJECT
  • :region - Override GOOGLE_CLOUD_REGION (defaults to "global")
  • :api_key - Explicit API key override (Vertex uses Express Mode)
  • :credentials_path - Override the Vertex ADC file path
  • :fetcher - Injectable 0-arity or 1-arity callback for :google_vertex
  • :cache - Cache server pid/name, or false to bypass cache
  • :env - Optional %{String.t() => String.t()} map overriding System.get_env/1
  • :ttl_margin_ms / :default_ttl_ms / :now_fn - Timing overrides when uncached or configuring cache

sanitize(struct)

@spec sanitize(term()) :: term()

Redacts secret values (access_token, api_key, Bearer ..., etc.) from maps, lists, tuples, exceptions, and strings so credentials never leak into logs or errors.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts an in-memory credential cache process.

Options:

  • :name - Optional registered name (defaults to nil)
  • :ttl_margin_ms - Safety margin subtracted from token expiration (default 60_000)
  • :default_ttl_ms - Fallback TTL when helper output omits expiration (default 3_600_000)
  • :now_fn - Zero-arity clock function returning monotonic/unix ms (default &System.system_time(:millisecond))