ExAzureCore.Auth.TokenServer (ex_azure_core v0.1.0)

Copy Markdown

GenServer for managing individual credential tokens with proactive refresh.

Each TokenServer instance manages a single credential's token lifecycle:

  • Fetches tokens from configured source
  • Stores tokens in Registry for efficient lookup
  • Proactively schedules refresh before expiry
  • Implements exponential backoff retry logic on failures

Options

  • :name (required) - Unique name for this credential instance
  • :source (required) - Token source configuration tuple
  • :refresh_before - Seconds before expiry to refresh (default: 300)
  • :prefetch - Initial token fetch strategy (:async or :sync, default: :async)
  • :max_retries - Maximum retry attempts (default: 10)
  • :retry_delay - Custom backoff function (default: exponential backoff)

Examples

{:ok, _pid} = ExAzureCore.Auth.TokenServer.start_link(
  name: :my_credential,
  source: {:client_assertion, %{
    tenant_id: "...",
    client_id: "...",
    scope: "https://graph.microsoft.com/.default",
    provider: :aws_cognito,
    provider_opts: [identity_id: "..."]
  }},
  prefetch: :sync
)

{:ok, token} = ExAzureCore.Auth.TokenServer.fetch(:my_credential)

Summary

Functions

Returns a specification to start this module under a supervisor.

Fetches the current token from the token server.

Starts a token server for a credential.

Types

state()

@type state() :: %{
  name: term(),
  source: term(),
  refresh_before: non_neg_integer(),
  max_retries: non_neg_integer(),
  retry_delay: (non_neg_integer() -> non_neg_integer()),
  retry_count: non_neg_integer()
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

fetch(name)

@spec fetch(term()) :: {:ok, map()} | {:error, term()}

Fetches the current token from the token server.

Returns the cached token if available, otherwise fetches a new one.

start_link(opts)

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

Starts a token server for a credential.

Options

See module documentation for available options.