@pithy-sh/auth 0.9.0 → 0.11.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 +8 -2
- package/dist/admin/users.js +1 -1
- package/dist/audit/actions.d.ts +40 -0
- package/dist/audit/actions.d.ts.map +1 -1
- package/dist/audit/actions.js +41 -1
- package/dist/audit/emit.d.ts +2 -0
- package/dist/audit/emit.d.ts.map +1 -1
- package/dist/audit/emit.js +2 -2
- package/dist/capability.d.ts +45 -1
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +40 -5
- 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 +4 -0
- package/dist/data/betterAuth.d.ts.map +1 -1
- package/dist/data/betterAuth.js +6 -2
- 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 -2
- package/dist/data/tables.d.ts.map +1 -1
- package/dist/data/tables.js +5 -3
- package/dist/data/userLifecycle.d.ts +62 -0
- package/dist/data/userLifecycle.d.ts.map +1 -0
- package/dist/data/userLifecycle.js +51 -0
- package/dist/http/adminRoutes.d.ts.map +1 -1
- package/dist/http/adminRoutes.js +270 -5
- package/dist/http/guards.d.ts +24 -1
- package/dist/http/guards.d.ts.map +1 -1
- package/dist/http/guards.js +59 -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 +30 -1
- package/dist/http/middleware.d.ts.map +1 -1
- package/dist/http/middleware.js +89 -7
- package/dist/http/providerRefusal.d.ts.map +1 -1
- package/dist/http/providerRefusal.js +8 -0
- package/dist/http/resolve.d.ts.map +1 -1
- package/dist/http/resolve.js +7 -4
- package/dist/http/responses.d.ts +180 -0
- package/dist/http/responses.d.ts.map +1 -1
- package/dist/http/responses.js +142 -1
- package/dist/http/routes.d.ts +1 -0
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +65 -4
- package/dist/http/schemas.d.ts +57 -0
- package/dist/http/schemas.d.ts.map +1 -1
- package/dist/http/schemas.js +51 -1
- package/dist/http/views.d.ts +39 -2
- package/dist/http/views.d.ts.map +1 -1
- package/dist/http/views.js +105 -1
- package/dist/instance/auth.d.ts +55 -3
- package/dist/instance/auth.d.ts.map +1 -1
- package/dist/instance/auth.js +53 -2
- 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 +7 -6
- package/dist/instance/providerSignInGate.d.ts.map +1 -1
- package/dist/instance/providerSignInGate.js +7 -6
- 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 +64 -3
- package/dist/instance/secrets.d.ts.map +1 -1
- package/dist/instance/secrets.js +44 -5
- package/dist/lifecycle/audited.d.ts +48 -0
- package/dist/lifecycle/audited.d.ts.map +1 -0
- package/dist/lifecycle/audited.js +65 -0
- package/dist/lifecycle/enforce.d.ts +75 -0
- package/dist/lifecycle/enforce.d.ts.map +1 -0
- package/dist/lifecycle/enforce.js +111 -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/reason.d.ts +11 -0
- package/dist/lifecycle/reason.d.ts.map +1 -0
- package/dist/lifecycle/reason.js +13 -0
- package/dist/lifecycle/request.d.ts +18 -0
- package/dist/lifecycle/request.d.ts.map +1 -0
- package/dist/lifecycle/request.js +31 -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 +272 -0
- package/dist/lifecycle/transitions.d.ts.map +1 -0
- package/dist/lifecycle/transitions.js +623 -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/0005_purge_once.d.ts +22 -0
- package/dist/migrations/0005_purge_once.d.ts.map +1 -0
- package/dist/migrations/0005_purge_once.js +35 -0
- package/dist/migrations/pluginTables.d.ts +8 -0
- package/dist/migrations/pluginTables.d.ts.map +1 -1
- package/dist/migrations/pluginTables.js +8 -1
- 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/purge/contribution.d.ts +47 -0
- package/dist/purge/contribution.d.ts.map +1 -0
- package/dist/purge/contribution.js +141 -0
- package/dist/purge/retryPolicy.d.ts +22 -0
- package/dist/purge/retryPolicy.d.ts.map +1 -0
- package/dist/purge/retryPolicy.js +26 -0
- package/dist/purge/run.d.ts +88 -0
- package/dist/purge/run.d.ts.map +1 -0
- package/dist/purge/run.js +102 -0
- package/dist/purge/specs.d.ts +71 -0
- package/dist/purge/specs.d.ts.map +1 -0
- package/dist/purge/specs.js +72 -0
- package/dist/purge/start.d.ts +42 -0
- package/dist/purge/start.d.ts.map +1 -0
- package/dist/purge/start.js +37 -0
- package/dist/purge/workflow.d.ts +58 -0
- package/dist/purge/workflow.d.ts.map +1 -0
- package/dist/purge/workflow.js +96 -0
- package/dist/test-utils/accountFootprint.d.ts +10 -0
- package/dist/test-utils/accountFootprint.d.ts.map +1 -0
- package/dist/test-utils/accountFootprint.js +44 -0
- package/dist/test-utils/liveApp.d.ts +21 -0
- package/dist/test-utils/liveApp.d.ts.map +1 -1
- package/dist/test-utils/liveApp.js +1 -0
- package/dist/test-utils/scopedBackend.d.ts +21 -0
- package/dist/test-utils/scopedBackend.d.ts.map +1 -1
- package/dist/test-utils/scopedBackend.js +7 -1
- 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 +217 -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/package.json +5 -5
- package/pithy.manifest.json +17 -3
- package/src/audit/actions.ts +42 -0
- package/src/audit/emit.ts +4 -2
- package/src/capability.ts +170 -52
- package/src/client/lifecycle.ts +27 -0
- package/src/client/projection.ts +4 -0
- package/src/data/betterAuth.ts +21 -1
- package/src/data/kitFields.ts +27 -0
- package/src/data/tables.ts +4 -2
- package/src/data/userLifecycle.ts +83 -0
- package/src/http/adminRoutes.ts +398 -2
- package/src/http/guards.ts +61 -1
- package/src/http/lifecycleGate.ts +78 -0
- package/src/http/middleware.ts +104 -2
- package/src/http/providerRefusal.ts +10 -0
- package/src/http/resolve.ts +8 -3
- package/src/http/responses.ts +245 -0
- package/src/http/routes.ts +93 -4
- package/src/http/schemas.ts +110 -0
- package/src/http/views.ts +147 -2
- package/src/instance/auth.ts +74 -2
- package/src/instance/microsoftEmail.ts +111 -0
- package/src/instance/providerSignInGate.ts +7 -6
- package/src/instance/providers.ts +8 -4
- package/src/instance/secrets.ts +59 -5
- package/src/lifecycle/audited.ts +104 -0
- package/src/lifecycle/enforce.ts +139 -0
- package/src/lifecycle/hooks.ts +54 -0
- package/src/lifecycle/reason.ts +11 -0
- package/src/lifecycle/request.ts +39 -0
- package/src/lifecycle/state.ts +101 -0
- package/src/lifecycle/transitions.ts +872 -0
- package/src/migrations/0004_user_lifecycle.ts +68 -0
- package/src/migrations/0005_purge_once.ts +42 -0
- package/src/migrations/pluginTables.ts +19 -1
- package/src/migrations/set.ts +5 -1
- package/src/purge/contribution.ts +191 -0
- package/src/purge/retryPolicy.ts +26 -0
- package/src/purge/run.ts +185 -0
- package/src/purge/specs.ts +95 -0
- package/src/purge/start.ts +79 -0
- package/src/purge/workflow.ts +144 -0
- package/src/test-utils/accountFootprint.ts +95 -0
- package/src/test-utils/liveApp.ts +2 -1
- package/src/test-utils/scopedBackend.ts +2 -1
- 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;
|
|
@@ -98,6 +102,29 @@ 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
|
+
notify: import("zod").ZodNullable<import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodLiteral<0>, import("zod").ZodLiteral<1>, import("zod").ZodBoolean, import("zod").ZodString]>, import("zod").ZodBoolean>>;
|
|
126
|
+
createdAt: import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodNumber, import("zod").ZodString, import("zod").ZodDate]>, import("zod").ZodDate>;
|
|
127
|
+
}, import("zod/v4/core").$strip>;
|
|
101
128
|
};
|
|
102
129
|
export type AuthTables = typeof authTables;
|
|
103
130
|
/** 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,62 @@
|
|
|
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
|
+
notify: z.ZodNullable<z.ZodCodec<z.ZodUnion<readonly [z.ZodLiteral<0>, z.ZodLiteral<1>, z.ZodBoolean, z.ZodString]>, z.ZodBoolean>>;
|
|
59
|
+
createdAt: z.ZodCodec<z.ZodUnion<readonly [z.ZodNumber, z.ZodString, z.ZodDate]>, z.ZodDate>;
|
|
60
|
+
}, z.core.$strip>;
|
|
61
|
+
export type UserLifecycleEvent = z.output<typeof UserLifecycleEvent>;
|
|
62
|
+
//# 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;;;;;;;;;;;;;;;;;;;;;;iBAyC5B,CAAC;AACJ,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,kBAAkB,CAAC,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { SQLiteBoolean, 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
|
+
notify: SQLiteBoolean.nullable().describe("For a hard deletion's `deleted` row, whether the purge sends the deletion-complete mail — false when the deletion asked for none. Null for every other row, and for a hard deletion that did not say. The purge reads it, so a purge the daily pass starts keeps the choice (#622)."),
|
|
48
|
+
createdAt: SQLiteDate.describe("When the transition happened. Ms-epoch in SQLite; a `Date` in app code.")
|
|
49
|
+
}).describe("One transition in `pithy_auth_user_lifecycle`: an account suspended, reinstated, deleted, restored or purged, with who did it and why.");
|
|
50
|
+
//#endregion
|
|
51
|
+
export { UserLifecycleAction, UserLifecycleActorType, UserLifecycleEvent };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adminRoutes.d.ts","sourceRoot":"","sources":["../../src/http/adminRoutes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;
|
|
1
|
+
{"version":3,"file":"adminRoutes.d.ts","sourceRoot":"","sources":["../../src/http/adminRoutes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAS7E,OAAO,KAAK,EAAW,IAAI,EAAE,MAAM,MAAM,CAAC;AAe1C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAuThD;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,UAAU,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,CAyc7F"}
|
package/dist/http/adminRoutes.js
CHANGED
|
@@ -5,16 +5,24 @@ import { AuthAuditActions } from "../audit/actions.js";
|
|
|
5
5
|
import { correlation, emitControlPlaneAction } from "../audit/emit.js";
|
|
6
6
|
import { deleteDevice, deviceSessionTokens } from "../device/registry.js";
|
|
7
7
|
import { getAuthInstance, resolveDb } from "./resolve.js";
|
|
8
|
-
import { AUTH_DEVICES_READ_SCOPE, AUTH_DEVICES_REVOKE_SCOPE, AUTH_SESSIONS_REVOKE_SCOPE, AUTH_USERS_LOGOUT_SCOPE, AUTH_USERS_READ_SCOPE } from "./guards.js";
|
|
8
|
+
import { AUTH_DEVICES_READ_SCOPE, AUTH_DEVICES_REVOKE_SCOPE, AUTH_SESSIONS_REVOKE_SCOPE, AUTH_USERS_DELETE_SCOPE, AUTH_USERS_EXPORT_SCOPE, AUTH_USERS_LOGOUT_SCOPE, AUTH_USERS_READ_SCOPE, AUTH_USERS_SUSPEND_SCOPE } from "./guards.js";
|
|
9
9
|
import { findSessionById, getUser, listDeviceRegistry, listUserDevices, listUserSessions, listUsers, userProviders, userSessionTokens } from "../admin/users.js";
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
10
|
+
import { lifecycleDeps } from "../lifecycle/request.js";
|
|
11
|
+
import "../lifecycle/reason.js";
|
|
12
|
+
import { deleteUser, reinstateUser, requirePresent, restoreUser, suspendUser } from "../lifecycle/transitions.js";
|
|
13
|
+
import { auditedLifecycleCall, recordLifecycleRefusal } from "../lifecycle/audited.js";
|
|
14
|
+
import { AUTH_PURGE_BINDING } from "../purge/specs.js";
|
|
15
|
+
import { startPurgeInstance } from "../purge/start.js";
|
|
16
|
+
import { AdminDeviceRevokeResponse, AdminDevicesResponse, AdminRevokeResponse, AdminUserDeleteResponse, AdminUserExportResponse, AdminUserReinstateResponse, AdminUserResponse, AdminUserRestoreResponse, AdminUserSuspendResponse, AdminUsersResponse } from "./responses.js";
|
|
17
|
+
import { DeleteUserBody, ExportUserQuery, ListDevicesQuery, ListUsersQuery, ReinstateUserBody, RestoreUserBody, RevokeDeviceBody, RevokeSessionBody, SuspendUserBody, UserIdParam } from "./schemas.js";
|
|
18
|
+
import { deletionView, deviceView, exportView, reinstatementView, restorationView, sessionView, suspensionView, userView } from "./views.js";
|
|
19
|
+
import { purgeExports } from "@pithy-sh/core/src/capability/purge";
|
|
13
20
|
import { InternalError, NotFoundError } from "@pithy-sh/core/src/error/pithyError";
|
|
14
21
|
import { describeRoute } from "@pithy-sh/core/src/http/routeSpec";
|
|
15
22
|
import { validate } from "@pithy-sh/core/src/http/validation";
|
|
16
|
-
import { requireControlPlane } from "@pithy-sh/core/src/controlPlane/http/guard";
|
|
17
23
|
import { MAX_PAGE_SIZE } from "@pithy-sh/core/src/data/cursor";
|
|
24
|
+
import { requireControlPlane } from "@pithy-sh/core/src/controlPlane/http/guard";
|
|
25
|
+
import { workflowBindingFor } from "@pithy-sh/core/src/workflow/dispatch";
|
|
18
26
|
//#region src/http/adminRoutes.ts
|
|
19
27
|
/**
|
|
20
28
|
* The control-plane admin surface: the reads a management dashboard's user panes resolve to, and the
|
|
@@ -26,6 +34,11 @@ import { MAX_PAGE_SIZE } from "@pithy-sh/core/src/data/cursor";
|
|
|
26
34
|
* POST {base}/admin/sessions/revoke auth:sessions:revoke json: RevokeSessionBody
|
|
27
35
|
* POST {base}/admin/users/:userId/sessions/revoke auth:users:logout param: UserIdParam
|
|
28
36
|
* POST {base}/admin/users/:userId/devices/revoke auth:devices:revoke param + json
|
|
37
|
+
* POST {base}/admin/users/:userId/suspend auth:users:suspend param + json: SuspendUserBody
|
|
38
|
+
* POST {base}/admin/users/:userId/reinstate auth:users:suspend param + json: ReinstateUserBody
|
|
39
|
+
* DELETE {base}/admin/users/:userId auth:users:delete param + json: DeleteUserBody
|
|
40
|
+
* POST {base}/admin/users/:userId/restore auth:users:delete param + json: RestoreUserBody
|
|
41
|
+
* GET {base}/admin/users/:userId/export auth:users:export param + query: ExportUserQuery
|
|
29
42
|
*
|
|
30
43
|
* ## Registered before the catch-all, or dead
|
|
31
44
|
*
|
|
@@ -132,6 +145,116 @@ async function revokeTokens(c, wiring, tokens) {
|
|
|
132
145
|
return tokens.length;
|
|
133
146
|
}
|
|
134
147
|
/**
|
|
148
|
+
* What a lifecycle route hands `../lifecycle/`: the transitions' dependencies, with the request's correlation
|
|
149
|
+
* and the connection on every audit event they write, and the operator as the actor.
|
|
150
|
+
*
|
|
151
|
+
* The actor is the token's verified `sub` — which person at the dashboard — the same id every other admin
|
|
152
|
+
* event records, so the trail and the history table name the same somebody.
|
|
153
|
+
*/
|
|
154
|
+
function lifecycleCall(c, wiring, caller, userId, reason) {
|
|
155
|
+
const correlated = context(c);
|
|
156
|
+
return {
|
|
157
|
+
deps: lifecycleDeps(c, wiring, {
|
|
158
|
+
...correlated,
|
|
159
|
+
metadata: { connectionId: caller.connectionId }
|
|
160
|
+
}),
|
|
161
|
+
actor: {
|
|
162
|
+
type: "control-plane",
|
|
163
|
+
id: caller.subject
|
|
164
|
+
},
|
|
165
|
+
restoreRoute: `POST ${wiring.config.basePath}/admin/users/${userId}/restore`,
|
|
166
|
+
audit: {
|
|
167
|
+
subject: caller.subject,
|
|
168
|
+
connectionId: caller.connectionId,
|
|
169
|
+
userId,
|
|
170
|
+
reason,
|
|
171
|
+
...correlated
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* A lifecycle route's body validator, which audits the bodies it refuses (#621). Every call is on the trail,
|
|
177
|
+
* a refusal included, and a body the validator turns away never reaches `auditedLifecycleCall`. So it is
|
|
178
|
+
* recorded here, as `denied` under the route's own action, with whatever reason the body carried — cut to the
|
|
179
|
+
* reason's own limit, so a refused essay is not stored whole — and the 400 is thrown as before.
|
|
180
|
+
*/
|
|
181
|
+
function lifecycleBody(schema, action) {
|
|
182
|
+
return validate("json", schema, async (raw, error, body) => {
|
|
183
|
+
const c = raw;
|
|
184
|
+
const caller = controlPlaneCaller(c);
|
|
185
|
+
const reason = body && typeof body === "object" ? body.reason : void 0;
|
|
186
|
+
await recordLifecycleRefusal(c.var.emit, {
|
|
187
|
+
action: typeof action === "string" ? action : action(body),
|
|
188
|
+
subject: caller.subject,
|
|
189
|
+
connectionId: caller.connectionId,
|
|
190
|
+
userId: c.req.valid("param").userId,
|
|
191
|
+
reason: typeof reason === "string" ? reason.slice(0, 500) : null,
|
|
192
|
+
...context(c)
|
|
193
|
+
}, error, {
|
|
194
|
+
code: "validation/invalid_input",
|
|
195
|
+
outcome: "denied"
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* End every session an account holds, after its columns are written — never before, for the reason the suspend
|
|
201
|
+
* route gives: enforcement refuses the account the moment the columns land, and revoking first would leave a
|
|
202
|
+
* window for a sign-in the revocation never saw. Through Better Auth's adapter, so `onSessionRevoked` hears
|
|
203
|
+
* each, and recorded under `auth/admin_user_sessions_revoked` with the cause.
|
|
204
|
+
*/
|
|
205
|
+
async function endSessions(c, wiring, caller, userId, cause) {
|
|
206
|
+
const revoked = await revokeTokens(c, wiring, await userSessionTokens(db(c, wiring), userId));
|
|
207
|
+
await emitControlPlaneAction(c.var.emit, {
|
|
208
|
+
action: AuthAuditActions.adminUserSessionsRevoked,
|
|
209
|
+
subject: caller.subject,
|
|
210
|
+
connectionId: caller.connectionId,
|
|
211
|
+
resourceType: "user",
|
|
212
|
+
resourceId: userId,
|
|
213
|
+
...context(c),
|
|
214
|
+
metadata: {
|
|
215
|
+
revoked,
|
|
216
|
+
cause
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
return revoked;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Start the purge of one account now (#622): `started` when this Worker binds the purge Workflow and it took the
|
|
223
|
+
* instance — or already held it — and `pending` when it binds none or the start failed; the account is due
|
|
224
|
+
* either way, and the daily pass starts it. A failed start is not this call's failure: the deletion is already
|
|
225
|
+
* written, and a 500 over it would invite a retry that finds the account deleted and says nothing new.
|
|
226
|
+
*
|
|
227
|
+
* Under the instance id the deletion names (`../purge/start.ts`), so a hard deletion repeated, or one the daily
|
|
228
|
+
* pass also finds due, is the one purge and never a second.
|
|
229
|
+
*/
|
|
230
|
+
async function startPurge(c, result, userId, notify) {
|
|
231
|
+
const log = c.var.log.child("auth");
|
|
232
|
+
const binding = workflowBindingFor(c.env, AUTH_PURGE_BINDING);
|
|
233
|
+
if (!binding) {
|
|
234
|
+
log.warn("a deletion found no purge Workflow bound; the purge is pending", { userId });
|
|
235
|
+
return "pending";
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
await startPurgeInstance(binding, {
|
|
239
|
+
userId,
|
|
240
|
+
deletedAt: result.deletedAt.getTime(),
|
|
241
|
+
...notify === void 0 ? {} : { notify },
|
|
242
|
+
at: /* @__PURE__ */ new Date()
|
|
243
|
+
});
|
|
244
|
+
return "started";
|
|
245
|
+
} catch (error) {
|
|
246
|
+
log.warn("the purge could not be started; the daily pass will start it", {
|
|
247
|
+
userId,
|
|
248
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
249
|
+
});
|
|
250
|
+
return "pending";
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** What becomes of a soft deletion's purge: due later, or — when its window has already closed — due now. */
|
|
254
|
+
function scheduledOrDue(result) {
|
|
255
|
+
return result.purgeAfter.getTime() > Date.now() ? "scheduled" : "due";
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
135
258
|
* Register the control-plane admin routes. Called by `createAuthRoutes` **before** the Better Auth
|
|
136
259
|
* catch-all; see the file header for why that ordering is load-bearing.
|
|
137
260
|
*/
|
|
@@ -301,6 +424,148 @@ function registerAuthAdminRoutes(wiring) {
|
|
|
301
424
|
removed
|
|
302
425
|
});
|
|
303
426
|
});
|
|
427
|
+
app.post(`${base}/admin/users/:userId/suspend`, describeRoute({
|
|
428
|
+
summary: "Suspend one user, with a reason: their sessions end and every sign-in is refused.",
|
|
429
|
+
verification: "control-plane",
|
|
430
|
+
scopes: [AUTH_USERS_SUSPEND_SCOPE],
|
|
431
|
+
responses: { 200: AdminUserSuspendResponse }
|
|
432
|
+
}), requireControlPlane(AUTH_USERS_SUSPEND_SCOPE), validate("param", UserIdParam), lifecycleBody(SuspendUserBody, AuthAuditActions.adminUserSuspended), async (c) => {
|
|
433
|
+
const { userId } = c.req.valid("param");
|
|
434
|
+
const { reason, until, notify } = c.req.valid("json");
|
|
435
|
+
const caller = controlPlaneCaller(c);
|
|
436
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
437
|
+
const result = await auditedLifecycleCall(c.var.emit, {
|
|
438
|
+
action: AuthAuditActions.adminUserSuspended,
|
|
439
|
+
...call.audit
|
|
440
|
+
}, () => suspendUser(call.deps, {
|
|
441
|
+
userId,
|
|
442
|
+
actor: call.actor,
|
|
443
|
+
reason,
|
|
444
|
+
until: until ?? null,
|
|
445
|
+
...notify === void 0 ? {} : { notify },
|
|
446
|
+
restoreRoute: call.restoreRoute
|
|
447
|
+
}), () => false);
|
|
448
|
+
const revoked = await revokeTokens(c, wiring, await userSessionTokens(db(c, wiring), userId));
|
|
449
|
+
await emitControlPlaneAction(c.var.emit, {
|
|
450
|
+
action: AuthAuditActions.adminUserSessionsRevoked,
|
|
451
|
+
subject: caller.subject,
|
|
452
|
+
connectionId: caller.connectionId,
|
|
453
|
+
resourceType: "user",
|
|
454
|
+
resourceId: userId,
|
|
455
|
+
...context(c),
|
|
456
|
+
metadata: {
|
|
457
|
+
revoked,
|
|
458
|
+
cause: "suspended"
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
return c.json(suspensionView(userId, result, revoked));
|
|
462
|
+
});
|
|
463
|
+
app.post(`${base}/admin/users/:userId/reinstate`, describeRoute({
|
|
464
|
+
summary: "Lift one user's suspension, with a reason. They sign in again; no session is restored.",
|
|
465
|
+
verification: "control-plane",
|
|
466
|
+
scopes: [AUTH_USERS_SUSPEND_SCOPE],
|
|
467
|
+
responses: { 200: AdminUserReinstateResponse }
|
|
468
|
+
}), requireControlPlane(AUTH_USERS_SUSPEND_SCOPE), validate("param", UserIdParam), lifecycleBody(ReinstateUserBody, AuthAuditActions.adminUserReinstated), async (c) => {
|
|
469
|
+
const { userId } = c.req.valid("param");
|
|
470
|
+
const { reason, notify } = c.req.valid("json");
|
|
471
|
+
const call = lifecycleCall(c, wiring, controlPlaneCaller(c), userId, reason);
|
|
472
|
+
const result = await auditedLifecycleCall(c.var.emit, {
|
|
473
|
+
action: AuthAuditActions.adminUserReinstated,
|
|
474
|
+
...call.audit
|
|
475
|
+
}, () => reinstateUser(call.deps, {
|
|
476
|
+
userId,
|
|
477
|
+
actor: call.actor,
|
|
478
|
+
reason,
|
|
479
|
+
...notify === void 0 ? {} : { notify },
|
|
480
|
+
restoreRoute: call.restoreRoute
|
|
481
|
+
}), (outcome) => !outcome.changed);
|
|
482
|
+
return c.json(reinstatementView(userId, result));
|
|
483
|
+
});
|
|
484
|
+
app.delete(`${base}/admin/users/:userId`, describeRoute({
|
|
485
|
+
summary: "Delete one user, with a reason: soft by default, restorable until the purge window closes; hard when named, purged now.",
|
|
486
|
+
verification: "control-plane",
|
|
487
|
+
scopes: [AUTH_USERS_DELETE_SCOPE],
|
|
488
|
+
responses: { 200: AdminUserDeleteResponse }
|
|
489
|
+
}), requireControlPlane(AUTH_USERS_DELETE_SCOPE), validate("param", UserIdParam), lifecycleBody(DeleteUserBody, (body) => body?.mode === "hard" ? AuthAuditActions.adminUserHardDeleted : AuthAuditActions.adminUserSoftDeleted), async (c) => {
|
|
490
|
+
const { userId } = c.req.valid("param");
|
|
491
|
+
const { mode, reason, notify } = c.req.valid("json");
|
|
492
|
+
const caller = controlPlaneCaller(c);
|
|
493
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
494
|
+
const action = mode === "hard" ? AuthAuditActions.adminUserHardDeleted : AuthAuditActions.adminUserSoftDeleted;
|
|
495
|
+
const result = await auditedLifecycleCall(c.var.emit, {
|
|
496
|
+
action,
|
|
497
|
+
...call.audit
|
|
498
|
+
}, () => deleteUser(call.deps, {
|
|
499
|
+
userId,
|
|
500
|
+
actor: call.actor,
|
|
501
|
+
reason,
|
|
502
|
+
mode,
|
|
503
|
+
...notify === void 0 ? {} : { notify }
|
|
504
|
+
}), (outcome) => outcome.outcome === "unchanged");
|
|
505
|
+
const sessionsRevoked = await endSessions(c, wiring, caller, userId, "deleted");
|
|
506
|
+
const disposition = mode === "hard" ? "due" : scheduledOrDue(result);
|
|
507
|
+
const purge = disposition === "due" ? await startPurge(c, result, userId, mode === "hard" ? notify : void 0) : disposition;
|
|
508
|
+
return c.json(deletionView(userId, mode, result, purge, sessionsRevoked));
|
|
509
|
+
});
|
|
510
|
+
app.post(`${base}/admin/users/:userId/restore`, describeRoute({
|
|
511
|
+
summary: "Restore one deleted user inside the purge window, with a reason. They sign in again; no session is restored.",
|
|
512
|
+
verification: "control-plane",
|
|
513
|
+
scopes: [AUTH_USERS_DELETE_SCOPE],
|
|
514
|
+
responses: { 200: AdminUserRestoreResponse }
|
|
515
|
+
}), requireControlPlane(AUTH_USERS_DELETE_SCOPE), validate("param", UserIdParam), lifecycleBody(RestoreUserBody, AuthAuditActions.adminUserRestored), async (c) => {
|
|
516
|
+
const { userId } = c.req.valid("param");
|
|
517
|
+
const { reason } = c.req.valid("json");
|
|
518
|
+
const call = lifecycleCall(c, wiring, controlPlaneCaller(c), userId, reason);
|
|
519
|
+
const result = await auditedLifecycleCall(c.var.emit, {
|
|
520
|
+
action: AuthAuditActions.adminUserRestored,
|
|
521
|
+
...call.audit
|
|
522
|
+
}, () => restoreUser(call.deps, {
|
|
523
|
+
userId,
|
|
524
|
+
actor: call.actor,
|
|
525
|
+
reason
|
|
526
|
+
}), (outcome) => !outcome.changed);
|
|
527
|
+
return c.json(restorationView(userId, result));
|
|
528
|
+
});
|
|
529
|
+
app.get(`${base}/admin/users/:userId/export`, describeRoute({
|
|
530
|
+
summary: "Export everything the kit holds about one user, from every composed capability, as one document.",
|
|
531
|
+
description: "The answer to an access request. One entry per composed capability that declares a purge contribution, in the order the purge erases them. Audited as a disclosure, with the reason.",
|
|
532
|
+
verification: "control-plane",
|
|
533
|
+
scopes: [AUTH_USERS_EXPORT_SCOPE],
|
|
534
|
+
responses: { 200: AdminUserExportResponse }
|
|
535
|
+
}), requireControlPlane(AUTH_USERS_EXPORT_SCOPE), validate("param", UserIdParam), validate("query", ExportUserQuery), async (c) => {
|
|
536
|
+
const { userId } = c.req.valid("param");
|
|
537
|
+
const { reason } = c.req.valid("query");
|
|
538
|
+
const caller = controlPlaneCaller(c);
|
|
539
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
540
|
+
const exportedAt = /* @__PURE__ */ new Date();
|
|
541
|
+
const entries = await auditedLifecycleCall(c.var.emit, {
|
|
542
|
+
action: AuthAuditActions.adminUserExported,
|
|
543
|
+
...call.audit
|
|
544
|
+
}, async () => {
|
|
545
|
+
await requirePresent(db(c, wiring), userId);
|
|
546
|
+
return purgeExports(wiring.purgeSteps ?? [], {
|
|
547
|
+
subject: {
|
|
548
|
+
type: "user",
|
|
549
|
+
id: userId
|
|
550
|
+
},
|
|
551
|
+
env: c.env
|
|
552
|
+
});
|
|
553
|
+
}, () => false);
|
|
554
|
+
await emitControlPlaneAction(c.var.emit, {
|
|
555
|
+
action: AuthAuditActions.adminUserExported,
|
|
556
|
+
subject: caller.subject,
|
|
557
|
+
connectionId: caller.connectionId,
|
|
558
|
+
resourceType: "user",
|
|
559
|
+
resourceId: userId,
|
|
560
|
+
...context(c),
|
|
561
|
+
metadata: {
|
|
562
|
+
reason,
|
|
563
|
+
disclosure: true,
|
|
564
|
+
capabilities: entries.map((entry) => `${entry.capability}:${entry.state}`)
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
return c.json(exportView(userId, exportedAt, entries));
|
|
568
|
+
});
|
|
304
569
|
};
|
|
305
570
|
}
|
|
306
571
|
//#endregion
|
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,32 @@ 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): `POST {base}/admin/users/:userId/suspend` and `/reinstate` (#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):
|
|
95
|
+
* `DELETE {base}/admin/users/:userId` and `POST …/restore` (#622). The
|
|
96
|
+
* one scope on this surface whose worst case cannot be undone: once the window closes, the person's rows are
|
|
97
|
+
* gone from every capability that held them.
|
|
98
|
+
*/
|
|
99
|
+
export declare const AUTH_USERS_DELETE_SCOPE: ControlPlaneScope;
|
|
100
|
+
/**
|
|
101
|
+
* Export everything the kit holds about one account — the answer to an access request (#620):
|
|
102
|
+
* `GET {base}/admin/users/:userId/export` (#622). A disclosure, and a far larger one than reading the user: it carries every capability's rows about
|
|
103
|
+
* them, so it is granted on its own and audited as one.
|
|
104
|
+
*/
|
|
105
|
+
export declare const AUTH_USERS_EXPORT_SCOPE: ControlPlaneScope;
|
|
87
106
|
/**
|
|
88
107
|
* Every control-plane scope auth defines — what `pithy dashboard connect` offers for this capability,
|
|
89
108
|
* and the list a manifest or a doc quotes rather than re-typing.
|
|
109
|
+
*
|
|
110
|
+
* **The lifecycle scopes were declared before the routes that require them** (#620): suspend and reinstate
|
|
111
|
+
* landed in #621, delete, restore and export in #622. Declaring the vocabulary here, once, is what let those
|
|
112
|
+
* issues build on one name each rather than each choosing its own.
|
|
90
113
|
*/
|
|
91
114
|
export declare const AUTH_CONTROL_PLANE_SCOPES: readonly ControlPlaneScope[];
|
|
92
115
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/http/guards.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sDAAsD,CAAC;AACvF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AAEH;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,EAAE,iBAAqC,CAAC;AAE1E;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,iBAAuC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,0BAA0B,EAAE,iBAA0C,CAAC;AAEpF;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,iBAAuC,CAAC;AAE9E;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,EAAE,iBAAyC,CAAC;AAElF
|
|
1
|
+
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/http/guards.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sDAAsD,CAAC;AACvF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AAEH;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,EAAE,iBAAqC,CAAC;AAE1E;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,iBAAuC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,0BAA0B,EAAE,iBAA0C,CAAC;AAEpF;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,iBAAuC,CAAC;AAE9E;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,EAAE,iBAAyC,CAAC;AAElF;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,EAAE,iBAAwC,CAAC;AAEhF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,EAAE,iBAAuC,CAAC;AAE9E;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,iBAAuC,CAAC;AAE9E;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,EAAE,SAAS,iBAAiB,EASjE,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,EAAE,CAsE9D"}
|