@pithy-sh/auth 0.6.7 → 0.7.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/dist/seeds/devSession.d.ts +19 -2
- package/dist/seeds/devSession.d.ts.map +1 -1
- package/dist/seeds/devSession.js +44 -12
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/docs/apple-signin.md +3 -8
- package/docs/facebook-oauth.md +3 -8
- package/docs/github-oauth.md +3 -8
- package/docs/google-oauth.md +3 -8
- package/package.json +5 -5
- package/src/seeds/devSession.ts +58 -12
- package/src/version.generated.ts +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import { DevLogin } from "@pithy-sh/core/src/seed/devLogin";
|
|
3
|
+
import { type DevLogin, DevLogins } from "@pithy-sh/core/src/seed/devLogin";
|
|
4
4
|
import { type SeedSet } from "@pithy-sh/core/src/seed/seed";
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
/**
|
|
@@ -51,7 +51,7 @@ export declare const AUTH_DEV_SESSION_SEED_ORDER: number;
|
|
|
51
51
|
export declare const DEV_SESSION_COOKIE_NAME: string;
|
|
52
52
|
/** The developer's machine-local preferences for this project, read from `~/.config/pithy/<project>/dev.json`. */
|
|
53
53
|
export declare const DevPreferences: z.ZodObject<{
|
|
54
|
-
user: z.ZodString
|
|
54
|
+
user: z.ZodOptional<z.ZodString>;
|
|
55
55
|
}, z.core.$strip>;
|
|
56
56
|
export type DevPreferences = z.output<typeof DevPreferences>;
|
|
57
57
|
/**
|
|
@@ -127,6 +127,23 @@ export declare function verifyDevLoginClaim(claim: string, secret: string, now?:
|
|
|
127
127
|
* opens the link, so the session is real, is theirs, and is the only thing a sign-out takes away.
|
|
128
128
|
*/
|
|
129
129
|
export declare function mintDevLogin(input: MintDevLoginInput): Promise<MintedDevLogin>;
|
|
130
|
+
/**
|
|
131
|
+
* Mint a claim for **every** user this run seeds, keyed by user id — `#667`.
|
|
132
|
+
*
|
|
133
|
+
* Cheap by construction: a claim is a signature over a user id and an expiry, so N of them add no seeded
|
|
134
|
+
* rows and a few hundred bytes each.
|
|
135
|
+
*
|
|
136
|
+
* **The record keeps the order it is handed, except for integer-like ids — and that is JS, not us.** Keys
|
|
137
|
+
* that are canonical array indices are enumerated first and in ascending numeric order, by the language and
|
|
138
|
+
* again by `JSON.parse`, so an adopter whose users carry stringified integer PKs gets a numerically ordered
|
|
139
|
+
* picker and `dev.json`'s "offered first" does not survive for them. Keying the artifact by user id is the
|
|
140
|
+
* design; holding the order for every possible id would mean a different artifact shape.
|
|
141
|
+
*/
|
|
142
|
+
export declare function mintDevLogins(input: {
|
|
143
|
+
users: readonly SeededUser[];
|
|
144
|
+
secret: string;
|
|
145
|
+
now?: Date;
|
|
146
|
+
}): Promise<DevLogins>;
|
|
130
147
|
/**
|
|
131
148
|
* The dev-login seed set. Composed by the auth capability; runs only in `dev`, only for a user this same run
|
|
132
149
|
* creates, and only when the developer has opted in with a `dev.json`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devSession.d.ts","sourceRoot":"","sources":["../../src/seeds/devSession.ts"],"names":[],"mappings":"AAMA,OAAO,EAAkC,QAAQ,EAAE,MAAM,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"devSession.d.ts","sourceRoot":"","sources":["../../src/seeds/devSession.ts"],"names":[],"mappings":"AAMA,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,7 +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 { DEV_LOGIN_FILE,
|
|
7
|
+
import { DEV_LOGIN_FILE, DevLogins } from "@pithy-sh/core/src/seed/devLogin";
|
|
8
8
|
import { normalizeAddress } from "@pithy-sh/core/src/address/address";
|
|
9
9
|
import { MAX_SEED_ORDER } from "@pithy-sh/core/src/seed/compose";
|
|
10
10
|
import { defineSeed } from "@pithy-sh/core/src/seed/seed";
|
|
@@ -66,7 +66,7 @@ const DEV_SESSION_COOKIE_NAME = sessionCookieName(DEV_PROTOCOL);
|
|
|
66
66
|
*/
|
|
67
67
|
const DEV_LOGIN_LIFETIME_MS = 31536e6;
|
|
68
68
|
/** The developer's machine-local preferences for this project, read from `~/.config/pithy/<project>/dev.json`. */
|
|
69
|
-
const DevPreferences = z.object({ user: z.string().describe("The email of the seeded user to sign in as. Must be a user this seed run creates, or the seed fails rather than
|
|
69
|
+
const DevPreferences = z.object({ user: z.string().optional().describe("The email of the seeded user to sign in as first. **This key is the dev-login opt-in**: without it no claim is minted at all, because `dev.json` has other tenants and its mere existence is not consent. With it, every seeded user gets a claim and this one is offered first. Must be a user this seed run creates, or the seed fails rather than naming nobody.") }).describe("A developer's machine-local dev preferences, from the Pithy config directory (`~/.config/pithy/<project>/dev.json`, or `%APPDATA%\\pithy\\<project>\\dev.json` on Windows) — outside the repo, so opting in needs no commit.");
|
|
70
70
|
/**
|
|
71
71
|
* A user this run seeds, read back out of the composed plan.
|
|
72
72
|
*
|
|
@@ -209,6 +209,26 @@ async function mintDevLogin(input) {
|
|
|
209
209
|
} };
|
|
210
210
|
}
|
|
211
211
|
/**
|
|
212
|
+
* Mint a claim for **every** user this run seeds, keyed by user id — `#667`.
|
|
213
|
+
*
|
|
214
|
+
* Cheap by construction: a claim is a signature over a user id and an expiry, so N of them add no seeded
|
|
215
|
+
* rows and a few hundred bytes each.
|
|
216
|
+
*
|
|
217
|
+
* **The record keeps the order it is handed, except for integer-like ids — and that is JS, not us.** Keys
|
|
218
|
+
* that are canonical array indices are enumerated first and in ascending numeric order, by the language and
|
|
219
|
+
* again by `JSON.parse`, so an adopter whose users carry stringified integer PKs gets a numerically ordered
|
|
220
|
+
* picker and `dev.json`'s "offered first" does not survive for them. Keying the artifact by user id is the
|
|
221
|
+
* design; holding the order for every possible id would mean a different artifact shape.
|
|
222
|
+
*/
|
|
223
|
+
async function mintDevLogins(input) {
|
|
224
|
+
const minted = await Promise.all(input.users.map((user) => mintDevLogin({
|
|
225
|
+
user,
|
|
226
|
+
secret: input.secret,
|
|
227
|
+
...input.now ? { now: input.now } : {}
|
|
228
|
+
})));
|
|
229
|
+
return Object.fromEntries(minted.map(({ login }) => [login.userId, login]));
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
212
232
|
* The users this run creates, whichever set contributes them.
|
|
213
233
|
*
|
|
214
234
|
* A row that does not parse is skipped rather than fatal: it belongs to some other set, which owns its own
|
|
@@ -225,17 +245,26 @@ function nameOneOf(users) {
|
|
|
225
245
|
if (users.length === 0) return "This run seeds no users at all. Add a user fixture to your app's seed set, or turn on seed.includeExamples for the example cast.";
|
|
226
246
|
return `Name one of the users this run seeds instead: ${users.map((user) => user.email).join(", ")}.`;
|
|
227
247
|
}
|
|
228
|
-
/**
|
|
229
|
-
|
|
248
|
+
/**
|
|
249
|
+
* The email `dev.json` names, or `undefined` when it names none. A file that does not parse is a failure.
|
|
250
|
+
*
|
|
251
|
+
* Split from resolving it because the two answers differ: naming nobody is a valid file that wants no dev
|
|
252
|
+
* login, and naming somebody this run does not create is a typo worth failing over.
|
|
253
|
+
*/
|
|
254
|
+
function namedUser(preferences, users) {
|
|
230
255
|
const parsed = DevPreferences.safeParse(preferences);
|
|
231
256
|
if (!parsed.success) throw fromZodError(parsed.error, {
|
|
232
|
-
message: "The dev.json preference file does not
|
|
257
|
+
message: "The dev.json preference file does not parse.",
|
|
233
258
|
action: `Set { "user": "<email>" } in it. ${nameOneOf(users)}`
|
|
234
259
|
});
|
|
235
|
-
|
|
260
|
+
return parsed.data.user;
|
|
261
|
+
}
|
|
262
|
+
/** Resolve the named email to a user this run seeds, or fail saying who it does seed. */
|
|
263
|
+
function requireNamedUser(named, users) {
|
|
264
|
+
const wanted = normalizeAddress(named);
|
|
236
265
|
const user = users.find((candidate) => normalizeAddress(candidate.email) === wanted);
|
|
237
266
|
if (!user) throw new ValidationError({
|
|
238
|
-
message: `dev.json asks to sign in as ${
|
|
267
|
+
message: `dev.json asks to sign in as ${named}, which this seed run does not create.`,
|
|
239
268
|
action: nameOneOf(users)
|
|
240
269
|
});
|
|
241
270
|
return user;
|
|
@@ -250,21 +279,24 @@ const authDevSessionSeed = defineSeed({
|
|
|
250
279
|
environments: ["dev"],
|
|
251
280
|
prepare: async (context) => {
|
|
252
281
|
if (context.preferences === void 0 || context.preferences === null) return {};
|
|
253
|
-
const
|
|
282
|
+
const users = seededUsers(context.seeded);
|
|
283
|
+
const named = namedUser(context.preferences, users);
|
|
284
|
+
if (named === void 0) return {};
|
|
285
|
+
const preferred = requireNamedUser(named, users);
|
|
254
286
|
const secret = await context.secret(AUTH_SESSION_SECRET);
|
|
255
287
|
if (!secret) throw new ValidationError({
|
|
256
288
|
message: "Cannot mint a dev session without this environment's auth secret.",
|
|
257
289
|
action: `Add ${AUTH_SESSION_SECRET} to this project's dev secrets file — pithy doctor prints its path — then seed again.`
|
|
258
290
|
});
|
|
259
|
-
const minted = await
|
|
260
|
-
user,
|
|
291
|
+
const minted = await mintDevLogins({
|
|
292
|
+
users: [preferred, ...users.filter((user) => user.id !== preferred.id)],
|
|
261
293
|
secret
|
|
262
294
|
});
|
|
263
295
|
return { artifacts: [{
|
|
264
296
|
file: DEV_LOGIN_FILE,
|
|
265
|
-
contents: `${JSON.stringify(
|
|
297
|
+
contents: `${JSON.stringify(DevLogins.encode(minted), null, 2)}\n`
|
|
266
298
|
}] };
|
|
267
299
|
}
|
|
268
300
|
});
|
|
269
301
|
//#endregion
|
|
270
|
-
export { AUTH_DEV_SESSION_SEED_ORDER, DEV_SESSION_COOKIE_NAME, DevPreferences, SeededUser, authDevSessionSeed, mintDevLogin, signCookieValue, signDevLoginClaim, verifyDevLoginClaim };
|
|
302
|
+
export { AUTH_DEV_SESSION_SEED_ORDER, DEV_SESSION_COOKIE_NAME, DevPreferences, SeededUser, authDevSessionSeed, mintDevLogin, mintDevLogins, signCookieValue, signDevLoginClaim, verifyDevLoginClaim };
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
/** This package's npm name — the join key against a release feed. */
|
|
4
4
|
export declare const PACKAGE_NAME = "@pithy-sh/auth";
|
|
5
5
|
/** This package's version, stamped from its own package.json at generation time. */
|
|
6
|
-
export declare const PACKAGE_VERSION = "0.
|
|
6
|
+
export declare const PACKAGE_VERSION = "0.7.0";
|
|
7
7
|
//# sourceMappingURL=version.generated.d.ts.map
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
/** This package's npm name — the join key against a release feed. */
|
|
5
5
|
const PACKAGE_NAME = "@pithy-sh/auth";
|
|
6
6
|
/** This package's version, stamped from its own package.json at generation time. */
|
|
7
|
-
const PACKAGE_VERSION = "0.
|
|
7
|
+
const PACKAGE_VERSION = "0.7.0";
|
|
8
8
|
//#endregion
|
|
9
9
|
export { PACKAGE_NAME, PACKAGE_VERSION };
|
package/docs/apple-signin.md
CHANGED
|
@@ -116,14 +116,9 @@ Apple only returns the user's name on the **first** authorization. Better Auth p
|
|
|
116
116
|
|
|
117
117
|
## When the credential will not read
|
|
118
118
|
|
|
119
|
-
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
A sign-in attempt with Apple then answers `503` with the code `auth/provider_unavailable` and a
|
|
123
|
-
message naming apple, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two
|
|
124
|
-
are different facts and never share an answer. The attempt is recorded in the audit trail as
|
|
125
|
-
`auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by
|
|
126
|
-
provisioning `auth-apple-credentials` for that environment, or turn the provider off in config.
|
|
119
|
+
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs **that provider and nothing else**. Magic link, OTP, and every other provider keep signing people in.
|
|
120
|
+
|
|
121
|
+
A sign-in attempt with Apple then answers `503` with the code `auth/provider_unavailable` and a message naming apple, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two are different facts and never share an answer. The attempt is recorded in the audit trail as `auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by provisioning `auth-apple-credentials` for that environment, or turn the provider off in config.
|
|
127
122
|
|
|
128
123
|
## Checklist
|
|
129
124
|
|
package/docs/facebook-oauth.md
CHANGED
|
@@ -74,14 +74,9 @@ Facebook is **not** in `trustedProviders`. Trusting the returned email is not th
|
|
|
74
74
|
|
|
75
75
|
## When the credential will not read
|
|
76
76
|
|
|
77
|
-
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
A sign-in attempt with Facebook then answers `503` with the code `auth/provider_unavailable` and a
|
|
81
|
-
message naming facebook, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two
|
|
82
|
-
are different facts and never share an answer. The attempt is recorded in the audit trail as
|
|
83
|
-
`auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by
|
|
84
|
-
provisioning `auth-facebook-credentials` for that environment, or turn the provider off in config.
|
|
77
|
+
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs **that provider and nothing else**. Magic link, OTP, and every other provider keep signing people in.
|
|
78
|
+
|
|
79
|
+
A sign-in attempt with Facebook then answers `503` with the code `auth/provider_unavailable` and a message naming facebook, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two are different facts and never share an answer. The attempt is recorded in the audit trail as `auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by provisioning `auth-facebook-credentials` for that environment, or turn the provider off in config.
|
|
85
80
|
|
|
86
81
|
## Checklist
|
|
87
82
|
|
package/docs/github-oauth.md
CHANGED
|
@@ -134,14 +134,9 @@ At no point does one account take an identity from another; one gives it up, aut
|
|
|
134
134
|
|
|
135
135
|
## When the credential will not read
|
|
136
136
|
|
|
137
|
-
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
A sign-in attempt with GitHub then answers `503` with the code `auth/provider_unavailable` and a
|
|
141
|
-
message naming github, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two
|
|
142
|
-
are different facts and never share an answer. The attempt is recorded in the audit trail as
|
|
143
|
-
`auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by
|
|
144
|
-
provisioning `auth-github-credentials` for that environment, or turn the provider off in config.
|
|
137
|
+
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs **that provider and nothing else**. Magic link, OTP, and every other provider keep signing people in.
|
|
138
|
+
|
|
139
|
+
A sign-in attempt with GitHub then answers `503` with the code `auth/provider_unavailable` and a message naming github, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two are different facts and never share an answer. The attempt is recorded in the audit trail as `auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by provisioning `auth-github-credentials` for that environment, or turn the provider off in config.
|
|
145
140
|
|
|
146
141
|
## Checklist
|
|
147
142
|
|
package/docs/google-oauth.md
CHANGED
|
@@ -96,14 +96,9 @@ A user who signed up with a magic link and later signs in with Google is linked
|
|
|
96
96
|
|
|
97
97
|
## When the credential will not read
|
|
98
98
|
|
|
99
|
-
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
A sign-in attempt with Google then answers `503` with the code `auth/provider_unavailable` and a
|
|
103
|
-
message naming google, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two
|
|
104
|
-
are different facts and never share an answer. The attempt is recorded in the audit trail as
|
|
105
|
-
`auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by
|
|
106
|
-
provisioning `auth-google-credentials` for that environment, or turn the provider off in config.
|
|
99
|
+
An enabled provider whose secret is missing, or whose stored value no longer matches its schema, costs **that provider and nothing else**. Magic link, OTP, and every other provider keep signing people in.
|
|
100
|
+
|
|
101
|
+
A sign-in attempt with Google then answers `503` with the code `auth/provider_unavailable` and a message naming google, rather than the `404 PROVIDER_NOT_FOUND` a provider nobody enabled gets — the two are different facts and never share an answer. The attempt is recorded in the audit trail as `auth/provider_unavailable`, which is where an operator learns a sign-in method is down. Fix it by provisioning `auth-google-credentials` for that environment, or turn the provider off in config.
|
|
107
102
|
|
|
108
103
|
## Checklist
|
|
109
104
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pithy-sh/auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"better-auth": "^1.7.1"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@pithy-sh/core": "^0.
|
|
46
|
-
"@pithy-sh/email": "^0.3.
|
|
47
|
-
"@pithy-sh/secrets": "^0.3.
|
|
48
|
-
"@pithy-sh/turnstile": "^0.4.
|
|
45
|
+
"@pithy-sh/core": "^0.9.0",
|
|
46
|
+
"@pithy-sh/email": "^0.3.11",
|
|
47
|
+
"@pithy-sh/secrets": "^0.3.2",
|
|
48
|
+
"@pithy-sh/turnstile": "^0.4.2",
|
|
49
49
|
"hono": "^4.13.2",
|
|
50
50
|
"kysely": "^0.29.0",
|
|
51
51
|
"zod": "^4.4.0"
|
package/src/seeds/devSession.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { normalizeAddress } from "@pithy-sh/core/src/address/address";
|
|
5
5
|
import { fromZodError, ValidationError } from "@pithy-sh/core/src/error/pithyError";
|
|
6
6
|
import { MAX_SEED_ORDER } from "@pithy-sh/core/src/seed/compose";
|
|
7
|
-
import { DEV_LOGIN_FILE, DEV_LOGIN_PATH, DevLogin } from "@pithy-sh/core/src/seed/devLogin";
|
|
7
|
+
import { DEV_LOGIN_FILE, DEV_LOGIN_PATH, type DevLogin, DevLogins } from "@pithy-sh/core/src/seed/devLogin";
|
|
8
8
|
import { defineSeed, type SeedPreparation, type SeedPrepareContext, type SeedSet } from "@pithy-sh/core/src/seed/seed";
|
|
9
9
|
import { z } from "zod";
|
|
10
10
|
import { DEV_PROTOCOL, sessionCookieName } from "../http/baseUrl";
|
|
@@ -76,8 +76,9 @@ export const DevPreferences = z
|
|
|
76
76
|
.object({
|
|
77
77
|
user: z
|
|
78
78
|
.string()
|
|
79
|
+
.optional()
|
|
79
80
|
.describe(
|
|
80
|
-
"The email of the seeded user to sign in as. Must be a user this seed run creates, or the seed fails rather than
|
|
81
|
+
"The email of the seeded user to sign in as first. **This key is the dev-login opt-in**: without it no claim is minted at all, because `dev.json` has other tenants and its mere existence is not consent. With it, every seeded user gets a claim and this one is offered first. Must be a user this seed run creates, or the seed fails rather than naming nobody.",
|
|
81
82
|
),
|
|
82
83
|
})
|
|
83
84
|
.describe(
|
|
@@ -266,6 +267,29 @@ export async function mintDevLogin(input: MintDevLoginInput): Promise<MintedDevL
|
|
|
266
267
|
};
|
|
267
268
|
}
|
|
268
269
|
|
|
270
|
+
/**
|
|
271
|
+
* Mint a claim for **every** user this run seeds, keyed by user id — `#667`.
|
|
272
|
+
*
|
|
273
|
+
* Cheap by construction: a claim is a signature over a user id and an expiry, so N of them add no seeded
|
|
274
|
+
* rows and a few hundred bytes each.
|
|
275
|
+
*
|
|
276
|
+
* **The record keeps the order it is handed, except for integer-like ids — and that is JS, not us.** Keys
|
|
277
|
+
* that are canonical array indices are enumerated first and in ascending numeric order, by the language and
|
|
278
|
+
* again by `JSON.parse`, so an adopter whose users carry stringified integer PKs gets a numerically ordered
|
|
279
|
+
* picker and `dev.json`'s "offered first" does not survive for them. Keying the artifact by user id is the
|
|
280
|
+
* design; holding the order for every possible id would mean a different artifact shape.
|
|
281
|
+
*/
|
|
282
|
+
export async function mintDevLogins(input: {
|
|
283
|
+
users: readonly SeededUser[];
|
|
284
|
+
secret: string;
|
|
285
|
+
now?: Date;
|
|
286
|
+
}): Promise<DevLogins> {
|
|
287
|
+
const minted = await Promise.all(
|
|
288
|
+
input.users.map((user) => mintDevLogin({ user, secret: input.secret, ...(input.now ? { now: input.now } : {}) })),
|
|
289
|
+
);
|
|
290
|
+
return Object.fromEntries(minted.map(({ login }) => [login.userId, login]));
|
|
291
|
+
}
|
|
292
|
+
|
|
269
293
|
/**
|
|
270
294
|
* The users this run creates, whichever set contributes them.
|
|
271
295
|
*
|
|
@@ -287,22 +311,32 @@ function nameOneOf(users: readonly SeededUser[]): string {
|
|
|
287
311
|
return `Name one of the users this run seeds instead: ${users.map((user) => user.email).join(", ")}.`;
|
|
288
312
|
}
|
|
289
313
|
|
|
290
|
-
/**
|
|
291
|
-
|
|
314
|
+
/**
|
|
315
|
+
* The email `dev.json` names, or `undefined` when it names none. A file that does not parse is a failure.
|
|
316
|
+
*
|
|
317
|
+
* Split from resolving it because the two answers differ: naming nobody is a valid file that wants no dev
|
|
318
|
+
* login, and naming somebody this run does not create is a typo worth failing over.
|
|
319
|
+
*/
|
|
320
|
+
function namedUser(preferences: unknown, users: readonly SeededUser[]): string | undefined {
|
|
292
321
|
const parsed = DevPreferences.safeParse(preferences);
|
|
293
322
|
if (!parsed.success) {
|
|
294
323
|
throw fromZodError(parsed.error, {
|
|
295
|
-
message: "The dev.json preference file does not
|
|
324
|
+
message: "The dev.json preference file does not parse.",
|
|
296
325
|
action: `Set { "user": "<email>" } in it. ${nameOneOf(users)}`,
|
|
297
326
|
});
|
|
298
327
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
328
|
+
return parsed.data.user;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Resolve the named email to a user this run seeds, or fail saying who it does seed. */
|
|
332
|
+
function requireNamedUser(named: string, users: readonly SeededUser[]): SeededUser {
|
|
333
|
+
// Both sides normalized: `dev.json` is hand-typed, and refusing over the capital in `Ada@example.com`
|
|
334
|
+
// would be a puzzle rather than an error.
|
|
335
|
+
const wanted = normalizeAddress(named);
|
|
302
336
|
const user = users.find((candidate) => normalizeAddress(candidate.email) === wanted);
|
|
303
337
|
if (!user) {
|
|
304
338
|
throw new ValidationError({
|
|
305
|
-
message: `dev.json asks to sign in as ${
|
|
339
|
+
message: `dev.json asks to sign in as ${named}, which this seed run does not create.`,
|
|
306
340
|
action: nameOneOf(users),
|
|
307
341
|
});
|
|
308
342
|
}
|
|
@@ -322,7 +356,17 @@ export const authDevSessionSeed: SeedSet = defineSeed({
|
|
|
322
356
|
// a magic link" true for everyone who never asked for anything else.
|
|
323
357
|
if (context.preferences === undefined || context.preferences === null) return {};
|
|
324
358
|
|
|
325
|
-
const
|
|
359
|
+
const users = seededUsers(context.seeded);
|
|
360
|
+
const named = namedUser(context.preferences, users);
|
|
361
|
+
// **No user named, no dev login — and `dev.json` is why the *key* has to be the opt-in rather than the
|
|
362
|
+
// file (`#154`, `#667`).** That file has other tenants: `writeBootstrapVars` creates it for bootstrap
|
|
363
|
+
// `.dev.vars` alone, with no `user` in it and no interest in signing anybody in. Treating its mere
|
|
364
|
+
// existence as consent would mint a live claim for every seeded user on a machine where nobody ever
|
|
365
|
+
// asked for one. `doctor/devPreferences.ts` holds the same contract, and this is the half that enforces
|
|
366
|
+
// it: naming a user is the opt-in, and it is also which identity `pithy dev` offers first.
|
|
367
|
+
if (named === undefined) return {};
|
|
368
|
+
|
|
369
|
+
const preferred = requireNamedUser(named, users);
|
|
326
370
|
const secret = await context.secret(AUTH_SESSION_SECRET);
|
|
327
371
|
if (!secret) {
|
|
328
372
|
throw new ValidationError({
|
|
@@ -336,12 +380,14 @@ export const authDevSessionSeed: SeedSet = defineSeed({
|
|
|
336
380
|
});
|
|
337
381
|
}
|
|
338
382
|
|
|
339
|
-
|
|
383
|
+
// The named user first, then everyone else in the order the run seeded them.
|
|
384
|
+
const ordered = [preferred, ...users.filter((user) => user.id !== preferred.id)];
|
|
385
|
+
const minted = await mintDevLogins({ users: ordered, secret });
|
|
340
386
|
// **No `d1` — `#572`.** This set writes a file and nothing else now. Nothing reaches
|
|
341
387
|
// `pithy_auth_sessions` until somebody opens the link and the route mints a session for them, which
|
|
342
388
|
// is what makes signing out of the app harmless to the way back in.
|
|
343
389
|
return {
|
|
344
|
-
artifacts: [{ file: DEV_LOGIN_FILE, contents: `${JSON.stringify(
|
|
390
|
+
artifacts: [{ file: DEV_LOGIN_FILE, contents: `${JSON.stringify(DevLogins.encode(minted), null, 2)}\n` }],
|
|
345
391
|
};
|
|
346
392
|
},
|
|
347
393
|
});
|
package/src/version.generated.ts
CHANGED