WeaviateEx.Auth.TokenManager (WeaviateEx v0.7.4)

View Source

GenServer for managing OIDC tokens with automatic refresh.

The TokenManager handles:

  • Initial token acquisition
  • Automatic token refresh before expiration
  • Thread-safe token access
  • Error recovery and retry logic

Usage

# Start with OIDC config
{:ok, pid} = TokenManager.start_link(
  oidc_config: oidc_config,
  auth: %{type: :oidc_client_credentials, client_id: "id", client_secret: "secret", scopes: []}
)

# Or start with issuer URL (auto-discovers OIDC config)
{:ok, pid} = TokenManager.start_link(
  issuer_url: "https://auth.example.com",
  auth: auth
)

# Get current token
{:ok, token} = TokenManager.get_token(pid)

# Get just the access token string
{:ok, access_token} = TokenManager.get_access_token(pid)

# Force immediate refresh
:ok = TokenManager.force_refresh(pid)

Options

  • :oidc_config - Pre-configured OIDC config struct
  • :issuer_url - Issuer URL for auto-discovery (alternative to :oidc_config)
  • :auth - Authentication credentials (required)
  • :name - GenServer name (optional)
  • :refresh_buffer_seconds - Refresh token this many seconds before expiry (default: 60)

Summary

Functions

Returns a child specification for starting TokenManager under a supervisor.

Force an immediate token refresh.

Get just the access token string.

Get the current token.

Start the TokenManager GenServer.

Types

state()

@type state() :: %{
  oidc_config: WeaviateEx.Auth.OIDC.Config.t() | nil,
  auth: map(),
  token: WeaviateEx.Auth.OIDC.TokenResponse.t() | nil,
  refresh_buffer_seconds: non_neg_integer(),
  refresh_timer: reference() | nil
}

Functions

child_spec(init_arg)

Returns a child specification for starting TokenManager under a supervisor.

Examples

# In your supervision tree
children = [
  {WeaviateEx.Auth.TokenManager,
   issuer_url: "https://auth.example.com",
   auth: %{type: :oidc_client_credentials, client_id: "id", client_secret: "secret", scopes: []},
   name: MyApp.WeaviateTokenManager}
]

Supervisor.start_link(children, strategy: :one_for_one)

force_refresh(server)

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

Force an immediate token refresh.

get_access_token(server)

@spec get_access_token(GenServer.server()) :: {:ok, String.t()} | {:error, :no_token}

Get just the access token string.

Convenience function that extracts the access_token from the token response.

get_token(server)

@spec get_token(GenServer.server()) ::
  {:ok, WeaviateEx.Auth.OIDC.TokenResponse.t()} | {:error, :no_token}

Get the current token.

Returns {:ok, token} if a valid token is available, or {:error, :no_token} if no token has been acquired.

start_link(opts)

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

Start the TokenManager GenServer.

Options

  • :oidc_config - Pre-configured OIDC config struct
  • :issuer_url - Issuer URL for auto-discovery
  • :auth - Authentication credentials (required)
  • :name - GenServer name (optional)
  • :refresh_buffer_seconds - Seconds before expiry to refresh (default: 60)