@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.
- package/CHANGELOG.md +20 -0
- package/README.md +65 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/jobs/index.d.ts +1 -0
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +3 -2
- package/dist/jobs/index.js.map +1 -1
- package/dist/jobs/password-reset-job.d.ts +19 -0
- package/dist/jobs/password-reset-job.d.ts.map +1 -0
- package/dist/jobs/password-reset-job.js +41 -0
- package/dist/jobs/password-reset-job.js.map +1 -0
- package/dist/options.d.ts +2 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +8 -0
- package/dist/options.js.map +1 -1
- package/dist/password-reset-sender.d.ts +8 -1
- package/dist/password-reset-sender.d.ts.map +1 -1
- package/dist/password-reset-sender.js.map +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/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/password-reset.d.ts +10 -2
- package/dist/server/password-reset.d.ts.map +1 -1
- package/dist/server/password-reset.js +32 -10
- package/dist/server/password-reset.js.map +1 -1
- package/module.json +1 -1
- package/package.json +2 -2
- package/src/index.ts +8 -3
- package/src/jobs/index.ts +4 -2
- package/src/jobs/password-reset-job.ts +43 -0
- package/src/options.ts +9 -1
- package/src/password-reset-sender.ts +9 -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/src/server/index.ts +1 -0
- package/src/server/password-reset.ts +35 -11
|
@@ -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
|
-
}
|
package/src/server/index.ts
CHANGED
|
@@ -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
|
|
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, type Err, type Ok, type SoftureConfig } from "@softure-ai/core";
|
|
@@ -8,6 +8,7 @@ import type { RateLimitRejection } from "@softure-ai/security";
|
|
|
8
8
|
import { consumeRateLimit } from "@softure-ai/security/server";
|
|
9
9
|
import { and, eq, gt, lte } from "drizzle-orm";
|
|
10
10
|
import type { AuthUser } from "../contract.js";
|
|
11
|
+
import type { PasswordResetSender } from "../password-reset-sender.js";
|
|
11
12
|
import { passwordResets, sessions, users } from "../schema.js";
|
|
12
13
|
import { getAuthOptions, getAuthRoutes } from "./options.js";
|
|
13
14
|
import { hashPassword } from "./password.js";
|
|
@@ -57,23 +58,46 @@ export async function requestPasswordReset(ctx: AuthContext, input: PasswordRese
|
|
|
57
58
|
|
|
58
59
|
/**
|
|
59
60
|
* Issues a reset link for the account with `email` (replacing a pending one) and hands it to the
|
|
60
|
-
* app's sender
|
|
61
|
+
* app's sender, or, with `passwordReset.enqueue`, enqueues the account's id for the reset job
|
|
62
|
+
* (`queued`), which issues and sends the link with retry. `no_account` when no account has that
|
|
63
|
+
* email. Sender, enqueuer and database errors propagate.
|
|
61
64
|
*/
|
|
62
|
-
export async function deliverPasswordReset(ctx: AuthContext, email: string): Promise<"sent" | "no_account"> {
|
|
63
|
-
const send = getAuthOptions(ctx.config).passwordReset
|
|
65
|
+
export async function deliverPasswordReset(ctx: AuthContext, email: string): Promise<"sent" | "queued" | "no_account"> {
|
|
66
|
+
const { send, enqueue } = getAuthOptions(ctx.config).passwordReset;
|
|
64
67
|
if (send === undefined) {
|
|
65
68
|
throw new Error("@softure-ai/auth: deliverPasswordReset needs auth({ passwordReset: { send } })");
|
|
66
69
|
}
|
|
67
|
-
const [user] = await ctx.db
|
|
68
|
-
.select({ id: users.id, email: users.email, createdAt: users.createdAt })
|
|
69
|
-
.from(users)
|
|
70
|
-
.where(eq(users.email, email))
|
|
71
|
-
.limit(1);
|
|
70
|
+
const [user] = await ctx.db.select(USER_COLUMNS).from(users).where(eq(users.email, email)).limit(1);
|
|
72
71
|
if (user === undefined) return "no_account";
|
|
72
|
+
if (enqueue !== undefined) {
|
|
73
|
+
await enqueue(ctx, user.id);
|
|
74
|
+
return "queued";
|
|
75
|
+
}
|
|
76
|
+
await issueAndSend(ctx, user, send);
|
|
77
|
+
return "sent";
|
|
78
|
+
}
|
|
73
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Issues a reset link for the account with `userId` and hands it to the app's sender: the work of
|
|
82
|
+
* the reset job (`@softure-ai/auth/jobs`). `no_account` when the account is gone. Sender and
|
|
83
|
+
* database errors propagate.
|
|
84
|
+
*/
|
|
85
|
+
export async function sendPasswordReset(ctx: AuthContext, userId: string): Promise<"sent" | "no_account"> {
|
|
86
|
+
const send = getAuthOptions(ctx.config).passwordReset.send;
|
|
87
|
+
if (send === undefined) {
|
|
88
|
+
throw new Error("@softure-ai/auth: sendPasswordReset needs auth({ passwordReset: { send } })");
|
|
89
|
+
}
|
|
90
|
+
const [user] = await ctx.db.select(USER_COLUMNS).from(users).where(eq(users.id, userId)).limit(1);
|
|
91
|
+
if (user === undefined) return "no_account";
|
|
92
|
+
await issueAndSend(ctx, user, send);
|
|
93
|
+
return "sent";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const USER_COLUMNS = { id: users.id, email: users.email, createdAt: users.createdAt };
|
|
97
|
+
|
|
98
|
+
async function issueAndSend(ctx: AuthContext, user: AuthUser, send: PasswordResetSender): Promise<void> {
|
|
74
99
|
const { token, expiresAt } = await issuePasswordReset(ctx, user.id);
|
|
75
100
|
await send(getPasswordResetLink(ctx.config, token), user, { expiresAt, locale: ctx.config.locale });
|
|
76
|
-
return "sent";
|
|
77
101
|
}
|
|
78
102
|
|
|
79
103
|
/** Stores a new reset token for `userId`, replacing a pending one, and returns the token. */
|