@softure-ai/auth 0.1.8 → 0.1.9

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 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.1.9
8
+
9
+ - `set-temporary-password` takes `--password-hash=<hash>` (or `--password-hash-file=-` from stdin): the script stores a
10
+ hash computed elsewhere instead of drawing a password, so the plain password never reaches the server. The report
11
+ then says `passwordFrom: "hash"`; a value that is not a module hash is a usage error and is never printed.
12
+ - `createSetTemporaryPasswordScript(config, { alphabet })` chooses the characters of a drawn password.
13
+ - `createTemporaryPassword({ alphabet, length })`, `READABLE_PASSWORD_ALPHABET` (no `0/O`, `1/l/I`, `-`, `_`) and
14
+ `isPasswordHash(value)` are exported from `@softure-ai/auth/server` for the operator's local half.
15
+
7
16
  ## 0.1.8
8
17
 
9
18
  - `logoutAction({ next })`, a `next` form field and `<LogoutButton next>` choose where logout goes; the path is
package/README.md CHANGED
@@ -319,7 +319,35 @@ process.exitCode = await runOpsScript({ script: createSetTemporaryPasswordScript
319
319
  `node set-temporary-password.mjs --email=owner@example.com` shows what it would end and rolls back
320
320
  (the printed password is not stored); `--commit` writes and prints the password to hand over. Send
321
321
  it only to the account's own address, and ask the owner to change it after logging in: the module
322
- does not force a change.
322
+ does not force a change. `createSetTemporaryPasswordScript(config, { alphabet: READABLE_PASSWORD_ALPHABET })`
323
+ draws the password from 57 letters and digits without `0/O`, `1/l/I`, `-` and `_`, for a password
324
+ someone dictates or types from a mail.
325
+
326
+ To keep the plain password off the server, make it and its hash on your own machine and give the
327
+ script only the hash. `createTemporaryPassword({ alphabet, length })`, `hashPassword` and
328
+ `getAuthOptions` come from `@softure-ai/auth/server`; with a given hash the length rule is yours, so
329
+ use at least `password.minLength` and 20:
330
+
331
+ ```ts
332
+ // scripts/temporary-password-hash.ts, run locally
333
+ import { createTemporaryPassword, getAuthOptions, hashPassword, READABLE_PASSWORD_ALPHABET } from "@softure-ai/auth/server";
334
+ import config from "../softure.config";
335
+
336
+ const { password } = getAuthOptions(config);
337
+ const plain = createTemporaryPassword({ alphabet: READABLE_PASSWORD_ALPHABET, length: Math.max(password.minLength, 20) });
338
+ console.error(`password to hand over: ${plain}`);
339
+ console.log(await hashPassword(plain, password.scrypt));
340
+ ```
341
+
342
+ ```sh
343
+ node temporary-password-hash.mjs | ssh app-host docker exec -i app node set-temporary-password.mjs --email=owner@example.com --password-hash-file=- --commit
344
+ ```
345
+
346
+ `--password-hash-file=-` reads the hash from stdin (`--password-hash=<hash>` works too, but lands in
347
+ shell history). The script refuses a value that is not a `hashPassword` hash without printing it,
348
+ stores the hash, ends the sessions and the reset link as before, and reports `passwordFrom: "hash"`
349
+ instead of a password. A hash made with other scrypt parameters than the config's is rehashed at
350
+ the first login. `isPasswordHash(value)` checks the format locally.
323
351
 
324
352
  **Password reset.** Pass a sender, and the login form links to the request page. With
325
353
  `@softure-ai/mailing` enabled, `mailingResetSender()` is that sender:
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.8",
39
+ version: "0.1.9",
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"],
@@ -2,11 +2,15 @@ import { type Clock, type SoftureConfig } from "@softure-ai/core";
2
2
  import { type OpsScript } from "@softure-ai/ops/scripts";
3
3
  export interface TemporaryPasswordScriptArgs {
4
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;
5
7
  }
6
8
  export interface TemporaryPasswordScriptOptions {
7
9
  /** The time stored as `password_changed_at`; the system clock by default. */
8
10
  readonly clock?: Clock;
11
+ /** The characters a drawn password uses (e.g. `READABLE_PASSWORD_ALPHABET`); base64url by default. */
12
+ readonly alphabet?: string;
9
13
  }
10
- /** `set-temporary-password --email=…`: a new random password, every session and reset link ended. */
14
+ /** `set-temporary-password --email=… [--password-hash=…]`: a new password, every session and reset link ended. */
11
15
  export declare function createSetTemporaryPasswordScript(config: SoftureConfig, options?: TemporaryPasswordScriptOptions): OpsScript<TemporaryPasswordScriptArgs>;
12
16
  //# sourceMappingURL=temporary-password.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"temporary-password.d.ts","sourceRoot":"","sources":["../../src/scripts/temporary-password.ts"],"names":[],"mappings":"AAKA,OAAO,EAAmB,KAAK,KAAK,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,EAAoC,KAAK,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAY3F,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,8BAA8B;IAC7C,6EAA6E;IAC7E,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;CACxB;AASD,qGAAqG;AACrG,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,aAAa,EACrB,OAAO,GAAE,8BAAmC,GAC3C,SAAS,CAAC,2BAA2B,CAAC,CAuBxC"}
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;AAc3F,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;AAQD,kHAAkH;AAClH,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,aAAa,EACrB,OAAO,GAAE,8BAAmC,GAC3C,SAAS,CAAC,2BAA2B,CAAC,CAwBxC"}
@@ -1,47 +1,59 @@
1
1
  // The operator's account recovery: a safe ops script (`@softure-ai/ops/scripts`), dry run by
2
- // default, `--commit` writes. It sets a random password, ends every session of the account and its
3
- // pending reset link, and prints the password once for the operator to hand over through a channel
4
- // that proves the owner (a reply to the account's own address). Reports carry the user id, never the email.
5
- import { randomBytes } from "node:crypto";
2
+ // default, `--commit` writes. It sets a new password, ends every session of the account and its
3
+ // pending reset link. Either the script draws a random password and prints it once for the operator
4
+ // to hand over through a channel that proves the owner (a reply to the account's own address), or
5
+ // the operator computes the password and its hash locally and passes only the hash
6
+ // (`--password-hash`, best through `--password-hash-file=-`), so the plain password never reaches
7
+ // the server. Reports carry the user id, never the email or the hash.
6
8
  import { ok, systemClock } from "@softure-ai/core";
7
9
  import { defineOpsScript, refuseOpsScript } from "@softure-ai/ops/scripts";
8
10
  import { eq } from "drizzle-orm";
9
11
  import { z } from "zod";
10
12
  import { passwordResets, sessions, users } from "../schema.js";
11
13
  import { getAuthOptions } from "../server/options.js";
12
- import { hashPassword } from "../server/password.js";
14
+ import { hashPassword, isPasswordHash } from "../server/password.js";
13
15
  import { revokeUserSessions } from "../server/sessions.js";
16
+ import { createTemporaryPassword } from "../server/temporary-password.js";
14
17
  import { normalizeEmail } from "../server/validation.js";
15
18
  /** Long enough to be out of guessing range in any rate limit window. */
16
19
  const MIN_TEMPORARY_LENGTH = 20;
17
- const scriptArgs = z.strictObject({ email: z.string().min(1, "--email=<account email> is required") });
18
- /** A random password of `length` base64url characters. */
19
- function createTemporaryPassword(length) {
20
- return randomBytes(Math.ceil((length * 3) / 4)).toString("base64url").slice(0, length);
21
- }
22
- /** `set-temporary-password --email=…`: a new random password, every session and reset link ended. */
20
+ const PASSWORD_HASH_KEY = "password-hash";
21
+ const scriptArgs = z.strictObject({
22
+ email: z.string().min(1, "--email=<account email> is required"),
23
+ // A constant message: the value is secret-derived and must not be echoed.
24
+ [PASSWORD_HASH_KEY]: z.string().refine(isPasswordHash, "is not a scrypt hash written by @softure-ai/auth").optional(),
25
+ });
26
+ /** `set-temporary-password --email=… [--password-hash=…]`: a new password, every session and reset link ended. */
23
27
  export function createSetTemporaryPasswordScript(config, options = {}) {
24
28
  const clock = options.clock ?? systemClock;
25
29
  return defineOpsScript({
26
30
  name: "set-temporary-password",
27
- description: "Sets a random password for the account with the given email and ends its sessions and reset link.",
28
- usage: ["--email=<account email>"],
31
+ 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>", `--${PASSWORD_HASH_KEY}=<hash> store this hashPassword() hash instead of drawing a password`],
29
33
  args: scriptArgs,
34
+ secrets: [PASSWORD_HASH_KEY],
30
35
  run: async (tx, args) => {
31
36
  const ctx = { db: tx, clock, config };
32
37
  const [account] = await tx.select({ id: users.id }).from(users).where(eq(users.email, normalizeEmail(args.email))).limit(1);
33
38
  if (account === undefined)
34
39
  return refuseOpsScript("no account has this email");
35
40
  const before = { userId: account.id, sessions: (await tx.select().from(sessions).where(eq(sessions.userId, account.id))).length };
36
- const authOptions = getAuthOptions(config);
37
- const temporaryPassword = createTemporaryPassword(Math.max(authOptions.password.minLength, MIN_TEMPORARY_LENGTH));
38
- const passwordHash = await hashPassword(temporaryPassword, authOptions.password.scrypt);
39
- await tx.update(users).set({ passwordHash, passwordChangedAt: clock.now() }).where(eq(users.id, account.id));
41
+ const password = await choosePassword(args[PASSWORD_HASH_KEY], config, options.alphabet);
42
+ await tx.update(users).set({ passwordHash: password.hash, passwordChangedAt: clock.now() }).where(eq(users.id, account.id));
40
43
  await revokeUserSessions(ctx, account.id);
41
44
  await tx.delete(passwordResets).where(eq(passwordResets.userId, account.id));
42
45
  const remaining = (await tx.select().from(sessions).where(eq(sessions.userId, account.id))).length;
43
- return ok({ before, after: { userId: account.id, sessions: remaining, temporaryPassword } });
46
+ const after = password.from === "hash" ? { passwordFrom: "hash" } : { temporaryPassword: password.plain };
47
+ return ok({ before, after: { userId: account.id, sessions: remaining, ...after } });
44
48
  },
45
49
  });
46
50
  }
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
+ }
47
59
  //# 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,mGAAmG;AACnG,mGAAmG;AACnG,4GAA4G;AAC5G,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,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,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC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;AACL,CAAC;AAID,6GAA6G;AAC7G,KAAK,UAAU,cAAc,CAAC,SAA6B,EAAE,MAAqB,EAAE,QAA4B;IAC9G,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"}
@@ -1,7 +1,7 @@
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
3
  export { getAuthOptions, getAuthRoutes, type AuthRoutes } from "./options.js";
4
- export { hashPassword, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword, type PasswordMatch } from "./password.js";
4
+ export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword, type PasswordMatch } from "./password.js";
5
5
  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";
6
6
  export { assertDeclaredRole, findUserRoles, getDeclaredRoles, grantRole, isConfiguredAdmin, isDeclaredRole, revokeRole, type RoleChange, type RoleErrorCode, } from "./roles.js";
7
7
  export { authPrivacyContributor, deleteAuthUserData, exportAuthUserData, type AuthUserData } from "./privacy.js";
@@ -9,4 +9,5 @@ export { isCurrentPassword } from "./reauthenticate.js";
9
9
  export { registerUser, type RegisterErrorCode, type RegisterInput, type RegisterResult } from "./register.js";
10
10
  export { createSession, findSessionUser, logoutSession, pruneSessions, revokeUserSessions, type AuthContext, type RevokeUserSessionsOptions, } from "./sessions.js";
11
11
  export { isRegistrationClosed } from "./switches.js";
12
+ export { createTemporaryPassword, READABLE_PASSWORD_ALPHABET, type TemporaryPasswordOptions } from "./temporary-password.js";
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -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,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAClI,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,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,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"}
@@ -3,7 +3,7 @@
3
3
  export { changePassword } from "./change-password.js";
4
4
  export { loginUser } from "./login.js";
5
5
  export { getAuthOptions, getAuthRoutes } from "./options.js";
6
- export { hashPassword, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword } from "./password.js";
6
+ export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword } from "./password.js";
7
7
  export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, } from "./password-reset.js";
8
8
  export { assertDeclaredRole, findUserRoles, getDeclaredRoles, grantRole, isConfiguredAdmin, isDeclaredRole, revokeRole, } from "./roles.js";
9
9
  export { authPrivacyContributor, deleteAuthUserData, exportAuthUserData } from "./privacy.js";
@@ -11,4 +11,5 @@ export { isCurrentPassword } from "./reauthenticate.js";
11
11
  export { registerUser } from "./register.js";
12
12
  export { createSession, findSessionUser, logoutSession, pruneSessions, revokeUserSessions, } from "./sessions.js";
13
13
  export { isRegistrationClosed } from "./switches.js";
14
+ export { createTemporaryPassword, READABLE_PASSWORD_ALPHABET } from "./temporary-password.js";
14
15
  //# sourceMappingURL=index.js.map
@@ -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,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAsB,MAAM,eAAe,CAAC;AAClI,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"}
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"}
@@ -22,4 +22,6 @@ export declare function needsRehash(storedHash: string, params: ScryptParams): b
22
22
  * The dummy hash is made once per parameter set.
23
23
  */
24
24
  export declare function verifyDummyPassword(password: string, params: ScryptParams): Promise<void>;
25
+ /** Whether `value` is in the `scrypt$N$r$p$salt$key` format this module writes and reads. */
26
+ export declare function isPasswordHash(value: string): boolean;
25
27
  //# sourceMappingURL=password.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"password.d.ts","sourceRoot":"","sources":["../../src/server/password.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD,8FAA8F;AAC9F,eAAO,MAAM,mBAAmB,OAAO,CAAC;AAQxC,uCAAuC;AACvC,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAI1F;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAMhG;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE5D,gFAAgF;AAChF,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE3F;AAMD,yEAAyE;AACzE,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAG7E;AAID;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAS/F"}
1
+ {"version":3,"file":"password.d.ts","sourceRoot":"","sources":["../../src/server/password.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD,8FAA8F;AAC9F,eAAO,MAAM,mBAAmB,OAAO,CAAC;AAQxC,uCAAuC;AACvC,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAI1F;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAMhG;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE5D,gFAAgF;AAChF,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE3F;AAMD,yEAAyE;AACzE,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAG7E;AAID;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAS/F;AAmBD,6FAA6F;AAC7F,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAErD"}
@@ -74,20 +74,28 @@ function deriveKey(password, salt, params) {
74
74
  });
75
75
  });
76
76
  }
77
- function parseHash(storedHash) {
78
- const [scheme, cost, blockSize, parallelization, salt, key, ...rest] = storedHash.split("$");
77
+ /** Whether `value` is in the `scrypt$N$r$p$salt$key` format this module writes and reads. */
78
+ export function isPasswordHash(value) {
79
+ return readHashParts(value) !== null;
80
+ }
81
+ function readHashParts(value) {
82
+ const [scheme, cost, blockSize, parallelization, salt, key, ...rest] = value.split("$");
79
83
  const params = { cost: Number(cost), blockSize: Number(blockSize), parallelization: Number(parallelization) };
80
84
  const isValid = scheme === "scrypt" &&
81
85
  rest.length === 0 &&
82
- Object.values(params).every((value) => Number.isSafeInteger(value) && value > 0) &&
86
+ Object.values(params).every((part) => Number.isSafeInteger(part) && part > 0) &&
83
87
  salt !== undefined &&
84
88
  salt !== "" &&
85
89
  key !== undefined &&
86
90
  key !== "";
87
- if (!isValid) {
91
+ return isValid ? { params, salt, key } : null;
92
+ }
93
+ function parseHash(storedHash) {
94
+ const parts = readHashParts(storedHash);
95
+ if (parts === null) {
88
96
  // The hash itself stays out of the message: it is secret-derived data.
89
97
  throw new Error("@softure-ai/auth: a stored password hash is not in the scrypt$N$r$p$salt$key format");
90
98
  }
91
- return { params, salt: Buffer.from(salt, "base64url"), key: Buffer.from(key, "base64url") };
99
+ return { params: parts.params, salt: Buffer.from(parts.salt, "base64url"), key: Buffer.from(parts.key, "base64url") };
92
100
  }
93
101
  //# sourceMappingURL=password.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"password.js","sourceRoot":"","sources":["../../src/server/password.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,mGAAmG;AACnG,iFAAiF;AACjF,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,eAAe,EAAsB,MAAM,aAAa,CAAC;AAGvF,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,8FAA8F;AAC9F,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAQxC,uCAAuC;AACvC,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,QAAgB,EAAE,MAAoB;IACvE,MAAM,IAAI,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC;IACrC,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IACrE,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,eAAe,EAAE,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC5I,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,QAAgB,EAAE,UAAkB;IACtE,MAAM,MAAM,GAAG,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EAAE,MAAM,CAAC,eAAe,CAAC,EAAE,CAAC;IAC9G,MAAM,OAAO,GACX,MAAM,KAAK,QAAQ;QACnB,IAAI,CAAC,MAAM,KAAK,CAAC;QACjB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC;QAC7E,IAAI,KAAK,SAAS;QAClB,IAAI,KAAK,EAAE;QACX,GAAG,KAAK,SAAS;QACjB,GAAG,KAAK,EAAE,CAAC;IACb,OAAO,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAChD,CAAC;AAED,SAAS,SAAS,CAAC,UAAkB;IACnC,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,CAAC,CAAC;IACxC,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,uEAAuE;QACvE,MAAM,IAAI,KAAK,CAAC,qFAAqF,CAAC,CAAC;IACzG,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,WAAW,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,WAAW,CAAC,EAAE,CAAC;AACxH,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Letters and digits without the ones that read alike when dictated or copied by hand (`0`/`O`,
3
+ * `1`/`l`/`I`) and without `-` and `_`: 57 characters.
4
+ */
5
+ export declare const READABLE_PASSWORD_ALPHABET = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
6
+ export interface TemporaryPasswordOptions {
7
+ /** The characters to draw from, each at most once; base64url by default. */
8
+ readonly alphabet?: string;
9
+ /** How many characters; 20 by default. Use at least the app's `password.minLength`. */
10
+ readonly length?: number;
11
+ }
12
+ /** A random password of `length` characters from `alphabet`. Throws on an unusable alphabet or length. */
13
+ export declare function createTemporaryPassword(options?: TemporaryPasswordOptions): string;
14
+ //# sourceMappingURL=temporary-password.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"temporary-password.d.ts","sourceRoot":"","sources":["../../src/server/temporary-password.ts"],"names":[],"mappings":"AASA;;;GAGG;AACH,eAAO,MAAM,0BAA0B,8DAA8D,CAAC;AAItG,MAAM,WAAW,wBAAwB;IACvC,4EAA4E;IAC5E,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,uFAAuF;IACvF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,0GAA0G;AAC1G,wBAAgB,uBAAuB,CAAC,OAAO,GAAE,wBAA6B,GAAG,MAAM,CAUtF"}
@@ -0,0 +1,26 @@
1
+ // Temporary passwords an operator hands over: random characters drawn uniformly with `randomInt`
2
+ // from `node:crypto`. The same function serves the `set-temporary-password` script and an
3
+ // operator's local half, which hashes the password with `hashPassword` and gives the script only
4
+ // the hash, so the plain password never reaches the server.
5
+ import { randomInt } from "node:crypto";
6
+ /** The URL-safe base64 alphabet: the default, what the script printed before alphabets existed. */
7
+ const BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
8
+ /**
9
+ * Letters and digits without the ones that read alike when dictated or copied by hand (`0`/`O`,
10
+ * `1`/`l`/`I`) and without `-` and `_`: 57 characters.
11
+ */
12
+ export const READABLE_PASSWORD_ALPHABET = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
13
+ const DEFAULT_LENGTH = 20;
14
+ /** A random password of `length` characters from `alphabet`. Throws on an unusable alphabet or length. */
15
+ export function createTemporaryPassword(options = {}) {
16
+ const alphabet = [...(options.alphabet ?? BASE64URL_ALPHABET)];
17
+ const length = options.length ?? DEFAULT_LENGTH;
18
+ if (!Number.isSafeInteger(length) || length < 1) {
19
+ throw new Error("@softure-ai/auth: createTemporaryPassword needs a whole length of at least 1");
20
+ }
21
+ if (alphabet.length < 2 || new Set(alphabet).size !== alphabet.length) {
22
+ throw new Error("@softure-ai/auth: createTemporaryPassword needs an alphabet of at least 2 distinct characters");
23
+ }
24
+ return Array.from({ length }, () => alphabet[randomInt(alphabet.length)]).join("");
25
+ }
26
+ //# sourceMappingURL=temporary-password.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"temporary-password.js","sourceRoot":"","sources":["../../src/server/temporary-password.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,0FAA0F;AAC1F,iGAAiG;AACjG,4DAA4D;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,mGAAmG;AACnG,MAAM,kBAAkB,GAAG,kEAAkE,CAAC;AAE9F;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,2DAA2D,CAAC;AAEtG,MAAM,cAAc,GAAG,EAAE,CAAC;AAS1B,0GAA0G;AAC1G,MAAM,UAAU,uBAAuB,CAAC,UAAoC,EAAE;IAC5E,MAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,IAAI,kBAAkB,CAAC,CAAC,CAAC;IAC/D,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,cAAc,CAAC;IAChD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CAAC,8EAA8E,CAAC,CAAC;IAClG,CAAC;IACD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;QACtE,MAAM,IAAI,KAAK,CAAC,+FAA+F,CAAC,CAAC;IACnH,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACrF,CAAC"}
package/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "auth",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "dependsOn": { "security": "^0.1.0", "mailing": "^0.1.0?", "ops": "^0.1.0?" },
5
5
  "dbSchema": "auth",
6
6
  "tables": ["users", "sessions", "user_roles", "password_resets"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softure-ai/auth",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
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",
package/src/index.ts CHANGED
@@ -41,7 +41,7 @@ export const AUTH_RATE_LIMIT_BUCKETS = {
41
41
  export const auth = defineModule({
42
42
  manifest: {
43
43
  id: MODULE_ID,
44
- version: "0.1.8",
44
+ version: "0.1.9",
45
45
  dependsOn: { security: "^0.1.0", mailing: "^0.1.0?", ops: "^0.1.0?" },
46
46
  dbSchema: "auth",
47
47
  tables: ["users", "sessions", "user_roles", "password_resets"],
@@ -1,38 +1,45 @@
1
1
  // The operator's account recovery: a safe ops script (`@softure-ai/ops/scripts`), dry run by
2
- // default, `--commit` writes. It sets a random password, ends every session of the account and its
3
- // pending reset link, and prints the password once for the operator to hand over through a channel
4
- // that proves the owner (a reply to the account's own address). Reports carry the user id, never the email.
5
- import { randomBytes } from "node:crypto";
2
+ // default, `--commit` writes. It sets a new password, ends every session of the account and its
3
+ // pending reset link. Either the script draws a random password and prints it once for the operator
4
+ // to hand over through a channel that proves the owner (a reply to the account's own address), or
5
+ // the operator computes the password and its hash locally and passes only the hash
6
+ // (`--password-hash`, best through `--password-hash-file=-`), so the plain password never reaches
7
+ // the server. Reports carry the user id, never the email or the hash.
6
8
  import { ok, systemClock, type Clock, type SoftureConfig } from "@softure-ai/core";
7
9
  import { defineOpsScript, refuseOpsScript, type OpsScript } from "@softure-ai/ops/scripts";
8
10
  import { eq } from "drizzle-orm";
9
11
  import { z } from "zod";
10
12
  import { passwordResets, sessions, users } from "../schema.js";
11
13
  import { getAuthOptions } from "../server/options.js";
12
- import { hashPassword } from "../server/password.js";
14
+ import { hashPassword, isPasswordHash } from "../server/password.js";
13
15
  import { revokeUserSessions, type AuthContext } from "../server/sessions.js";
16
+ import { createTemporaryPassword } from "../server/temporary-password.js";
14
17
  import { normalizeEmail } from "../server/validation.js";
15
18
 
16
19
  /** Long enough to be out of guessing range in any rate limit window. */
17
20
  const MIN_TEMPORARY_LENGTH = 20;
21
+ const PASSWORD_HASH_KEY = "password-hash";
18
22
 
19
23
  export interface TemporaryPasswordScriptArgs {
20
24
  readonly email: string;
25
+ /** A hash from `hashPassword`, computed where the password was made; the script then draws none. */
26
+ readonly "password-hash"?: string;
21
27
  }
22
28
 
23
29
  export interface TemporaryPasswordScriptOptions {
24
30
  /** The time stored as `password_changed_at`; the system clock by default. */
25
31
  readonly clock?: Clock;
32
+ /** The characters a drawn password uses (e.g. `READABLE_PASSWORD_ALPHABET`); base64url by default. */
33
+ readonly alphabet?: string;
26
34
  }
27
35
 
28
- const scriptArgs = z.strictObject({ email: z.string().min(1, "--email=<account email> is required") });
36
+ const scriptArgs = z.strictObject({
37
+ email: z.string().min(1, "--email=<account email> is required"),
38
+ // A constant message: the value is secret-derived and must not be echoed.
39
+ [PASSWORD_HASH_KEY]: z.string().refine(isPasswordHash, "is not a scrypt hash written by @softure-ai/auth").optional(),
40
+ });
29
41
 
30
- /** A random password of `length` base64url characters. */
31
- function createTemporaryPassword(length: number): string {
32
- return randomBytes(Math.ceil((length * 3) / 4)).toString("base64url").slice(0, length);
33
- }
34
-
35
- /** `set-temporary-password --email=…`: a new random password, every session and reset link ended. */
42
+ /** `set-temporary-password --email=… [--password-hash=…]`: a new password, every session and reset link ended. */
36
43
  export function createSetTemporaryPasswordScript(
37
44
  config: SoftureConfig,
38
45
  options: TemporaryPasswordScriptOptions = {},
@@ -40,23 +47,34 @@ export function createSetTemporaryPasswordScript(
40
47
  const clock = options.clock ?? systemClock;
41
48
  return defineOpsScript({
42
49
  name: "set-temporary-password",
43
- description: "Sets a random password for the account with the given email and ends its sessions and reset link.",
44
- usage: ["--email=<account email>"],
50
+ description:
51
+ "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>", `--${PASSWORD_HASH_KEY}=<hash> store this hashPassword() hash instead of drawing a password`],
45
53
  args: scriptArgs,
54
+ secrets: [PASSWORD_HASH_KEY],
46
55
  run: async (tx, args) => {
47
56
  const ctx: AuthContext = { db: tx, clock, config };
48
57
  const [account] = await tx.select({ id: users.id }).from(users).where(eq(users.email, normalizeEmail(args.email))).limit(1);
49
58
  if (account === undefined) return refuseOpsScript("no account has this email");
50
59
  const before = { userId: account.id, sessions: (await tx.select().from(sessions).where(eq(sessions.userId, account.id))).length };
51
60
 
52
- const authOptions = getAuthOptions(config);
53
- const temporaryPassword = createTemporaryPassword(Math.max(authOptions.password.minLength, MIN_TEMPORARY_LENGTH));
54
- const passwordHash = await hashPassword(temporaryPassword, authOptions.password.scrypt);
55
- await tx.update(users).set({ passwordHash, passwordChangedAt: clock.now() }).where(eq(users.id, account.id));
61
+ const password = await choosePassword(args[PASSWORD_HASH_KEY], config, options.alphabet);
62
+ await tx.update(users).set({ passwordHash: password.hash, passwordChangedAt: clock.now() }).where(eq(users.id, account.id));
56
63
  await revokeUserSessions(ctx, account.id);
57
64
  await tx.delete(passwordResets).where(eq(passwordResets.userId, account.id));
58
65
  const remaining = (await tx.select().from(sessions).where(eq(sessions.userId, account.id))).length;
59
- return ok({ before, after: { userId: account.id, sessions: remaining, temporaryPassword } });
66
+ const after = password.from === "hash" ? { passwordFrom: "hash" } : { temporaryPassword: password.plain };
67
+ return ok({ before, after: { userId: account.id, sessions: remaining, ...after } });
60
68
  },
61
69
  });
62
70
  }
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
+ }
@@ -3,7 +3,7 @@
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
5
  export { getAuthOptions, getAuthRoutes, type AuthRoutes } from "./options.js";
6
- export { hashPassword, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword, type PasswordMatch } from "./password.js";
6
+ export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword, type PasswordMatch } from "./password.js";
7
7
  export {
8
8
  deliverPasswordReset,
9
9
  findPasswordResetUser,
@@ -44,3 +44,4 @@ export {
44
44
  type RevokeUserSessionsOptions,
45
45
  } from "./sessions.js";
46
46
  export { isRegistrationClosed } from "./switches.js";
47
+ export { createTemporaryPassword, READABLE_PASSWORD_ALPHABET, type TemporaryPasswordOptions } from "./temporary-password.js";
@@ -89,20 +89,30 @@ function deriveKey(password: string, salt: Buffer, params: ScryptParams): Promis
89
89
  });
90
90
  }
91
91
 
92
- function parseHash(storedHash: string): ParsedHash {
93
- const [scheme, cost, blockSize, parallelization, salt, key, ...rest] = storedHash.split("$");
92
+ /** Whether `value` is in the `scrypt$N$r$p$salt$key` format this module writes and reads. */
93
+ export function isPasswordHash(value: string): boolean {
94
+ return readHashParts(value) !== null;
95
+ }
96
+
97
+ function readHashParts(value: string): { params: ScryptParams; salt: string; key: string } | null {
98
+ const [scheme, cost, blockSize, parallelization, salt, key, ...rest] = value.split("$");
94
99
  const params = { cost: Number(cost), blockSize: Number(blockSize), parallelization: Number(parallelization) };
95
100
  const isValid =
96
101
  scheme === "scrypt" &&
97
102
  rest.length === 0 &&
98
- Object.values(params).every((value) => Number.isSafeInteger(value) && value > 0) &&
103
+ Object.values(params).every((part) => Number.isSafeInteger(part) && part > 0) &&
99
104
  salt !== undefined &&
100
105
  salt !== "" &&
101
106
  key !== undefined &&
102
107
  key !== "";
103
- if (!isValid) {
108
+ return isValid ? { params, salt, key } : null;
109
+ }
110
+
111
+ function parseHash(storedHash: string): ParsedHash {
112
+ const parts = readHashParts(storedHash);
113
+ if (parts === null) {
104
114
  // The hash itself stays out of the message: it is secret-derived data.
105
115
  throw new Error("@softure-ai/auth: a stored password hash is not in the scrypt$N$r$p$salt$key format");
106
116
  }
107
- return { params, salt: Buffer.from(salt, "base64url"), key: Buffer.from(key, "base64url") };
117
+ return { params: parts.params, salt: Buffer.from(parts.salt, "base64url"), key: Buffer.from(parts.key, "base64url") };
108
118
  }
@@ -0,0 +1,36 @@
1
+ // Temporary passwords an operator hands over: random characters drawn uniformly with `randomInt`
2
+ // from `node:crypto`. The same function serves the `set-temporary-password` script and an
3
+ // operator's local half, which hashes the password with `hashPassword` and gives the script only
4
+ // the hash, so the plain password never reaches the server.
5
+ import { randomInt } from "node:crypto";
6
+
7
+ /** The URL-safe base64 alphabet: the default, what the script printed before alphabets existed. */
8
+ const BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
9
+
10
+ /**
11
+ * Letters and digits without the ones that read alike when dictated or copied by hand (`0`/`O`,
12
+ * `1`/`l`/`I`) and without `-` and `_`: 57 characters.
13
+ */
14
+ export const READABLE_PASSWORD_ALPHABET = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
15
+
16
+ const DEFAULT_LENGTH = 20;
17
+
18
+ export interface TemporaryPasswordOptions {
19
+ /** The characters to draw from, each at most once; base64url by default. */
20
+ readonly alphabet?: string;
21
+ /** How many characters; 20 by default. Use at least the app's `password.minLength`. */
22
+ readonly length?: number;
23
+ }
24
+
25
+ /** A random password of `length` characters from `alphabet`. Throws on an unusable alphabet or length. */
26
+ export function createTemporaryPassword(options: TemporaryPasswordOptions = {}): string {
27
+ const alphabet = [...(options.alphabet ?? BASE64URL_ALPHABET)];
28
+ const length = options.length ?? DEFAULT_LENGTH;
29
+ if (!Number.isSafeInteger(length) || length < 1) {
30
+ throw new Error("@softure-ai/auth: createTemporaryPassword needs a whole length of at least 1");
31
+ }
32
+ if (alphabet.length < 2 || new Set(alphabet).size !== alphabet.length) {
33
+ throw new Error("@softure-ai/auth: createTemporaryPassword needs an alphabet of at least 2 distinct characters");
34
+ }
35
+ return Array.from({ length }, () => alphabet[randomInt(alphabet.length)]).join("");
36
+ }