@pithy-sh/auth 0.9.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 +8 -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/capability.d.ts +26 -1
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +23 -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 +28 -2
- 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/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 +47 -6
- 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/routes.d.ts.map +1 -1
- package/dist/http/routes.js +20 -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/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/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 +2 -1
- package/dist/migrations/set.d.ts.map +1 -1
- package/dist/migrations/set.js +4 -2
- package/dist/peer.d.ts +4 -0
- package/dist/peer.d.ts.map +1 -1
- 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 +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/package.json +5 -5
- package/pithy.manifest.json +17 -3
- package/src/audit/actions.ts +23 -0
- package/src/audit/emit.ts +1 -1
- package/src/capability.ts +125 -54
- 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 +80 -0
- package/src/http/guards.ts +29 -1
- package/src/http/lifecycleGate.ts +78 -0
- package/src/http/middleware.ts +61 -2
- package/src/http/providerRefusal.ts +10 -0
- package/src/http/resolve.ts +8 -3
- package/src/http/routes.ts +38 -1
- package/src/instance/auth.ts +69 -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/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/0004_user_lifecycle.ts +68 -0
- package/src/migrations/set.ts +3 -1
- package/src/test-utils/liveApp.ts +2 -1
- package/src/test-utils/scopedBackend.ts +2 -1
- package/src/version.generated.ts +1 -1
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
4
|
+
import type { AuditEmit } from "@pithy-sh/core/src/audit/recorder";
|
|
5
|
+
import type { Logger } from "@pithy-sh/core/src/logger/logger";
|
|
6
|
+
import type { EnqueueInput } from "@pithy-sh/email/src/send/enqueue";
|
|
7
|
+
import type { AuthConfig } from "../capability";
|
|
8
|
+
import type { AuthDatabase } from "../data/tables";
|
|
9
|
+
import type { UserLifecycleActorType } from "../data/userLifecycle";
|
|
10
|
+
import type { UserLifecycleHooks } from "./hooks";
|
|
11
|
+
/**
|
|
12
|
+
* The account-lifecycle transitions — the one place an account's state is ever changed (#620).
|
|
13
|
+
*
|
|
14
|
+
* The admin routes (#621, #622), the self-service deletion, and the purge Workflow call these and write
|
|
15
|
+
* nothing themselves, so each transition happens the same way whoever asked for it:
|
|
16
|
+
*
|
|
17
|
+
* 1. **The columns move, conditionally, and the history row is written with them.** The `update` names the
|
|
18
|
+
* state it expects to find, so of two concurrent suspensions exactly one changes a row, and only that one
|
|
19
|
+
* goes on. A request that finds the account already where it asked to put it is `changed: false` and does
|
|
20
|
+
* nothing else — no row, no mail, no hook — which is what "once per transition" means.
|
|
21
|
+
* 2. **The row in `pithy_auth_user_lifecycle`** — the actor, the reason, the time — is the second statement of
|
|
22
|
+
* the same `d1.batch()`, which D1 runs as one transaction, and it is written only when the first moved the
|
|
23
|
+
* account. Two awaits would leave a gap: a failed insert after a committed update, and a retry that finds
|
|
24
|
+
* the account already moved, returns `changed: false`, and loses the row, the event, the mail and the hook.
|
|
25
|
+
* 3. **The audit event is emitted** — the trail answers who and from where; the row answers what happened.
|
|
26
|
+
* 4. **The mail is enqueued**, when the config sends it, in the account's own language.
|
|
27
|
+
* 5. **The hook fires.**
|
|
28
|
+
*
|
|
29
|
+
* Steps 3 to 5 report something that has already happened, so none of them can undo it: each swallows its own
|
|
30
|
+
* failure, and a throwing hook or an email binding that is not there is logged and the transition stands.
|
|
31
|
+
*
|
|
32
|
+
* **What these do not do is end sessions.** Enforcement does not need them to — a suspended account's session
|
|
33
|
+
* is refused at every door the moment the column is written (`./enforce.ts`) — and revoking them is a route's
|
|
34
|
+
* decision, made through Better Auth so `onSessionRevoked` hears about it.
|
|
35
|
+
*/
|
|
36
|
+
/** Who made a transition. */
|
|
37
|
+
export interface LifecycleActor {
|
|
38
|
+
/** The kind of principal. */
|
|
39
|
+
readonly type: UserLifecycleActorType;
|
|
40
|
+
/** Its id — the connection's for a management client, the user's own for self-service — or null. */
|
|
41
|
+
readonly id: string | null;
|
|
42
|
+
}
|
|
43
|
+
/** Everything a transition needs, resolved by the caller from its own context. */
|
|
44
|
+
export interface LifecycleDeps {
|
|
45
|
+
/** The auth tables. */
|
|
46
|
+
readonly db: AuthDatabase;
|
|
47
|
+
/** The same database as a binding, for the hooks — see `UserLifecycleListener`. */
|
|
48
|
+
readonly d1: D1Database;
|
|
49
|
+
/** The audit seam. A no-op when the audit capability is absent. */
|
|
50
|
+
readonly emit: AuditEmit;
|
|
51
|
+
/** The resolved config: the purge window, and which mails are sent. */
|
|
52
|
+
readonly config: Pick<AuthConfig, "retention" | "lifecycleEmails">;
|
|
53
|
+
/** The adopter's hooks. */
|
|
54
|
+
readonly hooks: UserLifecycleHooks;
|
|
55
|
+
/** The email capability's enqueue, bound to this run's env. Absent, nothing is mailed. */
|
|
56
|
+
readonly enqueueEmail?: ((input: EnqueueInput) => Promise<unknown>) | undefined;
|
|
57
|
+
/** Where a swallowed failure is reported. Absent, it is swallowed silently. */
|
|
58
|
+
readonly log?: Logger | undefined;
|
|
59
|
+
/** The clock. Defaults to the system's. */
|
|
60
|
+
readonly now?: (() => Date) | undefined;
|
|
61
|
+
}
|
|
62
|
+
/** What a transition did. */
|
|
63
|
+
export interface LifecycleTransition {
|
|
64
|
+
/** Whether the account moved. `false` when it was already where the call asked to put it. */
|
|
65
|
+
readonly changed: boolean;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Suspend an account, until `until` or until it is reinstated.
|
|
69
|
+
*
|
|
70
|
+
* A deleted account cannot be suspended — deletion already shuts it, and outranks a suspension — so that is a
|
|
71
|
+
* conflict rather than a no-op. A suspension whose end has passed is not in force, and a new one replaces it.
|
|
72
|
+
*/
|
|
73
|
+
export declare function suspendUser(deps: LifecycleDeps, input: {
|
|
74
|
+
userId: string;
|
|
75
|
+
actor: LifecycleActor;
|
|
76
|
+
reason: string | null;
|
|
77
|
+
until?: Date | null;
|
|
78
|
+
}): Promise<LifecycleTransition>;
|
|
79
|
+
/**
|
|
80
|
+
* Lift a suspension. An account that is not suspended — including one whose suspension ran out — is a no-op.
|
|
81
|
+
*
|
|
82
|
+
* Where the account stands is read through `accountStanding`, never off the raw column, because a suspension
|
|
83
|
+
* past its end leaves `suspendedAt` set and is not a suspension. A deleted account is a conflict, as it is
|
|
84
|
+
* for `suspendUser`: deletion outranks the suspension it keeps, so lifting that suspension would mail "you can
|
|
85
|
+
* sign in again" to somebody every sign-in still refuses. Restore it first.
|
|
86
|
+
*/
|
|
87
|
+
export declare function reinstateUser(deps: LifecycleDeps, input: {
|
|
88
|
+
userId: string;
|
|
89
|
+
actor: LifecycleActor;
|
|
90
|
+
reason: string | null;
|
|
91
|
+
}): Promise<LifecycleTransition>;
|
|
92
|
+
/**
|
|
93
|
+
* Delete an account, softly: it is shut now, and its data is erased once `retention.purgeAfterDays` have
|
|
94
|
+
* passed. Restorable until then. Audited as the account's own deletion when its holder is the actor.
|
|
95
|
+
*
|
|
96
|
+
* A suspension the account was under is left in place, so restoring the account does not lift it.
|
|
97
|
+
*/
|
|
98
|
+
export declare function deleteUser(deps: LifecycleDeps, input: {
|
|
99
|
+
userId: string;
|
|
100
|
+
actor: LifecycleActor;
|
|
101
|
+
reason: string | null;
|
|
102
|
+
}): Promise<LifecycleTransition>;
|
|
103
|
+
/**
|
|
104
|
+
* Restore a deleted account inside its purge window. After the purge there is no account, and that is a 404
|
|
105
|
+
* whose action says the data is gone.
|
|
106
|
+
*/
|
|
107
|
+
export declare function restoreUser(deps: LifecycleDeps, input: {
|
|
108
|
+
userId: string;
|
|
109
|
+
actor: LifecycleActor;
|
|
110
|
+
reason: string | null;
|
|
111
|
+
}): Promise<LifecycleTransition>;
|
|
112
|
+
/** Record that one purge step — one capability's contribution — erased its rows. */
|
|
113
|
+
export declare function recordPurgeStep(deps: LifecycleDeps, input: {
|
|
114
|
+
userId: string;
|
|
115
|
+
step: string;
|
|
116
|
+
}): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Finish a purge: the last mail, the record, the audit event, the hook.
|
|
119
|
+
*
|
|
120
|
+
* **The address is handed in, not read**, because by the time a purge finishes the user row is gone — the
|
|
121
|
+
* Workflow captures the recipient before its first step and passes it here. Finishing a purge that already
|
|
122
|
+
* finished is a no-op, so a retried Workflow step mails nobody twice.
|
|
123
|
+
*/
|
|
124
|
+
export declare function completePurge(deps: LifecycleDeps, input: {
|
|
125
|
+
userId: string;
|
|
126
|
+
recipient: {
|
|
127
|
+
email: string;
|
|
128
|
+
name: string | null;
|
|
129
|
+
locale: string | null;
|
|
130
|
+
};
|
|
131
|
+
}): Promise<LifecycleTransition>;
|
|
132
|
+
//# sourceMappingURL=transitions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transitions.d.ts","sourceRoot":"","sources":["../../src/lifecycle/transitions.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { AuthAuditActions } from "../audit/actions.js";
|
|
4
|
+
import { LifecycleColumns, accountStanding } from "./state.js";
|
|
5
|
+
import { ConflictError, NotFoundError, ValidationError } from "@pithy-sh/core/src/error/pithyError";
|
|
6
|
+
import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
|
|
7
|
+
import { formattingLocaleOf } from "@pithy-sh/core/src/i18n/locale";
|
|
8
|
+
import { sql } from "kysely";
|
|
9
|
+
//#region src/lifecycle/transitions.ts
|
|
10
|
+
/** The longest reason the kit stores. It is shown to the person, so it is a sentence, not a case file. */
|
|
11
|
+
const MAX_REASON = 500;
|
|
12
|
+
/** A reason, trimmed and bounded, or null when none was given. */
|
|
13
|
+
function reasonOf(reason) {
|
|
14
|
+
if (reason === null || reason === void 0) return null;
|
|
15
|
+
const trimmed = reason.trim();
|
|
16
|
+
if (trimmed === "") return null;
|
|
17
|
+
if (trimmed.length > MAX_REASON) throw new ValidationError({
|
|
18
|
+
message: `A reason is at most ${MAX_REASON} characters.`,
|
|
19
|
+
detail: `lifecycle reason of ${trimmed.length} characters`
|
|
20
|
+
});
|
|
21
|
+
return trimmed;
|
|
22
|
+
}
|
|
23
|
+
async function subjectOf(db, userId) {
|
|
24
|
+
const row = await db.selectFrom("pithyAuthUsers").select([
|
|
25
|
+
"email",
|
|
26
|
+
"name",
|
|
27
|
+
"locale",
|
|
28
|
+
"suspendedAt",
|
|
29
|
+
"suspendedUntil",
|
|
30
|
+
"deletedAt",
|
|
31
|
+
"purgeAfter"
|
|
32
|
+
]).where("id", "=", userId).executeTakeFirst();
|
|
33
|
+
if (!row) throw new NotFoundError({
|
|
34
|
+
message: "No such account.",
|
|
35
|
+
action: "If it was deleted and its purge has run, its data is gone and there is nothing to act on.",
|
|
36
|
+
detail: `lifecycle transition for user ${userId}, which has no row`
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
email: row.email,
|
|
40
|
+
name: row.name,
|
|
41
|
+
locale: row.locale ?? null,
|
|
42
|
+
columns: LifecycleColumns.parse(row)
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** A date written for its reader, in their language — the shape every lifecycle mail carries its dates in. */
|
|
46
|
+
function readable(date, locale) {
|
|
47
|
+
const tag = (locale ? formattingLocaleOf(locale) : null) ?? "en";
|
|
48
|
+
return new Intl.DateTimeFormat(tag, {
|
|
49
|
+
dateStyle: "long",
|
|
50
|
+
timeZone: "UTC"
|
|
51
|
+
}).format(date);
|
|
52
|
+
}
|
|
53
|
+
/** The history row's values, as the table stores them. */
|
|
54
|
+
function encoded(row) {
|
|
55
|
+
return {
|
|
56
|
+
userId: row.userId,
|
|
57
|
+
action: row.action,
|
|
58
|
+
actorType: row.actor.type,
|
|
59
|
+
actorId: row.actor.id,
|
|
60
|
+
reason: row.reason,
|
|
61
|
+
suspendedUntil: row.suspendedUntil ? SQLiteDate.encode(row.suspendedUntil) : null,
|
|
62
|
+
step: row.step ?? null,
|
|
63
|
+
createdAt: SQLiteDate.encode(row.at)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** A history row with no column change to pair it with: a purge step, a finished purge. Not swallowed. */
|
|
67
|
+
async function record(deps, row) {
|
|
68
|
+
await deps.db.insertInto("pithyAuthUserLifecycle").values(encoded(row)).execute();
|
|
69
|
+
}
|
|
70
|
+
/** Bind a compiled statement to D1, so it can join a `batch`. */
|
|
71
|
+
function prepared(d1, compiled) {
|
|
72
|
+
return d1.prepare(compiled.sql).bind(...compiled.parameters);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Move the columns and write the history row as one transaction, and say whether the account moved.
|
|
76
|
+
*
|
|
77
|
+
* The insert is guarded by `changes()`, which SQLite answers for the statement just before it — the update —
|
|
78
|
+
* so a conditional update that matched nothing writes no row. Not swallowed: until both are written, nothing
|
|
79
|
+
* after them runs.
|
|
80
|
+
*/
|
|
81
|
+
async function move(deps, update, row) {
|
|
82
|
+
const v = encoded(row);
|
|
83
|
+
const insert = deps.db.insertInto("pithyAuthUserLifecycle").columns([
|
|
84
|
+
"userId",
|
|
85
|
+
"action",
|
|
86
|
+
"actorType",
|
|
87
|
+
"actorId",
|
|
88
|
+
"reason",
|
|
89
|
+
"suspendedUntil",
|
|
90
|
+
"step",
|
|
91
|
+
"createdAt"
|
|
92
|
+
]).expression(deps.db.selectNoFrom([
|
|
93
|
+
sql.val(v.userId).as("userId"),
|
|
94
|
+
sql.val(v.action).as("action"),
|
|
95
|
+
sql.val(v.actorType).as("actorType"),
|
|
96
|
+
sql.val(v.actorId).as("actorId"),
|
|
97
|
+
sql.val(v.reason).as("reason"),
|
|
98
|
+
sql.val(v.suspendedUntil).as("suspendedUntil"),
|
|
99
|
+
sql.val(v.step).as("step"),
|
|
100
|
+
sql.val(v.createdAt).as("createdAt")
|
|
101
|
+
]).where(sql`changes()`, ">", 0)).compile();
|
|
102
|
+
const [updated] = await deps.d1.batch([prepared(deps.d1, update.compile()), prepared(deps.d1, insert)]);
|
|
103
|
+
return (updated?.meta.changes ?? 0) > 0;
|
|
104
|
+
}
|
|
105
|
+
/** The audit event, swallowed on failure — an audit write never breaks what it records. */
|
|
106
|
+
async function audit(deps, action, userId, actor, metadata) {
|
|
107
|
+
const event = {
|
|
108
|
+
action,
|
|
109
|
+
outcome: "success",
|
|
110
|
+
actorType: actor.type,
|
|
111
|
+
actorId: actor.id,
|
|
112
|
+
resourceType: "user",
|
|
113
|
+
resourceId: userId,
|
|
114
|
+
metadata
|
|
115
|
+
};
|
|
116
|
+
try {
|
|
117
|
+
await deps.emit(event);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
deps.log?.warn(`lifecycle audit ${action} failed; the transition stands`, { reason: messageOf(error) });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** One lifecycle mail, when the config sends it. Swallowed on failure, like the audit write. */
|
|
123
|
+
async function mail(deps, enabled, to, locale, template, payload) {
|
|
124
|
+
if (!enabled || !deps.enqueueEmail) return;
|
|
125
|
+
try {
|
|
126
|
+
await deps.enqueueEmail({
|
|
127
|
+
to,
|
|
128
|
+
template,
|
|
129
|
+
payload,
|
|
130
|
+
...locale ? { locale } : {}
|
|
131
|
+
});
|
|
132
|
+
} catch (error) {
|
|
133
|
+
deps.log?.warn(`lifecycle mail ${template} could not be enqueued; the transition stands`, { reason: messageOf(error) });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** Fire one hook. Swallowed and logged: the transition is already written, and a retry would fire nothing. */
|
|
137
|
+
async function fire(deps, name, hook, event) {
|
|
138
|
+
if (!hook) return;
|
|
139
|
+
try {
|
|
140
|
+
await hook(event, deps.d1);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
deps.log?.warn(`${name} threw; the transition stands`, {
|
|
143
|
+
userId: event.userId,
|
|
144
|
+
reason: messageOf(error)
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function messageOf(error) {
|
|
149
|
+
return error instanceof Error ? error.message : String(error);
|
|
150
|
+
}
|
|
151
|
+
/** Without a name, a greeting says "Hi," rather than greeting an empty string. */
|
|
152
|
+
function named(name) {
|
|
153
|
+
return name.trim() ? { name } : {};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Suspend an account, until `until` or until it is reinstated.
|
|
157
|
+
*
|
|
158
|
+
* A deleted account cannot be suspended — deletion already shuts it, and outranks a suspension — so that is a
|
|
159
|
+
* conflict rather than a no-op. A suspension whose end has passed is not in force, and a new one replaces it.
|
|
160
|
+
*/
|
|
161
|
+
async function suspendUser(deps, input) {
|
|
162
|
+
const at = (deps.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
163
|
+
const reason = reasonOf(input.reason);
|
|
164
|
+
const until = input.until ?? null;
|
|
165
|
+
if (until && until.getTime() <= at.getTime()) throw new ValidationError({ message: "A suspension must end in the future, or not at all." });
|
|
166
|
+
const subject = await subjectOf(deps.db, input.userId);
|
|
167
|
+
const standing = accountStanding(subject.columns, at);
|
|
168
|
+
if (standing.state === "deleted") throw new ConflictError({
|
|
169
|
+
message: "This account is deleted.",
|
|
170
|
+
action: "Restore it first, if it should be suspended instead."
|
|
171
|
+
});
|
|
172
|
+
if (standing.state === "suspended") return { changed: false };
|
|
173
|
+
if (!await move(deps, deps.db.updateTable("pithyAuthUsers").set({
|
|
174
|
+
suspendedAt: SQLiteDate.encode(at),
|
|
175
|
+
suspendedUntil: until ? SQLiteDate.encode(until) : null
|
|
176
|
+
}).where("id", "=", input.userId).where("deletedAt", "is", null).where((eb) => eb.or([eb("suspendedAt", "is", null), eb.and([eb("suspendedUntil", "is not", null), eb("suspendedUntil", "<=", SQLiteDate.encode(at))])])), {
|
|
177
|
+
userId: input.userId,
|
|
178
|
+
action: "suspended",
|
|
179
|
+
actor: input.actor,
|
|
180
|
+
reason,
|
|
181
|
+
at,
|
|
182
|
+
suspendedUntil: until
|
|
183
|
+
})) return { changed: false };
|
|
184
|
+
await audit(deps, AuthAuditActions.adminUserSuspended, input.userId, input.actor, {
|
|
185
|
+
reason,
|
|
186
|
+
suspendedUntil: until?.toISOString() ?? null
|
|
187
|
+
});
|
|
188
|
+
await mail(deps, deps.config.lifecycleEmails.accountSuspended, subject.email, subject.locale, "accountSuspended", {
|
|
189
|
+
...named(subject.name),
|
|
190
|
+
...reason ? { reason } : {},
|
|
191
|
+
...until ? { until: readable(until, subject.locale) } : {}
|
|
192
|
+
});
|
|
193
|
+
await fire(deps, "onUserSuspended", deps.hooks.onUserSuspended, {
|
|
194
|
+
userId: input.userId,
|
|
195
|
+
actor: input.actor,
|
|
196
|
+
reason,
|
|
197
|
+
at,
|
|
198
|
+
suspendedUntil: until
|
|
199
|
+
});
|
|
200
|
+
return { changed: true };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Lift a suspension. An account that is not suspended — including one whose suspension ran out — is a no-op.
|
|
204
|
+
*
|
|
205
|
+
* Where the account stands is read through `accountStanding`, never off the raw column, because a suspension
|
|
206
|
+
* past its end leaves `suspendedAt` set and is not a suspension. A deleted account is a conflict, as it is
|
|
207
|
+
* for `suspendUser`: deletion outranks the suspension it keeps, so lifting that suspension would mail "you can
|
|
208
|
+
* sign in again" to somebody every sign-in still refuses. Restore it first.
|
|
209
|
+
*/
|
|
210
|
+
async function reinstateUser(deps, input) {
|
|
211
|
+
const at = (deps.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
212
|
+
const reason = reasonOf(input.reason);
|
|
213
|
+
const subject = await subjectOf(deps.db, input.userId);
|
|
214
|
+
const standing = accountStanding(subject.columns, at);
|
|
215
|
+
if (standing.state === "deleted") throw new ConflictError({
|
|
216
|
+
message: "This account is deleted.",
|
|
217
|
+
action: "Restore it first. Its suspension stays in place through a restore, and can be lifted after."
|
|
218
|
+
});
|
|
219
|
+
if (standing.state !== "suspended") return { changed: false };
|
|
220
|
+
if (!await move(deps, deps.db.updateTable("pithyAuthUsers").set({
|
|
221
|
+
suspendedAt: null,
|
|
222
|
+
suspendedUntil: null
|
|
223
|
+
}).where("id", "=", input.userId).where("deletedAt", "is", null).where("suspendedAt", "is not", null).where((eb) => eb.or([eb("suspendedUntil", "is", null), eb("suspendedUntil", ">", SQLiteDate.encode(at))])), {
|
|
224
|
+
userId: input.userId,
|
|
225
|
+
action: "reinstated",
|
|
226
|
+
actor: input.actor,
|
|
227
|
+
reason,
|
|
228
|
+
at
|
|
229
|
+
})) return { changed: false };
|
|
230
|
+
await audit(deps, AuthAuditActions.adminUserReinstated, input.userId, input.actor, { reason });
|
|
231
|
+
await mail(deps, deps.config.lifecycleEmails.accountReinstated, subject.email, subject.locale, "accountReinstated", named(subject.name));
|
|
232
|
+
await fire(deps, "onUserReinstated", deps.hooks.onUserReinstated, {
|
|
233
|
+
userId: input.userId,
|
|
234
|
+
actor: input.actor,
|
|
235
|
+
reason,
|
|
236
|
+
at
|
|
237
|
+
});
|
|
238
|
+
return { changed: true };
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Delete an account, softly: it is shut now, and its data is erased once `retention.purgeAfterDays` have
|
|
242
|
+
* passed. Restorable until then. Audited as the account's own deletion when its holder is the actor.
|
|
243
|
+
*
|
|
244
|
+
* A suspension the account was under is left in place, so restoring the account does not lift it.
|
|
245
|
+
*/
|
|
246
|
+
async function deleteUser(deps, input) {
|
|
247
|
+
const at = (deps.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
248
|
+
const reason = reasonOf(input.reason);
|
|
249
|
+
const purgeAfter = new Date(at.getTime() + deps.config.retention.purgeAfterDays * 864e5);
|
|
250
|
+
const subject = await subjectOf(deps.db, input.userId);
|
|
251
|
+
if (subject.columns.deletedAt) return { changed: false };
|
|
252
|
+
if (!await move(deps, deps.db.updateTable("pithyAuthUsers").set({
|
|
253
|
+
deletedAt: SQLiteDate.encode(at),
|
|
254
|
+
purgeAfter: SQLiteDate.encode(purgeAfter)
|
|
255
|
+
}).where("id", "=", input.userId).where("deletedAt", "is", null), {
|
|
256
|
+
userId: input.userId,
|
|
257
|
+
action: "deleted",
|
|
258
|
+
actor: input.actor,
|
|
259
|
+
reason,
|
|
260
|
+
at
|
|
261
|
+
})) return { changed: false };
|
|
262
|
+
await audit(deps, input.actor.type === "user" && input.actor.id === input.userId ? AuthAuditActions.userSelfDeleted : AuthAuditActions.adminUserSoftDeleted, input.userId, input.actor, {
|
|
263
|
+
reason,
|
|
264
|
+
purgeAfter: purgeAfter.toISOString()
|
|
265
|
+
});
|
|
266
|
+
await mail(deps, deps.config.lifecycleEmails.deletionScheduled, subject.email, subject.locale, "deletionScheduled", {
|
|
267
|
+
...named(subject.name),
|
|
268
|
+
purgeOn: readable(purgeAfter, subject.locale)
|
|
269
|
+
});
|
|
270
|
+
await fire(deps, "onUserDeleted", deps.hooks.onUserDeleted, {
|
|
271
|
+
userId: input.userId,
|
|
272
|
+
actor: input.actor,
|
|
273
|
+
reason,
|
|
274
|
+
at,
|
|
275
|
+
purgeAfter
|
|
276
|
+
});
|
|
277
|
+
return { changed: true };
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Restore a deleted account inside its purge window. After the purge there is no account, and that is a 404
|
|
281
|
+
* whose action says the data is gone.
|
|
282
|
+
*/
|
|
283
|
+
async function restoreUser(deps, input) {
|
|
284
|
+
const at = (deps.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
285
|
+
const reason = reasonOf(input.reason);
|
|
286
|
+
if (!(await subjectOf(deps.db, input.userId)).columns.deletedAt) return { changed: false };
|
|
287
|
+
if (!await move(deps, deps.db.updateTable("pithyAuthUsers").set({
|
|
288
|
+
deletedAt: null,
|
|
289
|
+
purgeAfter: null
|
|
290
|
+
}).where("id", "=", input.userId).where("deletedAt", "is not", null), {
|
|
291
|
+
userId: input.userId,
|
|
292
|
+
action: "restored",
|
|
293
|
+
actor: input.actor,
|
|
294
|
+
reason,
|
|
295
|
+
at
|
|
296
|
+
})) return { changed: false };
|
|
297
|
+
await audit(deps, AuthAuditActions.adminUserRestored, input.userId, input.actor, { reason });
|
|
298
|
+
return { changed: true };
|
|
299
|
+
}
|
|
300
|
+
/** The purge's own actor: a scheduled job, with nobody calling. */
|
|
301
|
+
const PURGE = {
|
|
302
|
+
type: "system",
|
|
303
|
+
id: null
|
|
304
|
+
};
|
|
305
|
+
/** Record that one purge step — one capability's contribution — erased its rows. */
|
|
306
|
+
async function recordPurgeStep(deps, input) {
|
|
307
|
+
const at = (deps.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
308
|
+
await record(deps, {
|
|
309
|
+
userId: input.userId,
|
|
310
|
+
action: "purge_step",
|
|
311
|
+
actor: PURGE,
|
|
312
|
+
reason: null,
|
|
313
|
+
at,
|
|
314
|
+
step: input.step
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Finish a purge: the last mail, the record, the audit event, the hook.
|
|
319
|
+
*
|
|
320
|
+
* **The address is handed in, not read**, because by the time a purge finishes the user row is gone — the
|
|
321
|
+
* Workflow captures the recipient before its first step and passes it here. Finishing a purge that already
|
|
322
|
+
* finished is a no-op, so a retried Workflow step mails nobody twice.
|
|
323
|
+
*/
|
|
324
|
+
async function completePurge(deps, input) {
|
|
325
|
+
const at = (deps.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
326
|
+
if (await deps.db.selectFrom("pithyAuthUserLifecycle").select("id").where("userId", "=", input.userId).where("action", "=", "purged").executeTakeFirst()) return { changed: false };
|
|
327
|
+
await record(deps, {
|
|
328
|
+
userId: input.userId,
|
|
329
|
+
action: "purged",
|
|
330
|
+
actor: PURGE,
|
|
331
|
+
reason: null,
|
|
332
|
+
at
|
|
333
|
+
});
|
|
334
|
+
await audit(deps, AuthAuditActions.adminUserPurged, input.userId, PURGE, {});
|
|
335
|
+
const { email, name, locale } = input.recipient;
|
|
336
|
+
await mail(deps, deps.config.lifecycleEmails.deletionComplete, email, locale, "deletionComplete", {
|
|
337
|
+
...named(name ?? ""),
|
|
338
|
+
purgedOn: readable(at, locale)
|
|
339
|
+
});
|
|
340
|
+
await fire(deps, "onUserPurged", deps.hooks.onUserPurged, {
|
|
341
|
+
userId: input.userId,
|
|
342
|
+
actor: PURGE,
|
|
343
|
+
reason: null,
|
|
344
|
+
at
|
|
345
|
+
});
|
|
346
|
+
return { changed: true };
|
|
347
|
+
}
|
|
348
|
+
//#endregion
|
|
349
|
+
export { completePurge, deleteUser, recordPurgeStep, reinstateUser, restoreUser, suspendUser };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { Migration } from "kysely/migration";
|
|
4
|
+
/**
|
|
5
|
+
* The account lifecycle (#620): whether an account is suspended or deleted, and the record of how it got
|
|
6
|
+
* there.
|
|
7
|
+
*
|
|
8
|
+
* **Four columns on `pithy_auth_users`, so a session read already carries the state.** Better Auth loads the
|
|
9
|
+
* user row for every session it resolves, and returns these because `../data/kitFields.ts` declares them —
|
|
10
|
+
* so refusing a suspended account costs no query on the request that is refused, and none on the requests
|
|
11
|
+
* that are not.
|
|
12
|
+
*
|
|
13
|
+
* **One kit-owned table beside them, `pithy_auth_user_lifecycle`**, holding every transition with its
|
|
14
|
+
* actor, its reason and its time. The columns say what is true now; the table says what happened. No
|
|
15
|
+
* foreign key from it to the user: a purge erases the user row, and this table is the record that must
|
|
16
|
+
* outlive it (`../data/userLifecycle.ts`).
|
|
17
|
+
*
|
|
18
|
+
* **`integer`, not `text`, unlike every Better Auth date on the same table.** Better Auth writes those, as
|
|
19
|
+
* ISO-8601. Nothing but the kit writes these — they are `input: false` — so they take the house ms-epoch
|
|
20
|
+
* convention, and `type: "number"` in `kitFields.ts` keeps Better Auth from converting what it reads back.
|
|
21
|
+
*
|
|
22
|
+
* **Nullable, with no backfill.** Every account that exists when this runs is in neither state, and null is
|
|
23
|
+
* exactly that. SQLite would refuse `ADD COLUMN … NOT NULL` without a constant default anyway.
|
|
24
|
+
*
|
|
25
|
+
* **A `0004`, appended, never folded into `0001`.** `@pithy-sh/auth` has shipped, so its chain is history.
|
|
26
|
+
*/
|
|
27
|
+
export declare const auth_0004_user_lifecycle: Migration;
|
|
28
|
+
//# sourceMappingURL=0004_user_lifecycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"0004_user_lifecycle.d.ts","sourceRoot":"","sources":["../../src/migrations/0004_user_lifecycle.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAKlD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,wBAAwB,EAAE,SAmCtC,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
//#region src/migrations/0004_user_lifecycle.ts
|
|
4
|
+
/** The four state columns, in the order `up` adds them and `down` takes them away in reverse. */
|
|
5
|
+
const STATE_COLUMNS = [
|
|
6
|
+
"suspendedAt",
|
|
7
|
+
"suspendedUntil",
|
|
8
|
+
"deletedAt",
|
|
9
|
+
"purgeAfter"
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* The account lifecycle (#620): whether an account is suspended or deleted, and the record of how it got
|
|
13
|
+
* there.
|
|
14
|
+
*
|
|
15
|
+
* **Four columns on `pithy_auth_users`, so a session read already carries the state.** Better Auth loads the
|
|
16
|
+
* user row for every session it resolves, and returns these because `../data/kitFields.ts` declares them —
|
|
17
|
+
* so refusing a suspended account costs no query on the request that is refused, and none on the requests
|
|
18
|
+
* that are not.
|
|
19
|
+
*
|
|
20
|
+
* **One kit-owned table beside them, `pithy_auth_user_lifecycle`**, holding every transition with its
|
|
21
|
+
* actor, its reason and its time. The columns say what is true now; the table says what happened. No
|
|
22
|
+
* foreign key from it to the user: a purge erases the user row, and this table is the record that must
|
|
23
|
+
* outlive it (`../data/userLifecycle.ts`).
|
|
24
|
+
*
|
|
25
|
+
* **`integer`, not `text`, unlike every Better Auth date on the same table.** Better Auth writes those, as
|
|
26
|
+
* ISO-8601. Nothing but the kit writes these — they are `input: false` — so they take the house ms-epoch
|
|
27
|
+
* convention, and `type: "number"` in `kitFields.ts` keeps Better Auth from converting what it reads back.
|
|
28
|
+
*
|
|
29
|
+
* **Nullable, with no backfill.** Every account that exists when this runs is in neither state, and null is
|
|
30
|
+
* exactly that. SQLite would refuse `ADD COLUMN … NOT NULL` without a constant default anyway.
|
|
31
|
+
*
|
|
32
|
+
* **A `0004`, appended, never folded into `0001`.** `@pithy-sh/auth` has shipped, so its chain is history.
|
|
33
|
+
*/
|
|
34
|
+
const auth_0004_user_lifecycle = {
|
|
35
|
+
async up(db) {
|
|
36
|
+
for (const column of STATE_COLUMNS) await db.schema.alterTable("pithyAuthUsers").addColumn(column, "integer").execute();
|
|
37
|
+
await db.schema.createTable("pithyAuthUserLifecycle").addColumn("id", "integer", (c) => c.primaryKey().autoIncrement()).addColumn("userId", "text", (c) => c.notNull()).addColumn("action", "text", (c) => c.notNull()).addColumn("actorType", "text", (c) => c.notNull()).addColumn("actorId", "text").addColumn("reason", "text").addColumn("suspendedUntil", "integer").addColumn("step", "text").addColumn("createdAt", "integer", (c) => c.notNull()).execute();
|
|
38
|
+
await db.schema.createIndex("pithyAuthUserLifecycleUserIdIdx").on("pithyAuthUserLifecycle").columns(["userId", "id"]).execute();
|
|
39
|
+
},
|
|
40
|
+
async down(db) {
|
|
41
|
+
await db.schema.dropIndex("pithyAuthUserLifecycleUserIdIdx").execute();
|
|
42
|
+
await db.schema.dropTable("pithyAuthUserLifecycle").execute();
|
|
43
|
+
for (const column of [...STATE_COLUMNS].reverse()) await db.schema.alterTable("pithyAuthUsers").dropColumn(column).execute();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { auth_0004_user_lifecycle };
|
package/dist/migrations/set.d.ts
CHANGED
|
@@ -10,12 +10,13 @@
|
|
|
10
10
|
* migration. Adding a `0002` turned all five into a schema one column short of the code, and the symptom
|
|
11
11
|
* was `table pithy_auth_sessions has no column named authenticated_at` in thirteen unrelated tests.
|
|
12
12
|
*
|
|
13
|
-
* `0003` appended here and reached every reader, which is the point. `0001_init.workers.test.ts` is deliberately not a reader:
|
|
13
|
+
* `0003` and `0004` appended here and reached every reader, which is the point. `0001_init.workers.test.ts` is deliberately not a reader:
|
|
14
14
|
* its subject is that one migration's own `up` and `down`, so it names it directly.
|
|
15
15
|
*/
|
|
16
16
|
export declare const AUTH_MIGRATIONS: {
|
|
17
17
|
readonly "0001_init": import("kysely/migration").Migration;
|
|
18
18
|
readonly "0002_session_authenticated_at": import("kysely/migration").Migration;
|
|
19
19
|
readonly "0003_account_issuer_nullable": import("kysely/migration").Migration;
|
|
20
|
+
readonly "0004_user_lifecycle": import("kysely/migration").Migration;
|
|
20
21
|
};
|
|
21
22
|
//# sourceMappingURL=set.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../src/migrations/set.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../../src/migrations/set.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe;aAC1B,WAAW;aACX,+BAA+B;aAC/B,8BAA8B;aAC9B,qBAAqB;CACb,CAAC"}
|
package/dist/migrations/set.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { auth_0001_init } from "./0001_init.js";
|
|
4
4
|
import { auth_0002_session_authenticated_at } from "./0002_session_authenticated_at.js";
|
|
5
5
|
import { auth_0003_account_issuer_nullable } from "./0003_account_issuer_nullable.js";
|
|
6
|
+
import { auth_0004_user_lifecycle } from "./0004_user_lifecycle.js";
|
|
6
7
|
//#region src/migrations/set.ts
|
|
7
8
|
/**
|
|
8
9
|
* Every migration `@pithy-sh/auth` ships, keyed as `pithy migrate` composes them.
|
|
@@ -14,13 +15,14 @@ import { auth_0003_account_issuer_nullable } from "./0003_account_issuer_nullabl
|
|
|
14
15
|
* migration. Adding a `0002` turned all five into a schema one column short of the code, and the symptom
|
|
15
16
|
* was `table pithy_auth_sessions has no column named authenticated_at` in thirteen unrelated tests.
|
|
16
17
|
*
|
|
17
|
-
* `0003` appended here and reached every reader, which is the point. `0001_init.workers.test.ts` is deliberately not a reader:
|
|
18
|
+
* `0003` and `0004` appended here and reached every reader, which is the point. `0001_init.workers.test.ts` is deliberately not a reader:
|
|
18
19
|
* its subject is that one migration's own `up` and `down`, so it names it directly.
|
|
19
20
|
*/
|
|
20
21
|
const AUTH_MIGRATIONS = {
|
|
21
22
|
"0001_init": auth_0001_init,
|
|
22
23
|
"0002_session_authenticated_at": auth_0002_session_authenticated_at,
|
|
23
|
-
"0003_account_issuer_nullable": auth_0003_account_issuer_nullable
|
|
24
|
+
"0003_account_issuer_nullable": auth_0003_account_issuer_nullable,
|
|
25
|
+
"0004_user_lifecycle": auth_0004_user_lifecycle
|
|
24
26
|
};
|
|
25
27
|
//#endregion
|
|
26
28
|
export { AUTH_MIGRATIONS };
|
package/dist/peer.d.ts
CHANGED
|
@@ -30,6 +30,10 @@ export declare const authPeer: {
|
|
|
30
30
|
locale: import("zod").ZodNullable<import("zod").ZodString>;
|
|
31
31
|
createdAt: import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodDate]>, import("zod").ZodDate>;
|
|
32
32
|
updatedAt: import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodDate]>, import("zod").ZodDate>;
|
|
33
|
+
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>>>;
|
|
34
|
+
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>>>;
|
|
35
|
+
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>>>;
|
|
36
|
+
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>>>;
|
|
33
37
|
}, import("zod/v4/core").$strip>;
|
|
34
38
|
readonly Device: import("zod").ZodObject<{
|
|
35
39
|
id: import("zod").ZodString;
|
package/dist/peer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer.d.ts","sourceRoot":"","sources":["../src/peer.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,QAAQ
|
|
1
|
+
{"version":3,"file":"peer.d.ts","sourceRoot":"","sources":["../src/peer.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAA0C,CAAC;AAEhE,4DAA4D;AAC5D,MAAM,MAAM,QAAQ,GAAG,OAAO,QAAQ,CAAC"}
|
|
@@ -130,6 +130,27 @@ export declare const LIVE_REGISTRY: {
|
|
|
130
130
|
readonly documentation: "https://github.com/settings/developers";
|
|
131
131
|
};
|
|
132
132
|
};
|
|
133
|
+
"auth-microsoft-credentials": {
|
|
134
|
+
readonly backend: "d1";
|
|
135
|
+
readonly scope: "environment";
|
|
136
|
+
readonly rotatable: true;
|
|
137
|
+
readonly valueType: "json";
|
|
138
|
+
readonly schema: import("zod").ZodObject<{
|
|
139
|
+
clientId: import("zod").ZodString;
|
|
140
|
+
clientSecret: import("zod").ZodString;
|
|
141
|
+
tenantId: import("zod").ZodString;
|
|
142
|
+
}, import("zod/v4/core").$strip>;
|
|
143
|
+
readonly origin: {
|
|
144
|
+
readonly kind: "obtained";
|
|
145
|
+
readonly issuer: "microsoft";
|
|
146
|
+
readonly documentation: "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
|
|
147
|
+
};
|
|
148
|
+
readonly rotation: {
|
|
149
|
+
readonly kind: "manual";
|
|
150
|
+
readonly issuer: "microsoft";
|
|
151
|
+
readonly documentation: "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
|
|
152
|
+
};
|
|
153
|
+
};
|
|
133
154
|
"turnstile-secret-keys": {
|
|
134
155
|
readonly backend: "d1";
|
|
135
156
|
readonly scope: "environment";
|