auth/resolved_auth.ts

The account-grain credential a request resolved to, and the re-check a long-lived connection runs on it once it is registered.

The auth middleware chain resolves at most one credential per request — daemon token, then session cookie, then bearer, the first that authenticates winning — and leaves it on the Hono context as one value, ResolvedAuth (RESOLVED_AUTH_KEY, read with get_resolved_auth). Every reader takes that value, so the connections that outlive their request — a WebSocket, an SSE stream — carry it past the handshake, re-read it with revalidate_resolved_auth (or the whole admission re-check, recheck_admission), and close at its credential_deadline.

view source

Declarations
#

12 declarations

AdmissionRecheck
#

auth/resolved_auth.ts view source

AdmissionRecheck

type "admitted" | "credential_revoked" | "actor_inactive" | "role_revoked"

import type {AdmissionRecheck} from '@fuzdev/fuz_app/auth/resolved_auth.js';

What recheck_admission found: 'admitted', or the first check that refused — the credential, the acting actor, or the endpoint's role.

get_resolved_auth
#

auth/resolved_auth.ts view source

(c: Context<any, any, {}>): ResolvedAuth | null import {get_resolved_auth} from '@fuzdev/fuz_app/auth/resolved_auth.js';

The credential the auth middleware resolved for this request.

c

the request context, after the auth middleware chain

type Context<any, any, {}>

returns

ResolvedAuth | null

the resolved credential, or null for an unauthenticated request

recheck_admission
#

auth/resolved_auth.ts view source

(deps: QueryDeps, input: RecheckAdmissionInput): Promise<AdmissionRecheck> import {recheck_admission} from '@fuzdev/fuz_app/auth/resolved_auth.js';

Re-read everything a long-lived connection's gates read, once the connection is registered pending — the admission step a WebSocket upgrade and the audit stream share (twin of the Rust spine's admission re-check).

In order, stopping at the first refusal:

  1. The credential (revalidate_resolved_auth) → 'credential_revoked'.
  2. The acting actor the gates resolved, when there is one: it must still be an active actor of the account → 'actor_inactive'. A soft-deleted actor keeps its account's credentials and its own grant rows, so neither other re-read would refuse it.
  3. The role, when required_roles is non-empty: the acting actor's grants read afresh (refresh_role_grants), a global grant of any listed role admitting → 'role_revoked'. A scoped grant never admits at a role gate.

Every read must see what committed before it started, so deps.db is the pool-level Db (see revalidate_resolved_auth).

deps

query dependencies; the pool-level Db

input

the credential, the gates' request context, and the required roles

returns

Promise<AdmissionRecheck>

'admitted', or which check refused

throws

  • Error - propagated from the database. The caller must fail closed — an unchecked connection is not an admitted one.

RecheckAdmissionInput
#

auth/resolved_auth.ts view source

RecheckAdmissionInput import type {RecheckAdmissionInput} from '@fuzdev/fuz_app/auth/resolved_auth.js';

The connection being admitted, for recheck_admission.

resolved

The credential the request authenticated with (get_resolved_auth).

type ResolvedAuth

request_context

The request context the upgrade's gates ran against — its acting actor is re-read.

type RequestContext

required_roles

The roles the connection's gate required, any-of; [] for an ungated connection. A global grant of any of them admits, as the gate did.

type readonly string[]

resolved_auth_api_token_id
#

auth/resolved_auth.ts view source

(auth: ResolvedAuth | null): string | null import {resolved_auth_api_token_id} from '@fuzdev/fuz_app/auth/resolved_auth.js';

The token id of an API token credential, null for any other (or none).

auth

the resolved credential, or null for an anonymous caller

type ResolvedAuth | null

returns

string | null

the api_token.id, or null

resolved_auth_scope
#

auth/resolved_auth.ts view source

(auth: ResolvedAuth | null): { version: 1; kind: "full"; grandfathered?: boolean | undefined; } | { version: 1; kind: "methods"; methods: string[]; grandfathered?: boolean | undefined; } | null import {resolved_auth_scope} from '@fuzdev/fuz_app/auth/resolved_auth.js';

The credential's authority narrowing: an API token's own scope, full for a session or the daemon token (a browser session *is* full account authority — narrowing it is what role grants are for — and the daemon token is singular, filesystem-proved, and keeper-bound). null only for the anonymous caller, who holds no credential to narrow. Every token-scope gate reads this. The twin of the Rust spine's ResolvedAuth::scope.

auth

the resolved credential, or null for an anonymous caller

type ResolvedAuth | null

returns

TokenScope | null

the scope, or null for an anonymous caller

resolved_auth_session_hash
#

auth/resolved_auth.ts view source

(auth: ResolvedAuth | null): string | null import {resolved_auth_session_hash} from '@fuzdev/fuz_app/auth/resolved_auth.js';

The session hash of a session credential, null for any other (or none).

auth

the resolved credential, or null for an anonymous caller

type ResolvedAuth | null

returns

string | null

the auth_session.id, or null

ResolvedApiTokenAuth
#

auth/resolved_auth.ts view source

ResolvedApiTokenAuth import type {ResolvedApiTokenAuth} from '@fuzdev/fuz_app/auth/resolved_auth.js';

An Authorization: Bearer API token — the only credential a scope narrows.

inheritance

extends: ResolvedAuthBase

credential_type

type "api_token"

readonly

api_token_id

The api_token.id.

type string

readonly

scope

The authority the token was minted with, read off its row.

type TokenScope

readonly

ResolvedAuth
#

auth/resolved_auth.ts view source

ResolvedAuth

type ResolvedSessionAuth | ResolvedApiTokenAuth | ResolvedDaemonTokenAuth

import type {ResolvedAuth} from '@fuzdev/fuz_app/auth/resolved_auth.js';

An authenticated request's credential, account-grain — no actor.

One arm per credential type, each carrying only what belongs to it: the session hash on a session, the token id and scope on an API token, so a session with a narrowed scope or a bearer with a session hash cannot be built. The twin of the Rust spine's ResolvedAuth, whose ResolvedCredential enum holds the same correlation.

ResolvedDaemonTokenAuth
#

auth/resolved_auth.ts view source

ResolvedDaemonTokenAuth import type {ResolvedDaemonTokenAuth} from '@fuzdev/fuz_app/auth/resolved_auth.js';

The keeper-bound daemon token — full authority, no row, no expiry.

inheritance

extends: ResolvedAuthBase

credential_type

type "daemon_token"

readonly

credential_deadline

type null

readonly

ResolvedSessionAuth
#

auth/resolved_auth.ts view source

ResolvedSessionAuth import type {ResolvedSessionAuth} from '@fuzdev/fuz_app/auth/resolved_auth.js';

A session cookie — full account authority by construction.

inheritance

extends: ResolvedAuthBase

credential_type

type "session"

readonly

session_hash

Blake3 hash of the session token — the auth_session.id.

type string

readonly

revalidate_resolved_auth
#

auth/resolved_auth.ts view source

(deps: QueryDeps, resolved: ResolvedAuth): Promise<boolean> import {revalidate_resolved_auth} from '@fuzdev/fuz_app/auth/resolved_auth.js';

Re-check that a resolved credential still authenticates: true when it does, false when it has been revoked, has expired, or its account is gone.

For a connection that outlives its request — a WebSocket, an SSE stream — and is authorized once, at open. Such a connection is closed on revocation by identity, but only once it is *registered*; between the credential read and the registration a revocation closes nothing. So the opener registers first (un-admitted — ConnectionRegistry.register_pending / SseRegistry.subscribe_pending), then calls this, then admits. A revocation that committed before this read is seen by it; one whose close ran after the registration found the pending entry.

That needs each statement here to see everything committed before it started, so deps.db must be the pool-level Db (autocommit) or a READ COMMITTED transaction — never a REPEATABLE READ / SERIALIZABLE transaction opened before the registration, whose snapshot would predate it.

What is re-read, per credential — each through the same predicate the resolve path used, so the two cannot disagree about what "valid" means (the session and account legs call the middleware's own queries; the token leg shares its WHERE fragment):

  • Session — the auth_session row still exists, is unexpired, and belongs to the resolved account.
  • API token — the api_token row still exists, is unexpired, and belongs to the resolved account (by id; the raw token is not kept).
  • Daemon token — nothing of its own. The token has no row, and no revocation event: rotation closes no connection, so there is no close an opening connection could have missed. Its keeper account is still re-read.
  • Every credential — the account row still exists and is not soft-deleted.

It re-checks the credential, not what a revocation chose to close: a logout closes every connection of the account but deletes only its own session, so another session's connection opening concurrently passes this check and is refused only if that account-wide close found its pending registration — the same two outcomes an already-open connection has.

No side effects. Unlike the bearer middleware this never touches api_token.last_used_at / last_used_ip — re-checking a credential is not using it — and it writes nothing else.

The twin of the Rust spine's revalidate_resolved_auth.

deps

query dependencies; the pool-level Db, not a snapshot transaction

resolved

the credential the request authenticated with

returns

Promise<boolean>

whether the credential still authenticates

throws

  • Error - propagated from the database. The caller must fail closed — an unchecked credential is not a valid one.

Depends on
#

Imported by
#