@molecule/api-resource-user 1.4.7 → 1.4.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/README.md +2 -1
- package/dist/handlers/logIn.d.ts.map +1 -1
- package/dist/handlers/logIn.js +142 -29
- package/dist/handlers/logIn.js.map +1 -1
- package/dist/handlers/resetPassword.d.ts +14 -0
- package/dist/handlers/resetPassword.d.ts.map +1 -1
- package/dist/handlers/resetPassword.js +36 -3
- package/dist/handlers/resetPassword.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-10-
|
|
6
|
+
Generated: 2026-10-09T13:50:49.876Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource-user
|
|
@@ -548,6 +548,7 @@ Members:
|
|
|
548
548
|
- `handlers.oauthAuthorize` — function: OAuth initiation — `GET /users/oauth/:provider`.
|
|
549
549
|
- `handlers.read` — function: Reads a user by ID from the database. Attaches plan info (with expiration/renewal status) via the
|
|
550
550
|
- `handlers.readSelf` — function: Reads the AUTHENTICATED user from the session — no `:id` param. Backs
|
|
551
|
+
- `handlers.RESET_TOKEN_TTL_MS` — const: How long a password-reset token stays valid, in milliseconds. Shared with
|
|
551
552
|
- `handlers.ResetPasswordRequest` — interface: Request body for confirming a password reset using a token.
|
|
552
553
|
- `handlers.resetPassword` — function: Confirms a password reset by validating the one-time token previously generated by
|
|
553
554
|
- `handlers.update` — function: Updates a user's profile fields (username, name, email). Validates username format
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logIn.d.ts","sourceRoot":"","sources":["../../src/handlers/logIn.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAG/E,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"logIn.d.ts","sourceRoot":"","sources":["../../src/handlers/logIn.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAG/E,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;AA8DzC,oFAAoF;AACpF,MAAM,WAAW,YAAa,SAAQ,eAAe;IACnD,IAAI,EAAE;QACJ,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,kBAAkB,CAAC,EAAE,MAAM,CAAA;QAC3B,cAAc,CAAC,EAAE,MAAM,CAAA;QACvB,UAAU,CAAC,EAAE,MAAM,CAAA;KACpB,CAAA;CACF;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,KAAK,GAAI,6CAA6C,KAAK,CAAC,QAAQ,MACjE,KAAK,YAAY,EAAE,KAAK,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAmUvD,CAAA"}
|
package/dist/handlers/logIn.js
CHANGED
|
@@ -8,10 +8,13 @@ import { hashResetToken } from '../utilities/hashResetToken.js';
|
|
|
8
8
|
import { normalizeEmail } from '../utilities/normalizeEmail.js';
|
|
9
9
|
import { notify } from '../utilities/notify.js';
|
|
10
10
|
import { stripSensitiveUserColumns } from '../utilities/stripSensitiveUserColumns.js';
|
|
11
|
+
import { RESET_TOKEN_TTL_MS } from './resetPassword.js';
|
|
11
12
|
const analytics = getAnalytics();
|
|
12
13
|
const logger = getLogger();
|
|
13
14
|
/**
|
|
14
|
-
* Cached, real bcrypt hash used for the dummy compare on
|
|
15
|
+
* Cached, real bcrypt hash used for the dummy compare on every
|
|
16
|
+
* invalid-credentials path that would otherwise run no KDF (see
|
|
17
|
+
* {@link compareDummyHash}).
|
|
15
18
|
*
|
|
16
19
|
* Computed lazily (not at import time) via the password bond — which honors the
|
|
17
20
|
* configured cost (`SALT_ROUNDS`) — because the bond may not be wired when this
|
|
@@ -21,8 +24,44 @@ const logger = getLogger();
|
|
|
21
24
|
* defeating the timing-equalization defense and reintroducing a user-enumeration
|
|
22
25
|
* oracle. Deriving the dummy hash from the bond's `hash()` guarantees it is
|
|
23
26
|
* valid and matches the exact work factor of real password hashes.
|
|
27
|
+
*
|
|
28
|
+
* The PROMISE is what is cached, not the resolved value: an invalid-login burst
|
|
29
|
+
* is caller-controlled, and `hash()` is awaited for the whole cost of a KDF, so
|
|
30
|
+
* caching only after it resolves meant every concurrent refusal arriving while
|
|
31
|
+
* the cache was cold ran its OWN full-cost hash — N bcrypt runs where the cache
|
|
32
|
+
* exists to make it one in total. A FAILED hash is never left cached (a
|
|
33
|
+
* rejected promise would make every later refusal reuse it and skip the
|
|
34
|
+
* compare entirely, which is the timing oracle this whole path exists to close).
|
|
35
|
+
*/
|
|
36
|
+
let dummyHashPromise;
|
|
37
|
+
/**
|
|
38
|
+
* Runs one bcrypt compare against the cached dummy hash, for every request that
|
|
39
|
+
* supplies a password but reaches an invalid-credentials answer WITHOUT having
|
|
40
|
+
* run a real compare — the user-not-found path, the found path whose secrets
|
|
41
|
+
* row is missing, and the found path for an account that carries no password
|
|
42
|
+
* hash (an OAuth-only account). Without these, the ~100-300ms KDF runs for
|
|
43
|
+
* "wrong password" but not for the refusals next to it, so response timing on a
|
|
44
|
+
* known email discloses exactly which accounts exist and which are passwordless
|
|
45
|
+
* — an account-existence oracle aimed at the users with no password attack
|
|
46
|
+
* surface.
|
|
47
|
+
*
|
|
48
|
+
* @param password - The password the caller supplied.
|
|
24
49
|
*/
|
|
25
|
-
|
|
50
|
+
async function compareDummyHash(password) {
|
|
51
|
+
try {
|
|
52
|
+
dummyHashPromise ??= hash('molecule-dummy-password');
|
|
53
|
+
await compare(password, await dummyHashPromise);
|
|
54
|
+
}
|
|
55
|
+
catch (_error) {
|
|
56
|
+
// Best-effort timing equalizer only: a hashing/compare failure must not
|
|
57
|
+
// alter the response or surface an error — every caller answers the same
|
|
58
|
+
// generic invalid-credentials result below either way. Drop the cached
|
|
59
|
+
// promise too, so a hash that FAILED is retried by the next refusal
|
|
60
|
+
// instead of being reused (a rejected promise here would skip the compare
|
|
61
|
+
// for every later request, reopening the oracle this path closes).
|
|
62
|
+
dummyHashPromise = undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
26
65
|
/**
|
|
27
66
|
* Logs in a user by username or email. Supports password authentication, password reset token
|
|
28
67
|
* authentication (1-hour expiry), and optional two-factor verification via `@molecule/api-two-factor`.
|
|
@@ -65,17 +104,8 @@ export const logIn = ({ name: _name, tableName, schema: _schema }) => {
|
|
|
65
104
|
// and "wrong password" (bcrypt cost) reveals whether an account exists.
|
|
66
105
|
// The dummy hash MUST be a real, correctly-costed hash so the KDF actually
|
|
67
106
|
// runs at the same work factor as the wrong-password path (see dummyHash docs).
|
|
68
|
-
if (body.password)
|
|
69
|
-
|
|
70
|
-
dummyHash ??= await hash('molecule-dummy-password');
|
|
71
|
-
await compare(body.password, dummyHash);
|
|
72
|
-
}
|
|
73
|
-
catch (_error) {
|
|
74
|
-
// Best-effort timing equalizer only: a hashing/compare failure must not
|
|
75
|
-
// alter the response or surface an error — the not-found path always
|
|
76
|
-
// returns the same generic invalid-credentials result below.
|
|
77
|
-
}
|
|
78
|
-
}
|
|
107
|
+
if (body.password)
|
|
108
|
+
await compareDummyHash(body.password);
|
|
79
109
|
analytics
|
|
80
110
|
.track({ name: 'user.login_failed', properties: { reason: 'invalid_credentials' } })
|
|
81
111
|
.catch(() => { });
|
|
@@ -90,6 +120,18 @@ export const logIn = ({ name: _name, tableName, schema: _schema }) => {
|
|
|
90
120
|
// Get the user's secrets.
|
|
91
121
|
const secrets = await findById(`${tableName}Secrets`, user.id);
|
|
92
122
|
if (!secrets) {
|
|
123
|
+
// A user row whose secrets row is missing (logInOAuth logs-and-continues
|
|
124
|
+
// when the secrets insert fails) reached this refusal WITHOUT running any
|
|
125
|
+
// KDF, while "no such user" and "wrong password" each ran one — response
|
|
126
|
+
// timing on a known email then separated "this account exists" from both,
|
|
127
|
+
// the same oracle the other invalid-credentials paths fill (see
|
|
128
|
+
// {@link compareDummyHash}). The failure is tracked like them too: a
|
|
129
|
+
// probe against this account was refused either way.
|
|
130
|
+
if (body.password)
|
|
131
|
+
await compareDummyHash(body.password);
|
|
132
|
+
analytics
|
|
133
|
+
.track({ name: 'user.login_failed', properties: { reason: 'invalid_credentials' } })
|
|
134
|
+
.catch(() => { });
|
|
93
135
|
return {
|
|
94
136
|
statusCode: 403,
|
|
95
137
|
body: {
|
|
@@ -99,37 +141,57 @@ export const logIn = ({ name: _name, tableName, schema: _schema }) => {
|
|
|
99
141
|
};
|
|
100
142
|
}
|
|
101
143
|
let authenticated = false;
|
|
144
|
+
// Whether a REAL bcrypt compare already ran for this request (a stored
|
|
145
|
+
// password hash existed and was compared against): the dummy compare in
|
|
146
|
+
// the refusal path below must fill only the paths where no KDF ran, not
|
|
147
|
+
// stack a second one on top of a real compare.
|
|
148
|
+
let ranPasswordCompare = false;
|
|
149
|
+
// Whether authentication came from the one-time reset token (as opposed
|
|
150
|
+
// to a reusable password): its consumption is DEFERRED until every
|
|
151
|
+
// challenge that can still bounce the request (the 2FA challenge below)
|
|
152
|
+
// has passed, and then happens atomically before any session is minted.
|
|
153
|
+
// Consuming it up front burned the link on the 206 two-factor challenge,
|
|
154
|
+
// so the documented retry (same body plus `twoFactorToken`) always found
|
|
155
|
+
// a NULL token and could never complete the login — every attempt spent
|
|
156
|
+
// a fresh reset link for nothing.
|
|
157
|
+
let authenticatedByResetToken = false;
|
|
102
158
|
// Try password authentication.
|
|
103
159
|
if (body.password && secrets.passwordHash) {
|
|
160
|
+
ranPasswordCompare = true;
|
|
104
161
|
authenticated = await compare(body.password, secrets.passwordHash);
|
|
105
162
|
}
|
|
106
163
|
// Try password reset token (constant-time comparison to prevent timing
|
|
107
164
|
// attacks). The stored value is sha256(token) — hash the incoming one.
|
|
165
|
+
// Read-only here: the TTL is checked, but the token is NOT consumed yet.
|
|
108
166
|
if (!authenticated && body.passwordResetToken && secrets.passwordResetToken) {
|
|
109
167
|
const a = Buffer.from(hashResetToken(body.passwordResetToken), 'utf-8');
|
|
110
168
|
const b = Buffer.from(secrets.passwordResetToken, 'utf-8');
|
|
111
169
|
if (a.length === b.length && crypto.timingSafeEqual(a, b)) {
|
|
112
|
-
// Check if the token is still valid (within 1 hour).
|
|
170
|
+
// Check if the token is still valid (within 1 hour). The SAME gate
|
|
171
|
+
// as resetPassword — the same constant and the same boundary
|
|
172
|
+
// (`tokenAge` equal to the TTL is still valid, exactly as the reset
|
|
173
|
+
// confirmation treats it): an unparseable or future-dated timestamp
|
|
174
|
+
// is refused here too — this path mints an authenticated session,
|
|
175
|
+
// so it must not be looser than the reset path for the same
|
|
176
|
+
// one-hour credential.
|
|
113
177
|
if (secrets.passwordResetTokenAt) {
|
|
114
178
|
const tokenAge = Date.now() - new Date(secrets.passwordResetTokenAt).getTime();
|
|
115
|
-
if (tokenAge
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// requests holding the same token only the one that flips it to
|
|
119
|
-
// NULL matches a row — the other sees affected 0 and is refused.
|
|
120
|
-
// (A plain updateById cleared "whatever is there", so both
|
|
121
|
-
// requests passed and both minted sessions.)
|
|
122
|
-
const consumed = await updateMany(`${tableName}Secrets`, [
|
|
123
|
-
{ field: 'id', operator: '=', value: user.id },
|
|
124
|
-
{ field: 'passwordResetToken', operator: '=', value: secrets.passwordResetToken },
|
|
125
|
-
], { passwordResetToken: null, passwordResetTokenAt: null });
|
|
126
|
-
if (consumed.affected === 1)
|
|
127
|
-
authenticated = true;
|
|
179
|
+
if (Number.isFinite(tokenAge) && tokenAge >= 0 && tokenAge <= RESET_TOKEN_TTL_MS) {
|
|
180
|
+
authenticated = true;
|
|
181
|
+
authenticatedByResetToken = true;
|
|
128
182
|
}
|
|
129
183
|
}
|
|
130
184
|
}
|
|
131
185
|
}
|
|
132
186
|
if (!authenticated) {
|
|
187
|
+
// An account that exists but carries no password hash (an OAuth-only
|
|
188
|
+
// account) reached this refusal WITHOUT running any KDF, while "wrong
|
|
189
|
+
// password" and "no such user" each ran one — so response timing on a
|
|
190
|
+
// known email disclosed exactly which accounts are passwordless. Fill
|
|
191
|
+
// every no-KDF invalid-credentials path with the dummy compare (see
|
|
192
|
+
// {@link compareDummyHash}); never stack it on a real compare.
|
|
193
|
+
if (body.password && !ranPasswordCompare)
|
|
194
|
+
await compareDummyHash(body.password);
|
|
133
195
|
analytics
|
|
134
196
|
.track({ name: 'user.login_failed', properties: { reason: 'invalid_credentials' } })
|
|
135
197
|
.catch(() => { });
|
|
@@ -142,7 +204,26 @@ export const logIn = ({ name: _name, tableName, schema: _schema }) => {
|
|
|
142
204
|
};
|
|
143
205
|
}
|
|
144
206
|
// Check for two-factor authentication.
|
|
145
|
-
|
|
207
|
+
//
|
|
208
|
+
// Gated on `twoFactorEnabled` ALONE, then refused when the secret is
|
|
209
|
+
// missing — never `&& secrets.twoFactorSecret`. That `&&` made the state
|
|
210
|
+
// "2FA enabled, no secret stored" (a disable whose secret clear landed
|
|
211
|
+
// before its `twoFactorEnabled: false` write, a half-applied enable, or a
|
|
212
|
+
// repaired row) log the caller in on the password alone, silently
|
|
213
|
+
// dropping the very challenge the account turned on. `logInOAuth` refuses
|
|
214
|
+
// the same state outright ("an OAuth login must NOT issue a session on
|
|
215
|
+
// its own"), so this path must not be the looser way in for it.
|
|
216
|
+
if (user.twoFactorEnabled) {
|
|
217
|
+
if (!secrets.twoFactorSecret) {
|
|
218
|
+
logger.error('2FA is enabled for this account but no secret is stored; refusing the login rather than dropping the challenge', { userId: user.id });
|
|
219
|
+
return {
|
|
220
|
+
statusCode: 500,
|
|
221
|
+
body: {
|
|
222
|
+
error: t('user.error.twoFactorVerificationUnavailable'),
|
|
223
|
+
errorKey: 'user.error.twoFactorVerificationUnavailable',
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
146
227
|
if (!body.twoFactorToken) {
|
|
147
228
|
analytics.track({ name: 'user.two_factor_required', userId: user.id }).catch(() => { });
|
|
148
229
|
return { statusCode: 206, body: { twoFactorRequired: true } };
|
|
@@ -215,6 +296,34 @@ export const logIn = ({ name: _name, tableName, schema: _schema }) => {
|
|
|
215
296
|
};
|
|
216
297
|
}
|
|
217
298
|
}
|
|
299
|
+
// Consume the one-time reset token ATOMICALLY, now that every challenge
|
|
300
|
+
// that can still refuse the request has passed: the clear is
|
|
301
|
+
// WHERE-guarded on the very token value that was verified above, so of
|
|
302
|
+
// two concurrent requests holding the same token only the one that
|
|
303
|
+
// flips it to NULL matches a row — the other sees affected 0 and is
|
|
304
|
+
// refused instead of minting a session. (A plain updateById cleared
|
|
305
|
+
// "whatever is there", so both requests passed and both minted
|
|
306
|
+
// sessions.) This must stay AFTER the 2FA gate — a 206 challenge must
|
|
307
|
+
// not burn the link, the retry needs it — and BEFORE any session is
|
|
308
|
+
// minted below.
|
|
309
|
+
if (authenticatedByResetToken) {
|
|
310
|
+
const consumed = await updateMany(`${tableName}Secrets`, [
|
|
311
|
+
{ field: 'id', operator: '=', value: user.id },
|
|
312
|
+
{ field: 'passwordResetToken', operator: '=', value: secrets.passwordResetToken },
|
|
313
|
+
], { passwordResetToken: null, passwordResetTokenAt: null });
|
|
314
|
+
if (consumed.affected !== 1) {
|
|
315
|
+
analytics
|
|
316
|
+
.track({ name: 'user.login_failed', properties: { reason: 'invalid_credentials' } })
|
|
317
|
+
.catch(() => { });
|
|
318
|
+
return {
|
|
319
|
+
statusCode: 403,
|
|
320
|
+
body: {
|
|
321
|
+
error: t('user.error.invalidCredentials'),
|
|
322
|
+
errorKey: 'user.error.invalidCredentials',
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
}
|
|
218
327
|
// Create or update device.
|
|
219
328
|
const deviceId = await get('device')?.createOrUpdate(user.id, body.deviceName || 'Unknown');
|
|
220
329
|
if (!deviceId) {
|
|
@@ -243,7 +352,11 @@ export const logIn = ({ name: _name, tableName, schema: _schema }) => {
|
|
|
243
352
|
.track({
|
|
244
353
|
name: 'user.login',
|
|
245
354
|
userId: user.id,
|
|
246
|
-
|
|
355
|
+
// What ACTUALLY authenticated the request: a body carrying both a
|
|
356
|
+
// valid password and a reset token authenticated by the password and
|
|
357
|
+
// did NOT spend the link — calling that a reset_token login makes
|
|
358
|
+
// the event lie about which credential is still live.
|
|
359
|
+
properties: { method: authenticatedByResetToken ? 'reset_token' : 'password' },
|
|
247
360
|
})
|
|
248
361
|
.catch(() => { });
|
|
249
362
|
// `user` is a `SELECT *` row — strip secret columns (email-confirmation /
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logIn.js","sourceRoot":"","sources":["../../src/handlers/logIn.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAEhC,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACjE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAuB,MAAM,wBAAwB,CAAA;AAC3F,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAGtD,OAAO,KAAK,aAAa,MAAM,qBAAqB,CAAA;AAEpD,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAC/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAA;
|
|
1
|
+
{"version":3,"file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;YACnB,OAAO;gBACL,UAAU,EAAE,GAAG;gBACf,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,wBAAwB,CAAC,EAAE,QAAQ,EAAE,wBAAwB,EAAE;aACjF,CAAA;QACH,CAAC;IACH,CAAC,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { MoleculeRequest } from '@molecule/api-resource';
|
|
2
2
|
import type * as types from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* How long a password-reset token stays valid, in milliseconds. Shared with
|
|
5
|
+
* `logIn`, which accepts the same token as a login credential and must apply
|
|
6
|
+
* the SAME gate — the same constant AND the same boundary (`tokenAge` equal to
|
|
7
|
+
* the TTL is still valid in both), or one path becomes the looser way in for
|
|
8
|
+
* the other's one-hour credential.
|
|
9
|
+
*/
|
|
10
|
+
export declare const RESET_TOKEN_TTL_MS: number;
|
|
3
11
|
/** Request body for confirming a password reset using a token. */
|
|
4
12
|
export interface ResetPasswordRequest extends MoleculeRequest {
|
|
5
13
|
body: {
|
|
@@ -18,6 +26,12 @@ export interface ResetPasswordRequest extends MoleculeRequest {
|
|
|
18
26
|
* On success, all existing device sessions are invalidated to force a fresh login,
|
|
19
27
|
* mirroring the behavior of `updatePassword`.
|
|
20
28
|
*
|
|
29
|
+
* The new password and the token clear are written as ONE WHERE-guarded update
|
|
30
|
+
* matching the verified token hash, so the link is consumed atomically: of two
|
|
31
|
+
* concurrent requests holding the same link, only the one that flips the token
|
|
32
|
+
* to NULL matches a row — the other is refused with 401 and cannot overwrite
|
|
33
|
+
* the winner's password.
|
|
34
|
+
*
|
|
21
35
|
* @param resource - The user resource configuration (name, tableName, schema).
|
|
22
36
|
* @param resource.tableName - The database table name for users.
|
|
23
37
|
* @returns A request handler that responds with `{ statusCode: 200, body: { success: true } }` on success.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resetPassword.d.ts","sourceRoot":"","sources":["../../src/handlers/resetPassword.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAG7D,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"resetPassword.d.ts","sourceRoot":"","sources":["../../src/handlers/resetPassword.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAG7D,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;AAMzC;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,QAAiB,CAAA;AAEhD,kEAAkE;AAClE,MAAM,WAAW,oBAAqB,SAAQ,eAAe;IAC3D,IAAI,EAAE;QACJ,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,QAAQ,CAAC,EAAE,MAAM,CAAA;KAClB,CAAA;CACF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,aAAa,GAAI,6CAA6C,KAAK,CAAC,QAAQ,MACzE,KAAK,oBAAoB;;;;;;;;;;;;;;EA6JxC,CAAA"}
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import crypto from 'node:crypto';
|
|
2
2
|
import { get, getAnalytics, getLogger } from '@molecule/api-bond';
|
|
3
|
-
import { findOne,
|
|
3
|
+
import { findOne, updateMany } from '@molecule/api-database';
|
|
4
4
|
import { t } from '@molecule/api-i18n';
|
|
5
5
|
import { hash } from '@molecule/api-password';
|
|
6
6
|
import { invalidateAllDeviceExistsCache } from '../authorization.js';
|
|
7
7
|
import { hashResetToken } from '../utilities/hashResetToken.js';
|
|
8
8
|
const analytics = getAnalytics();
|
|
9
9
|
const logger = getLogger();
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* How long a password-reset token stays valid, in milliseconds. Shared with
|
|
12
|
+
* `logIn`, which accepts the same token as a login credential and must apply
|
|
13
|
+
* the SAME gate — the same constant AND the same boundary (`tokenAge` equal to
|
|
14
|
+
* the TTL is still valid in both), or one path becomes the looser way in for
|
|
15
|
+
* the other's one-hour credential.
|
|
16
|
+
*/
|
|
17
|
+
export const RESET_TOKEN_TTL_MS = 1000 * 60 * 60;
|
|
11
18
|
/**
|
|
12
19
|
* Confirms a password reset by validating the one-time token previously generated by
|
|
13
20
|
* `forgotPassword`, then hashing and storing the new password and clearing the token.
|
|
@@ -19,6 +26,12 @@ const RESET_TOKEN_TTL_MS = 1000 * 60 * 60;
|
|
|
19
26
|
* On success, all existing device sessions are invalidated to force a fresh login,
|
|
20
27
|
* mirroring the behavior of `updatePassword`.
|
|
21
28
|
*
|
|
29
|
+
* The new password and the token clear are written as ONE WHERE-guarded update
|
|
30
|
+
* matching the verified token hash, so the link is consumed atomically: of two
|
|
31
|
+
* concurrent requests holding the same link, only the one that flips the token
|
|
32
|
+
* to NULL matches a row — the other is refused with 401 and cannot overwrite
|
|
33
|
+
* the winner's password.
|
|
34
|
+
*
|
|
22
35
|
* @param resource - The user resource configuration (name, tableName, schema).
|
|
23
36
|
* @param resource.tableName - The database table name for users.
|
|
24
37
|
* @returns A request handler that responds with `{ statusCode: 200, body: { success: true } }` on success.
|
|
@@ -115,11 +128,31 @@ export const resetPassword = ({ name: _name, tableName, schema: _schema }) => {
|
|
|
115
128
|
};
|
|
116
129
|
}
|
|
117
130
|
const passwordHash = await hash(password);
|
|
118
|
-
|
|
131
|
+
// Consume the link ATOMICALLY: the password write and the token clear
|
|
132
|
+
// are one WHERE-guarded update matching the very token hash that was
|
|
133
|
+
// just verified, so of two concurrent requests holding the same link
|
|
134
|
+
// only the one that flips the token to NULL matches a row — the loser's
|
|
135
|
+
// write hits nothing and is refused below, instead of silently
|
|
136
|
+
// overwriting the winner's password with its own (a plain updateById
|
|
137
|
+
// let both requests "succeed": last write won and both got a 200).
|
|
138
|
+
const consumed = await updateMany(`${tableName}Secrets`, [
|
|
139
|
+
{ field: 'id', operator: '=', value: userId },
|
|
140
|
+
{ field: 'passwordResetToken', operator: '=', value: tokenHash },
|
|
141
|
+
], {
|
|
119
142
|
passwordHash,
|
|
120
143
|
passwordResetToken: null,
|
|
121
144
|
passwordResetTokenAt: null,
|
|
122
145
|
});
|
|
146
|
+
if (consumed.affected !== 1) {
|
|
147
|
+
// Another request spent this link between our read and this write.
|
|
148
|
+
return {
|
|
149
|
+
statusCode: 401,
|
|
150
|
+
body: {
|
|
151
|
+
error: t('user.error.invalidToken'),
|
|
152
|
+
errorKey: 'user.error.invalidToken',
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
123
156
|
try {
|
|
124
157
|
await get('device')?.deleteByUserId(userId);
|
|
125
158
|
// Evict this process's positive device-exists cache so no just-deleted
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resetPassword.js","sourceRoot":"","sources":["../../src/handlers/resetPassword.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAEhC,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACjE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AAC5D,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAG7C,OAAO,EAAE,8BAA8B,EAAE,MAAM,qBAAqB,CAAA;AAEpE,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAE/D,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;AAChC,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;AAE1B,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"resetPassword.js","sourceRoot":"","sources":["../../src/handlers/resetPassword.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAEhC,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,EAAE,M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