AttestoPhoenix.Store.EctoRefreshStore (AttestoPhoenix v5.0.0)

Copy Markdown View Source

PostgreSQL implementation of Attesto.RefreshStore.

Rotation is one family-serialized transaction. The transaction locks the family with a transaction-scoped advisory lock, locks the parent row, marks the parent consumed with its authenticated retry state, and inserts the successor before it commits. A blocked caller therefore sees either the complete committed winner or a complete reuse record; it can never observe a consumed parent before its successor exists.

Positive retry state is authenticated-encrypted before the transaction. The encryption key must be stable across every node that can serve the family. Strict rotation stores only %{retry_until: now, recoverable: false} and needs no encryption secret. Expired retry state is redacted by the sweeper, while a consumed parent is retained until that deadline or its own expiry, whichever comes first. Family revocation is also recorded in the separate RefreshFamilyRevocation table, whose tombstones are never swept.

Summary

Functions

Administratively binds existing families to an operator-asserted single issuer.

Strong primary read of a refresh record.

Persists a new unconsumed refresh token.

Revokes every family that has a token whose grant matches criteria (:subject, :client_id and/or :sid, ANDed; see Attesto.RevocationCriteria) and returns {:ok, count} with the number of families revoked.

Serializes family revocation with inserts and rotations, durably records the tombstone, and removes every row in the family.

Atomically rotates parent_hash into child.

Run synchronous work under the rotation/revocation family lock. Nested calls use the caller's transaction. A rollback never returns a success.

Types

rotation_error()

@type rotation_error() ::
  :family_revoked
  | :retry_state_unavailable
  | :token_conflict
  | :family_integrity_error
  | :invalid_rotation
  | :expired

Functions

backfill_issuer(issuer, opts \\ [])

@spec backfill_issuer(String.t(), keyword()) :: {:ok, map()} | {:error, atom(), map()}

Administratively binds existing families to an operator-asserted single issuer.

Requires assert_single_issuer: true. The default dry_run: true audits without changing rows; dry_run: false applies the binding. Each family is serialized with rotation and updated atomically, including authenticated retry bundles. Existing different bindings fail closed and are never replaced. Expiry, generation, revocation, and client-instance bindings are unchanged. Reads family identifiers in bounded batches (batch_size: 100, maximum 1,000). A failed family leaves that family untouched; previously committed families remain safely bound, so the operation can be repeated after resolving errors.

The configured repo and schema prefix identify the complete trusted store whose provenance the operator asserts. This API does not establish historical client-instance keys for pre-upgrade attested families.

get(token_hash)

Strong primary read of a refresh record.

Revoked families are intentionally hidden from the protocol layer. The query is always executed through the configured primary repo and carries the configured Ecto prefix explicitly.

insert(record)

@spec insert(Attesto.RefreshStore.entry()) ::
  :ok | {:error, :family_revoked | :conflict}

Persists a new unconsumed refresh token.

Family revocation and (family_id, generation)/token uniqueness are checked while holding the same family lock used by rotation and revocation.

revoke_families(criteria)

@spec revoke_families(Attesto.RevocationCriteria.criteria()) ::
  {:ok, non_neg_integer()}

Revokes every family that has a token whose grant matches criteria (:subject, :client_id and/or :sid, ANDed; see Attesto.RevocationCriteria) and returns {:ok, count} with the number of families revoked.

The matching family ids are read first, then each is revoked through revoke_family/1: locked, tombstoned, and its rows removed. Called inside a caller's transaction, all of it commits or rolls back with that transaction. The session identifier is matched inside the claims JSONB column under the criteria's :sid_claim key, not a dedicated column, so it needs no migration.

Access tokens already issued from those families are not affected; a self-contained JWT access token stays valid until it expires unless the protected resource consults introspection or the revocation denylist.

revoke_family(family_id)

@spec revoke_family(Attesto.RefreshStore.family_id()) :: :ok

Serializes family revocation with inserts and rotations, durably records the tombstone, and removes every row in the family.

rotate(parent_hash, child, successor, opts \\ [])

Atomically rotates parent_hash into child.

The successor is protected before any database work. A positive successor requires the stable refresh-successor secret; failure to protect it returns :retry_state_unavailable without touching the database. Strict mode uses an exact non-secret tombstone and remains usable without that secret.

with_family_lock(family_id, work)

@spec with_family_lock(String.t(), (-> term())) :: {:ok, term()} | {:error, term()}

Run synchronous work under the rotation/revocation family lock. Nested calls use the caller's transaction. A rollback never returns a success.