@softure-ai/auth 0.1.12 → 0.1.13
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/CHANGELOG.md +10 -0
- package/README.md +30 -0
- package/dist/index.js +1 -1
- package/dist/scripts/index.d.ts +1 -0
- package/dist/scripts/index.d.ts.map +1 -1
- package/dist/scripts/index.js +4 -2
- package/dist/scripts/index.js.map +1 -1
- package/dist/scripts/seed-account.d.ts +19 -0
- package/dist/scripts/seed-account.d.ts.map +1 -0
- package/dist/scripts/seed-account.js +95 -0
- package/dist/scripts/seed-account.js.map +1 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/only-account.d.ts +21 -0
- package/dist/server/only-account.d.ts.map +1 -0
- package/dist/server/only-account.js +34 -0
- package/dist/server/only-account.js.map +1 -0
- package/module.json +1 -1
- package/package.json +3 -3
- package/src/index.ts +1 -1
- package/src/scripts/index.ts +4 -2
- package/src/scripts/seed-account.ts +108 -0
- package/src/server/index.ts +7 -0
- package/src/server/only-account.ts +50 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,16 @@ Newest first. Each version lists what changed for an app that uses `@softure-ai/
|
|
|
4
4
|
production, the version gets a line `verified in: <app>@<commit>` ([docs/05](../../docs/05-adoption-playbook.md),
|
|
5
5
|
"Definition of done"). Versions before the first one below are described in their GitHub Releases (`auth@x.y.z`).
|
|
6
6
|
|
|
7
|
+
## 0.1.13
|
|
8
|
+
|
|
9
|
+
- `resolveOnlyAccount({ db, envName, env? })` in `@softure-ai/auth/server`: the account a local tool acts for, named by
|
|
10
|
+
an environment variable or the only one in the database; refuses `auth.account_unknown`, `auth.no_account` and
|
|
11
|
+
`auth.account_ambiguous` with a readable `message` (#411). An app drops its own copy of this lookup.
|
|
12
|
+
- `runSeedAccount({ config, env? })` in `@softure-ai/auth/scripts`: a development account from `SEED_EMAIL` /
|
|
13
|
+
`SEED_PASSWORD` through registration (the `onRegistered` hook runs), idempotent, refused under
|
|
14
|
+
`NODE_ENV=production`; returns the exit code (#411).
|
|
15
|
+
- The `@softure-ai/ui` peer range also accepts 0.2.0.
|
|
16
|
+
|
|
7
17
|
## 0.1.12
|
|
8
18
|
|
|
9
19
|
- `createAuthGuard({ redirect: "relative" })` answers with a path-only `Location` (`/login?next=…`), so a stack on a
|
package/README.md
CHANGED
|
@@ -363,6 +363,35 @@ the first login. `isPasswordHash(value)` checks the format locally, exactly: a h
|
|
|
363
363
|
a character outside base64url or a cost scrypt refuses is not a hash `hashPassword` writes, so the script refuses it
|
|
364
364
|
instead of storing a hash no password matches.
|
|
365
365
|
|
|
366
|
+
**Development seed.** `runSeedAccount` from `@softure-ai/auth/scripts` creates a login account for local
|
|
367
|
+
development from `SEED_EMAIL` and `SEED_PASSWORD` through `registerUser`, so the app's `onRegistered` hook runs as
|
|
368
|
+
for a real sign-up, and ends the session registration opens:
|
|
369
|
+
|
|
370
|
+
```ts
|
|
371
|
+
// scripts/seed.ts, run with `tsx scripts/seed.ts`
|
|
372
|
+
import { runSeedAccount } from "@softure-ai/auth/scripts";
|
|
373
|
+
import config from "../softure.config";
|
|
374
|
+
|
|
375
|
+
process.exitCode = await runSeedAccount({ config });
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
It is idempotent: an account with that email is left as it is (its password is not reset), exit 0. A refusal names
|
|
379
|
+
the variable and the code (`SEED_PASSWORD is shorter than password.minLength (auth.password_too_short)`, closed
|
|
380
|
+
registration, an invalid email) and writes nothing; a hook that throws rolls the account back. It refuses to run with
|
|
381
|
+
`NODE_ENV=production`, needs the security buckets like any registration, and never prints the password.
|
|
382
|
+
|
|
383
|
+
**One account for a local tool.** `resolveOnlyAccount({ db, envName, env? })` from `@softure-ai/auth/server` gives
|
|
384
|
+
the account a local tool acts for (a stdio MCP server, a measurement or seed script): the id in `env[envName]`
|
|
385
|
+
(`process.env` by default), which must name an account, else the only account in the database. It returns
|
|
386
|
+
`{ ok: true, value: id }` or a refusal with a `message` for the person running the tool: `auth.account_unknown`,
|
|
387
|
+
`auth.no_account`, or `auth.account_ambiguous` when there are several accounts and none is named. It never picks the
|
|
388
|
+
first of several.
|
|
389
|
+
|
|
390
|
+
```ts
|
|
391
|
+
const account = await resolveOnlyAccount({ db, envName: "MEASURE_USER_ID" });
|
|
392
|
+
if (!account.ok) throw new Error(account.message);
|
|
393
|
+
```
|
|
394
|
+
|
|
366
395
|
**Password reset.** Pass a sender, and the login form links to the request page. With
|
|
367
396
|
`@softure-ai/mailing` enabled, `mailingResetSender()` is that sender:
|
|
368
397
|
|
|
@@ -502,6 +531,7 @@ its NFC form fails, then rehashes it in NFC.
|
|
|
502
531
|
| Name | Required | Meaning |
|
|
503
532
|
| --- | --- | --- |
|
|
504
533
|
| `SOFTURE_SWITCH_AUTH_REGISTRATION_CLOSED` | no | `true` / `1` closes registration, `false` / `0` opens it, over the declared default. Any other value closes it and logs the variable name. When the app defines the switch in `@softure-ai/feature-switches`, that module reads this variable by its own rules (`on` / `off` too; an unreadable value gives the fail mode). |
|
|
534
|
+
| `SEED_EMAIL`, `SEED_PASSWORD` | for `runSeedAccount` | The development account the seed entry creates (section 4, "Development seed"). |
|
|
505
535
|
|
|
506
536
|
## 7. Switches
|
|
507
537
|
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
|
|
|
36
36
|
export const auth = defineModule({
|
|
37
37
|
manifest: {
|
|
38
38
|
id: MODULE_ID,
|
|
39
|
-
version: "0.1.
|
|
39
|
+
version: "0.1.13",
|
|
40
40
|
dependsOn: { security: "^0.1.0", mailing: "^0.1.0?", ops: "^0.1.0?" },
|
|
41
41
|
dbSchema: "auth",
|
|
42
42
|
tables: ["users", "sessions", "user_roles", "password_resets"],
|
package/dist/scripts/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { createGrantRoleScript, createRevokeRoleScript, type RoleScriptArgs, type RoleScriptOptions } from "./role-scripts.js";
|
|
2
2
|
export { createSetTemporaryPasswordScript, type TemporaryPasswordScriptArgs, type TemporaryPasswordScriptOptions, } from "./temporary-password.js";
|
|
3
|
+
export { runSeedAccount, type RunSeedAccountOptions } from "./seed-account.js";
|
|
3
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/scripts/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/scripts/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,KAAK,cAAc,EAAE,KAAK,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC/H,OAAO,EACL,gCAAgC,EAChC,KAAK,2BAA2B,EAChC,KAAK,8BAA8B,GACpC,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/scripts/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles and set a temporary password
|
|
2
|
-
//
|
|
1
|
+
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles and set a temporary password,
|
|
2
|
+
// and the development seed account
|
|
3
|
+
// (see the README, "Roles", "Account recovery" and "Development seed").
|
|
3
4
|
export { createGrantRoleScript, createRevokeRoleScript } from "./role-scripts.js";
|
|
4
5
|
export { createSetTemporaryPasswordScript, } from "./temporary-password.js";
|
|
6
|
+
export { runSeedAccount } from "./seed-account.js";
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/scripts/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/scripts/index.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,mCAAmC;AACnC,wEAAwE;AACxE,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAA+C,MAAM,mBAAmB,CAAC;AAC/H,OAAO,EACL,gCAAgC,GAGjC,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,cAAc,EAA8B,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Clock, type SoftureConfig } from "@softure-ai/core";
|
|
2
|
+
import { type Database } from "@softure-ai/db";
|
|
3
|
+
import { type CliOutput } from "@softure-ai/db/cli";
|
|
4
|
+
export interface RunSeedAccountOptions {
|
|
5
|
+
readonly config: SoftureConfig;
|
|
6
|
+
/** Default: `process.env`. Reads `SEED_EMAIL`, `SEED_PASSWORD` and `NODE_ENV`. */
|
|
7
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
8
|
+
/** An open database to use instead of the config's (tests); it is not closed. */
|
|
9
|
+
readonly database?: Database;
|
|
10
|
+
/** Default: the console. The password is never printed. */
|
|
11
|
+
readonly output?: CliOutput;
|
|
12
|
+
readonly clock?: Clock;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates the development account and returns the exit code: 0 created or already there, 1 refused
|
|
16
|
+
* or failed (nothing written), 2 `SEED_EMAIL` or `SEED_PASSWORD` missing.
|
|
17
|
+
*/
|
|
18
|
+
export declare function runSeedAccount(options: RunSeedAccountOptions): Promise<number>;
|
|
19
|
+
//# sourceMappingURL=seed-account.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-account.d.ts","sourceRoot":"","sources":["../../src/scripts/seed-account.ts"],"names":[],"mappings":"AAQA,OAAO,EAAyC,KAAK,KAAK,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACzG,OAAO,EAAuB,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACpE,OAAO,EAAoC,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAStF,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,kFAAkF;IAClF,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IAC5D,iFAAiF;IACjF,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,2DAA2D;IAC3D,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;CACxB;AAeD;;;GAGG;AACH,wBAAsB,cAAc,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,MAAM,CAAC,CAyCpF"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// A development login account from `SEED_EMAIL` and `SEED_PASSWORD`, created through the module's
|
|
2
|
+
// registration, so the app's `onRegistered` hook runs as for a real sign-up. Idempotent: an account
|
|
3
|
+
// with that email is left as it is. Not an ops script: it refuses to run with NODE_ENV=production.
|
|
4
|
+
//
|
|
5
|
+
// // scripts/seed.ts, run with `tsx scripts/seed.ts`
|
|
6
|
+
// import { runSeedAccount } from "@softure-ai/auth/scripts";
|
|
7
|
+
// import config from "../softure.config";
|
|
8
|
+
// process.exitCode = await runSeedAccount({ config });
|
|
9
|
+
import { errorLogLabel, safeError, systemClock } from "@softure-ai/core";
|
|
10
|
+
import { openCommandDatabase } from "@softure-ai/db";
|
|
11
|
+
import { EXIT_FAILED, EXIT_OK, EXIT_USAGE } from "@softure-ai/db/cli";
|
|
12
|
+
import { registerUser } from "../server/register.js";
|
|
13
|
+
import { logoutSession } from "../server/sessions.js";
|
|
14
|
+
import { normalizeEmail } from "../server/validation.js";
|
|
15
|
+
const NAME = "seed-account";
|
|
16
|
+
/** The rate limit key the seed counts registration attempts under. */
|
|
17
|
+
const SEED_CLIENT_KEY = "script:seed";
|
|
18
|
+
const REFUSALS = {
|
|
19
|
+
"auth.registration_closed": "registration is closed by a switch; open it for the seed run",
|
|
20
|
+
"auth.email_invalid": "SEED_EMAIL is not a valid email",
|
|
21
|
+
"auth.password_too_short": "SEED_PASSWORD is shorter than password.minLength",
|
|
22
|
+
"auth.password_too_long": "SEED_PASSWORD is longer than the module accepts",
|
|
23
|
+
"auth.consent_required": "registration requires consent",
|
|
24
|
+
};
|
|
25
|
+
const consoleOutput = {
|
|
26
|
+
log: (line) => console.log(line),
|
|
27
|
+
error: (line) => console.error(line),
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Creates the development account and returns the exit code: 0 created or already there, 1 refused
|
|
31
|
+
* or failed (nothing written), 2 `SEED_EMAIL` or `SEED_PASSWORD` missing.
|
|
32
|
+
*/
|
|
33
|
+
export async function runSeedAccount(options) {
|
|
34
|
+
const env = options.env ?? process.env;
|
|
35
|
+
const output = options.output ?? consoleOutput;
|
|
36
|
+
const email = env.SEED_EMAIL?.trim() ?? "";
|
|
37
|
+
const password = env.SEED_PASSWORD ?? "";
|
|
38
|
+
if (email === "" || password.trim() === "") {
|
|
39
|
+
output.error(`${NAME}: set SEED_EMAIL and SEED_PASSWORD to the development account's email and password`);
|
|
40
|
+
return EXIT_USAGE;
|
|
41
|
+
}
|
|
42
|
+
if (env.NODE_ENV === "production") {
|
|
43
|
+
output.error(`${NAME}: refused: NODE_ENV is production; the seed account is for development`);
|
|
44
|
+
return EXIT_FAILED;
|
|
45
|
+
}
|
|
46
|
+
const opened = await openDatabase(options);
|
|
47
|
+
if (!opened.ok) {
|
|
48
|
+
output.error(`${NAME}: ${opened.message}`);
|
|
49
|
+
return EXIT_FAILED;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const ctx = { db: opened.db, clock: options.clock ?? systemClock, config: options.config };
|
|
53
|
+
const registered = await registerUser(ctx, { email, password, hasConsented: true, clientKey: SEED_CLIENT_KEY });
|
|
54
|
+
if (registered.ok) {
|
|
55
|
+
// Registration signs the account in; the seed only wants the account.
|
|
56
|
+
await logoutSession(ctx, registered.value.session.token);
|
|
57
|
+
output.log(`${NAME}: created the account ${registered.value.user.email}`);
|
|
58
|
+
return EXIT_OK;
|
|
59
|
+
}
|
|
60
|
+
if (registered.error === "auth.email_taken") {
|
|
61
|
+
output.log(`${NAME}: ${normalizeEmail(email)} is already there; nothing changed`);
|
|
62
|
+
return EXIT_OK;
|
|
63
|
+
}
|
|
64
|
+
const reason = registered.error === "security.rate_limited" ? "too many registrations from the seed; wait and run it again" : REFUSALS[registered.error];
|
|
65
|
+
output.error(`${NAME}: refused: ${reason} (${registered.error})`);
|
|
66
|
+
return EXIT_FAILED;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
output.error(`${NAME}: failed: ${describeFailure(error)}`);
|
|
70
|
+
return EXIT_FAILED;
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await opened.close();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function openDatabase(options) {
|
|
77
|
+
if (options.database !== undefined)
|
|
78
|
+
return { ok: true, db: options.database, close: () => Promise.resolve() };
|
|
79
|
+
if (options.config.database === null)
|
|
80
|
+
return { ok: false, message: "the config has no database; set database.url in softure.config" };
|
|
81
|
+
try {
|
|
82
|
+
const opened = await openCommandDatabase(options.config.database, { max: 1 });
|
|
83
|
+
return { ok: true, db: opened.handle.db, close: opened.close };
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return { ok: false, message: `could not open the database: ${describeFailure(error)}` };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** The error message, except for database errors, whose message may carry query parameters (the password hash). */
|
|
90
|
+
function describeFailure(error) {
|
|
91
|
+
if (safeError(error).error === "core.database_failed")
|
|
92
|
+
return `database error (${errorLogLabel(error)})`;
|
|
93
|
+
return error instanceof Error ? error.message : String(error);
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=seed-account.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-account.js","sourceRoot":"","sources":["../../src/scripts/seed-account.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,oGAAoG;AACpG,mGAAmG;AACnG,EAAE;AACF,uDAAuD;AACvD,+DAA+D;AAC/D,4CAA4C;AAC5C,yDAAyD;AACzD,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,WAAW,EAAkC,MAAM,kBAAkB,CAAC;AACzG,OAAO,EAAE,mBAAmB,EAAiB,MAAM,gBAAgB,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,EAAkB,MAAM,oBAAoB,CAAC;AACtF,OAAO,EAAE,YAAY,EAA0B,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAoB,MAAM,uBAAuB,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD,MAAM,IAAI,GAAG,cAAc,CAAC;AAC5B,sEAAsE;AACtE,MAAM,eAAe,GAAG,aAAa,CAAC;AAatC,MAAM,QAAQ,GAA6E;IACzF,0BAA0B,EAAE,8DAA8D;IAC1F,oBAAoB,EAAE,iCAAiC;IACvD,yBAAyB,EAAE,kDAAkD;IAC7E,wBAAwB,EAAE,iDAAiD;IAC3E,uBAAuB,EAAE,+BAA+B;CACzD,CAAC;AAEF,MAAM,aAAa,GAAc;IAC/B,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;IAChC,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC;CACrC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,OAA8B;IACjE,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,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;QACH,MAAM,MAAM,GAAG,MAAM,mBAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;QAC9E,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC;IACjE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,gCAAgC,eAAe,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC;IAC1F,CAAC;AACH,CAAC;AAED,mHAAmH;AACnH,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,SAAS,CAAC,KAAK,CAAC,CAAC,KAAK,KAAK,sBAAsB;QAAE,OAAO,mBAAmB,aAAa,CAAC,KAAK,CAAC,GAAG,CAAC;IACzG,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC"}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { changePassword, type ChangePasswordErrorCode, type ChangePasswordInput, type ChangePasswordResult } from "./change-password.js";
|
|
2
2
|
export { loginUser, type LoginInput, type LoginResult } from "./login.js";
|
|
3
|
+
export { resolveOnlyAccount, type OnlyAccountError, type OnlyAccountErrorCode, type OnlyAccountResult, type ResolveOnlyAccountOptions, } from "./only-account.js";
|
|
3
4
|
export { getAuthOptions, getAuthRoutes, type AuthRoutes } from "./options.js";
|
|
4
5
|
export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword, type PasswordMatch } from "./password.js";
|
|
5
6
|
export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, type PasswordResetRequestInput, type PasswordResetRequestResult, type ResetPasswordErrorCode, type ResetPasswordInput, type ResetPasswordResult, } from "./password-reset.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,KAAK,uBAAuB,EAAE,KAAK,mBAAmB,EAAE,KAAK,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACzI,OAAO,EAAE,SAAS,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAClJ,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,iBAAiB,EACjB,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAC/B,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,GACzB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,UAAU,EACV,KAAK,UAAU,EACf,KAAK,aAAa,GACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AACjH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,YAAY,EA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B,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,UAAU,EACV,KAAK,UAAU,EACf,KAAK,aAAa,GACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AACjH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AAC9G,OAAO,EACL,aAAa,EACb,eAAe,EACf,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,KAAK,WAAW,EAChB,KAAK,yBAAyB,GAC/B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,KAAK,wBAAwB,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/server/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// (`{ db, clock, config }`) and never reads request scope; tokens and client keys come in as values.
|
|
3
3
|
export { changePassword } from "./change-password.js";
|
|
4
4
|
export { loginUser } from "./login.js";
|
|
5
|
+
export { resolveOnlyAccount, } from "./only-account.js";
|
|
5
6
|
export { getAuthOptions, getAuthRoutes } from "./options.js";
|
|
6
7
|
export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword } from "./password.js";
|
|
7
8
|
export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, } from "./password-reset.js";
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,qGAAqG;AACrG,OAAO,EAAE,cAAc,EAAqF,MAAM,sBAAsB,CAAC;AACzI,OAAO,EAAE,SAAS,EAAqC,MAAM,YAAY,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,aAAa,EAAmB,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAsB,MAAM,eAAe,CAAC;AAClJ,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,iBAAiB,GAMlB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,UAAU,GAGX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,kBAAkB,EAAqB,MAAM,cAAc,CAAC;AACjH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAmE,MAAM,eAAe,CAAC;AAC9G,OAAO,EACL,aAAa,EACb,eAAe,EACf,aAAa,EACb,aAAa,EACb,kBAAkB,GAGnB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,0BAA0B,EAAiC,MAAM,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,qGAAqG;AACrG,OAAO,EAAE,cAAc,EAAqF,MAAM,sBAAsB,CAAC;AACzI,OAAO,EAAE,SAAS,EAAqC,MAAM,YAAY,CAAC;AAC1E,OAAO,EACL,kBAAkB,GAKnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,aAAa,EAAmB,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAsB,MAAM,eAAe,CAAC;AAClJ,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,iBAAiB,GAMlB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,UAAU,GAGX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,kBAAkB,EAAqB,MAAM,cAAc,CAAC;AACjH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,YAAY,EAAmE,MAAM,eAAe,CAAC;AAC9G,OAAO,EACL,aAAa,EACb,eAAe,EACf,aAAa,EACb,aAAa,EACb,kBAAkB,GAGnB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,0BAA0B,EAAiC,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Err, type Ok } from "@softure-ai/core";
|
|
2
|
+
import type { Queryable } from "@softure-ai/db";
|
|
3
|
+
export type OnlyAccountErrorCode = "auth.account_unknown" | "auth.no_account" | "auth.account_ambiguous";
|
|
4
|
+
/** Why there is no account to use, with a line for the person running the tool. */
|
|
5
|
+
export type OnlyAccountError = Err<OnlyAccountErrorCode> & {
|
|
6
|
+
readonly message: string;
|
|
7
|
+
};
|
|
8
|
+
export type OnlyAccountResult = Ok<string> | OnlyAccountError;
|
|
9
|
+
export interface ResolveOnlyAccountOptions {
|
|
10
|
+
readonly db: Queryable;
|
|
11
|
+
/** The variable that may name the account's id, e.g. `MCP_USER_ID`. */
|
|
12
|
+
readonly envName: string;
|
|
13
|
+
/** Default: `process.env`. */
|
|
14
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The id of the account `envName` names (it must exist), or, when the variable is empty or unset,
|
|
18
|
+
* of the only account. Refuses an unknown id, an empty database and several accounts with none named.
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveOnlyAccount(options: ResolveOnlyAccountOptions): Promise<OnlyAccountResult>;
|
|
21
|
+
//# sourceMappingURL=only-account.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"only-account.d.ts","sourceRoot":"","sources":["../../src/server/only-account.ts"],"names":[],"mappings":"AAGA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAMhD,MAAM,MAAM,oBAAoB,GAAG,sBAAsB,GAAG,iBAAiB,GAAG,wBAAwB,CAAC;AAEzG,mFAAmF;AACnF,MAAM,MAAM,gBAAgB,GAAG,GAAG,CAAC,oBAAoB,CAAC,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAExF,MAAM,MAAM,iBAAiB,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,gBAAgB,CAAC;AAE9D,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,EAAE,EAAE,SAAS,CAAC;IACvB,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,8BAA8B;IAC9B,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;CAC7D;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAgBvG"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// The one account a local tool acts for (a stdio MCP server, a measurement or seed script): the
|
|
2
|
+
// account an environment variable names, else the only account in the database. Never the first of
|
|
3
|
+
// several: that could be another person's data.
|
|
4
|
+
import { err, ok } from "@softure-ai/core";
|
|
5
|
+
import { eq } from "drizzle-orm";
|
|
6
|
+
import { users } from "../schema.js";
|
|
7
|
+
const UUID_SHAPE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
8
|
+
/**
|
|
9
|
+
* The id of the account `envName` names (it must exist), or, when the variable is empty or unset,
|
|
10
|
+
* of the only account. Refuses an unknown id, an empty database and several accounts with none named.
|
|
11
|
+
*/
|
|
12
|
+
export async function resolveOnlyAccount(options) {
|
|
13
|
+
const { db, envName } = options;
|
|
14
|
+
const requested = (options.env ?? process.env)[envName]?.trim() ?? "";
|
|
15
|
+
if (requested !== "") {
|
|
16
|
+
const unknown = refuse("auth.account_unknown", `${envName} names no account`);
|
|
17
|
+
// Checked before the query: Postgres rejects a malformed uuid with an error, not an empty result.
|
|
18
|
+
if (!UUID_SHAPE.test(requested))
|
|
19
|
+
return unknown;
|
|
20
|
+
const [found] = await db.select({ id: users.id }).from(users).where(eq(users.id, requested)).limit(1);
|
|
21
|
+
return found === undefined ? unknown : ok(found.id);
|
|
22
|
+
}
|
|
23
|
+
const [only, second] = await db.select({ id: users.id }).from(users).limit(2);
|
|
24
|
+
if (only === undefined)
|
|
25
|
+
return refuse("auth.no_account", "the database has no account");
|
|
26
|
+
if (second !== undefined) {
|
|
27
|
+
return refuse("auth.account_ambiguous", `the database has several accounts; set ${envName} to the id of the one to use`);
|
|
28
|
+
}
|
|
29
|
+
return ok(only.id);
|
|
30
|
+
}
|
|
31
|
+
function refuse(code, message) {
|
|
32
|
+
return { ...err(code), message };
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=only-account.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"only-account.js","sourceRoot":"","sources":["../../src/server/only-account.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,mGAAmG;AACnG,gDAAgD;AAChD,OAAO,EAAE,GAAG,EAAE,EAAE,EAAqB,MAAM,kBAAkB,CAAC;AAE9D,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,MAAM,UAAU,GAAG,iEAAiE,CAAC;AAiBrF;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,OAAkC;IACzE,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAChC,MAAM,SAAS,GAAG,CAAC,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACtE,IAAI,SAAS,KAAK,EAAE,EAAE,CAAC;QACrB,MAAM,OAAO,GAAG,MAAM,CAAC,sBAAsB,EAAE,GAAG,OAAO,mBAAmB,CAAC,CAAC;QAC9E,kGAAkG;QAClG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC;YAAE,OAAO,OAAO,CAAC;QAChD,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACtG,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACtD,CAAC;IACD,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC9E,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC,iBAAiB,EAAE,6BAA6B,CAAC,CAAC;IACxF,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC,wBAAwB,EAAE,0CAA0C,OAAO,8BAA8B,CAAC,CAAC;IAC3H,CAAC;IACD,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACrB,CAAC;AAED,SAAS,MAAM,CAAC,IAA0B,EAAE,OAAe;IACzD,OAAO,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAC;AACnC,CAAC"}
|
package/module.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softure-ai/auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "SOFTURE AI auth module: users, scrypt passwords, database sessions, register with consent, login, logout, password change, roles, a proxy route guard, pages and forms for Next.js, and a password reset sender over @softure-ai/mailing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -80,13 +80,13 @@
|
|
|
80
80
|
"peerDependencies": {
|
|
81
81
|
"@softure-ai/mailing": "^0.1.0",
|
|
82
82
|
"@softure-ai/ops": "^0.1.0",
|
|
83
|
-
"@softure-ai/ui": "^0.1.0",
|
|
83
|
+
"@softure-ai/ui": "^0.1.0 || ^0.2.0",
|
|
84
84
|
"drizzle-orm": "^0.45.2",
|
|
85
85
|
"next": "^16.0.0",
|
|
86
86
|
"react": "^19.0.0"
|
|
87
87
|
},
|
|
88
88
|
"devDependencies": {
|
|
89
|
-
"@softure-ai/ui": "^0.1.7"
|
|
89
|
+
"@softure-ai/ui": "^0.1.7 || ^0.2.0"
|
|
90
90
|
},
|
|
91
91
|
"peerDependenciesMeta": {
|
|
92
92
|
"@softure-ai/mailing": {
|
package/src/index.ts
CHANGED
|
@@ -42,7 +42,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
|
|
|
42
42
|
export const auth = defineModule({
|
|
43
43
|
manifest: {
|
|
44
44
|
id: MODULE_ID,
|
|
45
|
-
version: "0.1.
|
|
45
|
+
version: "0.1.13",
|
|
46
46
|
dependsOn: { security: "^0.1.0", mailing: "^0.1.0?", ops: "^0.1.0?" },
|
|
47
47
|
dbSchema: "auth",
|
|
48
48
|
tables: ["users", "sessions", "user_roles", "password_resets"],
|
package/src/scripts/index.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles and set a temporary password
|
|
2
|
-
//
|
|
1
|
+
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles and set a temporary password,
|
|
2
|
+
// and the development seed account
|
|
3
|
+
// (see the README, "Roles", "Account recovery" and "Development seed").
|
|
3
4
|
export { createGrantRoleScript, createRevokeRoleScript, type RoleScriptArgs, type RoleScriptOptions } from "./role-scripts.js";
|
|
4
5
|
export {
|
|
5
6
|
createSetTemporaryPasswordScript,
|
|
6
7
|
type TemporaryPasswordScriptArgs,
|
|
7
8
|
type TemporaryPasswordScriptOptions,
|
|
8
9
|
} from "./temporary-password.js";
|
|
10
|
+
export { runSeedAccount, type RunSeedAccountOptions } from "./seed-account.js";
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// A development login account from `SEED_EMAIL` and `SEED_PASSWORD`, created through the module's
|
|
2
|
+
// registration, so the app's `onRegistered` hook runs as for a real sign-up. Idempotent: an account
|
|
3
|
+
// with that email is left as it is. Not an ops script: it refuses to run with NODE_ENV=production.
|
|
4
|
+
//
|
|
5
|
+
// // scripts/seed.ts, run with `tsx scripts/seed.ts`
|
|
6
|
+
// import { runSeedAccount } from "@softure-ai/auth/scripts";
|
|
7
|
+
// import config from "../softure.config";
|
|
8
|
+
// process.exitCode = await runSeedAccount({ config });
|
|
9
|
+
import { errorLogLabel, safeError, systemClock, type Clock, type SoftureConfig } from "@softure-ai/core";
|
|
10
|
+
import { openCommandDatabase, type Database } from "@softure-ai/db";
|
|
11
|
+
import { EXIT_FAILED, EXIT_OK, EXIT_USAGE, type CliOutput } from "@softure-ai/db/cli";
|
|
12
|
+
import { registerUser, type RegisterErrorCode } from "../server/register.js";
|
|
13
|
+
import { logoutSession, type AuthContext } from "../server/sessions.js";
|
|
14
|
+
import { normalizeEmail } from "../server/validation.js";
|
|
15
|
+
|
|
16
|
+
const NAME = "seed-account";
|
|
17
|
+
/** The rate limit key the seed counts registration attempts under. */
|
|
18
|
+
const SEED_CLIENT_KEY = "script:seed";
|
|
19
|
+
|
|
20
|
+
export interface RunSeedAccountOptions {
|
|
21
|
+
readonly config: SoftureConfig;
|
|
22
|
+
/** Default: `process.env`. Reads `SEED_EMAIL`, `SEED_PASSWORD` and `NODE_ENV`. */
|
|
23
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
24
|
+
/** An open database to use instead of the config's (tests); it is not closed. */
|
|
25
|
+
readonly database?: Database;
|
|
26
|
+
/** Default: the console. The password is never printed. */
|
|
27
|
+
readonly output?: CliOutput;
|
|
28
|
+
readonly clock?: Clock;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const REFUSALS: Readonly<Record<Exclude<RegisterErrorCode, "auth.email_taken">, string>> = {
|
|
32
|
+
"auth.registration_closed": "registration is closed by a switch; open it for the seed run",
|
|
33
|
+
"auth.email_invalid": "SEED_EMAIL is not a valid email",
|
|
34
|
+
"auth.password_too_short": "SEED_PASSWORD is shorter than password.minLength",
|
|
35
|
+
"auth.password_too_long": "SEED_PASSWORD is longer than the module accepts",
|
|
36
|
+
"auth.consent_required": "registration requires consent",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const consoleOutput: CliOutput = {
|
|
40
|
+
log: (line) => console.log(line),
|
|
41
|
+
error: (line) => console.error(line),
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates the development account and returns the exit code: 0 created or already there, 1 refused
|
|
46
|
+
* or failed (nothing written), 2 `SEED_EMAIL` or `SEED_PASSWORD` missing.
|
|
47
|
+
*/
|
|
48
|
+
export async function runSeedAccount(options: RunSeedAccountOptions): Promise<number> {
|
|
49
|
+
const env = options.env ?? process.env;
|
|
50
|
+
const output = options.output ?? consoleOutput;
|
|
51
|
+
const email = env.SEED_EMAIL?.trim() ?? "";
|
|
52
|
+
const password = env.SEED_PASSWORD ?? "";
|
|
53
|
+
if (email === "" || password.trim() === "") {
|
|
54
|
+
output.error(`${NAME}: set SEED_EMAIL and SEED_PASSWORD to the development account's email and password`);
|
|
55
|
+
return EXIT_USAGE;
|
|
56
|
+
}
|
|
57
|
+
if (env.NODE_ENV === "production") {
|
|
58
|
+
output.error(`${NAME}: refused: NODE_ENV is production; the seed account is for development`);
|
|
59
|
+
return EXIT_FAILED;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const opened = await openDatabase(options);
|
|
63
|
+
if (!opened.ok) {
|
|
64
|
+
output.error(`${NAME}: ${opened.message}`);
|
|
65
|
+
return EXIT_FAILED;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const ctx: AuthContext = { db: opened.db, clock: options.clock ?? systemClock, config: options.config };
|
|
69
|
+
const registered = await registerUser(ctx, { email, password, hasConsented: true, clientKey: SEED_CLIENT_KEY });
|
|
70
|
+
if (registered.ok) {
|
|
71
|
+
// Registration signs the account in; the seed only wants the account.
|
|
72
|
+
await logoutSession(ctx, registered.value.session.token);
|
|
73
|
+
output.log(`${NAME}: created the account ${registered.value.user.email}`);
|
|
74
|
+
return EXIT_OK;
|
|
75
|
+
}
|
|
76
|
+
if (registered.error === "auth.email_taken") {
|
|
77
|
+
output.log(`${NAME}: ${normalizeEmail(email)} is already there; nothing changed`);
|
|
78
|
+
return EXIT_OK;
|
|
79
|
+
}
|
|
80
|
+
const reason = registered.error === "security.rate_limited" ? "too many registrations from the seed; wait and run it again" : REFUSALS[registered.error];
|
|
81
|
+
output.error(`${NAME}: refused: ${reason} (${registered.error})`);
|
|
82
|
+
return EXIT_FAILED;
|
|
83
|
+
} catch (error) {
|
|
84
|
+
output.error(`${NAME}: failed: ${describeFailure(error)}`);
|
|
85
|
+
return EXIT_FAILED;
|
|
86
|
+
} finally {
|
|
87
|
+
await opened.close();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type OpenedDatabase = { readonly ok: true; readonly db: Database; readonly close: () => Promise<void> } | { readonly ok: false; readonly message: string };
|
|
92
|
+
|
|
93
|
+
async function openDatabase(options: RunSeedAccountOptions): Promise<OpenedDatabase> {
|
|
94
|
+
if (options.database !== undefined) return { ok: true, db: options.database, close: () => Promise.resolve() };
|
|
95
|
+
if (options.config.database === null) return { ok: false, message: "the config has no database; set database.url in softure.config" };
|
|
96
|
+
try {
|
|
97
|
+
const opened = await openCommandDatabase(options.config.database, { max: 1 });
|
|
98
|
+
return { ok: true, db: opened.handle.db, close: opened.close };
|
|
99
|
+
} catch (error) {
|
|
100
|
+
return { ok: false, message: `could not open the database: ${describeFailure(error)}` };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The error message, except for database errors, whose message may carry query parameters (the password hash). */
|
|
105
|
+
function describeFailure(error: unknown): string {
|
|
106
|
+
if (safeError(error).error === "core.database_failed") return `database error (${errorLogLabel(error)})`;
|
|
107
|
+
return error instanceof Error ? error.message : String(error);
|
|
108
|
+
}
|
package/src/server/index.ts
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
// (`{ db, clock, config }`) and never reads request scope; tokens and client keys come in as values.
|
|
3
3
|
export { changePassword, type ChangePasswordErrorCode, type ChangePasswordInput, type ChangePasswordResult } from "./change-password.js";
|
|
4
4
|
export { loginUser, type LoginInput, type LoginResult } from "./login.js";
|
|
5
|
+
export {
|
|
6
|
+
resolveOnlyAccount,
|
|
7
|
+
type OnlyAccountError,
|
|
8
|
+
type OnlyAccountErrorCode,
|
|
9
|
+
type OnlyAccountResult,
|
|
10
|
+
type ResolveOnlyAccountOptions,
|
|
11
|
+
} from "./only-account.js";
|
|
5
12
|
export { getAuthOptions, getAuthRoutes, type AuthRoutes } from "./options.js";
|
|
6
13
|
export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword, type PasswordMatch } from "./password.js";
|
|
7
14
|
export {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The one account a local tool acts for (a stdio MCP server, a measurement or seed script): the
|
|
2
|
+
// account an environment variable names, else the only account in the database. Never the first of
|
|
3
|
+
// several: that could be another person's data.
|
|
4
|
+
import { err, ok, type Err, type Ok } from "@softure-ai/core";
|
|
5
|
+
import type { Queryable } from "@softure-ai/db";
|
|
6
|
+
import { eq } from "drizzle-orm";
|
|
7
|
+
import { users } from "../schema.js";
|
|
8
|
+
|
|
9
|
+
const UUID_SHAPE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
10
|
+
|
|
11
|
+
export type OnlyAccountErrorCode = "auth.account_unknown" | "auth.no_account" | "auth.account_ambiguous";
|
|
12
|
+
|
|
13
|
+
/** Why there is no account to use, with a line for the person running the tool. */
|
|
14
|
+
export type OnlyAccountError = Err<OnlyAccountErrorCode> & { readonly message: string };
|
|
15
|
+
|
|
16
|
+
export type OnlyAccountResult = Ok<string> | OnlyAccountError;
|
|
17
|
+
|
|
18
|
+
export interface ResolveOnlyAccountOptions {
|
|
19
|
+
readonly db: Queryable;
|
|
20
|
+
/** The variable that may name the account's id, e.g. `MCP_USER_ID`. */
|
|
21
|
+
readonly envName: string;
|
|
22
|
+
/** Default: `process.env`. */
|
|
23
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The id of the account `envName` names (it must exist), or, when the variable is empty or unset,
|
|
28
|
+
* of the only account. Refuses an unknown id, an empty database and several accounts with none named.
|
|
29
|
+
*/
|
|
30
|
+
export async function resolveOnlyAccount(options: ResolveOnlyAccountOptions): Promise<OnlyAccountResult> {
|
|
31
|
+
const { db, envName } = options;
|
|
32
|
+
const requested = (options.env ?? process.env)[envName]?.trim() ?? "";
|
|
33
|
+
if (requested !== "") {
|
|
34
|
+
const unknown = refuse("auth.account_unknown", `${envName} names no account`);
|
|
35
|
+
// Checked before the query: Postgres rejects a malformed uuid with an error, not an empty result.
|
|
36
|
+
if (!UUID_SHAPE.test(requested)) return unknown;
|
|
37
|
+
const [found] = await db.select({ id: users.id }).from(users).where(eq(users.id, requested)).limit(1);
|
|
38
|
+
return found === undefined ? unknown : ok(found.id);
|
|
39
|
+
}
|
|
40
|
+
const [only, second] = await db.select({ id: users.id }).from(users).limit(2);
|
|
41
|
+
if (only === undefined) return refuse("auth.no_account", "the database has no account");
|
|
42
|
+
if (second !== undefined) {
|
|
43
|
+
return refuse("auth.account_ambiguous", `the database has several accounts; set ${envName} to the id of the one to use`);
|
|
44
|
+
}
|
|
45
|
+
return ok(only.id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function refuse(code: OnlyAccountErrorCode, message: string): OnlyAccountError {
|
|
49
|
+
return { ...err(code), message };
|
|
50
|
+
}
|