@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/src/http/adminRoutes.ts
CHANGED
|
@@ -2,13 +2,16 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import type { PithyHonoEnv } from "@pithy-sh/core/src/capability/capability";
|
|
5
|
+
import { purgeExports } from "@pithy-sh/core/src/capability/purge";
|
|
5
6
|
import type { ControlPlaneContext } from "@pithy-sh/core/src/controlPlane/context";
|
|
6
7
|
import { requireControlPlane } from "@pithy-sh/core/src/controlPlane/http/guard";
|
|
7
8
|
import { MAX_PAGE_SIZE } from "@pithy-sh/core/src/data/cursor";
|
|
8
9
|
import { InternalError, NotFoundError } from "@pithy-sh/core/src/error/pithyError";
|
|
9
10
|
import { describeRoute } from "@pithy-sh/core/src/http/routeSpec";
|
|
10
11
|
import { validate } from "@pithy-sh/core/src/http/validation";
|
|
12
|
+
import { workflowBindingFor } from "@pithy-sh/core/src/workflow/dispatch";
|
|
11
13
|
import type { Context, Hono } from "hono";
|
|
14
|
+
import type { z } from "zod";
|
|
12
15
|
import {
|
|
13
16
|
type AdminSubList,
|
|
14
17
|
findSessionById,
|
|
@@ -25,23 +28,66 @@ import { correlation, emitControlPlaneAction } from "../audit/emit";
|
|
|
25
28
|
import type { AuthWiring } from "../capability";
|
|
26
29
|
import { authDatabase } from "../data/tables";
|
|
27
30
|
import { deleteDevice, deviceSessionTokens } from "../device/registry";
|
|
31
|
+
import { auditedLifecycleCall, recordLifecycleRefusal } from "../lifecycle/audited";
|
|
32
|
+
import { MAX_REASON } from "../lifecycle/reason";
|
|
33
|
+
import { lifecycleDeps } from "../lifecycle/request";
|
|
34
|
+
import {
|
|
35
|
+
type DeleteTransition,
|
|
36
|
+
deleteUser,
|
|
37
|
+
type LifecycleActor,
|
|
38
|
+
reinstateUser,
|
|
39
|
+
requirePresent,
|
|
40
|
+
restoreUser,
|
|
41
|
+
suspendUser,
|
|
42
|
+
} from "../lifecycle/transitions";
|
|
43
|
+
import { AUTH_PURGE_BINDING } from "../purge/specs";
|
|
44
|
+
import { startPurgeInstance } from "../purge/start";
|
|
28
45
|
import {
|
|
29
46
|
AUTH_DEVICES_READ_SCOPE,
|
|
30
47
|
AUTH_DEVICES_REVOKE_SCOPE,
|
|
31
48
|
AUTH_SESSIONS_REVOKE_SCOPE,
|
|
49
|
+
AUTH_USERS_DELETE_SCOPE,
|
|
50
|
+
AUTH_USERS_EXPORT_SCOPE,
|
|
32
51
|
AUTH_USERS_LOGOUT_SCOPE,
|
|
33
52
|
AUTH_USERS_READ_SCOPE,
|
|
53
|
+
AUTH_USERS_SUSPEND_SCOPE,
|
|
34
54
|
} from "./guards";
|
|
35
55
|
import { getAuthInstance, resolveDb } from "./resolve";
|
|
36
56
|
import {
|
|
37
57
|
AdminDeviceRevokeResponse,
|
|
38
58
|
AdminDevicesResponse,
|
|
39
59
|
AdminRevokeResponse,
|
|
60
|
+
AdminUserDeleteResponse,
|
|
61
|
+
AdminUserExportResponse,
|
|
62
|
+
AdminUserReinstateResponse,
|
|
40
63
|
AdminUserResponse,
|
|
64
|
+
AdminUserRestoreResponse,
|
|
65
|
+
AdminUserSuspendResponse,
|
|
41
66
|
AdminUsersResponse,
|
|
42
67
|
} from "./responses";
|
|
43
|
-
import {
|
|
44
|
-
|
|
68
|
+
import {
|
|
69
|
+
DeleteUserBody,
|
|
70
|
+
ExportUserQuery,
|
|
71
|
+
ListDevicesQuery,
|
|
72
|
+
ListUsersQuery,
|
|
73
|
+
ReinstateUserBody,
|
|
74
|
+
RestoreUserBody,
|
|
75
|
+
RevokeDeviceBody,
|
|
76
|
+
RevokeSessionBody,
|
|
77
|
+
SuspendUserBody,
|
|
78
|
+
UserIdParam,
|
|
79
|
+
} from "./schemas";
|
|
80
|
+
import {
|
|
81
|
+
deletionView,
|
|
82
|
+
deviceView,
|
|
83
|
+
exportView,
|
|
84
|
+
type PurgeDisposition,
|
|
85
|
+
reinstatementView,
|
|
86
|
+
restorationView,
|
|
87
|
+
sessionView,
|
|
88
|
+
suspensionView,
|
|
89
|
+
userView,
|
|
90
|
+
} from "./views";
|
|
45
91
|
|
|
46
92
|
type Ctx = Context<PithyHonoEnv>;
|
|
47
93
|
|
|
@@ -55,6 +101,11 @@ type Ctx = Context<PithyHonoEnv>;
|
|
|
55
101
|
* POST {base}/admin/sessions/revoke auth:sessions:revoke json: RevokeSessionBody
|
|
56
102
|
* POST {base}/admin/users/:userId/sessions/revoke auth:users:logout param: UserIdParam
|
|
57
103
|
* POST {base}/admin/users/:userId/devices/revoke auth:devices:revoke param + json
|
|
104
|
+
* POST {base}/admin/users/:userId/suspend auth:users:suspend param + json: SuspendUserBody
|
|
105
|
+
* POST {base}/admin/users/:userId/reinstate auth:users:suspend param + json: ReinstateUserBody
|
|
106
|
+
* DELETE {base}/admin/users/:userId auth:users:delete param + json: DeleteUserBody
|
|
107
|
+
* POST {base}/admin/users/:userId/restore auth:users:delete param + json: RestoreUserBody
|
|
108
|
+
* GET {base}/admin/users/:userId/export auth:users:export param + query: ExportUserQuery
|
|
58
109
|
*
|
|
59
110
|
* ## Registered before the catch-all, or dead
|
|
60
111
|
*
|
|
@@ -169,6 +220,122 @@ async function revokeTokens(c: Ctx, wiring: AuthWiring, tokens: readonly string[
|
|
|
169
220
|
return tokens.length;
|
|
170
221
|
}
|
|
171
222
|
|
|
223
|
+
/**
|
|
224
|
+
* What a lifecycle route hands `../lifecycle/`: the transitions' dependencies, with the request's correlation
|
|
225
|
+
* and the connection on every audit event they write, and the operator as the actor.
|
|
226
|
+
*
|
|
227
|
+
* The actor is the token's verified `sub` — which person at the dashboard — the same id every other admin
|
|
228
|
+
* event records, so the trail and the history table name the same somebody.
|
|
229
|
+
*/
|
|
230
|
+
function lifecycleCall(c: Ctx, wiring: AuthWiring, caller: ControlPlaneContext, userId: string, reason: string) {
|
|
231
|
+
const correlated = context(c);
|
|
232
|
+
return {
|
|
233
|
+
deps: lifecycleDeps(c, wiring, { ...correlated, metadata: { connectionId: caller.connectionId } }),
|
|
234
|
+
actor: { type: "control-plane", id: caller.subject } satisfies LifecycleActor,
|
|
235
|
+
// Named so the refusal for a deleted account says which route comes first.
|
|
236
|
+
restoreRoute: `POST ${wiring.config.basePath}/admin/users/${userId}/restore`,
|
|
237
|
+
audit: { subject: caller.subject, connectionId: caller.connectionId, userId, reason, ...correlated },
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* A lifecycle route's body validator, which audits the bodies it refuses (#621). Every call is on the trail,
|
|
243
|
+
* a refusal included, and a body the validator turns away never reaches `auditedLifecycleCall`. So it is
|
|
244
|
+
* recorded here, as `denied` under the route's own action, with whatever reason the body carried — cut to the
|
|
245
|
+
* reason's own limit, so a refused essay is not stored whole — and the 400 is thrown as before.
|
|
246
|
+
*/
|
|
247
|
+
function lifecycleBody<Schema extends z.ZodType>(schema: Schema, action: string | ((body: unknown) => string)) {
|
|
248
|
+
return validate("json", schema, async (raw, error, body) => {
|
|
249
|
+
// The route validated its `:userId` before its body, so it is readable here.
|
|
250
|
+
const c = raw as Context<PithyHonoEnv, string, { out: { param: z.output<typeof UserIdParam> } }>;
|
|
251
|
+
const caller = controlPlaneCaller(c);
|
|
252
|
+
const reason = body && typeof body === "object" ? (body as { reason?: unknown }).reason : undefined;
|
|
253
|
+
await recordLifecycleRefusal(
|
|
254
|
+
c.var.emit,
|
|
255
|
+
{
|
|
256
|
+
action: typeof action === "string" ? action : action(body),
|
|
257
|
+
subject: caller.subject,
|
|
258
|
+
connectionId: caller.connectionId,
|
|
259
|
+
userId: c.req.valid("param").userId,
|
|
260
|
+
reason: typeof reason === "string" ? reason.slice(0, MAX_REASON) : null,
|
|
261
|
+
...context(c),
|
|
262
|
+
},
|
|
263
|
+
error,
|
|
264
|
+
// Hono's own refusal of a body that is not JSON carries no code; the client sees this one.
|
|
265
|
+
{ code: "validation/invalid_input", outcome: "denied" },
|
|
266
|
+
);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* End every session an account holds, after its columns are written — never before, for the reason the suspend
|
|
272
|
+
* route gives: enforcement refuses the account the moment the columns land, and revoking first would leave a
|
|
273
|
+
* window for a sign-in the revocation never saw. Through Better Auth's adapter, so `onSessionRevoked` hears
|
|
274
|
+
* each, and recorded under `auth/admin_user_sessions_revoked` with the cause.
|
|
275
|
+
*/
|
|
276
|
+
async function endSessions(
|
|
277
|
+
c: Ctx,
|
|
278
|
+
wiring: AuthWiring,
|
|
279
|
+
caller: ControlPlaneContext,
|
|
280
|
+
userId: string,
|
|
281
|
+
cause: string,
|
|
282
|
+
): Promise<number> {
|
|
283
|
+
const revoked = await revokeTokens(c, wiring, await userSessionTokens(db(c, wiring), userId));
|
|
284
|
+
await emitControlPlaneAction(c.var.emit, {
|
|
285
|
+
action: AuthAuditActions.adminUserSessionsRevoked,
|
|
286
|
+
subject: caller.subject,
|
|
287
|
+
connectionId: caller.connectionId,
|
|
288
|
+
resourceType: "user",
|
|
289
|
+
resourceId: userId,
|
|
290
|
+
...context(c),
|
|
291
|
+
metadata: { revoked, cause },
|
|
292
|
+
});
|
|
293
|
+
return revoked;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Start the purge of one account now (#622): `started` when this Worker binds the purge Workflow and it took the
|
|
298
|
+
* instance — or already held it — and `pending` when it binds none or the start failed; the account is due
|
|
299
|
+
* either way, and the daily pass starts it. A failed start is not this call's failure: the deletion is already
|
|
300
|
+
* written, and a 500 over it would invite a retry that finds the account deleted and says nothing new.
|
|
301
|
+
*
|
|
302
|
+
* Under the instance id the deletion names (`../purge/start.ts`), so a hard deletion repeated, or one the daily
|
|
303
|
+
* pass also finds due, is the one purge and never a second.
|
|
304
|
+
*/
|
|
305
|
+
async function startPurge(
|
|
306
|
+
c: Ctx,
|
|
307
|
+
result: DeleteTransition,
|
|
308
|
+
userId: string,
|
|
309
|
+
notify: boolean | undefined,
|
|
310
|
+
): Promise<PurgeDisposition> {
|
|
311
|
+
const log = c.var.log.child("auth");
|
|
312
|
+
const binding = workflowBindingFor(c.env as Record<string, unknown>, AUTH_PURGE_BINDING);
|
|
313
|
+
if (!binding) {
|
|
314
|
+
log.warn("a deletion found no purge Workflow bound; the purge is pending", { userId });
|
|
315
|
+
return "pending";
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
await startPurgeInstance(binding, {
|
|
319
|
+
userId,
|
|
320
|
+
deletedAt: result.deletedAt.getTime(),
|
|
321
|
+
...(notify === undefined ? {} : { notify }),
|
|
322
|
+
at: new Date(),
|
|
323
|
+
});
|
|
324
|
+
return "started";
|
|
325
|
+
} catch (error) {
|
|
326
|
+
log.warn("the purge could not be started; the daily pass will start it", {
|
|
327
|
+
userId,
|
|
328
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
329
|
+
});
|
|
330
|
+
return "pending";
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** What becomes of a soft deletion's purge: due later, or — when its window has already closed — due now. */
|
|
335
|
+
function scheduledOrDue(result: DeleteTransition): PurgeDisposition | "due" {
|
|
336
|
+
return result.purgeAfter.getTime() > Date.now() ? "scheduled" : "due";
|
|
337
|
+
}
|
|
338
|
+
|
|
172
339
|
/**
|
|
173
340
|
* Register the control-plane admin routes. Called by `createAuthRoutes` **before** the Better Auth
|
|
174
341
|
* catch-all; see the file header for why that ordering is load-bearing.
|
|
@@ -400,5 +567,234 @@ export function registerAuthAdminRoutes(wiring: AuthWiring): (app: Hono<PithyHon
|
|
|
400
567
|
return c.json({ revoked, removed } satisfies AdminDeviceRevokeResponse);
|
|
401
568
|
},
|
|
402
569
|
);
|
|
570
|
+
|
|
571
|
+
app.post(
|
|
572
|
+
`${base}/admin/users/:userId/suspend`,
|
|
573
|
+
describeRoute({
|
|
574
|
+
summary: "Suspend one user, with a reason: their sessions end and every sign-in is refused.",
|
|
575
|
+
verification: "control-plane",
|
|
576
|
+
scopes: [AUTH_USERS_SUSPEND_SCOPE],
|
|
577
|
+
responses: { 200: AdminUserSuspendResponse },
|
|
578
|
+
}),
|
|
579
|
+
requireControlPlane(AUTH_USERS_SUSPEND_SCOPE),
|
|
580
|
+
validate("param", UserIdParam),
|
|
581
|
+
lifecycleBody(SuspendUserBody, AuthAuditActions.adminUserSuspended),
|
|
582
|
+
async (c) => {
|
|
583
|
+
const { userId } = c.req.valid("param");
|
|
584
|
+
const { reason, until, notify } = c.req.valid("json");
|
|
585
|
+
const caller = controlPlaneCaller(c);
|
|
586
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
587
|
+
|
|
588
|
+
// Every call is audited: the transition audits a suspension it writes, and this records the refusals.
|
|
589
|
+
// A suspension always writes — a fresh one or a restatement — so there is no no-op to record.
|
|
590
|
+
const result = await auditedLifecycleCall(
|
|
591
|
+
c.var.emit,
|
|
592
|
+
{ action: AuthAuditActions.adminUserSuspended, ...call.audit },
|
|
593
|
+
() =>
|
|
594
|
+
suspendUser(call.deps, {
|
|
595
|
+
userId,
|
|
596
|
+
actor: call.actor,
|
|
597
|
+
reason,
|
|
598
|
+
until: until ?? null,
|
|
599
|
+
...(notify === undefined ? {} : { notify }),
|
|
600
|
+
restoreRoute: call.restoreRoute,
|
|
601
|
+
}),
|
|
602
|
+
() => false,
|
|
603
|
+
);
|
|
604
|
+
|
|
605
|
+
// After the columns, never before. Enforcement refuses the account the moment they are written, so a
|
|
606
|
+
// session alive in between meets `auth/account_suspended`; revoking first would leave a window where a
|
|
607
|
+
// sign-in could mint one the revocation never saw. Every call revokes, a restatement included, so a
|
|
608
|
+
// credential that slipped past an earlier one does not outlive this one. Through Better Auth's adapter,
|
|
609
|
+
// so `onSessionRevoked` hears each. A refresh token is a session token here, so this ends both.
|
|
610
|
+
const revoked = await revokeTokens(c, wiring, await userSessionTokens(db(c, wiring), userId));
|
|
611
|
+
await emitControlPlaneAction(c.var.emit, {
|
|
612
|
+
action: AuthAuditActions.adminUserSessionsRevoked,
|
|
613
|
+
subject: caller.subject,
|
|
614
|
+
connectionId: caller.connectionId,
|
|
615
|
+
resourceType: "user",
|
|
616
|
+
resourceId: userId,
|
|
617
|
+
...context(c),
|
|
618
|
+
metadata: { revoked, cause: "suspended" },
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
return c.json(suspensionView(userId, result, revoked) satisfies AdminUserSuspendResponse);
|
|
622
|
+
},
|
|
623
|
+
);
|
|
624
|
+
|
|
625
|
+
app.post(
|
|
626
|
+
`${base}/admin/users/:userId/reinstate`,
|
|
627
|
+
describeRoute({
|
|
628
|
+
summary: "Lift one user's suspension, with a reason. They sign in again; no session is restored.",
|
|
629
|
+
verification: "control-plane",
|
|
630
|
+
scopes: [AUTH_USERS_SUSPEND_SCOPE],
|
|
631
|
+
responses: { 200: AdminUserReinstateResponse },
|
|
632
|
+
}),
|
|
633
|
+
requireControlPlane(AUTH_USERS_SUSPEND_SCOPE),
|
|
634
|
+
validate("param", UserIdParam),
|
|
635
|
+
lifecycleBody(ReinstateUserBody, AuthAuditActions.adminUserReinstated),
|
|
636
|
+
async (c) => {
|
|
637
|
+
const { userId } = c.req.valid("param");
|
|
638
|
+
const { reason, notify } = c.req.valid("json");
|
|
639
|
+
const caller = controlPlaneCaller(c);
|
|
640
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
641
|
+
|
|
642
|
+
// An account with no suspension in force is a 200 that changed nothing — the state the caller asked
|
|
643
|
+
// for already holds — and is audited as exactly that.
|
|
644
|
+
const result = await auditedLifecycleCall(
|
|
645
|
+
c.var.emit,
|
|
646
|
+
{ action: AuthAuditActions.adminUserReinstated, ...call.audit },
|
|
647
|
+
() =>
|
|
648
|
+
reinstateUser(call.deps, {
|
|
649
|
+
userId,
|
|
650
|
+
actor: call.actor,
|
|
651
|
+
reason,
|
|
652
|
+
...(notify === undefined ? {} : { notify }),
|
|
653
|
+
restoreRoute: call.restoreRoute,
|
|
654
|
+
}),
|
|
655
|
+
(outcome) => !outcome.changed,
|
|
656
|
+
);
|
|
657
|
+
|
|
658
|
+
return c.json(reinstatementView(userId, result) satisfies AdminUserReinstateResponse);
|
|
659
|
+
},
|
|
660
|
+
);
|
|
661
|
+
|
|
662
|
+
app.delete(
|
|
663
|
+
`${base}/admin/users/:userId`,
|
|
664
|
+
describeRoute({
|
|
665
|
+
summary:
|
|
666
|
+
"Delete one user, with a reason: soft by default, restorable until the purge window closes; hard when named, purged now.",
|
|
667
|
+
verification: "control-plane",
|
|
668
|
+
scopes: [AUTH_USERS_DELETE_SCOPE],
|
|
669
|
+
responses: { 200: AdminUserDeleteResponse },
|
|
670
|
+
}),
|
|
671
|
+
requireControlPlane(AUTH_USERS_DELETE_SCOPE),
|
|
672
|
+
validate("param", UserIdParam),
|
|
673
|
+
// A refused body is recorded under the mode it asked for, so a malformed hard deletion is on the trail as
|
|
674
|
+
// an attempt at one. Only the exact `"hard"` reads as hard, as only it parses as hard.
|
|
675
|
+
lifecycleBody(DeleteUserBody, (body) =>
|
|
676
|
+
(body as { mode?: unknown } | null)?.mode === "hard"
|
|
677
|
+
? AuthAuditActions.adminUserHardDeleted
|
|
678
|
+
: AuthAuditActions.adminUserSoftDeleted,
|
|
679
|
+
),
|
|
680
|
+
async (c) => {
|
|
681
|
+
const { userId } = c.req.valid("param");
|
|
682
|
+
const { mode, reason, notify } = c.req.valid("json");
|
|
683
|
+
const caller = controlPlaneCaller(c);
|
|
684
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
685
|
+
const action = mode === "hard" ? AuthAuditActions.adminUserHardDeleted : AuthAuditActions.adminUserSoftDeleted;
|
|
686
|
+
|
|
687
|
+
// Every call is audited: the transition audits a deletion it writes, this records the refusals and the
|
|
688
|
+
// call that found the account already deleted.
|
|
689
|
+
const result = await auditedLifecycleCall(
|
|
690
|
+
c.var.emit,
|
|
691
|
+
{ action, ...call.audit },
|
|
692
|
+
() =>
|
|
693
|
+
deleteUser(call.deps, {
|
|
694
|
+
userId,
|
|
695
|
+
actor: call.actor,
|
|
696
|
+
reason,
|
|
697
|
+
mode,
|
|
698
|
+
...(notify === undefined ? {} : { notify }),
|
|
699
|
+
}),
|
|
700
|
+
(outcome) => outcome.outcome === "unchanged",
|
|
701
|
+
);
|
|
702
|
+
|
|
703
|
+
const sessionsRevoked = await endSessions(c, wiring, caller, userId, "deleted");
|
|
704
|
+
// A hard deletion starts its purge here. A soft one waits for the daily pass — unless its window has
|
|
705
|
+
// already closed, in which case it is due now and is started like a hard one.
|
|
706
|
+
const disposition = mode === "hard" ? "due" : scheduledOrDue(result);
|
|
707
|
+
const purge =
|
|
708
|
+
disposition === "due"
|
|
709
|
+
? await startPurge(c, result, userId, mode === "hard" ? notify : undefined)
|
|
710
|
+
: disposition;
|
|
711
|
+
return c.json(deletionView(userId, mode, result, purge, sessionsRevoked) satisfies AdminUserDeleteResponse);
|
|
712
|
+
},
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
app.post(
|
|
716
|
+
`${base}/admin/users/:userId/restore`,
|
|
717
|
+
describeRoute({
|
|
718
|
+
summary:
|
|
719
|
+
"Restore one deleted user inside the purge window, with a reason. They sign in again; no session is restored.",
|
|
720
|
+
verification: "control-plane",
|
|
721
|
+
scopes: [AUTH_USERS_DELETE_SCOPE],
|
|
722
|
+
responses: { 200: AdminUserRestoreResponse },
|
|
723
|
+
}),
|
|
724
|
+
requireControlPlane(AUTH_USERS_DELETE_SCOPE),
|
|
725
|
+
validate("param", UserIdParam),
|
|
726
|
+
lifecycleBody(RestoreUserBody, AuthAuditActions.adminUserRestored),
|
|
727
|
+
async (c) => {
|
|
728
|
+
const { userId } = c.req.valid("param");
|
|
729
|
+
const { reason } = c.req.valid("json");
|
|
730
|
+
const caller = controlPlaneCaller(c);
|
|
731
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
732
|
+
|
|
733
|
+
// After the purge there is no account, and the transition's 404 says the data is gone. A window that has
|
|
734
|
+
// closed is a conflict naming when. Both are refusals, and both are on the trail.
|
|
735
|
+
const result = await auditedLifecycleCall(
|
|
736
|
+
c.var.emit,
|
|
737
|
+
{ action: AuthAuditActions.adminUserRestored, ...call.audit },
|
|
738
|
+
() => restoreUser(call.deps, { userId, actor: call.actor, reason }),
|
|
739
|
+
(outcome) => !outcome.changed,
|
|
740
|
+
);
|
|
741
|
+
|
|
742
|
+
return c.json(restorationView(userId, result) satisfies AdminUserRestoreResponse);
|
|
743
|
+
},
|
|
744
|
+
);
|
|
745
|
+
|
|
746
|
+
app.get(
|
|
747
|
+
`${base}/admin/users/:userId/export`,
|
|
748
|
+
describeRoute({
|
|
749
|
+
summary: "Export everything the kit holds about one user, from every composed capability, as one document.",
|
|
750
|
+
description:
|
|
751
|
+
"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.",
|
|
752
|
+
verification: "control-plane",
|
|
753
|
+
scopes: [AUTH_USERS_EXPORT_SCOPE],
|
|
754
|
+
responses: { 200: AdminUserExportResponse },
|
|
755
|
+
}),
|
|
756
|
+
requireControlPlane(AUTH_USERS_EXPORT_SCOPE),
|
|
757
|
+
validate("param", UserIdParam),
|
|
758
|
+
validate("query", ExportUserQuery),
|
|
759
|
+
async (c) => {
|
|
760
|
+
const { userId } = c.req.valid("param");
|
|
761
|
+
const { reason } = c.req.valid("query");
|
|
762
|
+
const caller = controlPlaneCaller(c);
|
|
763
|
+
const call = lifecycleCall(c, wiring, caller, userId, reason);
|
|
764
|
+
const exportedAt = new Date();
|
|
765
|
+
|
|
766
|
+
// The account first, so an unknown or purged one is a 404 — the purged one saying its data is gone —
|
|
767
|
+
// and never a document of empty parts. Then every composed capability's part, each guarded on its own.
|
|
768
|
+
const entries = await auditedLifecycleCall(
|
|
769
|
+
c.var.emit,
|
|
770
|
+
{ action: AuthAuditActions.adminUserExported, ...call.audit },
|
|
771
|
+
async () => {
|
|
772
|
+
await requirePresent(db(c, wiring), userId);
|
|
773
|
+
return purgeExports(wiring.purgeSteps ?? [], {
|
|
774
|
+
subject: { type: "user", id: userId },
|
|
775
|
+
env: c.env as Record<string, unknown>,
|
|
776
|
+
});
|
|
777
|
+
},
|
|
778
|
+
() => false,
|
|
779
|
+
);
|
|
780
|
+
|
|
781
|
+
// A disclosure, recorded with who asked, why, and which parts answered — never with any of the data.
|
|
782
|
+
await emitControlPlaneAction(c.var.emit, {
|
|
783
|
+
action: AuthAuditActions.adminUserExported,
|
|
784
|
+
subject: caller.subject,
|
|
785
|
+
connectionId: caller.connectionId,
|
|
786
|
+
resourceType: "user",
|
|
787
|
+
resourceId: userId,
|
|
788
|
+
...context(c),
|
|
789
|
+
metadata: {
|
|
790
|
+
reason,
|
|
791
|
+
disclosure: true,
|
|
792
|
+
capabilities: entries.map((entry) => `${entry.capability}:${entry.state}`),
|
|
793
|
+
},
|
|
794
|
+
});
|
|
795
|
+
|
|
796
|
+
return c.json(exportView(userId, exportedAt, entries) satisfies AdminUserExportResponse);
|
|
797
|
+
},
|
|
798
|
+
);
|
|
403
799
|
};
|
|
404
800
|
}
|
package/src/http/guards.ts
CHANGED
|
@@ -29,7 +29,7 @@ import type { ControlPlaneScope } from "@pithy-sh/core/src/controlPlane/scope/sc
|
|
|
29
29
|
* every legitimate management call, permanently, and **no credential could fix it** — there is no user
|
|
30
30
|
* to sign in as. `controlPlaneIsolation.workers.test.ts` pins both directions of that separation.
|
|
31
31
|
*
|
|
32
|
-
* ##
|
|
32
|
+
* ## Eight scopes, because these are eight different blast radii
|
|
33
33
|
*
|
|
34
34
|
* The temptation is one `auth:admin` flag, and on this capability it is the most dangerous version of
|
|
35
35
|
* that mistake. **Reading a user is a privacy operation; revoking their sessions is an availability
|
|
@@ -92,9 +92,35 @@ export const AUTH_USERS_LOGOUT_SCOPE: ControlPlaneScope = "auth:users:logout";
|
|
|
92
92
|
*/
|
|
93
93
|
export const AUTH_DEVICES_REVOKE_SCOPE: ControlPlaneScope = "auth:devices:revoke";
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Suspend an account and reinstate it (#620): `POST {base}/admin/users/:userId/suspend` and `/reinstate` (#621). Ending somebody's access is an availability
|
|
97
|
+
* operation on one person, and reversible — which is what separates it from deleting them, and why it is
|
|
98
|
+
* granted apart from both reading them and erasing them.
|
|
99
|
+
*/
|
|
100
|
+
export const AUTH_USERS_SUSPEND_SCOPE: ControlPlaneScope = "auth:users:suspend";
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Delete an account, restore one inside its purge window, and start a purge (#620):
|
|
104
|
+
* `DELETE {base}/admin/users/:userId` and `POST …/restore` (#622). The
|
|
105
|
+
* one scope on this surface whose worst case cannot be undone: once the window closes, the person's rows are
|
|
106
|
+
* gone from every capability that held them.
|
|
107
|
+
*/
|
|
108
|
+
export const AUTH_USERS_DELETE_SCOPE: ControlPlaneScope = "auth:users:delete";
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Export everything the kit holds about one account — the answer to an access request (#620):
|
|
112
|
+
* `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
|
|
113
|
+
* them, so it is granted on its own and audited as one.
|
|
114
|
+
*/
|
|
115
|
+
export const AUTH_USERS_EXPORT_SCOPE: ControlPlaneScope = "auth:users:export";
|
|
116
|
+
|
|
95
117
|
/**
|
|
96
118
|
* Every control-plane scope auth defines — what `pithy dashboard connect` offers for this capability,
|
|
97
119
|
* and the list a manifest or a doc quotes rather than re-typing.
|
|
120
|
+
*
|
|
121
|
+
* **The lifecycle scopes were declared before the routes that require them** (#620): suspend and reinstate
|
|
122
|
+
* landed in #621, delete, restore and export in #622. Declaring the vocabulary here, once, is what let those
|
|
123
|
+
* issues build on one name each rather than each choosing its own.
|
|
98
124
|
*/
|
|
99
125
|
export const AUTH_CONTROL_PLANE_SCOPES: readonly ControlPlaneScope[] = [
|
|
100
126
|
AUTH_USERS_READ_SCOPE,
|
|
@@ -102,6 +128,9 @@ export const AUTH_CONTROL_PLANE_SCOPES: readonly ControlPlaneScope[] = [
|
|
|
102
128
|
AUTH_SESSIONS_REVOKE_SCOPE,
|
|
103
129
|
AUTH_USERS_LOGOUT_SCOPE,
|
|
104
130
|
AUTH_DEVICES_REVOKE_SCOPE,
|
|
131
|
+
AUTH_USERS_SUSPEND_SCOPE,
|
|
132
|
+
AUTH_USERS_DELETE_SCOPE,
|
|
133
|
+
AUTH_USERS_EXPORT_SCOPE,
|
|
105
134
|
];
|
|
106
135
|
|
|
107
136
|
/**
|
|
@@ -154,5 +183,36 @@ export function authAdminRoutes(basePath: string): AdminRoute[] {
|
|
|
154
183
|
scope: AUTH_DEVICES_REVOKE_SCOPE,
|
|
155
184
|
summary: "Sign one device out and forget it — for a phone reported lost.",
|
|
156
185
|
},
|
|
186
|
+
{
|
|
187
|
+
method: "POST",
|
|
188
|
+
path: `${basePath}/admin/users/:userId/suspend`,
|
|
189
|
+
scope: AUTH_USERS_SUSPEND_SCOPE,
|
|
190
|
+
summary: "Suspend a user, with a reason. Their sessions end and every sign-in is refused.",
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
method: "POST",
|
|
194
|
+
path: `${basePath}/admin/users/:userId/reinstate`,
|
|
195
|
+
scope: AUTH_USERS_SUSPEND_SCOPE,
|
|
196
|
+
summary: "Lift a suspension, with a reason. They sign in again; no session comes back.",
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
method: "DELETE",
|
|
200
|
+
path: `${basePath}/admin/users/:userId`,
|
|
201
|
+
scope: AUTH_USERS_DELETE_SCOPE,
|
|
202
|
+
summary:
|
|
203
|
+
"Delete a user, with a reason. Soft by default: restorable until the purge window closes. Hard, when named: erased now.",
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
method: "POST",
|
|
207
|
+
path: `${basePath}/admin/users/:userId/restore`,
|
|
208
|
+
scope: AUTH_USERS_DELETE_SCOPE,
|
|
209
|
+
summary: "Restore a deleted user inside the purge window, with a reason. They sign in again.",
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
method: "GET",
|
|
213
|
+
path: `${basePath}/admin/users/:userId/export`,
|
|
214
|
+
scope: AUTH_USERS_EXPORT_SCOPE,
|
|
215
|
+
summary: "Everything the kit holds about one user, from every capability — the answer to an access request.",
|
|
216
|
+
},
|
|
157
217
|
];
|
|
158
218
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Where a suspended or deleted account meets Better Auth's own endpoints (#620).
|
|
8
|
+
*
|
|
9
|
+
* The session middleware refuses such an account an `AuthContext`, which closes every kit route. Better Auth's
|
|
10
|
+
* catch-all is the one place that does not ask: it resolves the session itself, so `/token` would mint a fresh
|
|
11
|
+
* access token for an account the rest of the Worker refuses. `handleBetterAuth` therefore refuses the
|
|
12
|
+
* middleware's verdict here too — everywhere except the endpoints below.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The endpoints that never act as the account the presented credential names, relative to `basePath`.
|
|
17
|
+
*
|
|
18
|
+
* Signing out must always work. And each of the rest *starts* a session rather than using one, so a stale
|
|
19
|
+
* cookie a suspended person left on a shared machine must not stop somebody else signing in — the session
|
|
20
|
+
* that results is checked where it is created, in `session.create.before`. Everything else is refused by
|
|
21
|
+
* default: an endpoint nobody listed may be one a plugin added, and an account that may not act may not act
|
|
22
|
+
* there either.
|
|
23
|
+
*/
|
|
24
|
+
const ADMITTED_PREFIXES = [
|
|
25
|
+
"/sign-out",
|
|
26
|
+
"/sign-in/",
|
|
27
|
+
"/magic-link/verify",
|
|
28
|
+
"/email-otp/",
|
|
29
|
+
"/callback/",
|
|
30
|
+
"/oauth2/callback/",
|
|
31
|
+
] as const;
|
|
32
|
+
const ADMITTED_EXACT = new Set(["/jwks", "/ok", "/error"]);
|
|
33
|
+
|
|
34
|
+
/** Whether a refused account's credential may still reach this Better Auth endpoint (path relative to `basePath`). */
|
|
35
|
+
export function admitsRefusedAccount(path: string): boolean {
|
|
36
|
+
if (ADMITTED_EXACT.has(path)) return true;
|
|
37
|
+
return ADMITTED_PREFIXES.some((prefix) => path === prefix || path.startsWith(prefix));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The redirect a refused magic link answers with: back to the screen the link named, carrying the code as
|
|
42
|
+
* `?error=`, exactly as Better Auth's own `redirectWithError` does for the refusals it raises itself
|
|
43
|
+
* (`plugins/magic-link/index.mjs`). `null` when the target is not one this Worker trusts, and the caller
|
|
44
|
+
* answers the refusal as JSON instead.
|
|
45
|
+
*
|
|
46
|
+
* A browser following a magic link is mid-navigation, so a 403 body would strand it on the API's origin
|
|
47
|
+
* with nothing to read; the adopter's screen is where the copy for this state lives. Better Auth has already
|
|
48
|
+
* checked the URLs by the time a session is refused, so the trust check is defense in depth — a refusal must
|
|
49
|
+
* not become an open redirect.
|
|
50
|
+
*/
|
|
51
|
+
export function lifecycleRedirect(
|
|
52
|
+
requestUrl: URL,
|
|
53
|
+
baseURL: string,
|
|
54
|
+
trustedOrigins: readonly string[],
|
|
55
|
+
refusal: PithyError,
|
|
56
|
+
): Response | null {
|
|
57
|
+
const named = requestUrl.searchParams.get("errorCallbackURL") || requestUrl.searchParams.get("callbackURL") || "/";
|
|
58
|
+
let target: URL;
|
|
59
|
+
try {
|
|
60
|
+
target = new URL(named, baseURL);
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const trusted = new Set([new URL(baseURL).origin, ...trustedOrigins.flatMap((origin) => originOf(origin))]);
|
|
65
|
+
if (!trusted.has(target.origin)) return null;
|
|
66
|
+
target.searchParams.set("error", refusal.payload.code);
|
|
67
|
+
return new Response(null, { status: 302, headers: { location: target.toString() } });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** An origin's canonical form, or nothing for an entry that is not a URL (a bare deep-link scheme). */
|
|
71
|
+
function originOf(value: string): string[] {
|
|
72
|
+
try {
|
|
73
|
+
const origin = new URL(value).origin;
|
|
74
|
+
return origin === "null" ? [] : [origin];
|
|
75
|
+
} catch {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
}
|