@pithy-sh/auth 0.7.0 → 0.9.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 +4 -0
- package/dist/caller/certificate.d.ts +83 -0
- package/dist/caller/certificate.d.ts.map +1 -0
- package/dist/caller/certificate.js +92 -0
- package/dist/caller/request.d.ts +66 -0
- package/dist/caller/request.d.ts.map +1 -0
- package/dist/caller/request.js +71 -0
- package/dist/caller/token.d.ts +130 -0
- package/dist/caller/token.d.ts.map +1 -0
- package/dist/caller/token.js +207 -0
- package/dist/capability.d.ts +44 -1
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +7 -4
- package/dist/data/betterAuth.d.ts +1 -1
- package/dist/data/betterAuth.d.ts.map +1 -1
- package/dist/data/betterAuth.js +1 -1
- package/dist/data/tables.d.ts +1 -1
- package/dist/http/adminRoutes.d.ts.map +1 -1
- package/dist/http/adminRoutes.js +39 -8
- package/dist/http/credentialResign.d.ts +46 -0
- package/dist/http/credentialResign.d.ts.map +1 -0
- package/dist/http/credentialResign.js +203 -0
- package/dist/http/devLoginRoute.d.ts.map +1 -1
- package/dist/http/devLoginRoute.js +12 -5
- package/dist/http/errorCallbackUrl.d.ts.map +1 -1
- package/dist/http/errorCallbackUrl.js +15 -9
- package/dist/http/middleware.d.ts.map +1 -1
- package/dist/http/middleware.js +66 -15
- package/dist/http/providerRefusal.d.ts +8 -7
- package/dist/http/providerRefusal.d.ts.map +1 -1
- package/dist/http/providerRefusal.js +29 -32
- package/dist/http/resolve.d.ts.map +1 -1
- package/dist/http/resolve.js +18 -3
- package/dist/http/responses.d.ts +52 -0
- package/dist/http/responses.d.ts.map +1 -1
- package/dist/http/responses.js +25 -1
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +60 -9
- package/dist/http/schemas.d.ts +1 -1
- package/dist/http/schemas.js +1 -1
- package/dist/instance/auth.d.ts +1225 -2
- package/dist/instance/auth.d.ts.map +1 -1
- package/dist/instance/auth.js +6 -2
- package/dist/instance/jwksReencryption.d.ts +75 -0
- package/dist/instance/jwksReencryption.d.ts.map +1 -0
- package/dist/instance/jwksReencryption.js +160 -0
- package/dist/instance/providerSignInGate.d.ts +7 -6
- package/dist/instance/providerSignInGate.d.ts.map +1 -1
- package/dist/instance/providerSignInGate.js +16 -14
- package/dist/instance/providers.d.ts.map +1 -1
- package/dist/instance/providers.js +2 -1
- package/dist/instance/secrets.d.ts +168 -1
- package/dist/instance/secrets.d.ts.map +1 -1
- package/dist/instance/secrets.js +86 -3
- package/dist/instance/sessionSecrets.d.ts +45 -0
- package/dist/instance/sessionSecrets.d.ts.map +1 -0
- package/dist/instance/sessionSecrets.js +29 -0
- package/dist/migrations/0001_init.d.ts.map +1 -1
- package/dist/migrations/0003_account_issuer_nullable.d.ts +5 -0
- package/dist/migrations/0003_account_issuer_nullable.d.ts.map +1 -0
- package/dist/migrations/0003_account_issuer_nullable.js +131 -0
- package/dist/migrations/set.d.ts +2 -1
- package/dist/migrations/set.d.ts.map +1 -1
- package/dist/migrations/set.js +4 -2
- package/dist/seeds/devSession.d.ts.map +1 -1
- package/dist/seeds/devSession.js +2 -1
- package/dist/test-utils/liveApp.d.ts +5 -0
- package/dist/test-utils/liveApp.d.ts.map +1 -1
- package/dist/test-utils/liveApp.js +3 -3
- package/dist/test-utils/schemaDrift.d.ts +49 -0
- package/dist/test-utils/schemaDrift.d.ts.map +1 -0
- package/dist/test-utils/schemaDrift.js +35 -0
- package/dist/test-utils/scopedBackend.d.ts +183 -0
- package/dist/test-utils/scopedBackend.d.ts.map +1 -0
- package/dist/test-utils/scopedBackend.js +160 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/docs/server-authoritative.md +182 -0
- package/package.json +14 -11
- package/src/caller/certificate.ts +133 -0
- package/src/caller/request.ts +84 -0
- package/src/caller/token.ts +276 -0
- package/src/capability.ts +65 -4
- package/src/data/betterAuth.ts +2 -1
- package/src/http/adminRoutes.ts +46 -10
- package/src/http/credentialResign.ts +268 -0
- package/src/http/devLoginRoute.ts +19 -6
- package/src/http/errorCallbackUrl.ts +21 -13
- package/src/http/middleware.ts +100 -35
- package/src/http/providerRefusal.ts +30 -32
- package/src/http/resolve.ts +27 -3
- package/src/http/responses.ts +54 -0
- package/src/http/routes.ts +97 -11
- package/src/http/schemas.ts +1 -1
- package/src/instance/auth.ts +22 -4
- package/src/instance/jwksReencryption.ts +206 -0
- package/src/instance/providerSignInGate.ts +23 -21
- package/src/instance/providers.ts +2 -1
- package/src/instance/secrets.ts +94 -1
- package/src/instance/sessionSecrets.ts +64 -0
- package/src/migrations/0001_init.ts +8 -10
- package/src/migrations/0003_account_issuer_nullable.ts +199 -0
- package/src/migrations/set.ts +3 -1
- package/src/seeds/devSession.ts +2 -1
- package/src/test-utils/liveApp.ts +8 -6
- package/src/test-utils/schemaDrift.ts +82 -0
- package/src/test-utils/scopedBackend.ts +136 -0
- package/src/version.generated.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../../src/instance/secrets.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../../src/instance/secrets.ts"],"names":[],"mappings":"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"}
|
package/dist/instance/secrets.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { ValidationError } from "@pithy-sh/core/src/error/pithyError";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
import { defineSecretRegistry } from "@pithy-sh/secrets/src/registry";
|
|
5
6
|
import { sharedSecretsStore } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
@@ -28,6 +29,37 @@ const AUTH_GOOGLE_CREDENTIALS = "auth-google-credentials";
|
|
|
28
29
|
const AUTH_APPLE_CREDENTIALS = "auth-apple-credentials";
|
|
29
30
|
const AUTH_FACEBOOK_CREDENTIALS = "auth-facebook-credentials";
|
|
30
31
|
const AUTH_GITHUB_CREDENTIALS = "auth-github-credentials";
|
|
32
|
+
/**
|
|
33
|
+
* The JWKS private keys, as `auth-session-secret`'s overlap names them (#602).
|
|
34
|
+
*
|
|
35
|
+
* Better Auth encrypts every key in `pithy_auth_jwks` under the session secret, and a key never expires —
|
|
36
|
+
* so retiring the version one is sealed under would leave a signing key nothing can open. The session
|
|
37
|
+
* secret's registry entry names this dependent, `instance/jwksReencryption.ts` moves the keys under the
|
|
38
|
+
* newest version and records it, and the secrets manager retires nothing until it has.
|
|
39
|
+
*/
|
|
40
|
+
const AUTH_JWKS_DEPENDENT = "auth-jwks";
|
|
41
|
+
/**
|
|
42
|
+
* The Better Auth plugins that seal rows of their own under the session secret, by id.
|
|
43
|
+
*
|
|
44
|
+
* `two-factor` seals every TOTP secret with `symmetricEncrypt({ key: ctx.context.secretConfig })`, and its
|
|
45
|
+
* backup codes the same way under `storeBackupCodes: "encrypted"` (`plugins/two-factor/index.mjs`,
|
|
46
|
+
* `backup-codes/index.mjs`). Neither expires. Nothing in the kit re-encrypts them, so an unattended rotation
|
|
47
|
+
* beside one is refused at `auth()` rather than allowed to retire the version they are sealed under.
|
|
48
|
+
*/
|
|
49
|
+
const SESSION_SECRET_SEALING_PLUGINS = ["two-factor"];
|
|
50
|
+
/** Seconds in a day. */
|
|
51
|
+
const DAY_SECONDS = 86400;
|
|
52
|
+
/**
|
|
53
|
+
* How many days a replaced session secret keeps verifying: the session's own lifetime, rounded up.
|
|
54
|
+
*
|
|
55
|
+
* **That is the whole of the overlap's argument.** A session in use is re-signed on its next request
|
|
56
|
+
* (`http/credentialResign.ts`), and its expiry slides forward on use — so a session that goes the whole
|
|
57
|
+
* overlap without one request has expired by the time the overlap ends. Any shorter, and a session used
|
|
58
|
+
* yesterday is signed out when the old version retires.
|
|
59
|
+
*/
|
|
60
|
+
function sessionSecretOverlapDays(sessionExpiresIn) {
|
|
61
|
+
return Math.max(1, Math.ceil(sessionExpiresIn / DAY_SECONDS));
|
|
62
|
+
}
|
|
31
63
|
/** The Google OAuth credential pair, stored as one typed JSON secret. */
|
|
32
64
|
const GoogleOAuthCredentials = z.object({
|
|
33
65
|
clientId: z.string().meta({ multiline: false }).describe("The Google OAuth client id for this environment. Not secret on its own, but stored with the secret so the pair is atomic."),
|
|
@@ -76,7 +108,12 @@ const authSecretsRegistry = defineSecretRegistry({
|
|
|
76
108
|
encoding: "base64url"
|
|
77
109
|
}
|
|
78
110
|
},
|
|
79
|
-
rotation: { kind: "local" }
|
|
111
|
+
rotation: { kind: "local" },
|
|
112
|
+
overlap: {
|
|
113
|
+
days: 7,
|
|
114
|
+
minimumDays: 7,
|
|
115
|
+
reencrypt: [AUTH_JWKS_DEPENDENT]
|
|
116
|
+
}
|
|
80
117
|
},
|
|
81
118
|
[AUTH_GOOGLE_CREDENTIALS]: {
|
|
82
119
|
backend: "d1",
|
|
@@ -148,6 +185,44 @@ const authSecretsRegistry = defineSecretRegistry({
|
|
|
148
185
|
}
|
|
149
186
|
});
|
|
150
187
|
/**
|
|
188
|
+
* **The registry `auth()` contributes, from its own configuration (#602).**
|
|
189
|
+
*
|
|
190
|
+
* The session secret's overlap follows the configured session lifetime — {@link sessionSecretOverlapDays} —
|
|
191
|
+
* and it is both the overlap and its floor: an adopter who lengthens sessions lengthens the overlap with
|
|
192
|
+
* them, rather than holding a seven-day overlap against a thirty-day session. `rotateEveryDays` is
|
|
193
|
+
* `sessionSecretRotateEveryDays`, and only an adopter sets it: the kit turns no unattended rotation on.
|
|
194
|
+
*
|
|
195
|
+
* Checked by `defineSecretRegistry` like every registry, so a period no longer than the session lifetime is
|
|
196
|
+
* refused here, at `auth()`, naming the secret — rather than at a 03:00 cron that could only skip it.
|
|
197
|
+
*
|
|
198
|
+
* **A period is refused beside a plugin that seals rows under the session secret** —
|
|
199
|
+
* {@link SESSION_SECRET_SEALING_PLUGINS}. Only the JWKS keys are moved before a retirement; a plugin's own
|
|
200
|
+
* sealed rows would be left under the retired version, and nothing could open them again.
|
|
201
|
+
*/
|
|
202
|
+
function authSecretsRegistryFor(config) {
|
|
203
|
+
if (config.sessionSecretRotateEveryDays !== void 0) {
|
|
204
|
+
const sealing = (config.plugins ?? []).find((plugin) => SESSION_SECRET_SEALING_PLUGINS.includes(plugin.id));
|
|
205
|
+
if (sealing !== void 0) throw new ValidationError({
|
|
206
|
+
message: `${AUTH_SESSION_SECRET} cannot rotate unattended while the Better Auth "${sealing.id}" plugin is composed.`,
|
|
207
|
+
action: `Remove sessionSecretRotateEveryDays from auth({ ... }), or remove ${sealing.id}() from auth({ plugins: [...] }). ${sealing.id}() seals its secrets under ${AUTH_SESSION_SECRET}, and a rotation retires the version they are sealed under with nothing to move them first.`,
|
|
208
|
+
detail: `sessionSecretRotateEveryDays set with plugin "${sealing.id}", which seals rows under ${AUTH_SESSION_SECRET}`
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const days = sessionSecretOverlapDays(config.sessionExpiresIn);
|
|
212
|
+
return defineSecretRegistry({
|
|
213
|
+
...authSecretsRegistry,
|
|
214
|
+
[AUTH_SESSION_SECRET]: {
|
|
215
|
+
...authSecretsRegistry[AUTH_SESSION_SECRET],
|
|
216
|
+
overlap: {
|
|
217
|
+
days,
|
|
218
|
+
minimumDays: days,
|
|
219
|
+
reencrypt: [AUTH_JWKS_DEPENDENT]
|
|
220
|
+
},
|
|
221
|
+
...config.sessionSecretRotateEveryDays === void 0 ? {} : { rotateEveryDays: config.sessionSecretRotateEveryDays }
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
151
226
|
* Which provider a credential belongs to — the join the CLI must never make by spelling (#541).
|
|
152
227
|
*
|
|
153
228
|
* `auth-apple-credentials` and the `apple` config key look like each other and that is a coincidence:
|
|
@@ -182,10 +257,18 @@ function inapplicableProviderSecrets(providers) {
|
|
|
182
257
|
}
|
|
183
258
|
return declared;
|
|
184
259
|
}
|
|
185
|
-
/** Resolve the Better Auth session secret for this invocation. */
|
|
260
|
+
/** Resolve the Better Auth session secret for this invocation — the current version alone. */
|
|
186
261
|
async function resolveSessionSecret(env) {
|
|
187
262
|
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_SESSION_SECRET);
|
|
188
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Resolve every retained version of the session secret (#602) — what an instance needs to open values sealed
|
|
266
|
+
* under the previous version, and what a credential signed under it is verified against. One read through
|
|
267
|
+
* the same cached accessor as {@link resolveSessionSecret}.
|
|
268
|
+
*/
|
|
269
|
+
async function resolveSessionSecrets(env) {
|
|
270
|
+
return (await sharedSecretsStore(env, authSecretsRegistry)).getVersions(AUTH_SESSION_SECRET);
|
|
271
|
+
}
|
|
189
272
|
/** Resolve the Google OAuth credential pair for this invocation (only call when Google is enabled). */
|
|
190
273
|
async function resolveGoogleCredentials(env) {
|
|
191
274
|
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_GOOGLE_CREDENTIALS);
|
|
@@ -203,4 +286,4 @@ async function resolveGithubCredentials(env) {
|
|
|
203
286
|
return (await sharedSecretsStore(env, authSecretsRegistry)).get(AUTH_GITHUB_CREDENTIALS);
|
|
204
287
|
}
|
|
205
288
|
//#endregion
|
|
206
|
-
export { AUTH_APPLE_CREDENTIALS, AUTH_FACEBOOK_CREDENTIALS, AUTH_GITHUB_CREDENTIALS, AUTH_GOOGLE_CREDENTIALS, AUTH_SESSION_SECRET, AppleOAuthCredentials, FacebookOAuthCredentials, GithubOAuthCredentials, GoogleOAuthCredentials, authSecretsRegistry, inapplicableProviderSecrets, resolveAppleCredentials, resolveFacebookCredentials, resolveGithubCredentials, resolveGoogleCredentials, resolveSessionSecret };
|
|
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 };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* **The session secret as Better Auth needs it across a rotation (#602): every retained version.**
|
|
5
|
+
*
|
|
6
|
+
* `auth-session-secret` is `rotatable: true`, and an unattended rotation appends a version and keeps the
|
|
7
|
+
* previous one for the overlap. The kit used to hand Better Auth one value — the current one — which made
|
|
8
|
+
* the overlap decorative: the moment the version changed, the JWKS private keys were sealed under a value
|
|
9
|
+
* the instance no longer held. Better Auth 1.7 takes a version set instead (`secrets: [{ version, value }]`,
|
|
10
|
+
* newest first): it encrypts under the first and decrypts under whichever version a ciphertext names
|
|
11
|
+
* (`$ba$<version>$…`, `better-auth/dist/crypto/index.mjs`).
|
|
12
|
+
*
|
|
13
|
+
* **It versions encryption and not signatures.** With `secrets` set, `ctx.secret` is the first entry alone
|
|
14
|
+
* (`context/create-context.mjs:75`), and the session cookie is signed and verified under `ctx.secret`
|
|
15
|
+
* (`cookies/index.mjs:172`). So a cookie signed under the previous version still fails after a rotation;
|
|
16
|
+
* `http/credentialResign.ts` is what verifies it under a retained version and re-signs it.
|
|
17
|
+
*
|
|
18
|
+
* **`secret` is the oldest retained version, and that is deliberate.** With a version set, Better Auth reads
|
|
19
|
+
* `secret` only as the *legacy* secret — the one it tries on a bare ciphertext with no `$ba$` envelope. Every
|
|
20
|
+
* JWKS key written before the kit passed a version set is one of those, sealed under the value that was
|
|
21
|
+
* current then, which is the oldest the envelope still holds. Re-encryption moves them into the envelope;
|
|
22
|
+
* until it has, this is what opens them.
|
|
23
|
+
*/
|
|
24
|
+
/** A secret's retained versions, as `secretsStore(…).getVersions` answers. */
|
|
25
|
+
export interface SessionSecretVersions {
|
|
26
|
+
/** The version key reads resolve to. */
|
|
27
|
+
currentVersion: string;
|
|
28
|
+
/** Every retained version: version key → value. */
|
|
29
|
+
versions: Readonly<Record<string, string>>;
|
|
30
|
+
}
|
|
31
|
+
/** What `betterAuth()` is handed: the version set, newest first, and the legacy secret. */
|
|
32
|
+
export interface BetterAuthSecretOptions {
|
|
33
|
+
/** Every retained version, current first, then the rest newest first. */
|
|
34
|
+
secrets: {
|
|
35
|
+
version: number;
|
|
36
|
+
value: string;
|
|
37
|
+
}[];
|
|
38
|
+
/** The oldest retained version — Better Auth's legacy secret, for a ciphertext sealed before versions existed. */
|
|
39
|
+
secret: string;
|
|
40
|
+
}
|
|
41
|
+
/** The version set and legacy secret for `betterAuth()`. */
|
|
42
|
+
export declare function betterAuthSecrets(versions: SessionSecretVersions): BetterAuthSecretOptions;
|
|
43
|
+
/** Every retained value but the current one, newest first — what a credential signed before a rotation is tried under. */
|
|
44
|
+
export declare function priorSessionSecrets(versions: SessionSecretVersions): string[];
|
|
45
|
+
//# sourceMappingURL=sessionSecrets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sessionSecrets.d.ts","sourceRoot":"","sources":["../../src/instance/sessionSecrets.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,8EAA8E;AAC9E,MAAM,WAAW,qBAAqB;IACpC,wCAAwC;IACxC,cAAc,EAAE,MAAM,CAAC;IACvB,mDAAmD;IACnD,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAC5C;AAED,2FAA2F;AAC3F,MAAM,WAAW,uBAAuB;IACtC,yEAAyE;IACzE,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC9C,kHAAkH;IAClH,MAAM,EAAE,MAAM,CAAC;CAChB;AAYD,4DAA4D;AAC5D,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,qBAAqB,GAAG,uBAAuB,CAI1F;AAED,0HAA0H;AAC1H,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,qBAAqB,GAAG,MAAM,EAAE,CAI7E"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
//#region src/instance/sessionSecrets.ts
|
|
4
|
+
/** The retained versions, current first and the rest in descending numeric order. */
|
|
5
|
+
function ordered(versions) {
|
|
6
|
+
const prior = Object.entries(versions.versions).filter(([version]) => version !== versions.currentVersion).map(([version, value]) => ({
|
|
7
|
+
version: Number(version),
|
|
8
|
+
value
|
|
9
|
+
})).sort((left, right) => right.version - left.version);
|
|
10
|
+
const current = versions.versions[versions.currentVersion];
|
|
11
|
+
return current === void 0 ? prior : [{
|
|
12
|
+
version: Number(versions.currentVersion),
|
|
13
|
+
value: current
|
|
14
|
+
}, ...prior];
|
|
15
|
+
}
|
|
16
|
+
/** The version set and legacy secret for `betterAuth()`. */
|
|
17
|
+
function betterAuthSecrets(versions) {
|
|
18
|
+
const secrets = ordered(versions);
|
|
19
|
+
return {
|
|
20
|
+
secrets,
|
|
21
|
+
secret: [...secrets].sort((left, right) => left.version - right.version)[0]?.value ?? ""
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Every retained value but the current one, newest first — what a credential signed before a rotation is tried under. */
|
|
25
|
+
function priorSessionSecrets(versions) {
|
|
26
|
+
return ordered(versions).filter((one) => String(one.version) !== versions.currentVersion).map((one) => one.value);
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { betterAuthSecrets, priorSessionSecrets };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"0001_init.d.ts","sourceRoot":"","sources":["../../src/migrations/0001_init.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,cAAc,EAAE,
|
|
1
|
+
{"version":3,"file":"0001_init.d.ts","sourceRoot":"","sources":["../../src/migrations/0001_init.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,cAAc,EAAE,SA6L5B,CAAC;AAEF,oHAAoH;AACpH,eAAO,MAAM,oBAAoB,MAAM,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"0003_account_issuer_nullable.d.ts","sourceRoot":"","sources":["../../src/migrations/0003_account_issuer_nullable.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AA2JlD,eAAO,MAAM,iCAAiC,EAAE,SAsC/C,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { ConflictError, InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
4
|
+
import { sql } from "kysely";
|
|
5
|
+
//#region src/migrations/0003_account_issuer_nullable.ts
|
|
6
|
+
/**
|
|
7
|
+
* Relax `pithy_auth_accounts.issuer` to nullable and drop the `(issuer, accountId)` unique index (#787).
|
|
8
|
+
*
|
|
9
|
+
* **Better Auth reverted `issuer` in a patch release.** 1.7.0 made an account the pair `(issuer, accountId)`,
|
|
10
|
+
* required `issuer`, and `0001_init` followed it (#451). 1.7.3 restored the 1.6 model, which identifies an
|
|
11
|
+
* account by `(providerId, accountId)` again (better-auth/better-auth#11153), and stopped writing `issuer`
|
|
12
|
+
* at all. So on 1.7.3 and later every insert into this table failed on the `NOT NULL`, and every email OTP
|
|
13
|
+
* sign-in answered 500. 1.7.3 also validates the schema at initialization, which is where the logged
|
|
14
|
+
* `Database schema mismatch` came from.
|
|
15
|
+
*
|
|
16
|
+
* **This is Better Auth's own cleanup, from its 1.7 upgrade guide.** Drop the unique index first, then
|
|
17
|
+
* relax the constraint. No backfill. The column stays, with every value in it: a Worker migrated under
|
|
18
|
+
* 1.7.1 loses nothing, and the step can be reversed. Dropping the column is a separate decision.
|
|
19
|
+
*
|
|
20
|
+
* **A `0003`, never an edit to `0001_init`.** `0001` has shipped and has run against real rows, so it is
|
|
21
|
+
* history. A fresh install runs `0001` then this, and ends in the same schema as an upgraded one.
|
|
22
|
+
*
|
|
23
|
+
* **SQLite has no `ALTER COLUMN`, so relaxing a constraint is a table rebuild.** A new table, every row
|
|
24
|
+
* copied across, the old one dropped, the new one renamed into its place, its indexes recreated. Nothing
|
|
25
|
+
* references this table by foreign key, so dropping it cascades into nothing.
|
|
26
|
+
*
|
|
27
|
+
* **The rebuild reads the table it replaces rather than a column list written here.** An adopter's Better
|
|
28
|
+
* Auth plugin can add columns and indexes to this table through its own `0002_plugin_<id>` migration, which
|
|
29
|
+
* sorts before this one, and a rebuild from a fixed list would drop them and their data without a word. So
|
|
30
|
+
* the columns come from `pragma_table_info` and the indexes from `sqlite_master`, and only `issuer`'s
|
|
31
|
+
* constraint and the one index are changed.
|
|
32
|
+
*
|
|
33
|
+
* **The swap is one round trip.** The migration runner queues schema statements and sends a run of them as
|
|
34
|
+
* one `d1.batch()`, which D1 applies as one transaction. A raw `sql` template is never queued: it flushes
|
|
35
|
+
* the queue and runs alone. So the index drops, the table drop, the rename and every index recreate are
|
|
36
|
+
* built as schema statements, back to back, and either all of them apply or none does. Separate, a run that
|
|
37
|
+
* died after the drop would leave auth with no accounts table, and one that died after the rename would
|
|
38
|
+
* leave it with no indexes and a retry that records Success over the loss.
|
|
39
|
+
*
|
|
40
|
+
* `down` is the tested inverse: the same rebuild with `NOT NULL` back, then the unique index, in the same
|
|
41
|
+
* batch. It refuses before it touches anything when a row has no `issuer`, since the constraint cannot hold
|
|
42
|
+
* over that row, or when two rows share an `(issuer, accountId)`, since the index cannot.
|
|
43
|
+
*/
|
|
44
|
+
const ACCOUNTS = "pithy_auth_accounts";
|
|
45
|
+
const ACCOUNTS_NEXT = "pithy_auth_accounts_next";
|
|
46
|
+
/**
|
|
47
|
+
* Every column of the accounts table, in declaration order.
|
|
48
|
+
*
|
|
49
|
+
* The names come back snake_case, as the database stores them, and go back into the rebuild the same
|
|
50
|
+
* way: `CamelCasePlugin` leaves a name with no capital in it as it is.
|
|
51
|
+
*/
|
|
52
|
+
async function accountColumns(db) {
|
|
53
|
+
const { rows } = await sql`select name, type, "notnull", dflt_value as initial, pk from pragma_table_info(${ACCOUNTS}) order by cid`.execute(db);
|
|
54
|
+
return rows;
|
|
55
|
+
}
|
|
56
|
+
/** The DDL of every index on the accounts table that SQLite did not create for itself. */
|
|
57
|
+
async function accountIndexes(db) {
|
|
58
|
+
const { rows } = await sql`select name, sql from sqlite_master where type = 'index' and tbl_name = ${ACCOUNTS} and sql is not null order by name`.execute(db);
|
|
59
|
+
return rows;
|
|
60
|
+
}
|
|
61
|
+
/** One column's definition, as `create table` takes it. */
|
|
62
|
+
function columnDefinition(column, issuerNotNull) {
|
|
63
|
+
const parts = [sql.id(column.name), sql.raw(column.type)];
|
|
64
|
+
if (column.pk === 1) parts.push(sql`primary key`);
|
|
65
|
+
if ((column.name === "issuer" ? issuerNotNull : column.notnull === 1) && column.pk !== 1) parts.push(sql`not null`);
|
|
66
|
+
if (column.initial !== null) parts.push(sql`default ${sql.raw(column.initial)}`);
|
|
67
|
+
return sql.join(parts, sql` `);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Rebuild the accounts table with `issuer` nullable or not, keeping every other column, row and index.
|
|
71
|
+
* The unique `(issuer, accountId)` index is dropped before the rebuild and is not carried across.
|
|
72
|
+
*/
|
|
73
|
+
async function rebuild(db, issuerNotNull) {
|
|
74
|
+
const columns = await accountColumns(db);
|
|
75
|
+
if (!columns.some((column) => column.name === "issuer")) throw new InternalError({
|
|
76
|
+
message: `${ACCOUNTS} has no issuer column, so there is no constraint to change.`,
|
|
77
|
+
action: "Compare the table with auth's 0001_init. Something outside the migration chain has reshaped it."
|
|
78
|
+
});
|
|
79
|
+
if (columns.filter((column) => column.pk > 0).length !== 1) throw new InternalError({
|
|
80
|
+
message: `${ACCOUNTS} does not have the single-column primary key 0001_init gave it.`,
|
|
81
|
+
action: "Compare the table with auth's 0001_init. Something outside the migration chain has reshaped it."
|
|
82
|
+
});
|
|
83
|
+
await sql`drop table if exists ${sql.id(ACCOUNTS_NEXT)}`.execute(db);
|
|
84
|
+
await db.schema.dropIndex("pithyAuthAccountsIssuerAccountIdIdx").ifExists().execute();
|
|
85
|
+
const indexes = await accountIndexes(db);
|
|
86
|
+
await sql`create table ${sql.id(ACCOUNTS_NEXT)} (${sql.join(columns.map((column) => columnDefinition(column, issuerNotNull)))})`.execute(db);
|
|
87
|
+
const names = sql.join(columns.map((column) => sql.id(column.name)));
|
|
88
|
+
await sql`insert into ${sql.id(ACCOUNTS_NEXT)} (${names}) select ${names} from ${sql.id(ACCOUNTS)}`.execute(db);
|
|
89
|
+
for (const index of indexes) await db.withoutPlugins().schema.dropIndex(index.name).execute();
|
|
90
|
+
await db.schema.dropTable("pithyAuthAccounts").execute();
|
|
91
|
+
await db.schema.alterTable("pithyAuthAccountsNext").renameTo("pithyAuthAccounts").execute();
|
|
92
|
+
for (const index of indexes) await db.executeQuery(recreateIndex(db, index));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* One index's recreate, as a schema statement carrying the DDL SQLite kept for it, verbatim.
|
|
96
|
+
*
|
|
97
|
+
* Verbatim, because a plugin's index can be partial, on an expression, or collated, and rebuilding it from
|
|
98
|
+
* a builder would mean parsing that back out of its DDL. The DDL names the table by the name the rebuilt
|
|
99
|
+
* one has once renamed. The statement is a `create index` node with that text as its SQL, which is exactly
|
|
100
|
+
* what it is, and the node is what lets the runner queue it into the batch the drop and rename are in. The
|
|
101
|
+
* runner reads only the node's kind, so the builder's column is a placeholder that never reaches SQLite.
|
|
102
|
+
*/
|
|
103
|
+
function recreateIndex(db, index) {
|
|
104
|
+
return {
|
|
105
|
+
...db.withoutPlugins().schema.createIndex(index.name).on(ACCOUNTS).column("id").compile(),
|
|
106
|
+
sql: index.sql
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const auth_0003_account_issuer_nullable = {
|
|
110
|
+
async up(db) {
|
|
111
|
+
await rebuild(db, false);
|
|
112
|
+
},
|
|
113
|
+
async down(db) {
|
|
114
|
+
const { rows: unissued } = await sql`select count(*) as count from ${sql.id(ACCOUNTS)} where issuer is null`.execute(db);
|
|
115
|
+
const missing = Number(unissued[0]?.count ?? 0);
|
|
116
|
+
if (missing > 0) throw new ConflictError({
|
|
117
|
+
message: `Cannot restore NOT NULL on ${ACCOUNTS}.issuer. ${missing} account row(s) have no issuer. Nothing was changed.`,
|
|
118
|
+
action: "Better Auth 1.7.3 and later never write an issuer. Fill the column for those rows, or stay on this migration."
|
|
119
|
+
});
|
|
120
|
+
const { rows: shared } = await sql`select count(*) as count from (select 1 from ${sql.id(ACCOUNTS)} group by issuer, account_id having count(*) > 1)`.execute(db);
|
|
121
|
+
const duplicated = Number(shared[0]?.count ?? 0);
|
|
122
|
+
if (duplicated > 0) throw new ConflictError({
|
|
123
|
+
message: `Cannot restore the unique (issuer, account_id) index on ${ACCOUNTS}. ${duplicated} pair(s) are held by more than one row. Nothing was changed.`,
|
|
124
|
+
action: "Resolve the duplicate accounts first. Each pair is one account at one identity provider."
|
|
125
|
+
});
|
|
126
|
+
await rebuild(db, true);
|
|
127
|
+
await db.schema.createIndex("pithyAuthAccountsIssuerAccountIdIdx").on("pithyAuthAccounts").columns(["issuer", "accountId"]).unique().execute();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
export { auth_0003_account_issuer_nullable };
|
package/dist/migrations/set.d.ts
CHANGED
|
@@ -10,11 +10,12 @@
|
|
|
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
|
-
*
|
|
13
|
+
* `0003` 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
|
+
readonly "0003_account_issuer_nullable": import("kysely/migration").Migration;
|
|
19
20
|
};
|
|
20
21
|
//# 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":"AAOA;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe;aAC1B,WAAW;aACX,+BAA+B;aAC/B,8BAA8B;CACtB,CAAC"}
|
package/dist/migrations/set.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
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
|
+
import { auth_0003_account_issuer_nullable } from "./0003_account_issuer_nullable.js";
|
|
5
6
|
//#region src/migrations/set.ts
|
|
6
7
|
/**
|
|
7
8
|
* Every migration `@pithy-sh/auth` ships, keyed as `pithy migrate` composes them.
|
|
@@ -13,12 +14,13 @@ import { auth_0002_session_authenticated_at } from "./0002_session_authenticated
|
|
|
13
14
|
* migration. Adding a `0002` turned all five into a schema one column short of the code, and the symptom
|
|
14
15
|
* was `table pithy_auth_sessions has no column named authenticated_at` in thirteen unrelated tests.
|
|
15
16
|
*
|
|
16
|
-
*
|
|
17
|
+
* `0003` appended here and reached every reader, which is the point. `0001_init.workers.test.ts` is deliberately not a reader:
|
|
17
18
|
* its subject is that one migration's own `up` and `down`, so it names it directly.
|
|
18
19
|
*/
|
|
19
20
|
const AUTH_MIGRATIONS = {
|
|
20
21
|
"0001_init": auth_0001_init,
|
|
21
|
-
"0002_session_authenticated_at": auth_0002_session_authenticated_at
|
|
22
|
+
"0002_session_authenticated_at": auth_0002_session_authenticated_at,
|
|
23
|
+
"0003_account_issuer_nullable": auth_0003_account_issuer_nullable
|
|
22
24
|
};
|
|
23
25
|
//#endregion
|
|
24
26
|
export { AUTH_MIGRATIONS };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devSession.d.ts","sourceRoot":"","sources":["../../src/seeds/devSession.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"devSession.d.ts","sourceRoot":"","sources":["../../src/seeds/devSession.ts"],"names":[],"mappings":"AAOA,OAAO,EAAkC,KAAK,QAAQ,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAC5G,OAAO,EAA6D,KAAK,OAAO,EAAE,MAAM,8BAA8B,CAAC;AACvH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,2BAA2B,QAAiB,CAAC;AAM1D;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,uBAAuB,QAAkC,CAAC;AAUvE,kHAAkH;AAClH,eAAO,MAAM,cAAc;;iBAWxB,CAAC;AACJ,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,cAAc,CAAC,CAAC;AAE7D;;;;;;GAMG;AACH,eAAO,MAAM,UAAU;;;iBAKgF,CAAC;AACxG,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;AAErD,sGAAsG;AACtG,MAAM,WAAW,cAAc;IAC7B,gEAAgE;IAChE,KAAK,EAAE,QAAQ,CAAC;CACjB;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAWpF;AAED,+FAA+F;AAC/F,MAAM,WAAW,iBAAiB;IAChC,qCAAqC;IACrC,IAAI,EAAE,UAAU,CAAC;IACjB,2GAA2G;IAC3G,MAAM,EAAE,MAAM,CAAC;IACf,uDAAuD;IACvD,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,iBAAiB,GAAG;IAAE,SAAS,EAAE,IAAI,CAAA;CAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CASvG;AAED;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,GAAG,GAAE,IAAiB,GACrB,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,IAAI,CAAA;CAAE,GAAG,IAAI,CAAC,CAqBrD;AAkDD;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,cAAc,CAAC,CAWpF;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,aAAa,CAAC,KAAK,EAAE;IACzC,KAAK,EAAE,SAAS,UAAU,EAAE,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ,GAAG,OAAO,CAAC,SAAS,CAAC,CAKrB;AAuDD;;;GAGG;AACH,eAAO,MAAM,kBAAkB,EAAE,OA2C/B,CAAC"}
|
package/dist/seeds/devSession.js
CHANGED
|
@@ -4,6 +4,7 @@ import { DEV_PROTOCOL, sessionCookieName } from "../http/baseUrl.js";
|
|
|
4
4
|
import { AUTH_SESSION_SECRET } from "../instance/secrets.js";
|
|
5
5
|
import { ValidationError, fromZodError } from "@pithy-sh/core/src/error/pithyError";
|
|
6
6
|
import { z } from "zod";
|
|
7
|
+
import { commandName } from "@pithy-sh/core/src/cli/command";
|
|
7
8
|
import { DEV_LOGIN_FILE, DevLogins } from "@pithy-sh/core/src/seed/devLogin";
|
|
8
9
|
import { normalizeAddress } from "@pithy-sh/core/src/address/address";
|
|
9
10
|
import { MAX_SEED_ORDER } from "@pithy-sh/core/src/seed/compose";
|
|
@@ -286,7 +287,7 @@ const authDevSessionSeed = defineSeed({
|
|
|
286
287
|
const secret = await context.secret(AUTH_SESSION_SECRET);
|
|
287
288
|
if (!secret) throw new ValidationError({
|
|
288
289
|
message: "Cannot mint a dev session without this environment's auth secret.",
|
|
289
|
-
action: `Add ${AUTH_SESSION_SECRET} to this project's dev secrets file —
|
|
290
|
+
action: `Add ${AUTH_SESSION_SECRET} to this project's dev secrets file — ${commandName("doctor")} prints its path — then seed again.`
|
|
290
291
|
});
|
|
291
292
|
const minted = await mintDevLogins({
|
|
292
293
|
users: [preferred, ...users.filter((user) => user.id !== preferred.id)],
|
|
@@ -43,6 +43,11 @@ export declare const LIVE_REGISTRY: {
|
|
|
43
43
|
readonly rotation: {
|
|
44
44
|
readonly kind: "local";
|
|
45
45
|
};
|
|
46
|
+
readonly overlap: {
|
|
47
|
+
readonly days: 7;
|
|
48
|
+
readonly minimumDays: 7;
|
|
49
|
+
readonly reencrypt: readonly ["auth-jwks"];
|
|
50
|
+
};
|
|
46
51
|
};
|
|
47
52
|
"auth-google-credentials": {
|
|
48
53
|
readonly backend: "d1";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"liveApp.d.ts","sourceRoot":"","sources":["../../src/test-utils/liveApp.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAuB3E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,iHAAiH;AACjH,eAAO,MAAM,aAAa
|
|
1
|
+
{"version":3,"file":"liveApp.d.ts","sourceRoot":"","sources":["../../src/test-utils/liveApp.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAuB3E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,iHAAiH;AACjH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAA0D,CAAC;AAErF,4GAA4G;AAC5G,wBAAgB,gBAAgB,IAAI,IAAI,CAEvC;AAwBD,4GAA4G;AAC5G,MAAM,WAAW,aAAa;IAC5B,qFAAqF;IACrF,QAAQ,EAAE,MAAM,CAAC;IACjB,6BAA6B;IAC7B,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,uGAAuG;AACvG,MAAM,WAAW,cAAc;IAC7B;;;;;;;OAOG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB,mFAAmF;IACnF,MAAM,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;IAChE,yGAAyG;IACzG,SAAS,CAAC,EAAE;QAAE,IAAI,EAAE,SAAS,GAAG,WAAW,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;IAC7E;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,mFAAmF;AACnF,MAAM,WAAW,OAAO;IACtB,sGAAsG;IACtG,MAAM,EAAE,MAAM,CAAC;IACf,uGAAuG;IACvG,QAAQ,EAAE,MAAM,CAAC;IACjB,mGAAmG;IACnG,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,mDAAmD;IACnD,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1B,0GAA0G;IAC1G,QAAQ,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;IACrC;;;;;;;;;;OAUG;IACH,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC9C,yFAAyF;IACzF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAyF5E"}
|
|
@@ -19,7 +19,7 @@ import { devEncryptionKeys } from "@pithy-sh/secrets/src/test-utils/devEncryptio
|
|
|
19
19
|
import { seedSecrets } from "@pithy-sh/secrets/src/test-utils/secretFixtures";
|
|
20
20
|
import { turnstileSecretsRegistry } from "@pithy-sh/turnstile/src/secret/registry";
|
|
21
21
|
import { Hono } from "hono";
|
|
22
|
-
import { Miniflare } from "miniflare";
|
|
22
|
+
import { Miniflare, convertV4MiniflareOptions } from "miniflare";
|
|
23
23
|
//#region src/test-utils/liveApp.ts
|
|
24
24
|
/**
|
|
25
25
|
* A real auth Worker on a real port, for the live suites (#84).
|
|
@@ -80,14 +80,14 @@ const SESSION_SECRET = "live-e2e-secret-please-rotate-000000";
|
|
|
80
80
|
* forgets leaves a workerd process behind.
|
|
81
81
|
*/
|
|
82
82
|
async function startLiveApp(options) {
|
|
83
|
-
const miniflare = new Miniflare({
|
|
83
|
+
const miniflare = new Miniflare(convertV4MiniflareOptions({
|
|
84
84
|
modules: true,
|
|
85
85
|
script: "export default {};",
|
|
86
86
|
d1Databases: {
|
|
87
87
|
DB: "DB",
|
|
88
88
|
SECRETS: "SECRETS"
|
|
89
89
|
}
|
|
90
|
-
});
|
|
90
|
+
}));
|
|
91
91
|
const db = await miniflare.getD1Database("DB");
|
|
92
92
|
const env = {
|
|
93
93
|
DB: db,
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* What Better Auth writes against what the kit's tables hold, in both directions (#451, #787).
|
|
5
|
+
*
|
|
6
|
+
* **The first direction is #451's.** A column Better Auth writes and the table lacks fails every insert
|
|
7
|
+
* that names it: 1.7's `jwt` plugin added `alg` and `crv`, `0001_init` had neither, and every sign-in that
|
|
8
|
+
* minted a signing key died on `table pithy_auth_jwks has no column named alg`.
|
|
9
|
+
*
|
|
10
|
+
* **The second direction is #787's, and its absence let the first one's mirror image ship.** A `NOT NULL`
|
|
11
|
+
* column with no default that Better Auth does not write fails every insert into its table, because
|
|
12
|
+
* nothing supplies it. 1.7.3 stopped writing `pithy_auth_accounts.issuer`, `0001_init` still required it,
|
|
13
|
+
* and every email OTP sign-in answered 500. The first direction could not see it: every column Better Auth
|
|
14
|
+
* declared was there. This is the same comparison Better Auth's own initialization check makes
|
|
15
|
+
* (`@better-auth/core`'s `diffSchema`), held here so CI makes it at whatever version is installed.
|
|
16
|
+
*
|
|
17
|
+
* Both halves compare in the database's vocabulary: Better Auth's camelCase names are snake-cased the way
|
|
18
|
+
* the `CamelCasePlugin` does it, and `pragma_table_info` reports the rest.
|
|
19
|
+
*/
|
|
20
|
+
/** A field of a Better Auth model, as `getSchema` reports it. Only the column name matters here. */
|
|
21
|
+
export interface DeclaredField {
|
|
22
|
+
/** The column name, when it differs from the field's key. */
|
|
23
|
+
fieldName?: string;
|
|
24
|
+
}
|
|
25
|
+
/** A Better Auth model, as `getSchema` reports it. */
|
|
26
|
+
export interface DeclaredModel {
|
|
27
|
+
/** Every field the model writes, keyed by field name. */
|
|
28
|
+
fields: Record<string, DeclaredField>;
|
|
29
|
+
}
|
|
30
|
+
/** One column of a table, as `pragma_table_info` describes it. */
|
|
31
|
+
export interface ActualColumn {
|
|
32
|
+
/** The column name, snake_case. */
|
|
33
|
+
name: string;
|
|
34
|
+
/** Whether the column is `NOT NULL`. */
|
|
35
|
+
notNull: boolean;
|
|
36
|
+
/** Whether the column carries a default. */
|
|
37
|
+
hasDefault: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** camelCase → snake_case, the one transformation `CamelCasePlugin` applies to an identifier. */
|
|
40
|
+
export declare function snake(name: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Every disagreement between what Better Auth writes and what the tables hold, one line each.
|
|
43
|
+
*
|
|
44
|
+
* `actual` maps a snake_case table name to its columns, and a table it does not name is a table that does
|
|
45
|
+
* not exist. A model with no table is reported once, as such, rather than as every column it lacks. The
|
|
46
|
+
* `id` primary key counts as written: Better Auth generates one for every row.
|
|
47
|
+
*/
|
|
48
|
+
export declare function schemaDrift(declared: Record<string, DeclaredModel>, actual: Record<string, readonly ActualColumn[]>): string[];
|
|
49
|
+
//# sourceMappingURL=schemaDrift.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaDrift.d.ts","sourceRoot":"","sources":["../../src/test-utils/schemaDrift.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AAEH,oGAAoG;AACpG,MAAM,WAAW,aAAa;IAC5B,6DAA6D;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,sDAAsD;AACtD,MAAM,WAAW,aAAa;IAC5B,yDAAyD;IACzD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;CACvC;AAED,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC3B,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,wCAAwC;IACxC,OAAO,EAAE,OAAO,CAAC;IACjB,4CAA4C;IAC5C,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,iGAAiG;AACjG,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE1C;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EACvC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,CAAC,GAC9C,MAAM,EAAE,CAuBV"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
//#region src/test-utils/schemaDrift.ts
|
|
4
|
+
/** camelCase → snake_case, the one transformation `CamelCasePlugin` applies to an identifier. */
|
|
5
|
+
function snake(name) {
|
|
6
|
+
return name.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Every disagreement between what Better Auth writes and what the tables hold, one line each.
|
|
10
|
+
*
|
|
11
|
+
* `actual` maps a snake_case table name to its columns, and a table it does not name is a table that does
|
|
12
|
+
* not exist. A model with no table is reported once, as such, rather than as every column it lacks. The
|
|
13
|
+
* `id` primary key counts as written: Better Auth generates one for every row.
|
|
14
|
+
*/
|
|
15
|
+
function schemaDrift(declared, actual) {
|
|
16
|
+
const findings = [];
|
|
17
|
+
for (const [model, spec] of Object.entries(declared)) {
|
|
18
|
+
const table = snake(model);
|
|
19
|
+
const columns = actual[table];
|
|
20
|
+
if (columns === void 0 || columns.length === 0) {
|
|
21
|
+
findings.push(`${table}: no such table`);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const written = /* @__PURE__ */ new Set(["id", ...Object.entries(spec.fields).map(([field, definition]) => snake(definition.fieldName ?? field))]);
|
|
25
|
+
const present = new Set(columns.map((column) => column.name));
|
|
26
|
+
for (const column of written) if (!present.has(column)) findings.push(`${table}.${column}: missing`);
|
|
27
|
+
for (const column of columns) {
|
|
28
|
+
if (written.has(column.name) || !column.notNull || column.hasDefault) continue;
|
|
29
|
+
findings.push(`${table}.${column.name}: required, and Better Auth never writes it`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return findings;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { schemaDrift, snake };
|