ExFattureincloud.OAuth.TokenManager (ExFattureincloud v0.1.0)

Copy Markdown View Source

Optional supervised OAuth refresh coordinator for one OAuth connection.

Required options are :client_id, :load (a zero-arity callback returning {:ok, %OAuth.Token{}}) and :save (a callback accepting the new token and returning :ok). Set :client_secret for authorization-code applications; omit it for device-code applications. :client configures OAuth HTTP requests.

:refresh_margin is in seconds (default 60, capped at half the token lifetime). :retry_interval is in milliseconds (default 30,000). :automatic defaults to true; disabling it keeps renewal on demand. :name is a GenServer name and :id optionally distinguishes supervised children.

Refresh and persistence run in a task; concurrent callers share its result. New credentials are published only after :save succeeds. If persistence fails, the rotated credentials remain in memory and only saving is retried. Timeouts, server errors and invalid responses pause renewal because their outcome may be uncertain. invalid_grant requires authorization again. reload/1 recovers by reading the application's current stored credentials.

Use one owner per OAuth connection, even when it covers multiple companies. Coordination is local to this process; deployments on multiple nodes must elect one owner or provide distributed coordination. A process crash between remote renewal and successful persistence cannot restore an unsaved rotated token.

Summary

Types

The PID or registered name of a running token manager.

The current renewal state, cached token expiry, and pending-save flag.

A current access token or a structured authentication error.

Functions

Gets a current access token, sharing any refresh already in progress.

Returns a child specification. Set a distinct :id or :name for each connection.

Builds an API client using the manager's HTTP settings and current credentials.

Reloads persisted credentials when no refresh or save is pending.

Starts one refresh coordinator and loads its stored OAuth credentials.

Returns the renewal state, expiry, and pending-save flag without credentials.

Types

server()

@type server() :: GenServer.server()

The PID or registered name of a running token manager.

status()

@type status() :: %{
  status:
    :ready
    | :persistence_failed
    | :rate_limited
    | :refresh_failed
    | :reauthorization_required,
  expires_at: integer(),
  pending_save?: boolean()
}

The current renewal state, cached token expiry, and pending-save flag.

token_result()

@type token_result() :: {:ok, String.t()} | {:error, ExFattureincloud.Error.t()}

A current access token or a structured authentication error.

Functions

access_token(server, timeout \\ 30000)

@spec access_token(server(), timeout()) :: token_result()

Gets a current access token, sharing any refresh already in progress.

child_spec(init_arg)

@spec child_spec(keyword()) :: Supervisor.child_spec()

Returns a child specification. Set a distinct :id or :name for each connection.

client(server)

@spec client(server()) :: ExFattureincloud.Client.t()

Builds an API client using the manager's HTTP settings and current credentials.

reload(server)

@spec reload(server()) :: :ok | {:error, ExFattureincloud.Error.t()}

Reloads persisted credentials when no refresh or save is pending.

start_link(options)

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

Starts one refresh coordinator and loads its stored OAuth credentials.

status(server)

@spec status(server()) :: status()

Returns the renewal state, expiry, and pending-save flag without credentials.