@pithy-sh/auth 0.8.0 → 0.10.0
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/README.md +12 -2
- package/dist/audit/actions.d.ts +21 -0
- package/dist/audit/actions.d.ts.map +1 -1
- package/dist/audit/actions.js +22 -1
- package/dist/audit/emit.js +1 -1
- package/dist/caller/certificate.d.ts +83 -0
- package/dist/caller/certificate.d.ts.map +1 -0
- package/dist/caller/certificate.js +92 -0
- package/dist/caller/request.d.ts +66 -0
- package/dist/caller/request.d.ts.map +1 -0
- package/dist/caller/request.js +71 -0
- package/dist/caller/token.d.ts +130 -0
- package/dist/caller/token.d.ts.map +1 -0
- package/dist/caller/token.js +207 -0
- package/dist/capability.d.ts +70 -2
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +28 -7
- package/dist/client/lifecycle.d.ts +19 -0
- package/dist/client/lifecycle.d.ts.map +1 -0
- package/dist/client/lifecycle.js +25 -0
- package/dist/client/projection.d.ts +4 -0
- package/dist/client/projection.d.ts.map +1 -1
- package/dist/data/betterAuth.d.ts +5 -1
- package/dist/data/betterAuth.d.ts.map +1 -1
- package/dist/data/betterAuth.js +7 -3
- package/dist/data/kitFields.d.ts +20 -25
- package/dist/data/kitFields.d.ts.map +1 -1
- package/dist/data/kitFields.js +28 -32
- package/dist/data/tables.d.ts +29 -3
- package/dist/data/tables.d.ts.map +1 -1
- package/dist/data/tables.js +5 -3
- package/dist/data/userLifecycle.d.ts +61 -0
- package/dist/data/userLifecycle.d.ts.map +1 -0
- package/dist/data/userLifecycle.js +50 -0
- package/dist/http/credentialResign.d.ts +46 -0
- package/dist/http/credentialResign.d.ts.map +1 -0
- package/dist/http/credentialResign.js +203 -0
- package/dist/http/errorCallbackUrl.d.ts.map +1 -1
- package/dist/http/errorCallbackUrl.js +15 -9
- package/dist/http/guards.d.ts +23 -1
- package/dist/http/guards.d.ts.map +1 -1
- package/dist/http/guards.js +28 -3
- package/dist/http/lifecycleGate.d.ts +18 -0
- package/dist/http/lifecycleGate.d.ts.map +1 -0
- package/dist/http/lifecycleGate.js +75 -0
- package/dist/http/middleware.d.ts +5 -0
- package/dist/http/middleware.d.ts.map +1 -1
- package/dist/http/middleware.js +113 -21
- package/dist/http/providerRefusal.d.ts +8 -7
- package/dist/http/providerRefusal.d.ts.map +1 -1
- package/dist/http/providerRefusal.js +37 -32
- package/dist/http/resolve.d.ts.map +1 -1
- package/dist/http/resolve.js +23 -5
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +20 -1
- package/dist/instance/auth.d.ts +1280 -5
- package/dist/instance/auth.d.ts.map +1 -1
- package/dist/instance/auth.js +59 -4
- package/dist/instance/jwksReencryption.d.ts +75 -0
- package/dist/instance/jwksReencryption.d.ts.map +1 -0
- package/dist/instance/jwksReencryption.js +160 -0
- package/dist/instance/microsoftEmail.d.ts +70 -0
- package/dist/instance/microsoftEmail.d.ts.map +1 -0
- package/dist/instance/microsoftEmail.js +96 -0
- package/dist/instance/providerSignInGate.d.ts +14 -12
- package/dist/instance/providerSignInGate.d.ts.map +1 -1
- package/dist/instance/providerSignInGate.js +23 -20
- package/dist/instance/providers.d.ts +7 -5
- package/dist/instance/providers.d.ts.map +1 -1
- package/dist/instance/providers.js +5 -3
- package/dist/instance/secrets.d.ts +232 -4
- package/dist/instance/secrets.d.ts.map +1 -1
- package/dist/instance/secrets.js +129 -7
- package/dist/instance/sessionSecrets.d.ts +45 -0
- package/dist/instance/sessionSecrets.d.ts.map +1 -0
- package/dist/instance/sessionSecrets.js +29 -0
- package/dist/lifecycle/enforce.d.ts +40 -0
- package/dist/lifecycle/enforce.d.ts.map +1 -0
- package/dist/lifecycle/enforce.js +70 -0
- package/dist/lifecycle/hooks.d.ts +53 -0
- package/dist/lifecycle/hooks.d.ts.map +1 -0
- package/dist/lifecycle/hooks.js +2 -0
- package/dist/lifecycle/request.d.ts +15 -0
- package/dist/lifecycle/request.d.ts.map +1 -0
- package/dist/lifecycle/request.js +27 -0
- package/dist/lifecycle/state.d.ts +64 -0
- package/dist/lifecycle/state.d.ts.map +1 -0
- package/dist/lifecycle/state.js +90 -0
- package/dist/lifecycle/transitions.d.ts +132 -0
- package/dist/lifecycle/transitions.d.ts.map +1 -0
- package/dist/lifecycle/transitions.js +349 -0
- package/dist/migrations/0001_init.d.ts.map +1 -1
- package/dist/migrations/0003_account_issuer_nullable.d.ts +5 -0
- package/dist/migrations/0003_account_issuer_nullable.d.ts.map +1 -0
- package/dist/migrations/0003_account_issuer_nullable.js +131 -0
- package/dist/migrations/0004_user_lifecycle.d.ts +28 -0
- package/dist/migrations/0004_user_lifecycle.d.ts.map +1 -0
- package/dist/migrations/0004_user_lifecycle.js +47 -0
- package/dist/migrations/set.d.ts +3 -1
- package/dist/migrations/set.d.ts.map +1 -1
- package/dist/migrations/set.js +6 -2
- package/dist/peer.d.ts +4 -0
- package/dist/peer.d.ts.map +1 -1
- package/dist/test-utils/liveApp.d.ts +26 -0
- package/dist/test-utils/liveApp.d.ts.map +1 -1
- package/dist/test-utils/liveApp.js +1 -0
- package/dist/test-utils/schemaDrift.d.ts +49 -0
- package/dist/test-utils/schemaDrift.d.ts.map +1 -0
- package/dist/test-utils/schemaDrift.js +35 -0
- package/dist/test-utils/scopedBackend.d.ts +204 -0
- package/dist/test-utils/scopedBackend.d.ts.map +1 -0
- package/dist/test-utils/scopedBackend.js +166 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.d.ts.map +1 -1
- package/dist/version.generated.js +1 -1
- package/docs/account-lifecycle.md +105 -0
- package/docs/apple-signin.md +7 -1
- package/docs/facebook-oauth.md +7 -1
- package/docs/github-oauth.md +7 -1
- package/docs/google-oauth.md +7 -1
- package/docs/microsoft-oauth.md +177 -0
- package/docs/server-authoritative.md +182 -0
- package/package.json +11 -8
- package/pithy.manifest.json +17 -3
- package/src/audit/actions.ts +23 -0
- package/src/audit/emit.ts +1 -1
- package/src/caller/certificate.ts +133 -0
- package/src/caller/request.ts +84 -0
- package/src/caller/token.ts +276 -0
- package/src/capability.ts +170 -38
- package/src/client/lifecycle.ts +27 -0
- package/src/client/projection.ts +4 -0
- package/src/data/betterAuth.ts +23 -2
- package/src/data/kitFields.ts +27 -0
- package/src/data/tables.ts +4 -2
- package/src/data/userLifecycle.ts +80 -0
- package/src/http/credentialResign.ts +268 -0
- package/src/http/errorCallbackUrl.ts +21 -13
- package/src/http/guards.ts +29 -1
- package/src/http/lifecycleGate.ts +78 -0
- package/src/http/middleware.ts +161 -37
- package/src/http/providerRefusal.ts +40 -32
- package/src/http/resolve.ts +34 -5
- package/src/http/routes.ts +39 -2
- package/src/instance/auth.ts +91 -6
- package/src/instance/jwksReencryption.ts +206 -0
- package/src/instance/microsoftEmail.ts +111 -0
- package/src/instance/providerSignInGate.ts +30 -27
- package/src/instance/providers.ts +8 -4
- package/src/instance/secrets.ts +153 -6
- package/src/instance/sessionSecrets.ts +64 -0
- package/src/lifecycle/enforce.ts +89 -0
- package/src/lifecycle/hooks.ts +54 -0
- package/src/lifecycle/request.ts +31 -0
- package/src/lifecycle/state.ts +101 -0
- package/src/lifecycle/transitions.ts +483 -0
- package/src/migrations/0001_init.ts +8 -10
- package/src/migrations/0003_account_issuer_nullable.ts +199 -0
- package/src/migrations/0004_user_lifecycle.ts +68 -0
- package/src/migrations/set.ts +5 -1
- package/src/test-utils/liveApp.ts +2 -1
- package/src/test-utils/schemaDrift.ts +82 -0
- package/src/test-utils/scopedBackend.ts +137 -0
- package/src/version.generated.ts +1 -1
package/dist/data/kitFields.js
CHANGED
|
@@ -1,32 +1,6 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import { Locale } from "@pithy-sh/core/src/i18n/locale";
|
|
4
|
-
//#region src/data/kitFields.ts
|
|
5
|
-
/**
|
|
6
|
-
* The columns Pithy adds to Better Auth's own tables, declared **once**.
|
|
7
|
-
*
|
|
8
|
-
* Better Auth owns every read and write to `pithy_auth_users` and `pithy_auth_sessions` through its
|
|
9
|
-
* Kysely adapter, and it writes only the fields its options declare. So a column that exists in the
|
|
10
|
-
* migration and in the `User` Zod object and *not* here is null forever and absent from every user
|
|
11
|
-
* object the instance returns — a migration and a schema field that silently did nothing.
|
|
12
|
-
*
|
|
13
|
-
* ## Why one module rather than two agreeing declarations
|
|
14
|
-
*
|
|
15
|
-
* These have to appear in two places: `makeAuth`'s live options (`../instance/auth.ts`) and the
|
|
16
|
-
* schema baseline `pluginSchemaDelta` subtracts each adopter plugin's schema from
|
|
17
|
-
* (`../migrations/pluginTables.ts`). Both were written out by hand, with a comment in each saying it
|
|
18
|
-
* "must match" the other and a test claiming to hold them together. The test did not: it compared the
|
|
19
|
-
* baseline against the `User` schema and never imported `makeAuth` at all, so reverting the live
|
|
20
|
-
* declaration left the whole suite green while Better Auth silently stopped writing the column (#441).
|
|
21
|
-
*
|
|
22
|
-
* `docs/CONVENTIONS.md` is explicit that removing an invariant beats watching it. There is one object
|
|
23
|
-
* now, imported by both, and the two cannot disagree because there is no second thing to disagree
|
|
24
|
-
* with. That is also why the consequence is worth restating rather than diluting: leave a kit column
|
|
25
|
-
* out of the baseline and `pluginSchemaDelta` reports it as something an adopter's plugin brought, so
|
|
26
|
-
* a plugin that also declares a user `locale` emits `ALTER TABLE … ADD COLUMN locale` against a table
|
|
27
|
-
* that already has one — a duplicate-column failure part-way through a migration D1 cannot roll back,
|
|
28
|
-
* because it has no transactional DDL.
|
|
29
|
-
*/
|
|
30
4
|
/**
|
|
31
5
|
* The extra `pithy_auth_users` columns.
|
|
32
6
|
*
|
|
@@ -50,12 +24,34 @@ import { Locale } from "@pithy-sh/core/src/i18n/locale";
|
|
|
50
24
|
* that accepted a tag but refused `null` would let a reader pick a language and never take it back —
|
|
51
25
|
* `updateUser({ locale: null })` answering 400 for the one state the schema calls ordinary.
|
|
52
26
|
*/
|
|
53
|
-
const KIT_USER_FIELDS = {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
27
|
+
const KIT_USER_FIELDS = {
|
|
28
|
+
locale: {
|
|
29
|
+
type: "string",
|
|
30
|
+
required: false,
|
|
31
|
+
input: true,
|
|
32
|
+
validator: { input: Locale.nullable() }
|
|
33
|
+
},
|
|
34
|
+
suspendedAt: {
|
|
35
|
+
type: "number",
|
|
36
|
+
required: false,
|
|
37
|
+
input: false
|
|
38
|
+
},
|
|
39
|
+
suspendedUntil: {
|
|
40
|
+
type: "number",
|
|
41
|
+
required: false,
|
|
42
|
+
input: false
|
|
43
|
+
},
|
|
44
|
+
deletedAt: {
|
|
45
|
+
type: "number",
|
|
46
|
+
required: false,
|
|
47
|
+
input: false
|
|
48
|
+
},
|
|
49
|
+
purgeAfter: {
|
|
50
|
+
type: "number",
|
|
51
|
+
required: false,
|
|
52
|
+
input: false
|
|
53
|
+
}
|
|
54
|
+
};
|
|
59
55
|
/**
|
|
60
56
|
* **`authenticatedAt` carries three decisions a future author must not undo.**
|
|
61
57
|
*
|
package/dist/data/tables.d.ts
CHANGED
|
@@ -8,8 +8,8 @@ import type { Kysely } from "kysely";
|
|
|
8
8
|
* `pithy_auth_*` SQL. One source of truth shared by the capability wiring, the Better-Auth instance
|
|
9
9
|
* (whose Kysely adapter wraps this same Kysely), the device registry, and the migration.
|
|
10
10
|
*
|
|
11
|
-
* The first six tables are Better-Auth-managed; `pithyAuthDevices
|
|
12
|
-
* ours. Setting Better Auth's `modelName` to these exact camelCase keys lets the one `CamelCasePlugin`
|
|
11
|
+
* The first six tables are Better-Auth-managed; `pithyAuthDevices`, `pithyAuthRotatedTokens` and
|
|
12
|
+
* `pithyAuthUserLifecycle` are ours. Setting Better Auth's `modelName` to these exact camelCase keys lets the one `CamelCasePlugin`
|
|
13
13
|
* own the camelCase ↔ snake_case boundary uniformly for both Better Auth's queries and our migration DDL.
|
|
14
14
|
*/
|
|
15
15
|
export declare const authTables: {
|
|
@@ -22,6 +22,10 @@ export declare const authTables: {
|
|
|
22
22
|
locale: import("zod").ZodNullable<import("zod").ZodString>;
|
|
23
23
|
createdAt: import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodDate]>, import("zod").ZodDate>;
|
|
24
24
|
updatedAt: import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodDate]>, import("zod").ZodDate>;
|
|
25
|
+
suspendedAt: import("zod").ZodOptional<import("zod").ZodNullable<import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>>>;
|
|
26
|
+
suspendedUntil: import("zod").ZodOptional<import("zod").ZodNullable<import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>>>;
|
|
27
|
+
deletedAt: import("zod").ZodOptional<import("zod").ZodNullable<import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>>>;
|
|
28
|
+
purgeAfter: import("zod").ZodOptional<import("zod").ZodNullable<import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>>>;
|
|
25
29
|
}, import("zod/v4/core").$strip>;
|
|
26
30
|
pithyAuthSessions: import("zod").ZodObject<{
|
|
27
31
|
id: import("zod").ZodString;
|
|
@@ -38,7 +42,7 @@ export declare const authTables: {
|
|
|
38
42
|
}, import("zod/v4/core").$strip>;
|
|
39
43
|
pithyAuthAccounts: import("zod").ZodObject<{
|
|
40
44
|
id: import("zod").ZodString;
|
|
41
|
-
issuer: import("zod").ZodString
|
|
45
|
+
issuer: import("zod").ZodNullable<import("zod").ZodString>;
|
|
42
46
|
accountId: import("zod").ZodString;
|
|
43
47
|
providerId: import("zod").ZodString;
|
|
44
48
|
userId: import("zod").ZodString;
|
|
@@ -98,6 +102,28 @@ export declare const authTables: {
|
|
|
98
102
|
userId: import("zod").ZodString;
|
|
99
103
|
rotatedAt: import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>;
|
|
100
104
|
}, import("zod/v4/core").$strip>;
|
|
105
|
+
pithyAuthUserLifecycle: import("zod").ZodObject<{
|
|
106
|
+
id: import("zod").ZodNumber;
|
|
107
|
+
userId: import("zod").ZodString;
|
|
108
|
+
action: import("zod").ZodEnum<{
|
|
109
|
+
deleted: "deleted";
|
|
110
|
+
purge_step: "purge_step";
|
|
111
|
+
purged: "purged";
|
|
112
|
+
reinstated: "reinstated";
|
|
113
|
+
restored: "restored";
|
|
114
|
+
suspended: "suspended";
|
|
115
|
+
}>;
|
|
116
|
+
actorType: import("zod").ZodEnum<{
|
|
117
|
+
"control-plane": "control-plane";
|
|
118
|
+
system: "system";
|
|
119
|
+
user: "user";
|
|
120
|
+
}>;
|
|
121
|
+
actorId: import("zod").ZodNullable<import("zod").ZodString>;
|
|
122
|
+
reason: import("zod").ZodNullable<import("zod").ZodString>;
|
|
123
|
+
suspendedUntil: import("zod").ZodNullable<import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>>;
|
|
124
|
+
step: import("zod").ZodNullable<import("zod").ZodString>;
|
|
125
|
+
createdAt: import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>;
|
|
126
|
+
}, import("zod/v4/core").$strip>;
|
|
101
127
|
};
|
|
102
128
|
export type AuthTables = typeof authTables;
|
|
103
129
|
/** The typed Kysely view over the auth tables — shared by the Better-Auth adapter and the device registry. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../../src/data/tables.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../../src/data/tables.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAMrC;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU;IACrB,cAAc;;;;;;;;;;;;;;IACd,iBAAiB;;;;;;;;;;;;;IACjB,iBAAiB;;;;;;;;;;;;;;;;IACjB,sBAAsB;;;;;;;;IACtB,aAAa;;;;;;;;;IACb,kBAAkB;;;;;;IAClB,gBAAgB;;;;;;;;;;;;;;;;;IAChB,sBAAsB;;;;;;IACtB,sBAAsB;;;;;;;;;;;;;;;;;;;;;;CACvB,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,OAAO,UAAU,CAAC;AAE3C,8GAA8G;AAC9G,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAAC;AAE9D,yGAAyG;AACzG,wBAAgB,YAAY,CAAC,EAAE,EAAE,UAAU,GAAG,YAAY,CAEzD"}
|
package/dist/data/tables.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { Account, Jwks, RateLimit, Session, User, Verification } from "./betterAuth.js";
|
|
4
4
|
import { Device } from "./device.js";
|
|
5
5
|
import { RotatedToken } from "./rotatedToken.js";
|
|
6
|
+
import { UserLifecycleEvent } from "./userLifecycle.js";
|
|
6
7
|
import { createDatabase } from "@pithy-sh/core/src/data/db";
|
|
7
8
|
//#region src/data/tables.ts
|
|
8
9
|
/**
|
|
@@ -10,8 +11,8 @@ import { createDatabase } from "@pithy-sh/core/src/data/db";
|
|
|
10
11
|
* `pithy_auth_*` SQL. One source of truth shared by the capability wiring, the Better-Auth instance
|
|
11
12
|
* (whose Kysely adapter wraps this same Kysely), the device registry, and the migration.
|
|
12
13
|
*
|
|
13
|
-
* The first six tables are Better-Auth-managed; `pithyAuthDevices
|
|
14
|
-
* ours. Setting Better Auth's `modelName` to these exact camelCase keys lets the one `CamelCasePlugin`
|
|
14
|
+
* The first six tables are Better-Auth-managed; `pithyAuthDevices`, `pithyAuthRotatedTokens` and
|
|
15
|
+
* `pithyAuthUserLifecycle` are ours. Setting Better Auth's `modelName` to these exact camelCase keys lets the one `CamelCasePlugin`
|
|
15
16
|
* own the camelCase ↔ snake_case boundary uniformly for both Better Auth's queries and our migration DDL.
|
|
16
17
|
*/
|
|
17
18
|
const authTables = {
|
|
@@ -22,7 +23,8 @@ const authTables = {
|
|
|
22
23
|
pithyAuthJwks: Jwks,
|
|
23
24
|
pithyAuthRateLimit: RateLimit,
|
|
24
25
|
pithyAuthDevices: Device,
|
|
25
|
-
pithyAuthRotatedTokens: RotatedToken
|
|
26
|
+
pithyAuthRotatedTokens: RotatedToken,
|
|
27
|
+
pithyAuthUserLifecycle: UserLifecycleEvent
|
|
26
28
|
};
|
|
27
29
|
/** Build the auth Kysely over a D1 binding. Carries `CamelCasePlugin`, so query code stays camelCase. */
|
|
28
30
|
function authDatabase(d1) {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/**
|
|
5
|
+
* `pithy_auth_user_lifecycle` — every transition an account went through, who made it, and why.
|
|
6
|
+
*
|
|
7
|
+
* **The columns on `pithy_auth_users` answer "what is true now"; this table answers "what happened, and who
|
|
8
|
+
* did it".** The two are written together by `../lifecycle/transitions.ts` and never apart. The columns are
|
|
9
|
+
* what enforcement reads on every request, so they hold only the current state; the reason an operator gave
|
|
10
|
+
* for a suspension, and the history of every reinstatement before it, live here.
|
|
11
|
+
*
|
|
12
|
+
* **No foreign key to the user, deliberately.** A purge erases the user row and every row hanging off it, and
|
|
13
|
+
* this table is the one record that must survive it: the trail that the account existed, was deleted, and
|
|
14
|
+
* was erased, by whom and when. A cascade would take the evidence with the subject. So the row names the user
|
|
15
|
+
* by id and holds nothing else about them — no address, no name — and is the only `pithy_auth_*` row a purge
|
|
16
|
+
* leaves behind.
|
|
17
|
+
*
|
|
18
|
+
* Written by the kit through its own Kysely, so its dates take the house ms-epoch `SQLiteDate`.
|
|
19
|
+
*/
|
|
20
|
+
/** What happened to the account. */
|
|
21
|
+
export declare const UserLifecycleAction: z.ZodEnum<{
|
|
22
|
+
deleted: "deleted";
|
|
23
|
+
purge_step: "purge_step";
|
|
24
|
+
purged: "purged";
|
|
25
|
+
reinstated: "reinstated";
|
|
26
|
+
restored: "restored";
|
|
27
|
+
suspended: "suspended";
|
|
28
|
+
}>;
|
|
29
|
+
export type UserLifecycleAction = z.output<typeof UserLifecycleAction>;
|
|
30
|
+
/** Who made the transition. */
|
|
31
|
+
export declare const UserLifecycleActorType: z.ZodEnum<{
|
|
32
|
+
"control-plane": "control-plane";
|
|
33
|
+
system: "system";
|
|
34
|
+
user: "user";
|
|
35
|
+
}>;
|
|
36
|
+
export type UserLifecycleActorType = z.output<typeof UserLifecycleActorType>;
|
|
37
|
+
/** One transition, as stored. */
|
|
38
|
+
export declare const UserLifecycleEvent: z.ZodObject<{
|
|
39
|
+
id: z.ZodNumber;
|
|
40
|
+
userId: z.ZodString;
|
|
41
|
+
action: z.ZodEnum<{
|
|
42
|
+
deleted: "deleted";
|
|
43
|
+
purge_step: "purge_step";
|
|
44
|
+
purged: "purged";
|
|
45
|
+
reinstated: "reinstated";
|
|
46
|
+
restored: "restored";
|
|
47
|
+
suspended: "suspended";
|
|
48
|
+
}>;
|
|
49
|
+
actorType: z.ZodEnum<{
|
|
50
|
+
"control-plane": "control-plane";
|
|
51
|
+
system: "system";
|
|
52
|
+
user: "user";
|
|
53
|
+
}>;
|
|
54
|
+
actorId: z.ZodNullable<z.ZodString>;
|
|
55
|
+
reason: z.ZodNullable<z.ZodString>;
|
|
56
|
+
suspendedUntil: z.ZodNullable<z.ZodCodec<z.ZodUnion<readonly [z.ZodNumber, z.ZodString, z.ZodDate]>, z.ZodDate>>;
|
|
57
|
+
step: z.ZodNullable<z.ZodString>;
|
|
58
|
+
createdAt: z.ZodCodec<z.ZodUnion<readonly [z.ZodNumber, z.ZodString, z.ZodDate]>, z.ZodDate>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
export type UserLifecycleEvent = z.output<typeof UserLifecycleEvent>;
|
|
61
|
+
//# sourceMappingURL=userLifecycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"userLifecycle.d.ts","sourceRoot":"","sources":["../../src/data/userLifecycle.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;;;;;;GAeG;AAEH,oCAAoC;AACpC,eAAO,MAAM,mBAAmB;;;;;;;EAI7B,CAAC;AACJ,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEvE,+BAA+B;AAC/B,eAAO,MAAM,sBAAsB;;;;EAIhC,CAAC;AACJ,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAE7E,iCAAiC;AACjC,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;iBAsC5B,CAAC;AACJ,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,kBAAkB,CAAC,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
|
|
5
|
+
//#region src/data/userLifecycle.ts
|
|
6
|
+
/**
|
|
7
|
+
* `pithy_auth_user_lifecycle` — every transition an account went through, who made it, and why.
|
|
8
|
+
*
|
|
9
|
+
* **The columns on `pithy_auth_users` answer "what is true now"; this table answers "what happened, and who
|
|
10
|
+
* did it".** The two are written together by `../lifecycle/transitions.ts` and never apart. The columns are
|
|
11
|
+
* what enforcement reads on every request, so they hold only the current state; the reason an operator gave
|
|
12
|
+
* for a suspension, and the history of every reinstatement before it, live here.
|
|
13
|
+
*
|
|
14
|
+
* **No foreign key to the user, deliberately.** A purge erases the user row and every row hanging off it, and
|
|
15
|
+
* this table is the one record that must survive it: the trail that the account existed, was deleted, and
|
|
16
|
+
* was erased, by whom and when. A cascade would take the evidence with the subject. So the row names the user
|
|
17
|
+
* by id and holds nothing else about them — no address, no name — and is the only `pithy_auth_*` row a purge
|
|
18
|
+
* leaves behind.
|
|
19
|
+
*
|
|
20
|
+
* Written by the kit through its own Kysely, so its dates take the house ms-epoch `SQLiteDate`.
|
|
21
|
+
*/
|
|
22
|
+
/** What happened to the account. */
|
|
23
|
+
const UserLifecycleAction = z.enum([
|
|
24
|
+
"suspended",
|
|
25
|
+
"reinstated",
|
|
26
|
+
"deleted",
|
|
27
|
+
"restored",
|
|
28
|
+
"purge_step",
|
|
29
|
+
"purged"
|
|
30
|
+
]).describe("The transition: `suspended` and `reinstated`; `deleted` (by an operator, or by the account's owner) and `restored` inside the purge window; `purge_step`, one capability's rows erased during a purge, named in `step`; and `purged`, the erasure finished.");
|
|
31
|
+
/** Who made the transition. */
|
|
32
|
+
const UserLifecycleActorType = z.enum([
|
|
33
|
+
"control-plane",
|
|
34
|
+
"user",
|
|
35
|
+
"system"
|
|
36
|
+
]).describe("Who made the transition: `control-plane` (a management client, under a scoped grant), `user` (the account's own holder, deleting their own account), or `system` (the scheduled purge, with nobody calling).");
|
|
37
|
+
/** One transition, as stored. */
|
|
38
|
+
const UserLifecycleEvent = z.object({
|
|
39
|
+
id: z.number().int().describe("Primary key. Auto-incrementing, so a user's history reads in the order it was written."),
|
|
40
|
+
userId: z.string().describe("The account this happened to. Not a foreign key: the row outlives the user it names, because a purge must leave its own record behind. Indexed."),
|
|
41
|
+
action: UserLifecycleAction.describe("What happened to the account."),
|
|
42
|
+
actorType: UserLifecycleActorType.describe("Who made the transition."),
|
|
43
|
+
actorId: z.string().nullable().describe("The acting principal's id — the user's own id for a self-service deletion, the connection's for a management client. Null for `system`, and for a management client that named none."),
|
|
44
|
+
reason: z.string().nullable().describe("Why, in the words of whoever made the transition. For a suspension this is the sentence the locked-out person is shown, so it is written for them. Null for a transition nobody had to justify — a purge, a self-service deletion."),
|
|
45
|
+
suspendedUntil: SQLiteDate.nullable().describe("For a `suspended` row, when that suspension ends by itself; null for one that lasts until a reinstatement, and for every other action. Ms-epoch in SQLite."),
|
|
46
|
+
step: z.string().nullable().describe("For a `purge_step` row, the capability whose rows that step erased — the purge's step name. Null for every other action."),
|
|
47
|
+
createdAt: SQLiteDate.describe("When the transition happened. Ms-epoch in SQLite; a `Date` in app code.")
|
|
48
|
+
}).describe("One transition in `pithy_auth_user_lifecycle`: an account suspended, reinstated, deleted, restored or purged, with who did it and why.");
|
|
49
|
+
//#endregion
|
|
50
|
+
export { UserLifecycleAction, UserLifecycleActorType, UserLifecycleEvent };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { PithyHonoEnv } from "@pithy-sh/core/src/capability/capability";
|
|
4
|
+
import type { Context } from "hono";
|
|
5
|
+
import type { AuthInstance } from "../instance/auth";
|
|
6
|
+
/** Sign `value` the way better-call's `signCookieValue` does, before its URL encoding: `value.signature`. */
|
|
7
|
+
export declare function signCredential(value: string, secret: string): Promise<string>;
|
|
8
|
+
/**
|
|
9
|
+
* Which of `keys` a signed credential verifies under, by index, and the value it signs — or `null` when it
|
|
10
|
+
* is not a signed credential or verifies under none. Takes the decoded form, `value.signature`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function verifyCredential(signed: string, keys: readonly string[]): Promise<{
|
|
13
|
+
value: string;
|
|
14
|
+
key: number;
|
|
15
|
+
} | null>;
|
|
16
|
+
/** What was re-signed: the request's new headers, each re-signed cookie, and the re-signed bearer credential. */
|
|
17
|
+
export interface ResignedCredentials {
|
|
18
|
+
/** The request headers with every re-signed credential in place of the one that arrived. */
|
|
19
|
+
headers: Headers;
|
|
20
|
+
/** Each cookie re-signed, by name, with its new value in decoded form. */
|
|
21
|
+
cookies: {
|
|
22
|
+
name: string;
|
|
23
|
+
value: string;
|
|
24
|
+
}[];
|
|
25
|
+
/** The bearer credential re-signed, in decoded form, when one was. */
|
|
26
|
+
bearer?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Re-sign every credential in `headers` that verifies under a prior key: the cookies named in `names`, and
|
|
30
|
+
* a signed bearer token. Answers `null` when nothing needed it — including always when there is no prior
|
|
31
|
+
* key, so a deployment that has never rotated parses nothing.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resignCredentials(headers: Headers, keys: {
|
|
34
|
+
current: string;
|
|
35
|
+
prior: readonly string[];
|
|
36
|
+
}, names: readonly string[]): Promise<ResignedCredentials | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Carry this request's credentials across a rotation, then run the rest of the chain, then hand the
|
|
39
|
+
* re-signed credentials back on the response.
|
|
40
|
+
*
|
|
41
|
+
* Swaps `c.req.raw` for a request carrying the re-signed credentials, so the session middleware, the auth
|
|
42
|
+
* handler and every route that reads the request see one consistent set. A response that already sets the
|
|
43
|
+
* cookie — a sign-out, a refresh — keeps its own; this only fills the silence.
|
|
44
|
+
*/
|
|
45
|
+
export declare function withResignedCredentials(c: Context<PithyHonoEnv>, instance: AuthInstance, next: () => Promise<void>): Promise<void>;
|
|
46
|
+
//# sourceMappingURL=credentialResign.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentialResign.d.ts","sourceRoot":"","sources":["../../src/http/credentialResign.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAE7E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAmDrD,6GAA6G;AAC7G,wBAAsB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEnF;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,MAAM,EAAE,GACtB,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAWhD;AAuBD,iHAAiH;AACjH,MAAM,WAAW,mBAAmB;IAClC,4FAA4F;IAC5F,OAAO,EAAE,OAAO,CAAC;IACjB,0EAA0E;IAC1E,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC3C,sEAAsE;IACtE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,EACnD,KAAK,EAAE,SAAS,MAAM,EAAE,GACvB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAyCrC;AA0CD;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAC3C,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,EACxB,QAAQ,EAAE,YAAY,EACtB,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GACxB,OAAO,CAAC,IAAI,CAAC,CAkDf"}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { resolveSessionSecrets } from "../instance/secrets.js";
|
|
4
|
+
import { priorSessionSecrets } from "../instance/sessionSecrets.js";
|
|
5
|
+
//#region src/http/credentialResign.ts
|
|
6
|
+
/**
|
|
7
|
+
* **A session signed before a rotation, carried across it (#602).**
|
|
8
|
+
*
|
|
9
|
+
* Better Auth signs the session cookie — and the signed token a bearer client holds — with one secret,
|
|
10
|
+
* `ctx.secret`, which is the first entry of the version set and nothing else (`cookies/index.mjs:172`,
|
|
11
|
+
* `plugins/bearer/index.mjs`). So the moment `auth-session-secret` rotates, every credential issued under
|
|
12
|
+
* the previous version fails verification, and everyone is signed out at once. The overlap the registry
|
|
13
|
+
* declares would protect nothing.
|
|
14
|
+
*
|
|
15
|
+
* This closes that gap in front of Better Auth: before a request reaches it, a credential that fails under
|
|
16
|
+
* the current version and verifies under a **retained** prior one is re-signed under the current version.
|
|
17
|
+
* Better Auth then sees a credential it accepts, and the response carries the re-signed one back — a
|
|
18
|
+
* `Set-Cookie` for a browser, `set-auth-token` for a bearer client — so the next request needs no help.
|
|
19
|
+
*
|
|
20
|
+
* **It extends no trust.** A credential is re-signed only when it verifies under a version this deployment
|
|
21
|
+
* still holds, which is exactly the set Better Auth would accept if it verified signatures by version. The
|
|
22
|
+
* session it names is still looked up, still expired, still revoked by Better Auth as before. And the
|
|
23
|
+
* window closes on its own: once the rotation retires the previous version, nothing verifies under it.
|
|
24
|
+
*
|
|
25
|
+
* **The session's lifetime is the overlap.** A session in use is re-signed on its next request, inside the
|
|
26
|
+
* overlap; one not used for the whole overlap has expired anyway, because `auth-session-secret`'s overlap
|
|
27
|
+
* is declared at least as long as `sessionExpiresIn`, which slides on use.
|
|
28
|
+
*/
|
|
29
|
+
/** HMAC-SHA256 under `secret`, in standard base64 — the 44 characters better-call writes and checks. */
|
|
30
|
+
async function signature(value, secret) {
|
|
31
|
+
const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(secret), {
|
|
32
|
+
name: "HMAC",
|
|
33
|
+
hash: "SHA-256"
|
|
34
|
+
}, false, ["sign"]);
|
|
35
|
+
const bytes = new Uint8Array(await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(value)));
|
|
36
|
+
let binary = "";
|
|
37
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
38
|
+
return btoa(binary);
|
|
39
|
+
}
|
|
40
|
+
/** Constant-time comparison of two strings of equal length; unequal lengths are unequal. */
|
|
41
|
+
function sameSignature(left, right) {
|
|
42
|
+
if (left.length !== right.length) return false;
|
|
43
|
+
let difference = 0;
|
|
44
|
+
for (let index = 0; index < left.length; index++) difference |= left.charCodeAt(index) ^ right.charCodeAt(index);
|
|
45
|
+
return difference === 0;
|
|
46
|
+
}
|
|
47
|
+
/** Sign `value` the way better-call's `signCookieValue` does, before its URL encoding: `value.signature`. */
|
|
48
|
+
async function signCredential(value, secret) {
|
|
49
|
+
return `${value}.${await signature(value, secret)}`;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Which of `keys` a signed credential verifies under, by index, and the value it signs — or `null` when it
|
|
53
|
+
* is not a signed credential or verifies under none. Takes the decoded form, `value.signature`.
|
|
54
|
+
*/
|
|
55
|
+
async function verifyCredential(signed, keys) {
|
|
56
|
+
const dot = signed.lastIndexOf(".");
|
|
57
|
+
if (dot < 1) return null;
|
|
58
|
+
const value = signed.slice(0, dot);
|
|
59
|
+
const presented = signed.slice(dot + 1);
|
|
60
|
+
if (presented.length !== 44 || !presented.endsWith("=")) return null;
|
|
61
|
+
for (let index = 0; index < keys.length; index++) {
|
|
62
|
+
const key = keys[index];
|
|
63
|
+
if (sameSignature(await signature(value, key), presented)) return {
|
|
64
|
+
value,
|
|
65
|
+
key: index
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
/** `decodeURIComponent`, answering the input itself when it is not valid percent-encoding. */
|
|
71
|
+
function decoded(text) {
|
|
72
|
+
try {
|
|
73
|
+
return decodeURIComponent(text);
|
|
74
|
+
} catch {
|
|
75
|
+
return text;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Re-sign under `current` whichever of a credential's two forms was signed under a key in `prior`.
|
|
80
|
+
*
|
|
81
|
+
* Takes the decoded credential and answers the re-signed one, or `null` when it verifies under the current
|
|
82
|
+
* key already, under no key at all, or is not a signed credential.
|
|
83
|
+
*/
|
|
84
|
+
async function carried(signed, keys) {
|
|
85
|
+
const verified = await verifyCredential(signed, [keys.current, ...keys.prior]);
|
|
86
|
+
if (verified === null || verified.key === 0) return null;
|
|
87
|
+
return await signCredential(verified.value, keys.current);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Re-sign every credential in `headers` that verifies under a prior key: the cookies named in `names`, and
|
|
91
|
+
* a signed bearer token. Answers `null` when nothing needed it — including always when there is no prior
|
|
92
|
+
* key, so a deployment that has never rotated parses nothing.
|
|
93
|
+
*/
|
|
94
|
+
async function resignCredentials(headers, keys, names) {
|
|
95
|
+
if (keys.prior.length === 0) return null;
|
|
96
|
+
const cookies = [];
|
|
97
|
+
let bearer;
|
|
98
|
+
const header = headers.get("cookie");
|
|
99
|
+
let cookieHeader = header;
|
|
100
|
+
if (header !== null) {
|
|
101
|
+
const parts = header.split(";").map((part) => part.trim());
|
|
102
|
+
cookieHeader = (await Promise.all(parts.map(async (part) => {
|
|
103
|
+
const equals = part.indexOf("=");
|
|
104
|
+
if (equals < 1) return part;
|
|
105
|
+
const name = part.slice(0, equals);
|
|
106
|
+
if (!names.includes(name)) return part;
|
|
107
|
+
const fresh = await carried(decoded(part.slice(equals + 1)), keys);
|
|
108
|
+
if (fresh === null) return part;
|
|
109
|
+
cookies.push({
|
|
110
|
+
name,
|
|
111
|
+
value: fresh
|
|
112
|
+
});
|
|
113
|
+
return `${name}=${encodeURIComponent(fresh)}`;
|
|
114
|
+
}))).join("; ");
|
|
115
|
+
}
|
|
116
|
+
const authorization = headers.get("authorization");
|
|
117
|
+
let authorizationHeader = authorization;
|
|
118
|
+
if (authorization !== null && authorization.slice(0, 7).toLowerCase() === "bearer ") {
|
|
119
|
+
const token = decoded(authorization.slice(7).trim());
|
|
120
|
+
const fresh = token.includes(".") ? await carried(token, keys) : null;
|
|
121
|
+
if (fresh !== null) {
|
|
122
|
+
bearer = fresh;
|
|
123
|
+
authorizationHeader = `Bearer ${fresh}`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (cookies.length === 0 && bearer === void 0) return null;
|
|
127
|
+
const next = new Headers(headers);
|
|
128
|
+
if (cookieHeader !== null) next.set("cookie", cookieHeader);
|
|
129
|
+
if (authorizationHeader !== null) next.set("authorization", authorizationHeader);
|
|
130
|
+
return {
|
|
131
|
+
headers: next,
|
|
132
|
+
cookies,
|
|
133
|
+
...bearer === void 0 ? {} : { bearer }
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** One `Set-Cookie` value, with the attributes Better Auth itself would have written. */
|
|
137
|
+
function setCookie(name, value, attributes, maxAge) {
|
|
138
|
+
const parts = [`${name}=${encodeURIComponent(value)}`];
|
|
139
|
+
if (maxAge !== void 0) parts.push(`Max-Age=${Math.floor(maxAge)}`);
|
|
140
|
+
if (attributes.domain) parts.push(`Domain=${attributes.domain}`);
|
|
141
|
+
parts.push(`Path=${attributes.path ?? "/"}`);
|
|
142
|
+
if (attributes.httpOnly) parts.push("HttpOnly");
|
|
143
|
+
if (attributes.secure || attributes.partitioned) parts.push("Secure");
|
|
144
|
+
if (attributes.sameSite) parts.push(`SameSite=${attributes.sameSite.charAt(0).toUpperCase()}${attributes.sameSite.slice(1).toLowerCase()}`);
|
|
145
|
+
if (attributes.partitioned) parts.push("Partitioned");
|
|
146
|
+
return parts.join("; ");
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The credentials this request needs re-signed, with the instance context their cookies are written from —
|
|
150
|
+
* or `null` when there is no prior version, or nothing to re-sign.
|
|
151
|
+
*/
|
|
152
|
+
async function resignedFor(c, instance) {
|
|
153
|
+
const versions = await resolveSessionSecrets(c.env);
|
|
154
|
+
const current = versions.versions[versions.currentVersion];
|
|
155
|
+
const prior = priorSessionSecrets(versions);
|
|
156
|
+
if (current === void 0 || prior.length === 0) return null;
|
|
157
|
+
const context = await instance.$context;
|
|
158
|
+
const names = [context.authCookies.sessionToken.name, context.authCookies.dontRememberToken.name];
|
|
159
|
+
const resigned = await resignCredentials(c.req.raw.headers, {
|
|
160
|
+
current,
|
|
161
|
+
prior
|
|
162
|
+
}, names);
|
|
163
|
+
return resigned === null ? null : {
|
|
164
|
+
resigned,
|
|
165
|
+
context
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Carry this request's credentials across a rotation, then run the rest of the chain, then hand the
|
|
170
|
+
* re-signed credentials back on the response.
|
|
171
|
+
*
|
|
172
|
+
* Swaps `c.req.raw` for a request carrying the re-signed credentials, so the session middleware, the auth
|
|
173
|
+
* handler and every route that reads the request see one consistent set. A response that already sets the
|
|
174
|
+
* cookie — a sign-out, a refresh — keeps its own; this only fills the silence.
|
|
175
|
+
*/
|
|
176
|
+
async function withResignedCredentials(c, instance, next) {
|
|
177
|
+
const prepared = await resignedFor(c, instance).catch(() => null);
|
|
178
|
+
if (prepared === null) return await next();
|
|
179
|
+
const { resigned, context } = prepared;
|
|
180
|
+
const session = context.authCookies.sessionToken;
|
|
181
|
+
const dontRemember = context.authCookies.dontRememberToken;
|
|
182
|
+
c.req.raw = new Request(c.req.raw, { headers: resigned.headers });
|
|
183
|
+
await next();
|
|
184
|
+
const response = new Response(c.res.body, c.res);
|
|
185
|
+
const setting = response.headers.getSetCookie().map((line) => line.slice(0, line.indexOf("=")));
|
|
186
|
+
const remembered = !(resigned.headers.get("cookie") ?? "").split(";").some((part) => part.trim().startsWith(`${dontRemember.name}=`));
|
|
187
|
+
for (const cookie of resigned.cookies) {
|
|
188
|
+
if (setting.includes(cookie.name)) continue;
|
|
189
|
+
const isSession = cookie.name === session.name;
|
|
190
|
+
const attributes = isSession ? session.attributes : dontRemember.attributes;
|
|
191
|
+
response.headers.append("set-cookie", setCookie(cookie.name, cookie.value, attributes, isSession && remembered ? context.sessionConfig.expiresIn : void 0));
|
|
192
|
+
}
|
|
193
|
+
if (resigned.bearer !== void 0 && !response.headers.has("set-auth-token")) {
|
|
194
|
+
response.headers.set("set-auth-token", encodeURIComponent(resigned.bearer));
|
|
195
|
+
const exposed = new Set((response.headers.get("access-control-expose-headers") ?? "").split(",").map((header) => header.trim()).filter(Boolean));
|
|
196
|
+
exposed.add("set-auth-token");
|
|
197
|
+
response.headers.set("access-control-expose-headers", [...exposed].join(", "));
|
|
198
|
+
}
|
|
199
|
+
c.res = void 0;
|
|
200
|
+
c.res = response;
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
export { resignCredentials, signCredential, verifyCredential, withResignedCredentials };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorCallbackUrl.d.ts","sourceRoot":"","sources":["../../src/http/errorCallbackUrl.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errorCallbackUrl.d.ts","sourceRoot":"","sources":["../../src/http/errorCallbackUrl.ts"],"names":[],"mappings":"AAuKA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAgDnF;AA0ED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAkBlG"}
|
|
@@ -17,22 +17,28 @@ import { InternalError, ValidationError } from "@pithy-sh/core/src/error/pithyEr
|
|
|
17
17
|
* | 1 | `…?error=access_denied` | read the *first* `error`, which the caller had planted |
|
|
18
18
|
* | 2 | `…/sign-in#x` | the appended `error=` landed inside the fragment; `searchParams` saw nothing |
|
|
19
19
|
*
|
|
20
|
-
* The third shape is the diagnosis rather than another entry. `better-auth`'s `redirectOnError`
|
|
21
|
-
* (`dist/api/routes/callback.mjs:78`)
|
|
20
|
+
* The third shape is the diagnosis rather than another entry. At 1.7.1, `better-auth`'s `redirectOnError`
|
|
21
|
+
* (`dist/api/routes/callback.mjs:78`) was string concatenation that never parsed:
|
|
22
22
|
*
|
|
23
23
|
* ```js
|
|
24
24
|
* const url = `${baseURL}${baseURL.includes("?") ? "&" : "?"}${params.toString()}`;
|
|
25
25
|
* ```
|
|
26
26
|
*
|
|
27
27
|
* and `baseURL` there is `errorURL`, which `oauth2/state.mjs:27` stored as the caller's raw string. So the
|
|
28
|
-
* collapse
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
28
|
+
* collapse parsed the output of a construction the caller steered, and **every input shape where that
|
|
29
|
+
* construction and parsing disagree is a new bypass.** `#x` was the third; a fourth exists for as long as
|
|
30
|
+
* the caller chooses the string being built on. Patching the parser again finds it later, from a report,
|
|
31
|
+
* rather than now.
|
|
32
|
+
*
|
|
33
|
+
* At 1.7.11 the line is `appendQueryParams(baseURL, params)`, from `@better-auth/core`'s
|
|
34
|
+
* `dist/utils/url.mjs:40`. It parses the input, sets the query before any fragment, and throws on a `//`
|
|
35
|
+
* or `/\` input. That closes the fragment shape inside the dependency and leaves the planted `error`
|
|
36
|
+
* open, since the existing query is kept as text. The guard does not lean on either fact. The argument
|
|
37
|
+
* above is about who chooses the string, and that has not changed.
|
|
32
38
|
*
|
|
33
39
|
* ## The invariant this module holds
|
|
34
40
|
*
|
|
35
|
-
* By the time `redirectOnError`
|
|
41
|
+
* By the time `redirectOnError` appends to it, `errorURL` is **a URL the kit produced**:
|
|
36
42
|
*
|
|
37
43
|
* - no fragment — not a populated one, and not a bare trailing `#` either, which has an empty
|
|
38
44
|
* `URL.hash` and still swallows everything appended after it;
|
|
@@ -40,7 +46,7 @@ import { InternalError, ValidationError } from "@pithy-sh/core/src/error/pithyEr
|
|
|
40
46
|
* - a `?` in it **iff** it carries a query, so appending `?k=v` or `&k=v` — whichever the dependency
|
|
41
47
|
* picks — yields exactly one readable query either way.
|
|
42
48
|
*
|
|
43
|
-
* Then the
|
|
49
|
+
* Then the append is predictable by construction, under raw concatenation or a parse, and the collapse is reading a string this
|
|
44
50
|
* capability wrote rather than one an attacker composed.
|
|
45
51
|
*
|
|
46
52
|
* ## Refused, never quietly rewritten
|
|
@@ -63,7 +69,7 @@ import { InternalError, ValidationError } from "@pithy-sh/core/src/error/pithyEr
|
|
|
63
69
|
*
|
|
64
70
|
* Every request through `handleBetterAuth` carrying an `errorCallbackURL` is guarded — `/sign-in/social`,
|
|
65
71
|
* `/link-social`, `/sign-in/magic-link`. Deciding *per route* which of the dependency's code paths parse
|
|
66
|
-
* the value and which
|
|
72
|
+
* the value and which build on it raw is exactly the out-guessing above, and it is a claim that goes stale
|
|
67
73
|
* on the next bump. One field, one rule. (An adopter whose error screen is a hash route passes it as a
|
|
68
74
|
* query parameter instead; the kit's own templates already do.)
|
|
69
75
|
*
|
package/dist/http/guards.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ import type { ControlPlaneScope } from "@pithy-sh/core/src/controlPlane/scope/sc
|
|
|
27
27
|
* every legitimate management call, permanently, and **no credential could fix it** — there is no user
|
|
28
28
|
* to sign in as. `controlPlaneIsolation.workers.test.ts` pins both directions of that separation.
|
|
29
29
|
*
|
|
30
|
-
* ##
|
|
30
|
+
* ## Eight scopes, because these are eight different blast radii
|
|
31
31
|
*
|
|
32
32
|
* The temptation is one `auth:admin` flag, and on this capability it is the most dangerous version of
|
|
33
33
|
* that mistake. **Reading a user is a privacy operation; revoking their sessions is an availability
|
|
@@ -84,9 +84,31 @@ export declare const AUTH_USERS_LOGOUT_SCOPE: ControlPlaneScope;
|
|
|
84
84
|
* push token go with it.
|
|
85
85
|
*/
|
|
86
86
|
export declare const AUTH_DEVICES_REVOKE_SCOPE: ControlPlaneScope;
|
|
87
|
+
/**
|
|
88
|
+
* Suspend an account and reinstate it (#620, the routes in #621). Ending somebody's access is an availability
|
|
89
|
+
* operation on one person, and reversible — which is what separates it from deleting them, and why it is
|
|
90
|
+
* granted apart from both reading them and erasing them.
|
|
91
|
+
*/
|
|
92
|
+
export declare const AUTH_USERS_SUSPEND_SCOPE: ControlPlaneScope;
|
|
93
|
+
/**
|
|
94
|
+
* Delete an account, restore one inside its purge window, and start a purge (#620, the routes in #622). The
|
|
95
|
+
* one scope on this surface whose worst case cannot be undone: once the window closes, the person's rows are
|
|
96
|
+
* gone from every capability that held them.
|
|
97
|
+
*/
|
|
98
|
+
export declare const AUTH_USERS_DELETE_SCOPE: ControlPlaneScope;
|
|
99
|
+
/**
|
|
100
|
+
* Export everything the kit holds about one account — the answer to an access request (#620, the route in
|
|
101
|
+
* #622). A disclosure, and a far larger one than reading the user: it carries every capability's rows about
|
|
102
|
+
* them, so it is granted on its own and audited as one.
|
|
103
|
+
*/
|
|
104
|
+
export declare const AUTH_USERS_EXPORT_SCOPE: ControlPlaneScope;
|
|
87
105
|
/**
|
|
88
106
|
* Every control-plane scope auth defines — what `pithy dashboard connect` offers for this capability,
|
|
89
107
|
* and the list a manifest or a doc quotes rather than re-typing.
|
|
108
|
+
*
|
|
109
|
+
* **The three lifecycle scopes are declared before the routes that require them.** The suspend, delete and
|
|
110
|
+
* export routes land in #621 and #622; declaring the vocabulary here, once, is what lets those issues build on
|
|
111
|
+
* one name each rather than three issues each choosing their own.
|
|
90
112
|
*/
|
|
91
113
|
export declare const AUTH_CONTROL_PLANE_SCOPES: readonly ControlPlaneScope[];
|
|
92
114
|
/**
|