@softure-ai/auth 0.2.1 → 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.
Files changed (57) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +65 -4
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +3 -3
  6. package/dist/index.js.map +1 -1
  7. package/dist/jobs/index.d.ts +1 -0
  8. package/dist/jobs/index.d.ts.map +1 -1
  9. package/dist/jobs/index.js +3 -2
  10. package/dist/jobs/index.js.map +1 -1
  11. package/dist/jobs/password-reset-job.d.ts +19 -0
  12. package/dist/jobs/password-reset-job.d.ts.map +1 -0
  13. package/dist/jobs/password-reset-job.js +41 -0
  14. package/dist/jobs/password-reset-job.js.map +1 -0
  15. package/dist/options.d.ts +2 -1
  16. package/dist/options.d.ts.map +1 -1
  17. package/dist/options.js +8 -0
  18. package/dist/options.js.map +1 -1
  19. package/dist/password-reset-sender.d.ts +8 -1
  20. package/dist/password-reset-sender.d.ts.map +1 -1
  21. package/dist/password-reset-sender.js.map +1 -1
  22. package/dist/scripts/create-account.d.ts +18 -0
  23. package/dist/scripts/create-account.d.ts.map +1 -0
  24. package/dist/scripts/create-account.js +56 -0
  25. package/dist/scripts/create-account.js.map +1 -0
  26. package/dist/scripts/index.d.ts +1 -0
  27. package/dist/scripts/index.d.ts.map +1 -1
  28. package/dist/scripts/index.js +4 -3
  29. package/dist/scripts/index.js.map +1 -1
  30. package/dist/scripts/password-choice.d.ts +22 -0
  31. package/dist/scripts/password-choice.d.ts.map +1 -0
  32. package/dist/scripts/password-choice.js +23 -0
  33. package/dist/scripts/password-choice.js.map +1 -0
  34. package/dist/scripts/temporary-password.d.ts.map +1 -1
  35. package/dist/scripts/temporary-password.js +4 -19
  36. package/dist/scripts/temporary-password.js.map +1 -1
  37. package/dist/server/index.d.ts +1 -1
  38. package/dist/server/index.d.ts.map +1 -1
  39. package/dist/server/index.js +1 -1
  40. package/dist/server/index.js.map +1 -1
  41. package/dist/server/password-reset.d.ts +10 -2
  42. package/dist/server/password-reset.d.ts.map +1 -1
  43. package/dist/server/password-reset.js +32 -10
  44. package/dist/server/password-reset.js.map +1 -1
  45. package/module.json +1 -1
  46. package/package.json +2 -2
  47. package/src/index.ts +8 -3
  48. package/src/jobs/index.ts +4 -2
  49. package/src/jobs/password-reset-job.ts +43 -0
  50. package/src/options.ts +9 -1
  51. package/src/password-reset-sender.ts +9 -1
  52. package/src/scripts/create-account.ts +71 -0
  53. package/src/scripts/index.ts +4 -3
  54. package/src/scripts/password-choice.ts +33 -0
  55. package/src/scripts/temporary-password.ts +4 -22
  56. package/src/server/index.ts +1 -0
  57. package/src/server/password-reset.ts +35 -11
@@ -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;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
+ {"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
- /** Long enough to be out of guessing range in any rate limit window. */
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
- // 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(),
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>", `--${PASSWORD_HASH_KEY}=<hash> store this hashPassword() hash instead of drawing a password`],
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
- const after = password.from === "hash" ? { passwordFrom: "hash" } : { temporaryPassword: password.plain };
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,C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C;YAC7E,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YACnG,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,sBAAsB,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,CAAC;QACjH,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -3,7 +3,7 @@ export { loginUser, type LoginInput, type LoginResult } from "./login.js";
3
3
  export { resolveOnlyAccount, type OnlyAccountError, type OnlyAccountErrorCode, type OnlyAccountResult, type ResolveOnlyAccountOptions, } from "./only-account.js";
4
4
  export { getAuthOptions, getAuthRoutes, type AuthRoutes } from "./options.js";
5
5
  export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword, type PasswordMatch } from "./password.js";
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";
6
+ export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, sendPasswordReset, type PasswordResetRequestInput, type PasswordResetRequestResult, type ResetPasswordErrorCode, type ResetPasswordInput, type ResetPasswordResult, } from "./password-reset.js";
7
7
  export { assertDeclaredRole, findUserRoles, getDeclaredRoles, grantRole, isConfiguredAdmin, isDeclaredRole, revokeRole, type RoleChange, type RoleErrorCode, } from "./roles.js";
8
8
  export { authPrivacyContributor, deleteAuthUserData, exportAuthUserData, type AuthUserData } from "./privacy.js";
9
9
  export { isCurrentPassword } from "./reauthenticate.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,EACL,kBAAkB,EAClB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,yBAAyB,GAC/B,MAAM,mBAAmB,CAAC;AAC3B,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,Y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AK,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,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"}
@@ -5,7 +5,7 @@ export { loginUser } from "./login.js";
5
5
  export { resolveOnlyAccount, } from "./only-account.js";
6
6
  export { getAuthOptions, getAuthRoutes } from "./options.js";
7
7
  export { hashPassword, isPasswordHash, matchPassword, MAX_PASSWORD_LENGTH, needsRehash, verifyPassword } from "./password.js";
8
- export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, } from "./password-reset.js";
8
+ export { deliverPasswordReset, findPasswordResetUser, getPasswordResetLink, isPasswordResetEnabled, issuePasswordReset, prunePasswordResets, requestPasswordReset, resetPassword, RESET_TOKEN_PARAM, sendPasswordReset, } from "./password-reset.js";
9
9
  export { assertDeclaredRole, findUserRoles, getDeclaredRoles, grantRole, isConfiguredAdmin, isDeclaredRole, revokeRole, } from "./roles.js";
10
10
  export { authPrivacyContributor, deleteAuthUserData, exportAuthUserData } from "./privacy.js";
11
11
  export { isCurrentPassword } from "./reauthenticate.js";
@@ -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,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,OA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,CAAC"}
@@ -22,9 +22,17 @@ export type PasswordResetRequestResult = Ok<{
22
22
  export declare function requestPasswordReset(ctx: AuthContext, input: PasswordResetRequestInput): Promise<PasswordResetRequestResult>;
23
23
  /**
24
24
  * Issues a reset link for the account with `email` (replacing a pending one) and hands it to the
25
- * app's sender. `no_account` when no account has that email. Sender and database errors propagate.
25
+ * app's sender, or, with `passwordReset.enqueue`, enqueues the account's id for the reset job
26
+ * (`queued`), which issues and sends the link with retry. `no_account` when no account has that
27
+ * email. Sender, enqueuer and database errors propagate.
26
28
  */
27
- export declare function deliverPasswordReset(ctx: AuthContext, email: string): Promise<"sent" | "no_account">;
29
+ export declare function deliverPasswordReset(ctx: AuthContext, email: string): Promise<"sent" | "queued" | "no_account">;
30
+ /**
31
+ * Issues a reset link for the account with `userId` and hands it to the app's sender: the work of
32
+ * the reset job (`@softure-ai/auth/jobs`). `no_account` when the account is gone. Sender and
33
+ * database errors propagate.
34
+ */
35
+ export declare function sendPasswordReset(ctx: AuthContext, userId: string): Promise<"sent" | "no_account">;
28
36
  /** Stores a new reset token for `userId`, replacing a pending one, and returns the token. */
29
37
  export declare function issuePasswordReset(ctx: AuthContext, userId: string): Promise<{
30
38
  readonly token: string;
@@ -1 +1 @@
1
- {"version":3,"file":"password-reset.d.ts","sourceRoot":"","sources":["../../src/server/password-reset.ts"],"names":[],"mappings":"AAKA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAClF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAK/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAMjD,oEAAoE;AACpE,eAAO,MAAM,iBAAiB,UAAU,CAAC;AAEzC,sFAAsF;AACtF,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,aAAa,GAAG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,WAAW,EAAE,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CA2B7G;AAED,uFAAuF;AACvF,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAG3E"}
@@ -1,6 +1,6 @@
1
1
  // Password reset by token. A request counts its buckets and answers the same for every email;
2
- // `deliverPasswordReset` then issues the token and calls the app's sender (the Next adapter runs it
3
- // after the response, so neither its timing nor its failures reveal an account). A reset link
2
+ // `deliverPasswordReset` then issues the token and calls the app's sender, or enqueues the reset job
3
+ // (the Next adapter runs it after the response, so neither its timing nor its failures reveal an account). A reset link
4
4
  // carries 32 random bytes; only their sha256 is stored, one pending link per account, and the reset
5
5
  // deletes the row in the transaction that sets the new password and ends every session.
6
6
  import { err, ok } from "@softure-ai/core";
@@ -42,23 +42,45 @@ export async function requestPasswordReset(ctx, input) {
42
42
  }
43
43
  /**
44
44
  * Issues a reset link for the account with `email` (replacing a pending one) and hands it to the
45
- * app's sender. `no_account` when no account has that email. Sender and database errors propagate.
45
+ * app's sender, or, with `passwordReset.enqueue`, enqueues the account's id for the reset job
46
+ * (`queued`), which issues and sends the link with retry. `no_account` when no account has that
47
+ * email. Sender, enqueuer and database errors propagate.
46
48
  */
47
49
  export async function deliverPasswordReset(ctx, email) {
48
- const send = getAuthOptions(ctx.config).passwordReset.send;
50
+ const { send, enqueue } = getAuthOptions(ctx.config).passwordReset;
49
51
  if (send === undefined) {
50
52
  throw new Error("@softure-ai/auth: deliverPasswordReset needs auth({ passwordReset: { send } })");
51
53
  }
52
- const [user] = await ctx.db
53
- .select({ id: users.id, email: users.email, createdAt: users.createdAt })
54
- .from(users)
55
- .where(eq(users.email, email))
56
- .limit(1);
54
+ const [user] = await ctx.db.select(USER_COLUMNS).from(users).where(eq(users.email, email)).limit(1);
55
+ if (user === undefined)
56
+ return "no_account";
57
+ if (enqueue !== undefined) {
58
+ await enqueue(ctx, user.id);
59
+ return "queued";
60
+ }
61
+ await issueAndSend(ctx, user, send);
62
+ return "sent";
63
+ }
64
+ /**
65
+ * Issues a reset link for the account with `userId` and hands it to the app's sender: the work of
66
+ * the reset job (`@softure-ai/auth/jobs`). `no_account` when the account is gone. Sender and
67
+ * database errors propagate.
68
+ */
69
+ export async function sendPasswordReset(ctx, userId) {
70
+ const send = getAuthOptions(ctx.config).passwordReset.send;
71
+ if (send === undefined) {
72
+ throw new Error("@softure-ai/auth: sendPasswordReset needs auth({ passwordReset: { send } })");
73
+ }
74
+ const [user] = await ctx.db.select(USER_COLUMNS).from(users).where(eq(users.id, userId)).limit(1);
57
75
  if (user === undefined)
58
76
  return "no_account";
77
+ await issueAndSend(ctx, user, send);
78
+ return "sent";
79
+ }
80
+ const USER_COLUMNS = { id: users.id, email: users.email, createdAt: users.createdAt };
81
+ async function issueAndSend(ctx, user, send) {
59
82
  const { token, expiresAt } = await issuePasswordReset(ctx, user.id);
60
83
  await send(getPasswordResetLink(ctx.config, token), user, { expiresAt, locale: ctx.config.locale });
61
- return "sent";
62
84
  }
63
85
  /** Stores a new reset token for `userId`, replacing a pending one, and returns the token. */
64
86
  export async function issuePasswordReset(ctx, userId) {
@@ -1 +1 @@
1
- {"version":3,"file":"password-reset.js","sourceRoot":"","sources":["../../src/server/password-reset.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,oGAAoG;AACpG,8FAA8F;AAC9F,oGAAoG;AACpG,wFAAwF;AACxF,OAAO,EAAE,GAAG,EAAE,EAAE,EAAyC,MAAM,kBAAkB,CAAC;AAElF,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7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AAS,CAAC,EAAE,EAAE,CAAC,cAAc,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,CAAC;aACtF,SAAS,EAAE,CAAC;QACf,IAAI,QAAQ,KAAK,SAAS;YAAE,OAAO,GAAG,CAAC,0BAA0B,CAAC,CAAC;QACnE,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1G,MAAM,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;QACtE,OAAO,EAAE,EAAE,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC;AAED,uFAAuF;AACvF,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,GAAgB;IACxD,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,cAAc,CAAC,SAAS,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IACtH,OAAO,OAAO,CAAC,MAAM,CAAC;AACxB,CAAC"}
package/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "auth",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "dependsOn": { "security": "^0.2.0", "mailing": "^0.2.0?", "ops": "^0.2.0?", "jobs": "^0.1.1?" },
5
5
  "dbSchema": "auth",
6
6
  "tables": ["users", "sessions", "user_roles", "password_resets"],
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@softure-ai/auth",
3
- "version": "0.2.1",
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.",
3
+ "version": "0.2.3",
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",
7
7
  "sideEffects": false,
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // Public API of @softure-ai/auth: the module factory for softure.config.ts, its types, messages and
2
2
  // tables, and the development reset sender. Database work is in `@softure-ai/auth/server`, the Next.js adapter in `/next`, the route
3
- // guard in `/proxy`, the forms in `/ui` and the role scripts in `/scripts`.
3
+ // guard in `/proxy`, the reset job in `/jobs`, the forms in `/ui` and the role scripts in `/scripts`.
4
4
  import { defineModule, resolveMigrationsDir } from "@softure-ai/core";
5
5
  import type { RateLimitBucketInput } from "@softure-ai/security";
6
6
  import { authMessages } from "./messages/index.js";
@@ -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.1",
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"],
@@ -94,7 +94,12 @@ export type {
94
94
  } from "./contract.js";
95
95
  export { authMessages, getAuthErrorMessage, type AuthMessages } from "./messages/index.js";
96
96
  export type { AuthOptions, AuthOptionsInput, OnRegisteredHook, RewriteRedirect, RewriteRedirectContext, ScryptParams } from "./options.js";
97
- export { consolePasswordResetSender, type PasswordResetDetails, type PasswordResetSender } from "./password-reset-sender.js";
97
+ export {
98
+ consolePasswordResetSender,
99
+ type PasswordResetDetails,
100
+ type PasswordResetEnqueuer,
101
+ type PasswordResetSender,
102
+ } from "./password-reset-sender.js";
98
103
  export { getSessionCookie, type SessionCookie } from "./session-cookie.js";
99
104
  export { toSafeNextPath } from "./safe-next-path.js";
100
105
  export { ADMIN_ROLE, ROLE_NAME_PATTERN } from "./roles.js";
package/src/jobs/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- // The jobs adapter of @softure-ai/auth: its prune functions as `@softure-ai/jobs` handlers and schedules (an optional
2
- // peer dependency; only apps that import this entry need it).
1
+ // The jobs adapter of @softure-ai/auth: its prune functions as `@softure-ai/jobs` handlers and schedules, and the
2
+ // password reset mail sent by a job with retry (an optional peer dependency; only apps that import this entry need it).
3
3
  import { ok } from "@softure-ai/core";
4
4
  import { defineJobHandler, type JobHandlers, type JobSchedulesInput } from "@softure-ai/jobs";
5
5
  import { z } from "zod";
@@ -31,3 +31,5 @@ export const authJobSchedules = {
31
31
  "auth.prune-sessions": { every: "1h" },
32
32
  "auth.prune-password-resets": { every: "1d" },
33
33
  } satisfies JobSchedulesInput;
34
+
35
+ export { enqueuePasswordReset, PASSWORD_RESET_QUEUE, passwordResetJobHandler, passwordResetJobHandlers } from "./password-reset-job.js";
@@ -0,0 +1,43 @@
1
+ // The password reset job: the request enqueues the account's id (never the address or a token), and
2
+ // the handler issues a fresh link and calls the app's sender. Only the token's hash is stored, so the
3
+ // link cannot be rebuilt from the database: every attempt issues a new one, which replaces the last.
4
+ // A retry after a send that was not recorded therefore mails a second link, and only the newer works.
5
+ import { err, errorLogLabel, ok } from "@softure-ai/core";
6
+ import { defineJobHandler, failJob, type JobHandlers } from "@softure-ai/jobs";
7
+ import { enqueueJob } from "@softure-ai/jobs/server";
8
+ import { z } from "zod";
9
+ import type { PasswordResetEnqueuer } from "../password-reset-sender.js";
10
+ import { getAuthOptions } from "../server/options.js";
11
+ import { sendPasswordReset } from "../server/password-reset.js";
12
+
13
+ /** The queue of the reset job; define it in `jobs({ queues })`. */
14
+ export const PASSWORD_RESET_QUEUE = "auth.password-reset";
15
+
16
+ /**
17
+ * `auth({ passwordReset: { send, enqueue: enqueuePasswordReset } })`: enqueues `{ userId }` on
18
+ * `auth.password-reset`. While a job of the account is pending, a new request joins it.
19
+ */
20
+ export const enqueuePasswordReset: PasswordResetEnqueuer = async (ctx, userId) => {
21
+ await enqueueJob(ctx, { queue: PASSWORD_RESET_QUEUE, payload: { userId }, dedupKey: userId });
22
+ };
23
+
24
+ /**
25
+ * Issues the link and calls `passwordReset.send`. A gone account ends the job without a mail; a
26
+ * failing sender is logged by its label (never the link) and retried with the queue's backoff.
27
+ */
28
+ export const passwordResetJobHandler = defineJobHandler({
29
+ payload: z.object({ userId: z.uuid() }),
30
+ run: async ({ userId }, ctx) => {
31
+ if (getAuthOptions(ctx.config).passwordReset.send === undefined) return failJob("auth.password_reset_unavailable");
32
+ try {
33
+ await sendPasswordReset(ctx, userId);
34
+ } catch (error) {
35
+ console.error(`@softure-ai/auth: the password reset job failed: ${errorLogLabel(error)}`);
36
+ return err("auth.password_reset_send_failed");
37
+ }
38
+ return ok();
39
+ },
40
+ });
41
+
42
+ /** The reset job for the worker, next to the prune handlers: `handlers: { ...authJobHandlers, ...passwordResetJobHandlers }`. */
43
+ export const passwordResetJobHandlers: JobHandlers = { [PASSWORD_RESET_QUEUE]: passwordResetJobHandler };
package/src/options.ts CHANGED
@@ -3,7 +3,7 @@ import type { ModuleContext, SoftureConfig } from "@softure-ai/core";
3
3
  import type { Queryable } from "@softure-ai/db";
4
4
  import { z } from "zod";
5
5
  import type { RegisteredEvent } from "./contract.js";
6
- import type { PasswordResetSender } from "./password-reset-sender.js";
6
+ import type { PasswordResetEnqueuer, PasswordResetSender } from "./password-reset-sender.js";
7
7
  import { ROLE_NAME_PATTERN } from "./roles.js";
8
8
 
9
9
  /**
@@ -158,6 +158,11 @@ export const authOptionsSchema = z.strictObject({
158
158
  .strictObject({
159
159
  /** Delivers reset links. Without it password reset is off: no link, and its pages are not found. */
160
160
  send: z.custom<PasswordResetSender>((value) => typeof value === "function", "must be a function").optional(),
161
+ /**
162
+ * Sends the link from a background job instead of the request (`enqueuePasswordReset` of
163
+ * `@softure-ai/auth/jobs`): the job's handler issues the link and calls `send`, with retry.
164
+ */
165
+ enqueue: z.custom<PasswordResetEnqueuer>((value) => typeof value === "function", "must be a function").optional(),
161
166
  /** How long a reset link works. */
162
167
  ttlMinutes: z.number().int().min(5).max(1440).default(60),
163
168
  })
@@ -167,6 +172,9 @@ export const authOptionsSchema = z.strictObject({
167
172
  /** Rewrites the path of every auth redirect (e.g. `tagRedirect` from `@softure-ai/analytics/next`). */
168
173
  rewriteRedirect: z.custom<RewriteRedirect>((value) => typeof value === "function", "must be a function").optional(),
169
174
  }).superRefine((options, context) => {
175
+ if (options.passwordReset.enqueue !== undefined && options.passwordReset.send === undefined) {
176
+ context.addIssue({ code: "custom", path: ["passwordReset", "enqueue"], message: "needs passwordReset.send, which the job calls" });
177
+ }
170
178
  const legacy = options.legacySession?.cookieName;
171
179
  const base = options.cookie.name;
172
180
  if (legacy !== undefined && [base, `__Host-${base}`, `__Secure-${base}`].includes(legacy)) {
@@ -1,6 +1,7 @@
1
1
  // The hook through which auth hands a password reset link to the app, which delivers it (a mail,
2
2
  // e.g. through `mailingResetSender()` of `@softure-ai/auth/mailing`). Auth never sends anything itself.
3
- import type { Locale } from "@softure-ai/core";
3
+ import type { Locale, ModuleContext } from "@softure-ai/core";
4
+ import type { Queryable } from "@softure-ai/db";
4
5
  import type { AuthUser } from "./contract.js";
5
6
 
6
7
  export interface PasswordResetDetails {
@@ -31,3 +32,10 @@ export const consolePasswordResetSender: PasswordResetSender = (link, user, deta
31
32
  console.info(`@softure-ai/auth: password reset link for ${user.email}, valid until ${details.expiresAt.toISOString()}: ${link}`);
32
33
  return Promise.resolve();
33
34
  };
35
+
36
+ /**
37
+ * Hands a reset to a background job instead of sending it in the request: `enqueuePasswordReset` of
38
+ * `@softure-ai/auth/jobs` enqueues `{ userId }` on the `auth.password-reset` queue, whose handler issues
39
+ * the link and calls the sender with retry. `ctx.db` is the request's database.
40
+ */
41
+ export type PasswordResetEnqueuer = (ctx: ModuleContext<Queryable>, userId: string) => Promise<void>;
@@ -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
+ }
@@ -1,10 +1,11 @@
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").
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
+ }