@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
|
@@ -21,12 +21,16 @@ import type { SessionSecretVersions } from "./sessionSecrets";
|
|
|
21
21
|
* Read only when the provider is enabled; rotated in the Meta app dashboard, not on our schedule.
|
|
22
22
|
* - `auth-github-credentials` — the GitHub OAuth app client id and secret, as a typed JSON value.
|
|
23
23
|
* Read only when the provider is enabled; rotated in GitHub Developer settings, not on our schedule.
|
|
24
|
+
* - `auth-microsoft-credentials` — the Microsoft Entra ID app registration's client id, client secret,
|
|
25
|
+
* and tenant, as a typed JSON value. The tenant rides inside because it means nothing apart from the
|
|
26
|
+
* registration it belongs to. Rotatable: an Entra client secret expires (24 months at most).
|
|
24
27
|
*/
|
|
25
28
|
export declare const AUTH_SESSION_SECRET = "auth-session-secret";
|
|
26
29
|
export declare const AUTH_GOOGLE_CREDENTIALS = "auth-google-credentials";
|
|
27
30
|
export declare const AUTH_APPLE_CREDENTIALS = "auth-apple-credentials";
|
|
28
31
|
export declare const AUTH_FACEBOOK_CREDENTIALS = "auth-facebook-credentials";
|
|
29
32
|
export declare const AUTH_GITHUB_CREDENTIALS = "auth-github-credentials";
|
|
33
|
+
export declare const AUTH_MICROSOFT_CREDENTIALS = "auth-microsoft-credentials";
|
|
30
34
|
/**
|
|
31
35
|
* The JWKS private keys, as `auth-session-secret`'s overlap names them (#602).
|
|
32
36
|
*
|
|
@@ -79,6 +83,19 @@ export declare const GithubOAuthCredentials: z.ZodObject<{
|
|
|
79
83
|
clientSecret: z.ZodString;
|
|
80
84
|
}, z.core.$strip>;
|
|
81
85
|
export type GithubOAuthCredentials = z.infer<typeof GithubOAuthCredentials>;
|
|
86
|
+
/**
|
|
87
|
+
* The Microsoft Entra ID credentials, stored as one typed JSON secret.
|
|
88
|
+
*
|
|
89
|
+
* **`tenantId` is required and may not be empty.** Better Auth builds its endpoints from
|
|
90
|
+
* `options.tenantId || "common"`, so an empty string would not fail: it would quietly open a
|
|
91
|
+
* single-tenant app to every Entra tenant and every personal account.
|
|
92
|
+
*/
|
|
93
|
+
export declare const MicrosoftOAuthCredentials: z.ZodObject<{
|
|
94
|
+
clientId: z.ZodString;
|
|
95
|
+
clientSecret: z.ZodString;
|
|
96
|
+
tenantId: z.ZodString;
|
|
97
|
+
}, z.core.$strip>;
|
|
98
|
+
export type MicrosoftOAuthCredentials = z.infer<typeof MicrosoftOAuthCredentials>;
|
|
82
99
|
export declare const authSecretsRegistry: {
|
|
83
100
|
readonly "auth-session-secret": {
|
|
84
101
|
readonly backend: "d1";
|
|
@@ -184,6 +201,27 @@ export declare const authSecretsRegistry: {
|
|
|
184
201
|
readonly documentation: "https://github.com/settings/developers";
|
|
185
202
|
};
|
|
186
203
|
};
|
|
204
|
+
readonly "auth-microsoft-credentials": {
|
|
205
|
+
readonly backend: "d1";
|
|
206
|
+
readonly scope: "environment";
|
|
207
|
+
readonly rotatable: true;
|
|
208
|
+
readonly valueType: "json";
|
|
209
|
+
readonly schema: z.ZodObject<{
|
|
210
|
+
clientId: z.ZodString;
|
|
211
|
+
clientSecret: z.ZodString;
|
|
212
|
+
tenantId: z.ZodString;
|
|
213
|
+
}, z.core.$strip>;
|
|
214
|
+
readonly origin: {
|
|
215
|
+
readonly kind: "obtained";
|
|
216
|
+
readonly issuer: "microsoft";
|
|
217
|
+
readonly documentation: "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
|
|
218
|
+
};
|
|
219
|
+
readonly rotation: {
|
|
220
|
+
readonly kind: "manual";
|
|
221
|
+
readonly issuer: "microsoft";
|
|
222
|
+
readonly documentation: "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
|
|
223
|
+
};
|
|
224
|
+
};
|
|
187
225
|
};
|
|
188
226
|
/**
|
|
189
227
|
* **The registry `auth()` contributes, from its own configuration (#602).**
|
|
@@ -288,6 +326,27 @@ export declare function authSecretsRegistryFor(config: {
|
|
|
288
326
|
readonly documentation: "https://github.com/settings/developers";
|
|
289
327
|
};
|
|
290
328
|
};
|
|
329
|
+
readonly "auth-microsoft-credentials": {
|
|
330
|
+
readonly backend: "d1";
|
|
331
|
+
readonly scope: "environment";
|
|
332
|
+
readonly rotatable: true;
|
|
333
|
+
readonly valueType: "json";
|
|
334
|
+
readonly schema: z.ZodObject<{
|
|
335
|
+
clientId: z.ZodString;
|
|
336
|
+
clientSecret: z.ZodString;
|
|
337
|
+
tenantId: z.ZodString;
|
|
338
|
+
}, z.core.$strip>;
|
|
339
|
+
readonly origin: {
|
|
340
|
+
readonly kind: "obtained";
|
|
341
|
+
readonly issuer: "microsoft";
|
|
342
|
+
readonly documentation: "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
|
|
343
|
+
};
|
|
344
|
+
readonly rotation: {
|
|
345
|
+
readonly kind: "manual";
|
|
346
|
+
readonly issuer: "microsoft";
|
|
347
|
+
readonly documentation: "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
|
|
348
|
+
};
|
|
349
|
+
};
|
|
291
350
|
readonly "auth-session-secret": {
|
|
292
351
|
readonly backend: "d1";
|
|
293
352
|
readonly scope: "environment";
|
|
@@ -320,15 +379,15 @@ export declare function authSecretsRegistryFor(config: {
|
|
|
320
379
|
* nothing makes a secret name and a config key the same thing, and a reporting command that matched
|
|
321
380
|
* them by substring would be right by luck. Stated here, once, beside both.
|
|
322
381
|
*/
|
|
323
|
-
declare const PROVIDER_CREDENTIALS: readonly [readonly ["google", "auth-google-credentials"], readonly ["apple", "auth-apple-credentials"], readonly ["facebook", "auth-facebook-credentials"], readonly ["github", "auth-github-credentials"]];
|
|
324
|
-
/** The
|
|
382
|
+
declare const PROVIDER_CREDENTIALS: readonly [readonly ["google", "auth-google-credentials"], readonly ["apple", "auth-apple-credentials"], readonly ["facebook", "auth-facebook-credentials"], readonly ["github", "auth-github-credentials"], readonly ["microsoft", "auth-microsoft-credentials"]];
|
|
383
|
+
/** The five toggles this reads off the resolved `AuthConfig` — nothing else about it is its business. */
|
|
325
384
|
export type AuthProviderToggles = Record<(typeof PROVIDER_CREDENTIALS)[number][0], {
|
|
326
385
|
enabled: boolean;
|
|
327
386
|
}>;
|
|
328
387
|
/**
|
|
329
388
|
* **The provider credentials this composition cannot reach**, as the `inapplicableSecrets` contribution.
|
|
330
389
|
*
|
|
331
|
-
* Auth declares
|
|
390
|
+
* Auth declares five credentials whether or not a project runs five providers, so a project on Google
|
|
332
391
|
* and GitHub carries two secrets nothing will ever read. `pithy doctor` asked for them every run under
|
|
333
392
|
* *"fine to leave until you need it"* — softening, not silence, and "until you need it" is wrong about a
|
|
334
393
|
* provider the configuration has not enabled.
|
|
@@ -356,5 +415,7 @@ export declare function resolveAppleCredentials(env: SecretsStoreEnv): Promise<A
|
|
|
356
415
|
export declare function resolveFacebookCredentials(env: SecretsStoreEnv): Promise<FacebookOAuthCredentials>;
|
|
357
416
|
/** Resolve the GitHub OAuth credential pair for this invocation (only call when GitHub is enabled). */
|
|
358
417
|
export declare function resolveGithubCredentials(env: SecretsStoreEnv): Promise<GithubOAuthCredentials>;
|
|
418
|
+
/** Resolve the Microsoft Entra ID credentials for this invocation (only call when Microsoft is enabled). */
|
|
419
|
+
export declare function resolveMicrosoftCredentials(env: SecretsStoreEnv): Promise<MicrosoftOAuthCredentials>;
|
|
359
420
|
export {};
|
|
360
421
|
//# sourceMappingURL=secrets.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../../src/instance/secrets.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EA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AE,CAAC,CAAC;AAEzG;;;;;;;;;;;;;GAaG;AACH,wBAAgB,2BAA2B,CAAC,SAAS,EAAE,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAOlG;AAED,8FAA8F;AAC9F,wBAAsB,oBAAoB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAGhF;AAED;;;;GAIG;AACH,wBAAsB,qBAAqB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAGhG;AAED,uGAAuG;AACvG,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAGpG;AAED,mGAAmG;AACnG,wBAAsB,uBAAuB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAGlG;AAED,2GAA2G;AAC3G,wBAAsB,0BAA0B,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAGxG;AAED,uGAAuG;AACvG,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAGpG;AAED,4GAA4G;AAC5G,wBAAsB,2BAA2B,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAG1G"}
|
package/dist/instance/secrets.js
CHANGED
|
@@ -23,12 +23,16 @@ import { sharedSecretsStore } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
|
23
23
|
* Read only when the provider is enabled; rotated in the Meta app dashboard, not on our schedule.
|
|
24
24
|
* - `auth-github-credentials` — the GitHub OAuth app client id and secret, as a typed JSON value.
|
|
25
25
|
* Read only when the provider is enabled; rotated in GitHub Developer settings, not on our schedule.
|
|
26
|
+
* - `auth-microsoft-credentials` — the Microsoft Entra ID app registration's client id, client secret,
|
|
27
|
+
* and tenant, as a typed JSON value. The tenant rides inside because it means nothing apart from the
|
|
28
|
+
* registration it belongs to. Rotatable: an Entra client secret expires (24 months at most).
|
|
26
29
|
*/
|
|
27
30
|
const AUTH_SESSION_SECRET = "auth-session-secret";
|
|
28
31
|
const AUTH_GOOGLE_CREDENTIALS = "auth-google-credentials";
|
|
29
32
|
const AUTH_APPLE_CREDENTIALS = "auth-apple-credentials";
|
|
30
33
|
const AUTH_FACEBOOK_CREDENTIALS = "auth-facebook-credentials";
|
|
31
34
|
const AUTH_GITHUB_CREDENTIALS = "auth-github-credentials";
|
|
35
|
+
const AUTH_MICROSOFT_CREDENTIALS = "auth-microsoft-credentials";
|
|
32
36
|
/**
|
|
33
37
|
* The JWKS private keys, as `auth-session-secret`'s overlap names them (#602).
|
|
34
38
|
*
|
|
@@ -82,17 +86,30 @@ const GithubOAuthCredentials = z.object({
|
|
|
82
86
|
clientSecret: z.string().meta({ multiline: false }).describe("The GitHub OAuth app client secret for this environment. Never committed, never an env literal.")
|
|
83
87
|
}).describe("A GitHub OAuth credential pair (`clientId` + `clientSecret`) for one environment.");
|
|
84
88
|
/**
|
|
89
|
+
* The Microsoft Entra ID credentials, stored as one typed JSON secret.
|
|
90
|
+
*
|
|
91
|
+
* **`tenantId` is required and may not be empty.** Better Auth builds its endpoints from
|
|
92
|
+
* `options.tenantId || "common"`, so an empty string would not fail: it would quietly open a
|
|
93
|
+
* single-tenant app to every Entra tenant and every personal account.
|
|
94
|
+
*/
|
|
95
|
+
const MicrosoftOAuthCredentials = z.object({
|
|
96
|
+
clientId: z.string().meta({ multiline: false }).describe("The Application (client) ID of the Entra ID app registration for this environment. Not secret on its own, but stored with the secret so the set is atomic."),
|
|
97
|
+
clientSecret: z.string().meta({ multiline: false }).describe("A client secret value from the same app registration. Expires (24 months at most), so rotate it on schedule. Never committed, never an env literal."),
|
|
98
|
+
tenantId: z.string().min(1).meta({ multiline: false }).describe("Who may sign in: a directory (tenant) ID for one organization, `organizations` for any work or school account, `consumers` for personal accounts only, or `common` for both.")
|
|
99
|
+
}).describe("A Microsoft Entra ID credential set (`clientId` + `clientSecret` + `tenantId`) for one environment.");
|
|
100
|
+
/**
|
|
85
101
|
* Where a human goes for each OAuth credential pair, and where the same human rotates it.
|
|
86
102
|
*
|
|
87
103
|
* **The settings page, not the product homepage.** The point is to end a search, not to start one — and
|
|
88
|
-
* these
|
|
89
|
-
* can offer. Named constants because origin and rotation are the same page for all
|
|
104
|
+
* these five are the case with no command and no possible one, so the link is the entire remedy the kit
|
|
105
|
+
* can offer. Named constants because origin and rotation are the same page for all five, and two copies
|
|
90
106
|
* of a URL is one that goes stale.
|
|
91
107
|
*/
|
|
92
108
|
const GOOGLE_CREDENTIALS_PAGE = "https://console.cloud.google.com/apis/credentials";
|
|
93
109
|
const APPLE_KEYS_PAGE = "https://developer.apple.com/account/resources/authkeys/list";
|
|
94
110
|
const FACEBOOK_APPS_PAGE = "https://developers.facebook.com/apps/";
|
|
95
111
|
const GITHUB_OAUTH_APPS_PAGE = "https://github.com/settings/developers";
|
|
112
|
+
const MICROSOFT_APP_REGISTRATIONS_PAGE = "https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade";
|
|
96
113
|
const authSecretsRegistry = defineSecretRegistry({
|
|
97
114
|
[AUTH_SESSION_SECRET]: {
|
|
98
115
|
backend: "d1",
|
|
@@ -182,6 +199,23 @@ const authSecretsRegistry = defineSecretRegistry({
|
|
|
182
199
|
issuer: "github",
|
|
183
200
|
documentation: GITHUB_OAUTH_APPS_PAGE
|
|
184
201
|
}
|
|
202
|
+
},
|
|
203
|
+
[AUTH_MICROSOFT_CREDENTIALS]: {
|
|
204
|
+
backend: "d1",
|
|
205
|
+
scope: "environment",
|
|
206
|
+
rotatable: true,
|
|
207
|
+
valueType: "json",
|
|
208
|
+
schema: MicrosoftOAuthCredentials,
|
|
209
|
+
origin: {
|
|
210
|
+
kind: "obtained",
|
|
211
|
+
issuer: "microsoft",
|
|
212
|
+
documentation: MICROSOFT_APP_REGISTRATIONS_PAGE
|
|
213
|
+
},
|
|
214
|
+
rotation: {
|
|
215
|
+
kind: "manual",
|
|
216
|
+
issuer: "microsoft",
|
|
217
|
+
documentation: MICROSOFT_APP_REGISTRATIONS_PAGE
|
|
218
|
+
}
|
|
185
219
|
}
|
|
186
220
|
});
|
|
187
221
|
/**
|
|
@@ -233,12 +267,13 @@ const PROVIDER_CREDENTIALS = [
|
|
|
233
267
|
["google", AUTH_GOOGLE_CREDENTIALS],
|
|
234
268
|
["apple", AUTH_APPLE_CREDENTIALS],
|
|
235
269
|
["facebook", AUTH_FACEBOOK_CREDENTIALS],
|
|
236
|
-
["github", AUTH_GITHUB_CREDENTIALS]
|
|
270
|
+
["github", AUTH_GITHUB_CREDENTIALS],
|
|
271
|
+
["microsoft", AUTH_MICROSOFT_CREDENTIALS]
|
|
237
272
|
];
|
|
238
273
|
/**
|
|
239
274
|
* **The provider credentials this composition cannot reach**, as the `inapplicableSecrets` contribution.
|
|
240
275
|
*
|
|
241
|
-
* Auth declares
|
|
276
|
+
* Auth declares five credentials whether or not a project runs five providers, so a project on Google
|
|
242
277
|
* and GitHub carries two secrets nothing will ever read. `pithy doctor` asked for them every run under
|
|
243
278
|
* *"fine to leave until you need it"* — softening, not silence, and "until you need it" is wrong about a
|
|
244
279
|
* provider the configuration has not enabled.
|
|
@@ -285,5 +320,9 @@ async function resolveFacebookCredentials(env) {
|
|
|
285
320
|
async function resolveGithubCredentials(env) {
|
|
286
321
|
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_GITHUB_CREDENTIALS);
|
|
287
322
|
}
|
|
323
|
+
/** Resolve the Microsoft Entra ID credentials for this invocation (only call when Microsoft is enabled). */
|
|
324
|
+
async function resolveMicrosoftCredentials(env) {
|
|
325
|
+
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_MICROSOFT_CREDENTIALS);
|
|
326
|
+
}
|
|
288
327
|
//#endregion
|
|
289
|
-
export { AUTH_APPLE_CREDENTIALS, AUTH_FACEBOOK_CREDENTIALS, AUTH_GITHUB_CREDENTIALS, AUTH_GOOGLE_CREDENTIALS, AUTH_JWKS_DEPENDENT, AUTH_SESSION_SECRET, AppleOAuthCredentials, FacebookOAuthCredentials, GithubOAuthCredentials, GoogleOAuthCredentials, SESSION_SECRET_SEALING_PLUGINS, authSecretsRegistry, authSecretsRegistryFor, inapplicableProviderSecrets, resolveAppleCredentials, resolveFacebookCredentials, resolveGithubCredentials, resolveGoogleCredentials, resolveSessionSecret, resolveSessionSecrets, sessionSecretOverlapDays };
|
|
328
|
+
export { AUTH_APPLE_CREDENTIALS, AUTH_FACEBOOK_CREDENTIALS, AUTH_GITHUB_CREDENTIALS, AUTH_GOOGLE_CREDENTIALS, AUTH_JWKS_DEPENDENT, AUTH_MICROSOFT_CREDENTIALS, AUTH_SESSION_SECRET, AppleOAuthCredentials, FacebookOAuthCredentials, GithubOAuthCredentials, GoogleOAuthCredentials, MicrosoftOAuthCredentials, SESSION_SECRET_SEALING_PLUGINS, authSecretsRegistry, authSecretsRegistryFor, inapplicableProviderSecrets, resolveAppleCredentials, resolveFacebookCredentials, resolveGithubCredentials, resolveGoogleCredentials, resolveMicrosoftCredentials, resolveSessionSecret, resolveSessionSecrets, sessionSecretOverlapDays };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { AuditEmit } from "@pithy-sh/core/src/audit/recorder";
|
|
4
|
+
/**
|
|
5
|
+
* Every control-plane lifecycle call is audited, including the ones that changed nothing (#621).
|
|
6
|
+
*
|
|
7
|
+
* A transition that moves the account writes its own audit event, beside the history row, in
|
|
8
|
+
* `./transitions.ts`. That leaves two kinds of call with no event, and both matter to somebody reading the
|
|
9
|
+
* trail after an incident: **a refusal** — an unknown account, a deleted one, an end in the past — which says
|
|
10
|
+
* a credential was trying, and **a call that found the account already where it asked to put it**, which
|
|
11
|
+
* says somebody pressed the button. This records both, under the same action as the transition, so one
|
|
12
|
+
* filter finds every attempt: a refusal is `denied` (or `failure` for a fault) with the code, and a no-op is
|
|
13
|
+
* a `success` with `changed: false`. A refusal is still thrown, unchanged.
|
|
14
|
+
*
|
|
15
|
+
* A body the route's validator refuses never reaches the call, so the route records it with
|
|
16
|
+
* {@link recordLifecycleRefusal}, on the same event, before the 400 is thrown.
|
|
17
|
+
*/
|
|
18
|
+
/** Who called, about whom, and why — what every lifecycle audit event carries. */
|
|
19
|
+
export interface LifecycleCall {
|
|
20
|
+
/** The audit action the transition writes when it moves the account. */
|
|
21
|
+
readonly action: string;
|
|
22
|
+
/** The management client's verified `sub`: which person acted. */
|
|
23
|
+
readonly subject: string;
|
|
24
|
+
/** The connection the call came through. */
|
|
25
|
+
readonly connectionId: string;
|
|
26
|
+
/** The account acted on. */
|
|
27
|
+
readonly userId: string;
|
|
28
|
+
/** The reason the caller gave — `null` for a refused body that carried none. */
|
|
29
|
+
readonly reason: string | null;
|
|
30
|
+
/** The request's correlation. */
|
|
31
|
+
readonly ip?: string | undefined;
|
|
32
|
+
readonly userAgent?: string | undefined;
|
|
33
|
+
readonly requestId?: string | undefined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Record one refused lifecycle call — `denied` with its code, or `failure` for a fault. `code` names the
|
|
37
|
+
* refusal when the thrown error carries none of its own, as a body Hono could not parse does not.
|
|
38
|
+
*/
|
|
39
|
+
export declare function recordLifecycleRefusal(emit: AuditEmit, call: LifecycleCall, error: unknown, fallback?: {
|
|
40
|
+
code: string;
|
|
41
|
+
outcome: "denied" | "failure";
|
|
42
|
+
}): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Run one lifecycle call and audit whatever the transition did not. `unchanged` says whether a result is a
|
|
45
|
+
* call that changed nothing; the event for a call that did change something is the transition's own.
|
|
46
|
+
*/
|
|
47
|
+
export declare function auditedLifecycleCall<T>(emit: AuditEmit, call: LifecycleCall, run: () => Promise<T>, unchanged: (result: T) => boolean): Promise<T>;
|
|
48
|
+
//# sourceMappingURL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YZ"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { emitControlPlaneAction } from "../audit/emit.js";
|
|
4
|
+
import { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
5
|
+
//#region src/lifecycle/audited.ts
|
|
6
|
+
/** The public code of a thrown error, or `core/internal` for anything that is not a `PithyError`. */
|
|
7
|
+
function codeOf(error) {
|
|
8
|
+
return error instanceof PithyError ? error.payload.code : "core/internal";
|
|
9
|
+
}
|
|
10
|
+
/** A refusal this Worker made on purpose is `denied`; a fault, a 5xx or a throw that is not ours, `failure`. */
|
|
11
|
+
function outcomeOf(error) {
|
|
12
|
+
return error instanceof PithyError && error.payload.status < 500 ? "denied" : "failure";
|
|
13
|
+
}
|
|
14
|
+
/** The event every lifecycle audit record shares, before its outcome and metadata. */
|
|
15
|
+
function eventOf(call) {
|
|
16
|
+
return {
|
|
17
|
+
action: call.action,
|
|
18
|
+
subject: call.subject,
|
|
19
|
+
connectionId: call.connectionId,
|
|
20
|
+
resourceType: "user",
|
|
21
|
+
resourceId: call.userId,
|
|
22
|
+
ip: call.ip,
|
|
23
|
+
userAgent: call.userAgent,
|
|
24
|
+
requestId: call.requestId
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Record one refused lifecycle call — `denied` with its code, or `failure` for a fault. `code` names the
|
|
29
|
+
* refusal when the thrown error carries none of its own, as a body Hono could not parse does not.
|
|
30
|
+
*/
|
|
31
|
+
async function recordLifecycleRefusal(emit, call, error, fallback) {
|
|
32
|
+
const ours = error instanceof PithyError;
|
|
33
|
+
await emitControlPlaneAction(emit, {
|
|
34
|
+
...eventOf(call),
|
|
35
|
+
outcome: ours || !fallback ? outcomeOf(error) : fallback.outcome,
|
|
36
|
+
metadata: {
|
|
37
|
+
reason: call.reason,
|
|
38
|
+
changed: false,
|
|
39
|
+
code: ours || !fallback ? codeOf(error) : fallback.code
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Run one lifecycle call and audit whatever the transition did not. `unchanged` says whether a result is a
|
|
45
|
+
* call that changed nothing; the event for a call that did change something is the transition's own.
|
|
46
|
+
*/
|
|
47
|
+
async function auditedLifecycleCall(emit, call, run, unchanged) {
|
|
48
|
+
let result;
|
|
49
|
+
try {
|
|
50
|
+
result = await run();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
await recordLifecycleRefusal(emit, call, error);
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
if (unchanged(result)) await emitControlPlaneAction(emit, {
|
|
56
|
+
...eventOf(call),
|
|
57
|
+
metadata: {
|
|
58
|
+
reason: call.reason,
|
|
59
|
+
changed: false
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { auditedLifecycleCall, recordLifecycleRefusal };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
4
|
+
import type { AuthDatabase } from "../data/tables";
|
|
5
|
+
import { LifecycleColumns } from "./state";
|
|
6
|
+
/**
|
|
7
|
+
* The reads enforcement needs, and the one place a refusal is remembered for the rest of a request.
|
|
8
|
+
*
|
|
9
|
+
* ## Where an account is refused
|
|
10
|
+
*
|
|
11
|
+
* - **Every request** — the session middleware reads the state off the user Better Auth resolved for the
|
|
12
|
+
* session, which carries the columns, so the check costs no query. A refused account gets no
|
|
13
|
+
* `AuthContext`, so every `requireAuth()` in the kit refuses it whoever wrote the guard; this capability's
|
|
14
|
+
* own `requireAuth()` raises the refusal itself, with its code, rather than a 401.
|
|
15
|
+
* - **Every sign-in and every token refresh** — `session.create.before` in `../instance/auth.ts`. Every way
|
|
16
|
+
* a session comes into being goes through `createSession`, so one hook covers magic link, OTP, every
|
|
17
|
+
* social callback and `/token/rotate` alike.
|
|
18
|
+
* - **Better Auth's own endpoints that act as the session's holder** — `/token`, `/get-session`,
|
|
19
|
+
* `/update-user` and the rest — refused in `handleBetterAuth` from the middleware's verdict, because
|
|
20
|
+
* Better Auth resolves its own session there and never asks this capability.
|
|
21
|
+
*
|
|
22
|
+
* The reason for a suspension is the one read only a refused request pays for: it lives in
|
|
23
|
+
* `pithy_auth_user_lifecycle`, not on the user row, so an account in good standing never costs a lookup.
|
|
24
|
+
*/
|
|
25
|
+
/** The reason given for the suspension in force — the newest `suspended` row — or null when none was written. */
|
|
26
|
+
export declare function suspensionReason(db: AuthDatabase, userId: string): Promise<string | null>;
|
|
27
|
+
/** An account's lifecycle columns, read from its row, or `null` when there is no such account. */
|
|
28
|
+
export declare function readLifecycleColumns(db: AuthDatabase, userId: string): Promise<LifecycleColumns | null>;
|
|
29
|
+
/**
|
|
30
|
+
* The refusal for this account at `now`, or `null` when it may act.
|
|
31
|
+
*
|
|
32
|
+
* `columns` is what the caller already holds — the session's user object, a row it read. Absent, the row is
|
|
33
|
+
* read here, which is the sign-in path: a session about to be created has no user object yet.
|
|
34
|
+
*/
|
|
35
|
+
export declare function lifecycleRefusalFor(db: AuthDatabase, userId: string, columns: Partial<LifecycleColumns> | null | undefined, now: Date): Promise<PithyError | null>;
|
|
36
|
+
/** The holder of a refused credential: whose session it is. Known because the credential itself is genuine. */
|
|
37
|
+
export interface RefusedHolder {
|
|
38
|
+
/** The account the session belongs to. */
|
|
39
|
+
readonly userId: string;
|
|
40
|
+
/** The session the request presented. */
|
|
41
|
+
readonly sessionId: string;
|
|
42
|
+
}
|
|
43
|
+
/** Remember that this request's credential belongs to an account that may not act, and whose it is. */
|
|
44
|
+
export declare function noteRefusal(context: object, refusal: PithyError, holder?: RefusedHolder): void;
|
|
45
|
+
/** The refusal the session middleware reached for this request, if it reached one. */
|
|
46
|
+
export declare function noticedRefusal(context: object): PithyError | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* The holder of a **suspended** account's credential on this request, or `undefined` (#622).
|
|
49
|
+
*
|
|
50
|
+
* Only a suspension: a deleted account has nothing left to delete, and a check that could not be made has
|
|
51
|
+
* established nobody. The one reader is the self-service deletion, through `../http/middleware.ts`.
|
|
52
|
+
*/
|
|
53
|
+
export declare function suspendedHolder(context: object): RefusedHolder | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* The header a sign-in carries to ask for a session that can do nothing but delete its account, and the one
|
|
56
|
+
* value it takes (#622).
|
|
57
|
+
*
|
|
58
|
+
* A suspension revokes every session the account holds and refuses every new one, so without this a suspended
|
|
59
|
+
* person would hold no credential and could get none — and the one route a suspension leaves open, deleting the
|
|
60
|
+
* account, would be one nobody could reach. With it, the sign-in completes: the person still proves who they are,
|
|
61
|
+
* by the code, the link or the provider, exactly as for any sign-in. **The session that results is still a
|
|
62
|
+
* suspended account's**, so the session middleware refuses it everywhere, Better Auth's own endpoints included,
|
|
63
|
+
* and only `DELETE /auth/account` admits it. Nothing about the header is trusted beyond that: it asks for a
|
|
64
|
+
* session no more capable than none, save for leaving.
|
|
65
|
+
*/
|
|
66
|
+
export declare const ACCOUNT_DELETION_INTENT_HEADER = "x-pithy-intent";
|
|
67
|
+
/** The value of `ACCOUNT_DELETION_INTENT_HEADER` that asks for a deletion-only session. */
|
|
68
|
+
export declare const ACCOUNT_DELETION_INTENT = "delete-account";
|
|
69
|
+
/**
|
|
70
|
+
* Whether a sign-in refused with `refusal` may still create its session: only a **suspended** account's, and only
|
|
71
|
+
* when the request asks for nothing but deleting it. A deleted account has nothing left to delete, and a check
|
|
72
|
+
* that could not be made has established nobody, so neither is admitted.
|
|
73
|
+
*/
|
|
74
|
+
export declare function admitsDeletionSignIn(refusal: PithyError, headers: Headers | null | undefined): boolean;
|
|
75
|
+
//# sourceMappingURL=enforce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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AS,CAG1E;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,8BAA8B,mBAAmB,CAAC;AAC/D,2FAA2F;AAC3F,eAAO,MAAM,uBAAuB,mBAAmB,CAAC;AAExD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAItG"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { LifecycleColumns, accountStanding, lifecycleRefusal } from "./state.js";
|
|
4
|
+
//#region src/lifecycle/enforce.ts
|
|
5
|
+
/**
|
|
6
|
+
* The reads enforcement needs, and the one place a refusal is remembered for the rest of a request.
|
|
7
|
+
*
|
|
8
|
+
* ## Where an account is refused
|
|
9
|
+
*
|
|
10
|
+
* - **Every request** — the session middleware reads the state off the user Better Auth resolved for the
|
|
11
|
+
* session, which carries the columns, so the check costs no query. A refused account gets no
|
|
12
|
+
* `AuthContext`, so every `requireAuth()` in the kit refuses it whoever wrote the guard; this capability's
|
|
13
|
+
* own `requireAuth()` raises the refusal itself, with its code, rather than a 401.
|
|
14
|
+
* - **Every sign-in and every token refresh** — `session.create.before` in `../instance/auth.ts`. Every way
|
|
15
|
+
* a session comes into being goes through `createSession`, so one hook covers magic link, OTP, every
|
|
16
|
+
* social callback and `/token/rotate` alike.
|
|
17
|
+
* - **Better Auth's own endpoints that act as the session's holder** — `/token`, `/get-session`,
|
|
18
|
+
* `/update-user` and the rest — refused in `handleBetterAuth` from the middleware's verdict, because
|
|
19
|
+
* Better Auth resolves its own session there and never asks this capability.
|
|
20
|
+
*
|
|
21
|
+
* The reason for a suspension is the one read only a refused request pays for: it lives in
|
|
22
|
+
* `pithy_auth_user_lifecycle`, not on the user row, so an account in good standing never costs a lookup.
|
|
23
|
+
*/
|
|
24
|
+
/** The reason given for the suspension in force — the newest `suspended` row — or null when none was written. */
|
|
25
|
+
async function suspensionReason(db, userId) {
|
|
26
|
+
const reason = (await db.selectFrom("pithyAuthUserLifecycle").select("reason").where("userId", "=", userId).where("action", "=", "suspended").orderBy("id", "desc").limit(1).executeTakeFirst())?.reason?.trim();
|
|
27
|
+
return reason ? reason : null;
|
|
28
|
+
}
|
|
29
|
+
/** An account's lifecycle columns, read from its row, or `null` when there is no such account. */
|
|
30
|
+
async function readLifecycleColumns(db, userId) {
|
|
31
|
+
const row = await db.selectFrom("pithyAuthUsers").select([
|
|
32
|
+
"suspendedAt",
|
|
33
|
+
"suspendedUntil",
|
|
34
|
+
"deletedAt",
|
|
35
|
+
"purgeAfter"
|
|
36
|
+
]).where("id", "=", userId).executeTakeFirst();
|
|
37
|
+
return row ? LifecycleColumns.parse(row) : null;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The refusal for this account at `now`, or `null` when it may act.
|
|
41
|
+
*
|
|
42
|
+
* `columns` is what the caller already holds — the session's user object, a row it read. Absent, the row is
|
|
43
|
+
* read here, which is the sign-in path: a session about to be created has no user object yet.
|
|
44
|
+
*/
|
|
45
|
+
async function lifecycleRefusalFor(db, userId, columns, now) {
|
|
46
|
+
const held = columns ?? await readLifecycleColumns(db, userId) ?? {};
|
|
47
|
+
const standing = accountStanding(held, now);
|
|
48
|
+
if (standing.state === "active") return null;
|
|
49
|
+
const reason = standing.state === "suspended" ? await suspensionReason(db, userId) : null;
|
|
50
|
+
return lifecycleRefusal(userId, standing, reason);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The refusal the session middleware reached for this request, keyed by the request's context.
|
|
54
|
+
*
|
|
55
|
+
* A WeakMap rather than a request variable because `PithyVars` is core's, and this is a fact only this
|
|
56
|
+
* capability's own guards read — a refused account already has no `AuthContext`, which is the part every
|
|
57
|
+
* other capability's guard needs. Keyed by the context the way `../http/resolve.ts` caches the instance, so
|
|
58
|
+
* it dies with the request.
|
|
59
|
+
*
|
|
60
|
+
* **The holder rides with it (#622).** A suspended account may still delete itself — the app stores require
|
|
61
|
+
* that a person can always leave — and that one route has to know whose account the refused credential
|
|
62
|
+
* opens. Nothing else reads it: the holder is reachable only through `accountHolder`, and only that route uses it.
|
|
63
|
+
*/
|
|
64
|
+
const refusals = /* @__PURE__ */ new WeakMap();
|
|
65
|
+
/** Remember that this request's credential belongs to an account that may not act, and whose it is. */
|
|
66
|
+
function noteRefusal(context, refusal, holder) {
|
|
67
|
+
refusals.set(context, {
|
|
68
|
+
refusal,
|
|
69
|
+
holder
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** The refusal the session middleware reached for this request, if it reached one. */
|
|
73
|
+
function noticedRefusal(context) {
|
|
74
|
+
return refusals.get(context)?.refusal;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The holder of a **suspended** account's credential on this request, or `undefined` (#622).
|
|
78
|
+
*
|
|
79
|
+
* Only a suspension: a deleted account has nothing left to delete, and a check that could not be made has
|
|
80
|
+
* established nobody. The one reader is the self-service deletion, through `../http/middleware.ts`.
|
|
81
|
+
*/
|
|
82
|
+
function suspendedHolder(context) {
|
|
83
|
+
const noted = refusals.get(context);
|
|
84
|
+
return noted?.refusal.payload.code === "auth/account_suspended" ? noted.holder : void 0;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The header a sign-in carries to ask for a session that can do nothing but delete its account, and the one
|
|
88
|
+
* value it takes (#622).
|
|
89
|
+
*
|
|
90
|
+
* A suspension revokes every session the account holds and refuses every new one, so without this a suspended
|
|
91
|
+
* person would hold no credential and could get none — and the one route a suspension leaves open, deleting the
|
|
92
|
+
* account, would be one nobody could reach. With it, the sign-in completes: the person still proves who they are,
|
|
93
|
+
* by the code, the link or the provider, exactly as for any sign-in. **The session that results is still a
|
|
94
|
+
* suspended account's**, so the session middleware refuses it everywhere, Better Auth's own endpoints included,
|
|
95
|
+
* and only `DELETE /auth/account` admits it. Nothing about the header is trusted beyond that: it asks for a
|
|
96
|
+
* session no more capable than none, save for leaving.
|
|
97
|
+
*/
|
|
98
|
+
const ACCOUNT_DELETION_INTENT_HEADER = "x-pithy-intent";
|
|
99
|
+
/** The value of `ACCOUNT_DELETION_INTENT_HEADER` that asks for a deletion-only session. */
|
|
100
|
+
const ACCOUNT_DELETION_INTENT = "delete-account";
|
|
101
|
+
/**
|
|
102
|
+
* Whether a sign-in refused with `refusal` may still create its session: only a **suspended** account's, and only
|
|
103
|
+
* when the request asks for nothing but deleting it. A deleted account has nothing left to delete, and a check
|
|
104
|
+
* that could not be made has established nobody, so neither is admitted.
|
|
105
|
+
*/
|
|
106
|
+
function admitsDeletionSignIn(refusal, headers) {
|
|
107
|
+
if (refusal.payload.code !== "auth/account_suspended") return false;
|
|
108
|
+
return headers?.get(ACCOUNT_DELETION_INTENT_HEADER)?.trim().toLowerCase() === ACCOUNT_DELETION_INTENT;
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
export { ACCOUNT_DELETION_INTENT, ACCOUNT_DELETION_INTENT_HEADER, admitsDeletionSignIn, lifecycleRefusalFor, noteRefusal, noticedRefusal, readLifecycleColumns, suspendedHolder, suspensionReason };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
4
|
+
import type { UserLifecycleActorType } from "../data/userLifecycle";
|
|
5
|
+
/**
|
|
6
|
+
* The four seams an adopter's own code follows the account lifecycle through, without polling (#620).
|
|
7
|
+
*
|
|
8
|
+
* **On the transition, not on a route.** An account is suspended from the management surface today and may be
|
|
9
|
+
* from somewhere else tomorrow; a listener hung off one route would miss the others, which is the argument
|
|
10
|
+
* `onSessionRevoked` already makes for the session row. `./transitions.ts` is the only place a transition is
|
|
11
|
+
* written, so it is the only place these fire — once per transition that actually happened, never for a
|
|
12
|
+
* request that found the account already in the state it asked for.
|
|
13
|
+
*
|
|
14
|
+
* **Non-fatal by contract.** By the time a hook runs the transition is written and recorded. A hook that throws
|
|
15
|
+
* is logged and swallowed, so it can neither undo the transition nor turn it into an error the caller retries
|
|
16
|
+
* — a retry would find the account already moved and fire nothing, and the adopter's side effect would be
|
|
17
|
+
* lost either way.
|
|
18
|
+
*/
|
|
19
|
+
/** What happened, handed to every hook. */
|
|
20
|
+
export interface UserLifecycleHookEvent {
|
|
21
|
+
/** The account it happened to. */
|
|
22
|
+
readonly userId: string;
|
|
23
|
+
/** Who made the transition: the kind, and the principal's id where there is one. */
|
|
24
|
+
readonly actor: {
|
|
25
|
+
readonly type: UserLifecycleActorType;
|
|
26
|
+
readonly id: string | null;
|
|
27
|
+
};
|
|
28
|
+
/** The reason given, or null when the transition needed none. */
|
|
29
|
+
readonly reason: string | null;
|
|
30
|
+
/** When the transition was written. */
|
|
31
|
+
readonly at: Date;
|
|
32
|
+
/** For a suspension, when it ends by itself; null for one that lasts until a reinstatement. */
|
|
33
|
+
readonly suspendedUntil?: Date | null;
|
|
34
|
+
/** For a deletion, when the account's data may be erased. */
|
|
35
|
+
readonly purgeAfter?: Date | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* One hook. Handed the request's D1 binding, for the reason `SessionRevokedListener` is: the listener almost
|
|
39
|
+
* certainly writes to the same database under a different capability's view of it.
|
|
40
|
+
*/
|
|
41
|
+
export type UserLifecycleListener = (event: UserLifecycleHookEvent, d1: D1Database) => void | Promise<void>;
|
|
42
|
+
/** The four hooks, each optional. Passed to `auth()` beside the config. */
|
|
43
|
+
export interface UserLifecycleHooks {
|
|
44
|
+
/** An account was suspended. */
|
|
45
|
+
readonly onUserSuspended?: UserLifecycleListener;
|
|
46
|
+
/** A suspended account was reinstated. */
|
|
47
|
+
readonly onUserReinstated?: UserLifecycleListener;
|
|
48
|
+
/** An account was deleted — by an operator or by its owner — and its purge window began. */
|
|
49
|
+
readonly onUserDeleted?: UserLifecycleListener;
|
|
50
|
+
/** A deleted account's data was erased. The last thing that happens to it. */
|
|
51
|
+
readonly onUserPurged?: UserLifecycleListener;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/lifecycle/hooks.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAEpE;;;;;;;;;;;;;GAaG;AAEH,2CAA2C;AAC3C,MAAM,WAAW,sBAAsB;IACrC,kCAAkC;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oFAAoF;IACpF,QAAQ,CAAC,KAAK,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;QAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IACtF,iEAAiE;IACjE,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,uCAAuC;IACvC,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAClB,+FAA+F;IAC/F,QAAQ,CAAC,cAAc,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACtC,6DAA6D;IAC7D,QAAQ,CAAC,UAAU,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG,CAAC,KAAK,EAAE,sBAAsB,EAAE,EAAE,EAAE,UAAU,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE5G,2EAA2E;AAC3E,MAAM,WAAW,kBAAkB;IACjC,gCAAgC;IAChC,QAAQ,CAAC,eAAe,CAAC,EAAE,qBAAqB,CAAC;IACjD,0CAA0C;IAC1C,QAAQ,CAAC,gBAAgB,CAAC,EAAE,qBAAqB,CAAC;IAClD,4FAA4F;IAC5F,QAAQ,CAAC,aAAa,CAAC,EAAE,qBAAqB,CAAC;IAC/C,8EAA8E;IAC9E,QAAQ,CAAC,YAAY,CAAC,EAAE,qBAAqB,CAAC;CAC/C"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* The longest reason the kit stores. It is shown to the person, so it is a sentence, not a case file.
|
|
5
|
+
*
|
|
6
|
+
* A leaf with no imports, because two programs read it: the transitions, which refuse a longer one, and the
|
|
7
|
+
* request schemas in `../http/schemas.ts`, which a browser program compiles (`tooling/browser-scopes`). Kept
|
|
8
|
+
* in `./transitions.ts`, it pulled the email capability and the Workers types into that program (#621).
|
|
9
|
+
*/
|
|
10
|
+
export declare const MAX_REASON = 500;
|
|
11
|
+
//# sourceMappingURL=reason.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reason.d.ts","sourceRoot":"","sources":["../../src/lifecycle/reason.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,MAAM,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
//#region src/lifecycle/reason.ts
|
|
4
|
+
/**
|
|
5
|
+
* The longest reason the kit stores. It is shown to the person, so it is a sentence, not a case file.
|
|
6
|
+
*
|
|
7
|
+
* A leaf with no imports, because two programs read it: the transitions, which refuse a longer one, and the
|
|
8
|
+
* request schemas in `../http/schemas.ts`, which a browser program compiles (`tooling/browser-scopes`). Kept
|
|
9
|
+
* in `./transitions.ts`, it pulled the email capability and the Workers types into that program (#621).
|
|
10
|
+
*/
|
|
11
|
+
const MAX_REASON = 500;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { MAX_REASON };
|