@softure-ai/auth 0.2.2 → 0.2.3
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 +9 -0
- package/README.md +25 -1
- package/dist/index.js +1 -1
- package/dist/scripts/create-account.d.ts +18 -0
- package/dist/scripts/create-account.d.ts.map +1 -0
- package/dist/scripts/create-account.js +56 -0
- package/dist/scripts/create-account.js.map +1 -0
- package/dist/scripts/index.d.ts +1 -0
- package/dist/scripts/index.d.ts.map +1 -1
- package/dist/scripts/index.js +4 -3
- package/dist/scripts/index.js.map +1 -1
- package/dist/scripts/password-choice.d.ts +22 -0
- package/dist/scripts/password-choice.d.ts.map +1 -0
- package/dist/scripts/password-choice.js +23 -0
- package/dist/scripts/password-choice.js.map +1 -0
- package/dist/scripts/temporary-password.d.ts.map +1 -1
- package/dist/scripts/temporary-password.js +4 -19
- package/dist/scripts/temporary-password.js.map +1 -1
- package/module.json +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/scripts/create-account.ts +71 -0
- package/src/scripts/index.ts +4 -3
- package/src/scripts/password-choice.ts +33 -0
- package/src/scripts/temporary-password.ts +4 -22
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,15 @@ 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.2.3
|
|
8
|
+
|
|
9
|
+
- `createCreateAccountScript(config, { maxAccounts?, alphabet?, clock? })` in `@softure-ai/auth/scripts` (#521): the
|
|
10
|
+
`create-account` ops script creates an account while registration is closed, with a drawn temporary password or a
|
|
11
|
+
given `--password-hash`, dry run by default. It refuses an existing or invalid email and, with `maxAccounts`, a
|
|
12
|
+
table that already holds that many accounts (the table is locked for the run). Migrate (optional): an app that
|
|
13
|
+
inserted its first account into `auth.users` itself, or added its own index to hold one account, uses the script
|
|
14
|
+
with `maxAccounts: 1` instead.
|
|
15
|
+
|
|
7
16
|
## 0.2.2
|
|
8
17
|
|
|
9
18
|
- Password reset mails can leave from a background job with retry (#494): `auth({ passwordReset: { send, enqueue:
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Users and sessions in the `auth` schema, scrypt password hashes, opaque session
|
|
|
12
12
|
login, logout, password change and password reset as server actions and pages, `getCurrentUser` / `requireUser`
|
|
13
13
|
for server code, roles (`requireRole`, `authorizeRole`, `hasRole`, and `grant-role` /
|
|
14
14
|
`revoke-role` scripts), account recovery (`revokeUserSessions` and a `set-temporary-password`
|
|
15
|
-
script), `createAuthGuard` for the app's `proxy.ts`, and a health check that
|
|
15
|
+
script), a `create-account` script for the first account of an app with registration closed, `createAuthGuard` for the app's `proxy.ts`, and a health check that
|
|
16
16
|
`GET /api/health` of `@softure-ai/ops` runs (every auth table answers, no rows read).
|
|
17
17
|
`@softure-ai/auth/mailing` sends password reset mails through `@softure-ai/mailing`
|
|
18
18
|
(`mailingResetSender()`); `@softure-ai/auth/jobs` sends them from a background job of `@softure-ai/jobs` with
|
|
@@ -366,6 +366,28 @@ the first login. `isPasswordHash(value)` checks the format locally, exactly: a h
|
|
|
366
366
|
a character outside base64url or a cost scrypt refuses is not a hash `hashPassword` writes, so the script refuses it
|
|
367
367
|
instead of storing a hash no password matches.
|
|
368
368
|
|
|
369
|
+
**First account.** An app with registration closed (`registrationClosed: true`, no `/register` page) creates its
|
|
370
|
+
owner's account with the `create-account` ops script. It draws a temporary password exactly like
|
|
371
|
+
`set-temporary-password` (or stores a `--password-hash` made locally, the same way), refuses an invalid email and an
|
|
372
|
+
email that already has an account, and works whether registration is open or not:
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
// scripts/create-account.ts (bundled and run like grant-role)
|
|
376
|
+
import { createCreateAccountScript } from "@softure-ai/auth/scripts";
|
|
377
|
+
import { runOpsScript } from "@softure-ai/ops/scripts";
|
|
378
|
+
import config from "../softure.config";
|
|
379
|
+
|
|
380
|
+
process.exitCode = await runOpsScript({ script: createCreateAccountScript(config, { maxAccounts: 1 }), argv: process.argv.slice(2), config });
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
`node create-account.mjs --email=owner@example.com` shows the account it would create and rolls back; `--commit`
|
|
384
|
+
writes and prints the password once (or `passwordFrom: "hash"`). The report names the user id and the number of
|
|
385
|
+
accounts, never the email. `maxAccounts` (a whole number of at least 1, no limit by default) makes the script refuse
|
|
386
|
+
once `auth.users` holds that many accounts, whoever created them; the script locks the table for its run, so two runs
|
|
387
|
+
cannot both pass the limit. The app's `onRegistered` hook does not run (no consent, no registration fields): an app
|
|
388
|
+
that keeps its own rows per account creates them in its own script. `alphabet` and `clock` work as for
|
|
389
|
+
`createSetTemporaryPasswordScript`.
|
|
390
|
+
|
|
369
391
|
**Development seed.** `runSeedAccount` from `@softure-ai/auth/scripts` creates a login account for local
|
|
370
392
|
development from `SEED_EMAIL` and `SEED_PASSWORD` through `registerUser`, so the app's `onRegistered` hook runs as
|
|
371
393
|
for a real sign-up, and ends the session registration opens:
|
|
@@ -666,5 +688,7 @@ Rate limit rows of `security` hold only SHA-256 prefixes of the email and user i
|
|
|
666
688
|
- A `next` path longer than 8192 characters is replaced by the default target, with a warning in
|
|
667
689
|
the log that names its length.
|
|
668
690
|
- A temporary password from the script is not forced to change at the next login.
|
|
691
|
+
- `maxAccounts` limits the `create-account` script only; registration does not count accounts. An app that must hold
|
|
692
|
+
at most N accounts keeps registration closed.
|
|
669
693
|
- A registration attempt reveals whether an email has an account (`auth.email_taken`); the
|
|
670
694
|
`register` rate limit bounds how fast anyone can ask.
|
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.2.
|
|
39
|
+
version: "0.2.3",
|
|
40
40
|
dependsOn: { security: "^0.2.0", mailing: "^0.2.0?", ops: "^0.2.0?", jobs: "^0.1.1?" },
|
|
41
41
|
dbSchema: "auth",
|
|
42
42
|
tables: ["users", "sessions", "user_roles", "password_resets"],
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Clock, type SoftureConfig } from "@softure-ai/core";
|
|
2
|
+
import { type OpsScript } from "@softure-ai/ops/scripts";
|
|
3
|
+
export interface CreateAccountScriptArgs {
|
|
4
|
+
readonly email: string;
|
|
5
|
+
/** A hash from `hashPassword`, computed where the password was made; the script then draws none. */
|
|
6
|
+
readonly "password-hash"?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface CreateAccountScriptOptions {
|
|
9
|
+
/** The time stored as `created_at` and `password_changed_at`; the system clock by default. */
|
|
10
|
+
readonly clock?: Clock;
|
|
11
|
+
/** The characters a drawn password uses (e.g. `READABLE_PASSWORD_ALPHABET`); base64url by default. */
|
|
12
|
+
readonly alphabet?: string;
|
|
13
|
+
/** The most accounts the app holds (e.g. 1 for a single-owner app); the script refuses beyond it. No limit by default. */
|
|
14
|
+
readonly maxAccounts?: number;
|
|
15
|
+
}
|
|
16
|
+
/** `create-account --email=… [--password-hash=…]`: a new account, with or without registration open. */
|
|
17
|
+
export declare function createCreateAccountScript(config: SoftureConfig, options?: CreateAccountScriptOptions): OpsScript<CreateAccountScriptArgs>;
|
|
18
|
+
//# sourceMappingURL=create-account.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-account.d.ts","sourceRoot":"","sources":["../../src/scripts/create-account.ts"],"names":[],"mappings":"AAOA,OAAO,EAAmB,KAAK,KAAK,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,EAAoC,KAAK,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAO3F,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,oGAAoG;IACpG,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CACnC;AAED,MAAM,WAAW,0BAA0B;IACzC,8FAA8F;IAC9F,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACvB,sGAAsG;IACtG,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,0HAA0H;IAC1H,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B;AAOD,wGAAwG;AACxG,wBAAgB,yBAAyB,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,GAAE,0BAA+B,GAAG,SAAS,CAAC,uBAAuB,CAAC,CAkC7I"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// The first account of an app whose registration is closed: a safe ops script
|
|
2
|
+
// (`@softure-ai/ops/scripts`), dry run by default, `--commit` writes. It creates the account with a
|
|
3
|
+
// drawn temporary password, printed once, or with a `hashPassword` hash computed elsewhere
|
|
4
|
+
// (`--password-hash`, best through `--password-hash-file=-`). With `maxAccounts` it refuses once the
|
|
5
|
+
// table holds that many accounts; the table is locked for the run, so two runs cannot both pass.
|
|
6
|
+
// The app's `onRegistered` hook does not run: nobody gave consent and there are no registration
|
|
7
|
+
// fields. Reports carry the user id, never the email or the hash.
|
|
8
|
+
import { ok, systemClock } from "@softure-ai/core";
|
|
9
|
+
import { defineOpsScript, refuseOpsScript } from "@softure-ai/ops/scripts";
|
|
10
|
+
import { count, sql } from "drizzle-orm";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import { users } from "../schema.js";
|
|
13
|
+
import { parseEmail } from "../server/validation.js";
|
|
14
|
+
import { choosePassword, describeChosenPassword, PASSWORD_HASH_KEY, PASSWORD_HASH_USAGE, passwordHashArg } from "./password-choice.js";
|
|
15
|
+
const scriptArgs = z.strictObject({
|
|
16
|
+
email: z.string().min(1, "--email=<account email> is required"),
|
|
17
|
+
[PASSWORD_HASH_KEY]: passwordHashArg,
|
|
18
|
+
});
|
|
19
|
+
/** `create-account --email=… [--password-hash=…]`: a new account, with or without registration open. */
|
|
20
|
+
export function createCreateAccountScript(config, options = {}) {
|
|
21
|
+
const { maxAccounts } = options;
|
|
22
|
+
if (maxAccounts !== undefined && !(Number.isInteger(maxAccounts) && maxAccounts >= 1)) {
|
|
23
|
+
throw new Error("@softure-ai/auth: createCreateAccountScript needs maxAccounts to be a whole number of at least 1");
|
|
24
|
+
}
|
|
25
|
+
const clock = options.clock ?? systemClock;
|
|
26
|
+
return defineOpsScript({
|
|
27
|
+
name: "create-account",
|
|
28
|
+
description: "Creates an account with the given email and a random password (or the given hash), also while registration is closed.",
|
|
29
|
+
usage: ["--email=<account email>", PASSWORD_HASH_USAGE],
|
|
30
|
+
args: scriptArgs,
|
|
31
|
+
secrets: [PASSWORD_HASH_KEY],
|
|
32
|
+
run: async (tx, args) => {
|
|
33
|
+
const email = parseEmail(args.email);
|
|
34
|
+
if (!email.ok)
|
|
35
|
+
return refuseOpsScript("the email is not valid");
|
|
36
|
+
// Conflicts with itself and with every insert, until the script's transaction ends.
|
|
37
|
+
await tx.execute(sql `LOCK TABLE auth.users IN SHARE ROW EXCLUSIVE MODE`);
|
|
38
|
+
const [counted] = await tx.select({ accounts: count() }).from(users);
|
|
39
|
+
const accounts = counted?.accounts ?? 0;
|
|
40
|
+
if (maxAccounts !== undefined && accounts >= maxAccounts) {
|
|
41
|
+
return refuseOpsScript(`the app already holds ${String(accounts)} account(s), its maxAccounts`);
|
|
42
|
+
}
|
|
43
|
+
const password = await choosePassword(args[PASSWORD_HASH_KEY], config, options.alphabet);
|
|
44
|
+
const now = clock.now();
|
|
45
|
+
const [created] = await tx
|
|
46
|
+
.insert(users)
|
|
47
|
+
.values({ email: email.value, passwordHash: password.hash, createdAt: now, passwordChangedAt: now })
|
|
48
|
+
.onConflictDoNothing({ target: users.email })
|
|
49
|
+
.returning({ id: users.id });
|
|
50
|
+
if (created === undefined)
|
|
51
|
+
return refuseOpsScript("an account with this email already exists");
|
|
52
|
+
return ok({ before: { accounts }, after: { userId: created.id, accounts: accounts + 1, ...describeChosenPassword(password) } });
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=create-account.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-account.js","sourceRoot":"","sources":["../../src/scripts/create-account.ts"],"names":[],"mappings":"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"}
|
package/dist/scripts/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
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 { createCreateAccountScript, type CreateAccountScriptArgs, type CreateAccountScriptOptions } from "./create-account.js";
|
|
3
4
|
export { runSeedAccount, type RunSeedAccountOptions } from "./seed-account.js";
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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"}
|
|
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,yBAAyB,EAAE,KAAK,uBAAuB,EAAE,KAAK,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAC/H,OAAO,EAAE,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/scripts/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles
|
|
2
|
-
// and the development seed account
|
|
3
|
-
// (see the README, "Roles", "Account recovery" and "Development seed").
|
|
1
|
+
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles, set a temporary password and
|
|
2
|
+
// create an account, and the development seed account
|
|
3
|
+
// (see the README, "Roles", "Account recovery", "First account" and "Development seed").
|
|
4
4
|
export { createGrantRoleScript, createRevokeRoleScript } from "./role-scripts.js";
|
|
5
5
|
export { createSetTemporaryPasswordScript, } from "./temporary-password.js";
|
|
6
|
+
export { createCreateAccountScript } from "./create-account.js";
|
|
6
7
|
export { runSeedAccount } from "./seed-account.js";
|
|
7
8
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/scripts/index.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/scripts/index.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,sDAAsD;AACtD,yFAAyF;AACzF,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAA+C,MAAM,mBAAmB,CAAC;AAC/H,OAAO,EACL,gCAAgC,GAGjC,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,yBAAyB,EAAiE,MAAM,qBAAqB,CAAC;AAC/H,OAAO,EAAE,cAAc,EAA8B,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SoftureConfig } from "@softure-ai/core";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export declare const PASSWORD_HASH_KEY = "password-hash";
|
|
4
|
+
export declare const PASSWORD_HASH_USAGE = "--password-hash=<hash> store this hashPassword() hash instead of drawing a password";
|
|
5
|
+
export declare const passwordHashArg: z.ZodOptional<z.ZodString>;
|
|
6
|
+
export type ChosenPassword = {
|
|
7
|
+
readonly from: "hash";
|
|
8
|
+
readonly hash: string;
|
|
9
|
+
} | {
|
|
10
|
+
readonly from: "drawn";
|
|
11
|
+
readonly hash: string;
|
|
12
|
+
readonly plain: string;
|
|
13
|
+
};
|
|
14
|
+
/** The given hash, or a drawn password of at least 20 characters and `password.minLength`, with its hash. */
|
|
15
|
+
export declare function choosePassword(givenHash: string | undefined, config: SoftureConfig, alphabet: string | undefined): Promise<ChosenPassword>;
|
|
16
|
+
/** What a report says about the stored password: never the hash, the drawn password once. */
|
|
17
|
+
export declare function describeChosenPassword(password: ChosenPassword): {
|
|
18
|
+
passwordFrom: "hash";
|
|
19
|
+
} | {
|
|
20
|
+
temporaryPassword: string;
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=password-choice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"password-choice.d.ts","sourceRoot":"","sources":["../../src/scripts/password-choice.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAOxB,eAAO,MAAM,iBAAiB,kBAAkB,CAAC;AACjD,eAAO,MAAM,mBAAmB,yFAAgG,CAAC;AAGjI,eAAO,MAAM,eAAe,4BAAmG,CAAC;AAEhI,MAAM,MAAM,cAAc,GACtB;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9E,6GAA6G;AAC7G,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,cAAc,CAAC,CAKhJ;AAED,6FAA6F;AAC7F,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,cAAc,GAAG;IAAE,YAAY,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAEzH"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { getAuthOptions } from "../server/options.js";
|
|
3
|
+
import { hashPassword, isPasswordHash } from "../server/password.js";
|
|
4
|
+
import { createTemporaryPassword } from "../server/temporary-password.js";
|
|
5
|
+
/** Long enough to be out of guessing range in any rate limit window. */
|
|
6
|
+
const MIN_TEMPORARY_LENGTH = 20;
|
|
7
|
+
export const PASSWORD_HASH_KEY = "password-hash";
|
|
8
|
+
export const PASSWORD_HASH_USAGE = `--${PASSWORD_HASH_KEY}=<hash> store this hashPassword() hash instead of drawing a password`;
|
|
9
|
+
// A constant message: the value is secret-derived and must not be echoed.
|
|
10
|
+
export const passwordHashArg = z.string().refine(isPasswordHash, "is not a scrypt hash written by @softure-ai/auth").optional();
|
|
11
|
+
/** The given hash, or a drawn password of at least 20 characters and `password.minLength`, with its hash. */
|
|
12
|
+
export async function choosePassword(givenHash, config, alphabet) {
|
|
13
|
+
if (givenHash !== undefined)
|
|
14
|
+
return { from: "hash", hash: givenHash };
|
|
15
|
+
const { password } = getAuthOptions(config);
|
|
16
|
+
const plain = createTemporaryPassword({ alphabet, length: Math.max(password.minLength, MIN_TEMPORARY_LENGTH) });
|
|
17
|
+
return { from: "drawn", hash: await hashPassword(plain, password.scrypt), plain };
|
|
18
|
+
}
|
|
19
|
+
/** What a report says about the stored password: never the hash, the drawn password once. */
|
|
20
|
+
export function describeChosenPassword(password) {
|
|
21
|
+
return password.from === "hash" ? { passwordFrom: "hash" } : { temporaryPassword: password.plain };
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=password-choice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"password-choice.js","sourceRoot":"","sources":["../../src/scripts/password-choice.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAE1E,wEAAwE;AACxE,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAChC,MAAM,CAAC,MAAM,iBAAiB,GAAG,eAAe,CAAC;AACjD,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,iBAAiB,uEAAuE,CAAC;AAEjI,0EAA0E;AAC1E,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,cAAc,EAAE,kDAAkD,CAAC,CAAC,QAAQ,EAAE,CAAC;AAMhI,6GAA6G;AAC7G,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,SAA6B,EAAE,MAAqB,EAAE,QAA4B;IACrH,IAAI,SAAS,KAAK,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;IACtE,MAAM,EAAE,QAAQ,EAAE,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IAC5C,MAAM,KAAK,GAAG,uBAAuB,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,EAAE,oBAAoB,CAAC,EAAE,CAAC,CAAC;IAChH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;AACpF,CAAC;AAED,6FAA6F;AAC7F,MAAM,UAAU,sBAAsB,CAAC,QAAwB;IAC7D,OAAO,QAAQ,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,iBAAiB,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC;AACrG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"temporary-password.d.ts","sourceRoot":"","sources":["../../src/scripts/temporary-password.ts"],"names":[],"mappings":"AAOA,OAAO,EAAmB,KAAK,KAAK,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,EAAoC,KAAK,SAAS,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"temporary-password.d.ts","sourceRoot":"","sources":["../../src/scripts/temporary-password.ts"],"names":[],"mappings":"AAOA,OAAO,EAAmB,KAAK,KAAK,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,EAAoC,KAAK,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAQ3F,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,oGAAoG;IACpG,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CACnC;AAED,MAAM,WAAW,8BAA8B;IAC7C,6EAA6E;IAC7E,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACvB,sGAAsG;IACtG,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B;AAOD,kHAAkH;AAClH,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,aAAa,EACrB,OAAO,GAAE,8BAAmC,GAC3C,SAAS,CAAC,2BAA2B,CAAC,CAuBxC"}
|
|
@@ -10,18 +10,12 @@ import { defineOpsScript, refuseOpsScript } from "@softure-ai/ops/scripts";
|
|
|
10
10
|
import { eq } from "drizzle-orm";
|
|
11
11
|
import { z } from "zod";
|
|
12
12
|
import { passwordResets, sessions, users } from "../schema.js";
|
|
13
|
-
import { getAuthOptions } from "../server/options.js";
|
|
14
|
-
import { hashPassword, isPasswordHash } from "../server/password.js";
|
|
15
13
|
import { revokeUserSessions } from "../server/sessions.js";
|
|
16
|
-
import { createTemporaryPassword } from "../server/temporary-password.js";
|
|
17
14
|
import { normalizeEmail } from "../server/validation.js";
|
|
18
|
-
|
|
19
|
-
const MIN_TEMPORARY_LENGTH = 20;
|
|
20
|
-
const PASSWORD_HASH_KEY = "password-hash";
|
|
15
|
+
import { choosePassword, describeChosenPassword, PASSWORD_HASH_KEY, PASSWORD_HASH_USAGE, passwordHashArg } from "./password-choice.js";
|
|
21
16
|
const scriptArgs = z.strictObject({
|
|
22
17
|
email: z.string().min(1, "--email=<account email> is required"),
|
|
23
|
-
|
|
24
|
-
[PASSWORD_HASH_KEY]: z.string().refine(isPasswordHash, "is not a scrypt hash written by @softure-ai/auth").optional(),
|
|
18
|
+
[PASSWORD_HASH_KEY]: passwordHashArg,
|
|
25
19
|
});
|
|
26
20
|
/** `set-temporary-password --email=… [--password-hash=…]`: a new password, every session and reset link ended. */
|
|
27
21
|
export function createSetTemporaryPasswordScript(config, options = {}) {
|
|
@@ -29,7 +23,7 @@ export function createSetTemporaryPasswordScript(config, options = {}) {
|
|
|
29
23
|
return defineOpsScript({
|
|
30
24
|
name: "set-temporary-password",
|
|
31
25
|
description: "Sets a new password for the account with the given email (a random one, or the given hash) and ends its sessions and reset link.",
|
|
32
|
-
usage: ["--email=<account email>",
|
|
26
|
+
usage: ["--email=<account email>", PASSWORD_HASH_USAGE],
|
|
33
27
|
args: scriptArgs,
|
|
34
28
|
secrets: [PASSWORD_HASH_KEY],
|
|
35
29
|
run: async (tx, args) => {
|
|
@@ -43,17 +37,8 @@ export function createSetTemporaryPasswordScript(config, options = {}) {
|
|
|
43
37
|
await revokeUserSessions(ctx, account.id);
|
|
44
38
|
await tx.delete(passwordResets).where(eq(passwordResets.userId, account.id));
|
|
45
39
|
const remaining = (await tx.select().from(sessions).where(eq(sessions.userId, account.id))).length;
|
|
46
|
-
|
|
47
|
-
return ok({ before, after: { userId: account.id, sessions: remaining, ...after } });
|
|
40
|
+
return ok({ before, after: { userId: account.id, sessions: remaining, ...describeChosenPassword(password) } });
|
|
48
41
|
},
|
|
49
42
|
});
|
|
50
43
|
}
|
|
51
|
-
/** The given hash, or a drawn password of at least 20 characters and `password.minLength`, with its hash. */
|
|
52
|
-
async function choosePassword(givenHash, config, alphabet) {
|
|
53
|
-
if (givenHash !== undefined)
|
|
54
|
-
return { from: "hash", hash: givenHash };
|
|
55
|
-
const { password } = getAuthOptions(config);
|
|
56
|
-
const plain = createTemporaryPassword({ alphabet, length: Math.max(password.minLength, MIN_TEMPORARY_LENGTH) });
|
|
57
|
-
return { from: "drawn", hash: await hashPassword(plain, password.scrypt), plain };
|
|
58
|
-
}
|
|
59
44
|
//# sourceMappingURL=temporary-password.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"temporary-password.js","sourceRoot":"","sources":["../../src/scripts/temporary-password.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,gGAAgG;AAChG,oGAAoG;AACpG,kGAAkG;AAClG,mFAAmF;AACnF,kGAAkG;AAClG,sEAAsE;AACtE,OAAO,EAAE,EAAE,EAAE,WAAW,EAAkC,MAAM,kBAAkB,CAAC;AACnF,OAAO,EAAE,eAAe,EAAE,eAAe,EAAkB,MAAM,yBAAyB,CAAC;AAC3F,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"temporary-password.js","sourceRoot":"","sources":["../../src/scripts/temporary-password.ts"],"names":[],"mappings":"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"}
|
package/module.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softure-ai/auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
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, sent in the request or by a background job through @softure-ai/jobs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
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.2.
|
|
45
|
+
version: "0.2.3",
|
|
46
46
|
dependsOn: { security: "^0.2.0", mailing: "^0.2.0?", ops: "^0.2.0?", jobs: "^0.1.1?" },
|
|
47
47
|
dbSchema: "auth",
|
|
48
48
|
tables: ["users", "sessions", "user_roles", "password_resets"],
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The first account of an app whose registration is closed: a safe ops script
|
|
2
|
+
// (`@softure-ai/ops/scripts`), dry run by default, `--commit` writes. It creates the account with a
|
|
3
|
+
// drawn temporary password, printed once, or with a `hashPassword` hash computed elsewhere
|
|
4
|
+
// (`--password-hash`, best through `--password-hash-file=-`). With `maxAccounts` it refuses once the
|
|
5
|
+
// table holds that many accounts; the table is locked for the run, so two runs cannot both pass.
|
|
6
|
+
// The app's `onRegistered` hook does not run: nobody gave consent and there are no registration
|
|
7
|
+
// fields. Reports carry the user id, never the email or the hash.
|
|
8
|
+
import { ok, systemClock, type Clock, type SoftureConfig } from "@softure-ai/core";
|
|
9
|
+
import { defineOpsScript, refuseOpsScript, type OpsScript } from "@softure-ai/ops/scripts";
|
|
10
|
+
import { count, sql } from "drizzle-orm";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import { users } from "../schema.js";
|
|
13
|
+
import { parseEmail } from "../server/validation.js";
|
|
14
|
+
import { choosePassword, describeChosenPassword, PASSWORD_HASH_KEY, PASSWORD_HASH_USAGE, passwordHashArg } from "./password-choice.js";
|
|
15
|
+
|
|
16
|
+
export interface CreateAccountScriptArgs {
|
|
17
|
+
readonly email: string;
|
|
18
|
+
/** A hash from `hashPassword`, computed where the password was made; the script then draws none. */
|
|
19
|
+
readonly "password-hash"?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CreateAccountScriptOptions {
|
|
23
|
+
/** The time stored as `created_at` and `password_changed_at`; the system clock by default. */
|
|
24
|
+
readonly clock?: Clock;
|
|
25
|
+
/** The characters a drawn password uses (e.g. `READABLE_PASSWORD_ALPHABET`); base64url by default. */
|
|
26
|
+
readonly alphabet?: string;
|
|
27
|
+
/** The most accounts the app holds (e.g. 1 for a single-owner app); the script refuses beyond it. No limit by default. */
|
|
28
|
+
readonly maxAccounts?: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const scriptArgs = z.strictObject({
|
|
32
|
+
email: z.string().min(1, "--email=<account email> is required"),
|
|
33
|
+
[PASSWORD_HASH_KEY]: passwordHashArg,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** `create-account --email=… [--password-hash=…]`: a new account, with or without registration open. */
|
|
37
|
+
export function createCreateAccountScript(config: SoftureConfig, options: CreateAccountScriptOptions = {}): OpsScript<CreateAccountScriptArgs> {
|
|
38
|
+
const { maxAccounts } = options;
|
|
39
|
+
if (maxAccounts !== undefined && !(Number.isInteger(maxAccounts) && maxAccounts >= 1)) {
|
|
40
|
+
throw new Error("@softure-ai/auth: createCreateAccountScript needs maxAccounts to be a whole number of at least 1");
|
|
41
|
+
}
|
|
42
|
+
const clock = options.clock ?? systemClock;
|
|
43
|
+
return defineOpsScript({
|
|
44
|
+
name: "create-account",
|
|
45
|
+
description: "Creates an account with the given email and a random password (or the given hash), also while registration is closed.",
|
|
46
|
+
usage: ["--email=<account email>", PASSWORD_HASH_USAGE],
|
|
47
|
+
args: scriptArgs,
|
|
48
|
+
secrets: [PASSWORD_HASH_KEY],
|
|
49
|
+
run: async (tx, args) => {
|
|
50
|
+
const email = parseEmail(args.email);
|
|
51
|
+
if (!email.ok) return refuseOpsScript("the email is not valid");
|
|
52
|
+
// Conflicts with itself and with every insert, until the script's transaction ends.
|
|
53
|
+
await tx.execute(sql`LOCK TABLE auth.users IN SHARE ROW EXCLUSIVE MODE`);
|
|
54
|
+
const [counted] = await tx.select({ accounts: count() }).from(users);
|
|
55
|
+
const accounts = counted?.accounts ?? 0;
|
|
56
|
+
if (maxAccounts !== undefined && accounts >= maxAccounts) {
|
|
57
|
+
return refuseOpsScript(`the app already holds ${String(accounts)} account(s), its maxAccounts`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const password = await choosePassword(args[PASSWORD_HASH_KEY], config, options.alphabet);
|
|
61
|
+
const now = clock.now();
|
|
62
|
+
const [created] = await tx
|
|
63
|
+
.insert(users)
|
|
64
|
+
.values({ email: email.value, passwordHash: password.hash, createdAt: now, passwordChangedAt: now })
|
|
65
|
+
.onConflictDoNothing({ target: users.email })
|
|
66
|
+
.returning({ id: users.id });
|
|
67
|
+
if (created === undefined) return refuseOpsScript("an account with this email already exists");
|
|
68
|
+
return ok({ before: { accounts }, after: { userId: created.id, accounts: accounts + 1, ...describeChosenPassword(password) } });
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
}
|
package/src/scripts/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles
|
|
2
|
-
// and the development seed account
|
|
3
|
-
// (see the README, "Roles", "Account recovery" and "Development seed").
|
|
1
|
+
// `@softure-ai/auth/scripts`: ops scripts that grant and revoke roles, set a temporary password and
|
|
2
|
+
// create an account, and the development seed account
|
|
3
|
+
// (see the README, "Roles", "Account recovery", "First account" and "Development seed").
|
|
4
4
|
export { createGrantRoleScript, createRevokeRoleScript, type RoleScriptArgs, type RoleScriptOptions } from "./role-scripts.js";
|
|
5
5
|
export {
|
|
6
6
|
createSetTemporaryPasswordScript,
|
|
7
7
|
type TemporaryPasswordScriptArgs,
|
|
8
8
|
type TemporaryPasswordScriptOptions,
|
|
9
9
|
} from "./temporary-password.js";
|
|
10
|
+
export { createCreateAccountScript, type CreateAccountScriptArgs, type CreateAccountScriptOptions } from "./create-account.js";
|
|
10
11
|
export { runSeedAccount, type RunSeedAccountOptions } from "./seed-account.js";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The password an ops script stores: a drawn temporary one, or a `hashPassword` hash computed where
|
|
2
|
+
// the password was made (`--password-hash`, best through `--password-hash-file=-`), so the plain
|
|
3
|
+
// password never reaches the server. Shared by `set-temporary-password` and `create-account`.
|
|
4
|
+
import type { SoftureConfig } from "@softure-ai/core";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { getAuthOptions } from "../server/options.js";
|
|
7
|
+
import { hashPassword, isPasswordHash } from "../server/password.js";
|
|
8
|
+
import { createTemporaryPassword } from "../server/temporary-password.js";
|
|
9
|
+
|
|
10
|
+
/** Long enough to be out of guessing range in any rate limit window. */
|
|
11
|
+
const MIN_TEMPORARY_LENGTH = 20;
|
|
12
|
+
export const PASSWORD_HASH_KEY = "password-hash";
|
|
13
|
+
export const PASSWORD_HASH_USAGE = `--${PASSWORD_HASH_KEY}=<hash> store this hashPassword() hash instead of drawing a password`;
|
|
14
|
+
|
|
15
|
+
// A constant message: the value is secret-derived and must not be echoed.
|
|
16
|
+
export const passwordHashArg = z.string().refine(isPasswordHash, "is not a scrypt hash written by @softure-ai/auth").optional();
|
|
17
|
+
|
|
18
|
+
export type ChosenPassword =
|
|
19
|
+
| { readonly from: "hash"; readonly hash: string }
|
|
20
|
+
| { readonly from: "drawn"; readonly hash: string; readonly plain: string };
|
|
21
|
+
|
|
22
|
+
/** The given hash, or a drawn password of at least 20 characters and `password.minLength`, with its hash. */
|
|
23
|
+
export async function choosePassword(givenHash: string | undefined, config: SoftureConfig, alphabet: string | undefined): Promise<ChosenPassword> {
|
|
24
|
+
if (givenHash !== undefined) return { from: "hash", hash: givenHash };
|
|
25
|
+
const { password } = getAuthOptions(config);
|
|
26
|
+
const plain = createTemporaryPassword({ alphabet, length: Math.max(password.minLength, MIN_TEMPORARY_LENGTH) });
|
|
27
|
+
return { from: "drawn", hash: await hashPassword(plain, password.scrypt), plain };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What a report says about the stored password: never the hash, the drawn password once. */
|
|
31
|
+
export function describeChosenPassword(password: ChosenPassword): { passwordFrom: "hash" } | { temporaryPassword: string } {
|
|
32
|
+
return password.from === "hash" ? { passwordFrom: "hash" } : { temporaryPassword: password.plain };
|
|
33
|
+
}
|
|
@@ -10,15 +10,9 @@ import { defineOpsScript, refuseOpsScript, type OpsScript } from "@softure-ai/op
|
|
|
10
10
|
import { eq } from "drizzle-orm";
|
|
11
11
|
import { z } from "zod";
|
|
12
12
|
import { passwordResets, sessions, users } from "../schema.js";
|
|
13
|
-
import { getAuthOptions } from "../server/options.js";
|
|
14
|
-
import { hashPassword, isPasswordHash } from "../server/password.js";
|
|
15
13
|
import { revokeUserSessions, type AuthContext } from "../server/sessions.js";
|
|
16
|
-
import { createTemporaryPassword } from "../server/temporary-password.js";
|
|
17
14
|
import { normalizeEmail } from "../server/validation.js";
|
|
18
|
-
|
|
19
|
-
/** Long enough to be out of guessing range in any rate limit window. */
|
|
20
|
-
const MIN_TEMPORARY_LENGTH = 20;
|
|
21
|
-
const PASSWORD_HASH_KEY = "password-hash";
|
|
15
|
+
import { choosePassword, describeChosenPassword, PASSWORD_HASH_KEY, PASSWORD_HASH_USAGE, passwordHashArg } from "./password-choice.js";
|
|
22
16
|
|
|
23
17
|
export interface TemporaryPasswordScriptArgs {
|
|
24
18
|
readonly email: string;
|
|
@@ -35,8 +29,7 @@ export interface TemporaryPasswordScriptOptions {
|
|
|
35
29
|
|
|
36
30
|
const scriptArgs = z.strictObject({
|
|
37
31
|
email: z.string().min(1, "--email=<account email> is required"),
|
|
38
|
-
|
|
39
|
-
[PASSWORD_HASH_KEY]: z.string().refine(isPasswordHash, "is not a scrypt hash written by @softure-ai/auth").optional(),
|
|
32
|
+
[PASSWORD_HASH_KEY]: passwordHashArg,
|
|
40
33
|
});
|
|
41
34
|
|
|
42
35
|
/** `set-temporary-password --email=… [--password-hash=…]`: a new password, every session and reset link ended. */
|
|
@@ -49,7 +42,7 @@ export function createSetTemporaryPasswordScript(
|
|
|
49
42
|
name: "set-temporary-password",
|
|
50
43
|
description:
|
|
51
44
|
"Sets a new password for the account with the given email (a random one, or the given hash) and ends its sessions and reset link.",
|
|
52
|
-
usage: ["--email=<account email>",
|
|
45
|
+
usage: ["--email=<account email>", PASSWORD_HASH_USAGE],
|
|
53
46
|
args: scriptArgs,
|
|
54
47
|
secrets: [PASSWORD_HASH_KEY],
|
|
55
48
|
run: async (tx, args) => {
|
|
@@ -63,18 +56,7 @@ export function createSetTemporaryPasswordScript(
|
|
|
63
56
|
await revokeUserSessions(ctx, account.id);
|
|
64
57
|
await tx.delete(passwordResets).where(eq(passwordResets.userId, account.id));
|
|
65
58
|
const remaining = (await tx.select().from(sessions).where(eq(sessions.userId, account.id))).length;
|
|
66
|
-
|
|
67
|
-
return ok({ before, after: { userId: account.id, sessions: remaining, ...after } });
|
|
59
|
+
return ok({ before, after: { userId: account.id, sessions: remaining, ...describeChosenPassword(password) } });
|
|
68
60
|
},
|
|
69
61
|
});
|
|
70
62
|
}
|
|
71
|
-
|
|
72
|
-
type ChosenPassword = { readonly from: "hash"; readonly hash: string } | { readonly from: "drawn"; readonly hash: string; readonly plain: string };
|
|
73
|
-
|
|
74
|
-
/** The given hash, or a drawn password of at least 20 characters and `password.minLength`, with its hash. */
|
|
75
|
-
async function choosePassword(givenHash: string | undefined, config: SoftureConfig, alphabet: string | undefined): Promise<ChosenPassword> {
|
|
76
|
-
if (givenHash !== undefined) return { from: "hash", hash: givenHash };
|
|
77
|
-
const { password } = getAuthOptions(config);
|
|
78
|
-
const plain = createTemporaryPassword({ alphabet, length: Math.max(password.minLength, MIN_TEMPORARY_LENGTH) });
|
|
79
|
-
return { from: "drawn", hash: await hashPassword(plain, password.scrypt), plain };
|
|
80
|
-
}
|