@ultimat3/auth 31.3.0 → 31.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CLAUDE.md CHANGED
@@ -164,12 +164,13 @@ A rule marked `†` has its full text in [`docs/history/auth-rules.md`](../../do
164
164
  | `policy-bridge.ts` | the one funnel: identity → `Actor`, all four `ActorKind`s |
165
165
  | `session.ts` | two expiries, rotation, revocation, device list, the cookie |
166
166
  | `adapter.ts` | the seam; `builtin-adapter.ts` (Postgres) + `memory-adapter.ts` |
167
+ | `tables.ts` (`./schema`) | every auth table the boot applies — `AUTH_TABLES`, the limiter and TOTP DDL, and `authorization-server-schema.ts` re-exported. A LEAF: it imports nothing else (`schema-subpath.test.ts`), so `FRAMEWORK_SCHEMA` keeps the barrel off `ROLE=migrate` (`MIGRATE_CEILING`). The per-table constants stay module-private (removed from the API in 22.0.0). Also on `.` |
167
168
  | `adapter-subpath.ts` | `@ultimat3/auth/adapter`: the `authLimiter`/`totpReplay`/`authServerStore` port types, their installers and resets, and `installKdfGate` |
168
169
  | `authorization-server-installed.ts` | the `authServerStore` slot: `authorizationServer()` with no `store` reads it per CALL (never captured: `runtime.ts` may build the server first); empty, Postgres over the ambient pool |
169
170
  | `builtin-adapter-violation.ts` | a Postgres unique violation on an auth table → `authUniqueViolation` |
170
171
  | `rate-limit.ts` | per-ip, per-account and per-org buckets as reservations, lockout, scope check, `loginFailed()` |
171
172
  | `login-attempt.ts` | one attempt's reservations across the three buckets, taken before the check and settled after it |
172
- | `rate-limit-postgres.ts` | the SHARED limiter: one table, one row per key, a one-statement take, over a structural `PgExecutor` |
173
+ | `rate-limit-postgres.ts` | the SHARED limiter: one row per key, a one-statement take, over a structural `PgExecutor`. Its DDL is in `tables.ts` |
173
174
  | `limiter-install.ts` | the host's one install point for that limiter — the factory, what it built, and the purge over it, and the TOTP guard's |
174
175
  | `oauth.ts` | `OAuthProvider`, PKCE, `beginOAuth`, the callback gate. No I/O, no env |
175
176
  | `oauth-builtins.ts` | the two shipped IdPs (`github`, `google`), as data. Imports only the type, so no cycle |
@@ -180,7 +181,7 @@ A rule marked `†` has its full text in [`docs/history/auth-rules.md`](../../do
180
181
  | `jwt-bearer-resolver.ts` | an external bearer JWT → `{ actor, scopes } \| null` |
181
182
  | `revocation.ts` | per-user, per-org and before-an-instant sweeps; `disableUser` (sessions and api keys) |
182
183
  | `mfa-challenge.ts` | the sealed challenge `X_MFA_REQUIRED` carries, and `completeMfa` |
183
- | `mfa-replay-postgres.ts` | the shared replay guard: one table, the claim, the sweep |
184
+ | `mfa-replay-postgres.ts` | the shared replay guard: the claim and the sweep. Its DDL is in `tables.ts` |
184
185
  | `mfa-secret.ts` | the TOTP secret at rest: seal on write, open on read, the one-shot that seals older rows |
185
186
  | `directory.ts` | `describeUser` (allow-list projection), `listOrgUsers`, external-id lookup |
186
187
  | `privileges.ts` | `updatePrivileges` — the grant, the rotation it requires, and the sessions a changed password ends |
package/README.md CHANGED
@@ -740,6 +740,17 @@ brought up to date with no hand-run migration. There is nothing to paste: the pe
740
740
  constants and `X_USERS_MIGRATION_1_3` are gone, and a hand-written migration that pasted them
741
741
  should be deleted. They are still not `entity()` declarations, so `x db gen` does not see them.
742
742
 
743
+ **Read the DDL from `@ultimat3/auth/schema`, a leaf — `As of 2026-11-10`.** It exports
744
+ `AUTH_TABLES`, `AUTH_TABLE_NAMES`, `AUTH_SERVER_TABLES`, `SQL_AUTH_SERVER_TABLES`,
745
+ `SQL_AUTH_LIMIT_TABLES` and `SQL_TOTP_REPLAY_TABLE`, and imports no runtime: reading table names
746
+ costs 2 modules, not the barrel's 106. `FRAMEWORK_SCHEMA` reads it there, which took `ROLE=migrate`
747
+ from 977 modules to 873. The `.` barrel still exports the same six values, so a caller that also
748
+ needs `postgresAuthAdapter` or a store keeps one import.
749
+
750
+ ```ts
751
+ import { AUTH_TABLE_NAMES, AUTH_TABLES } from '@ultimat3/auth/schema';
752
+ ```
753
+
743
754
  ## Sessions are not a write path
744
755
 
745
756
  `verifySession` used to `UPDATE x_sessions … RETURNING *` on **every** authenticated request: one
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/auth",
3
- "version": "31.3.0",
3
+ "version": "31.4.1",
4
4
  "description": "Sessions, passwords, OAuth, MFA and api keys — resolved to one Actor",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -31,6 +31,7 @@
31
31
  "./adapter": "./src/adapter-subpath.ts",
32
32
  "./client": "./src/client.ts",
33
33
  "./saml-test-idp": "./src/saml-test-idp.ts",
34
+ "./schema": "./src/tables.ts",
34
35
  "./soft-authenticator": "./src/soft-authenticator.ts"
35
36
  },
36
37
  "files": [
@@ -49,8 +50,8 @@
49
50
  "test": "bun test"
50
51
  },
51
52
  "dependencies": {
52
- "@ultimat3/core": "31.3.0",
53
- "@ultimat3/db": "31.3.0",
54
- "@ultimat3/schema": "31.3.0"
53
+ "@ultimat3/core": "31.4.1",
54
+ "@ultimat3/db": "31.4.1",
55
+ "@ultimat3/schema": "31.4.1"
55
56
  }
56
57
  }
package/src/index.ts CHANGED
@@ -75,7 +75,6 @@ export type {
75
75
  export type { PostgresAuthServerStoreOptions } from './authorization-server-postgres';
76
76
  export { postgresAuthServerStore } from './authorization-server-postgres';
77
77
  export type { ReadRequestResult } from './authorization-server-request';
78
- export { AUTH_SERVER_TABLES, SQL_AUTH_SERVER_TABLES } from './authorization-server-schema';
79
78
  export type { AuthServerStore } from './authorization-server-store';
80
79
  export { memoryAuthServerStore } from './authorization-server-store';
81
80
  export type {
@@ -212,7 +211,7 @@ export type {
212
211
  PostgresTotpReplayGuard,
213
212
  PostgresTotpReplayGuardOptions,
214
213
  } from './mfa-replay-postgres';
215
- export { postgresTotpReplayGuard, SQL_TOTP_REPLAY_TABLE } from './mfa-replay-postgres';
214
+ export { postgresTotpReplayGuard } from './mfa-replay-postgres';
216
215
  export type { MfaSecretStore, SealMfaSecretsReport } from './mfa-secret';
217
216
  export {
218
217
  countUnsealedMfaSecrets,
@@ -369,10 +368,7 @@ export {
369
368
  orgRateLimit,
370
369
  } from './rate-limit';
371
370
  export type { PostgresAuthLimiter, PostgresAuthLimiterOptions } from './rate-limit-postgres';
372
- export {
373
- postgresAuthLimiter,
374
- SQL_AUTH_LIMIT_TABLES,
375
- } from './rate-limit-postgres';
371
+ export { postgresAuthLimiter } from './rate-limit-postgres';
376
372
  export type { DisabledUser } from './revocation';
377
373
  export {
378
374
  disableUser,
@@ -419,9 +415,14 @@ export type {
419
415
  } from './subject-target';
420
416
  export { authSubjectTarget } from './subject-target';
421
417
 
418
+ /** Also `@ultimat3/auth/schema`, the leaf a schema reader imports instead of this barrel. */
422
419
  export {
420
+ AUTH_SERVER_TABLES,
423
421
  AUTH_TABLE_NAMES,
424
422
  AUTH_TABLES,
423
+ SQL_AUTH_LIMIT_TABLES,
424
+ SQL_AUTH_SERVER_TABLES,
425
+ SQL_TOTP_REPLAY_TABLE,
425
426
  } from './tables';
426
427
  export {
427
428
  randomToken,
@@ -4,18 +4,6 @@
4
4
  import type { PgExecutor } from '@ultimat3/core';
5
5
  import { TOTP_DRIFT_STEPS, type TotpReplayGuard, totpStep } from './mfa';
6
6
 
7
- /** Applied by the boot, never by an app migration: `SQL_AUTH_LIMIT_TABLES`' rule. */
8
- export const SQL_TOTP_REPLAY_TABLE = `
9
- create table if not exists x_totp_steps (
10
- subject text not null,
11
- step bigint not null,
12
- used_at timestamptz not null,
13
- primary key (subject, step)
14
- );
15
-
16
- create index if not exists x_totp_steps_step_idx on x_totp_steps (step);
17
- `;
18
-
19
7
  /** `$1` subject, `$2` step, `$3` the caller's clock. A row back is the first use; none, a replay. */
20
8
  export const SQL_TOTP_CLAIM = `
21
9
  insert into x_totp_steps (subject, step, used_at)
@@ -6,35 +6,6 @@ import type { Clock, PgExecutor } from '@ultimat3/core';
6
6
  import { accountLocked, authWriteFailed } from './errors';
7
7
  import type { AuthLimiter, AuthRateLimitPolicy, AuthReservation } from './rate-limit';
8
8
 
9
- /**
10
- * Applied by the boot, never by an app migration — the rule `SQL_IDEMPOTENCY_TABLE` follows, so
11
- * `x dev`, the container's `web` role and `ROLE=migrate` all install it.
12
- *
13
- * ONE row per key, holding every attempt still inside the window. The window this package enforces
14
- * is a SLIDING one, so the instants are kept (a counter plus a window end is a fixed window, which
15
- * admits `maxAttempts` at the end of one and `maxAttempts` again at the start of the next). They
16
- * are kept IN the row because the row is what a concurrent take waits on: a count over a second
17
- * table reads its statement's snapshot, and that snapshot predates the wait.
18
- *
19
- * The two `add column` lines are the upgrade of a table made before the reservation, on the rule
20
- * `x_users` follows: additive, defaulted, idempotent, applied at every boot. The name and the
21
- * first two columns are unchanged on purpose — a replica still running the previous release reads
22
- * and writes this table as it always did, so a lockout holds on both sides of a rolling deploy.
23
- */
24
- export const SQL_AUTH_LIMIT_TABLES = `
25
- create table if not exists x_auth_lockouts (
26
- key text primary key,
27
- locked_until_ms bigint not null,
28
- attempts_ms bigint[] not null default '{}',
29
- admitted boolean not null default true
30
- );
31
-
32
- alter table x_auth_lockouts add column if not exists attempts_ms bigint[] not null default '{}';
33
- alter table x_auth_lockouts add column if not exists admitted boolean not null default true;
34
-
35
- create index if not exists x_auth_lockouts_until_idx on x_auth_lockouts (locked_until_ms);
36
- `;
37
-
38
9
  /**
39
10
  * The attempts still inside the window, read off the row as it is AFTER its lock is taken. It is
40
11
  * repeated in the statement below rather than computed once in a CTE, and the repetition is
package/src/tables.ts CHANGED
@@ -1,7 +1,11 @@
1
- // Single responsibility: the DDL `PostgresAuthAdapter` expects, as `AUTH_TABLES` — applied at every boot
2
- // by `@ultimat3/cli`'s `FRAMEWORK_SCHEMA`, upgrades of an older table included. Plain strings, so
3
- // what auth stores is verifiable by reading, never by trusting. There is nothing to paste into a
4
- // migration any more: the per-table exports and `X_USERS_MIGRATION_1_3` are gone (22.0.0).
1
+ // Single responsibility: every auth table the boot applies, and nothing else — shipped as the LEAF
2
+ // `@ultimat3/auth/schema`, so `@ultimat3/cli`'s `FRAMEWORK_SCHEMA` reads the DDL in every role
3
+ // (`ROLE=migrate` first) without the barrel. Imports only its sibling leaf; `schema-subpath.test.ts`
4
+ // refuses anything more. Plain strings: what auth stores is verifiable by reading, never by trusting.
5
+ //
6
+ // `AUTH_TABLES` is the DDL `PostgresAuthAdapter` expects, upgrades of an older table included.
7
+ // There is nothing to paste into a migration: the per-table exports and `X_USERS_MIGRATION_1_3`
8
+ // are gone (22.0.0), and the per-table constants below stay module-private for that reason.
5
9
  //
6
10
  // What is at rest, stated column by column rather than as a claim:
7
11
  //
@@ -25,7 +29,7 @@
25
29
  // through its own `linkAccount` has somewhere to put them, and so an existing deployment needs
26
30
  // no migration to stop.
27
31
 
28
- export const X_USERS_TABLE = `create table if not exists x_users (
32
+ const X_USERS_TABLE = `create table if not exists x_users (
29
33
  id uuid primary key,
30
34
  email text not null unique,
31
35
  email_verified_at timestamptz,
@@ -58,7 +62,7 @@ const X_USERS_UPGRADE = [
58
62
  ].join(';\n');
59
63
 
60
64
  // `id` is the public half of the cookie; `token_hash` is sha256 of the secret half.
61
- export const X_SESSIONS_TABLE = `create table if not exists x_sessions (
65
+ const X_SESSIONS_TABLE = `create table if not exists x_sessions (
62
66
  id text primary key,
63
67
  user_id uuid not null references x_users (id) on delete cascade,
64
68
  token_hash text not null,
@@ -73,7 +77,7 @@ create index if not exists x_sessions_user_id_idx on x_sessions (user_id);
73
77
  create index if not exists x_sessions_absolute_expires_at_idx on x_sessions (absolute_expires_at);
74
78
  create index if not exists x_sessions_created_at_idx on x_sessions (created_at)`;
75
79
 
76
- export const X_ACCOUNTS_TABLE = `create table if not exists x_accounts (
80
+ const X_ACCOUNTS_TABLE = `create table if not exists x_accounts (
77
81
  id uuid primary key,
78
82
  user_id uuid not null references x_users (id) on delete cascade,
79
83
  provider text not null,
@@ -88,7 +92,7 @@ export const X_ACCOUNTS_TABLE = `create table if not exists x_accounts (
88
92
  )`;
89
93
 
90
94
  // One live token per (purpose, identifier): issuing a new one overwrites the old.
91
- export const X_VERIFICATIONS_TABLE = `create table if not exists x_verifications (
95
+ const X_VERIFICATIONS_TABLE = `create table if not exists x_verifications (
92
96
  id text primary key,
93
97
  purpose text not null,
94
98
  identifier text not null,
@@ -99,7 +103,7 @@ export const X_VERIFICATIONS_TABLE = `create table if not exists x_verifications
99
103
  unique (purpose, identifier)
100
104
  )`;
101
105
 
102
- export const X_API_KEYS_TABLE = `create table if not exists x_api_keys (
106
+ const X_API_KEYS_TABLE = `create table if not exists x_api_keys (
103
107
  id text primary key,
104
108
  prefix text not null unique,
105
109
  key_hash text not null,
@@ -114,7 +118,7 @@ export const X_API_KEYS_TABLE = `create table if not exists x_api_keys (
114
118
 
115
119
  // Passkeys (`passkey-*.ts`). Byte fields are base64url text; the challenge row holds the
116
120
  // challenge's sha256, never the challenge; evidence carries its own key so it verifies alone.
117
- export const X_PASSKEYS_TABLE = `create table if not exists x_passkeys (
121
+ const X_PASSKEYS_TABLE = `create table if not exists x_passkeys (
118
122
  id text primary key,
119
123
  user_id uuid not null references x_users (id) on delete cascade,
120
124
  public_key text not null,
@@ -126,7 +130,7 @@ export const X_PASSKEYS_TABLE = `create table if not exists x_passkeys (
126
130
  );
127
131
  create index if not exists x_passkeys_user_id_idx on x_passkeys (user_id)`;
128
132
 
129
- export const X_PASSKEY_CHALLENGES_TABLE = `create table if not exists x_passkey_challenges (
133
+ const X_PASSKEY_CHALLENGES_TABLE = `create table if not exists x_passkey_challenges (
130
134
  id text primary key,
131
135
  ceremony text not null,
132
136
  user_id uuid references x_users (id) on delete cascade,
@@ -137,7 +141,7 @@ export const X_PASSKEY_CHALLENGES_TABLE = `create table if not exists x_passkey_
137
141
  );
138
142
  create index if not exists x_passkey_challenges_expires_at_idx on x_passkey_challenges (expires_at)`;
139
143
 
140
- export const X_PASSKEY_EVIDENCE_TABLE = `create table if not exists x_passkey_evidence (
144
+ const X_PASSKEY_EVIDENCE_TABLE = `create table if not exists x_passkey_evidence (
141
145
  id text primary key,
142
146
  user_id uuid not null references x_users (id) on delete cascade,
143
147
  credential_id text not null,
@@ -175,3 +179,49 @@ export const AUTH_TABLE_NAMES: readonly string[] = Object.freeze([
175
179
  'x_passkey_challenges',
176
180
  'x_passkey_evidence',
177
181
  ]);
182
+
183
+ /**
184
+ * `postgresAuthLimiter`'s table (`rate-limit-postgres.ts`).
185
+ *
186
+ * Applied by the boot, never by an app migration — the rule `SQL_IDEMPOTENCY_TABLE` follows, so
187
+ * `x dev`, the container's `web` role and `ROLE=migrate` all install it.
188
+ *
189
+ * ONE row per key, holding every attempt still inside the window. The window this package enforces
190
+ * is a SLIDING one, so the instants are kept (a counter plus a window end is a fixed window, which
191
+ * admits `maxAttempts` at the end of one and `maxAttempts` again at the start of the next). They
192
+ * are kept IN the row because the row is what a concurrent take waits on: a count over a second
193
+ * table reads its statement's snapshot, and that snapshot predates the wait.
194
+ *
195
+ * The two `add column` lines are the upgrade of a table made before the reservation, on the rule
196
+ * `x_users` follows: additive, defaulted, idempotent, applied at every boot. The name and the
197
+ * first two columns are unchanged on purpose — a replica still running the previous release reads
198
+ * and writes this table as it always did, so a lockout holds on both sides of a rolling deploy.
199
+ */
200
+ export const SQL_AUTH_LIMIT_TABLES = `
201
+ create table if not exists x_auth_lockouts (
202
+ key text primary key,
203
+ locked_until_ms bigint not null,
204
+ attempts_ms bigint[] not null default '{}',
205
+ admitted boolean not null default true
206
+ );
207
+
208
+ alter table x_auth_lockouts add column if not exists attempts_ms bigint[] not null default '{}';
209
+ alter table x_auth_lockouts add column if not exists admitted boolean not null default true;
210
+
211
+ create index if not exists x_auth_lockouts_until_idx on x_auth_lockouts (locked_until_ms);
212
+ `;
213
+
214
+ /** `postgresTotpReplayGuard`'s spent steps (`mfa-replay-postgres.ts`). Applied by the boot, likewise. */
215
+ export const SQL_TOTP_REPLAY_TABLE = `
216
+ create table if not exists x_totp_steps (
217
+ subject text not null,
218
+ step bigint not null,
219
+ used_at timestamptz not null,
220
+ primary key (subject, step)
221
+ );
222
+
223
+ create index if not exists x_totp_steps_step_idx on x_totp_steps (step);
224
+ `;
225
+
226
+ /** The authorization server's five tables: their own leaf, re-exported so `./schema` is one import. */
227
+ export { AUTH_SERVER_TABLES, SQL_AUTH_SERVER_TABLES } from './authorization-server-schema';