@jskit-ai/auth-provider-local-core 0.2.17 → 0.2.19

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
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - Completing a password reset consumes the user's other password resets (`rec_…` tokens) only, leaving an app's own kinds of recovery token, such as sign-in links, open: the backend contract's `recovery.consumeForUser(userId, { idPrefix })` takes the kind it consumes, and the file backend requires it as the database backend does.
5
6
  - Security: confirming a registration takes the password it was registered with as well as its link's code, `confirmRegistration({ token, password })`. A missing or wrong password creates nothing, keeps the registration and its link, and is refused with 400 `auth.local.register.confirmationPasswordInvalid` (on the `password` field; new message key `jskit.auth.local.register.confirmationPasswordInvalid`). Registering an email again replaces its waiting registration whole, password, display name and invitation included, and a new link from `resendRegisterConfirmation` keeps the registration's password and is no longer sent for an expired registration. So whoever registers an email, before its owner or after, the account its link creates has the password of the person who confirmed it: before, a registration that waited kept its password when the email's owner registered later, so whoever registered first could sign in to the account the owner then confirmed, and the owner's link created no account they could tell apart.
6
7
  - The file backend refuses a second account with a taken id or email with a `ConflictError` (409) coded `auth.local.accounts.exists` ("An account with this id or email already exists."), where it threw a plain `Error` with no code, so a caller that retries on a duplicate can recognise it.
7
8
  - The local file backend takes over a store lock left by a crashed process (older than 30 s, its process gone) at its first attempt, instead of after waiting out the 2 s lock timeout. A lock whose process is alive is still never removed, however old.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-local-core",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -67,7 +67,7 @@
67
67
  }
68
68
  },
69
69
  "peerDependencies": {
70
- "@jskit-ai/auth-core": "0.2.14",
71
- "@jskit-ai/kernel": "0.2.14"
70
+ "@jskit-ai/auth-core": "0.2.16",
71
+ "@jskit-ai/kernel": "0.2.16"
72
72
  }
73
73
  }
@@ -311,10 +311,13 @@ function createTx(state, touched) {
311
311
  mark("recovery");
312
312
  return clone(record);
313
313
  },
314
- async consumeForUser(userId) {
314
+ async consumeForUser(userId, { idPrefix } = {}) {
315
+ if (typeof idPrefix !== "string" || !idPrefix) {
316
+ throw new TypeError("recovery.consumeForUser() takes the idPrefix of the tokens it consumes, such as \"rec_\".");
317
+ }
315
318
  let count = 0;
316
319
  for (const record of state.recovery) {
317
- if (record.userId === userId && !record.usedAt) {
320
+ if (record.userId === userId && !record.usedAt && record.id.startsWith(idPrefix)) {
318
321
  record.usedAt = nowIso();
319
322
  count += 1;
320
323
  }
@@ -6,6 +6,9 @@ import { createId, isExpiredIso, isoFromNow, randomToken, sha256Base64url, verif
6
6
 
7
7
  const RECOVERY_SESSION_TTL_SECONDS = 15 * 60;
8
8
  const RECOVERY_TOKEN_TTL_SECONDS = 60 * 60;
9
+ // A password reset's recovery tokens have ids starting `rec_`, which tells them from an app's own kinds (such as a
10
+ // sign-in link's): completing a reset consumes the user's other resets and leaves the other kinds open.
11
+ const RESET_TOKEN_KIND = "rec";
9
12
 
10
13
  /**
11
14
  * The `auth.local.recovery-sender` default when no feature provides one: outside
@@ -49,7 +52,7 @@ function createLocalPasswordFlows({ backend, config, passwords, profileProjector
49
52
  const token = randomToken(32);
50
53
  recoveryUrl = `${config.appPublicUrl.replace(/\/$/, "")}/auth/reset-password?token=${encodeURIComponent(token)}&type=recovery`;
51
54
  await tx.recovery.create({
52
- id: createId("rec"),
55
+ id: createId(RESET_TOKEN_KIND),
53
56
  userId: user.id,
54
57
  tokenHash: sha256Base64url(token),
55
58
  expiresAt: isoFromNow(RECOVERY_TOKEN_TTL_SECONDS)
@@ -115,7 +118,7 @@ function createLocalPasswordFlows({ backend, config, passwords, profileProjector
115
118
  }
116
119
  const user = await tx.users.updatePassword(String(payload.sub), password);
117
120
  await tx.sessions.revokeForUser(String(payload.sub));
118
- await tx.recovery.consumeForUser(String(payload.sub));
121
+ await tx.recovery.consumeForUser(String(payload.sub), { idPrefix: `${RESET_TOKEN_KIND}_` });
119
122
  // Recovery is the way back in for an account that failed sign-ins keep waiting.
120
123
  if (user) {
121
124
  await tx.signInFailures.clear(user.email);
@@ -3,7 +3,10 @@
3
3
 
4
4
  /**
5
5
  * One transaction of a local auth backend. A find answers null when nothing
6
- * matches; a bulk change answers how many records it changed.
6
+ * matches; a bulk change answers how many records it changed. A recovery
7
+ * token's kind is the start of its id (a password reset's is `rec_`), and
8
+ * `consumeForUser` consumes the user's unused tokens of the kind its idPrefix
9
+ * names, never their others.
7
10
  * @typedef {object} AuthLocalBackendTransaction
8
11
  * @property {{
9
12
  * create(input: object): Promise<object>,
@@ -24,7 +27,7 @@
24
27
  * create(input: object): Promise<object>,
25
28
  * findByTokenHash(tokenHash: string): Promise<object | null>,
26
29
  * consume(tokenId: string): Promise<object | null>, the consumed token, or null when it is missing or already used
27
- * consumeForUser(userId: string): Promise<number>
30
+ * consumeForUser(userId: string, options: { idPrefix: string }): Promise<number>
28
31
  * }} recovery
29
32
  * @property {{
30
33
  * create(input: object): Promise<object>,
@@ -987,13 +987,18 @@ test("local auth provider completes recovery through a recovery-scoped session",
987
987
  assert.equal(login.actor.email, "recovery@example.com");
988
988
  });
989
989
 
990
- test("local auth reset invalidates other outstanding recovery tokens for the user", async () => {
991
- const { authService } = await createStartedApp();
990
+ test("local auth reset invalidates the user's other password resets and leaves their other kinds of token open", async () => {
991
+ const { authService, backend } = await createStartedApp();
992
992
  await authService.register({
993
993
  email: "recovery-tokens@example.com",
994
994
  password: "correct horse battery staple",
995
995
  displayName: "Recovery Tokens"
996
996
  });
997
+ // An application's own kind of recovery token, such as a sign-in link, told apart by its id's prefix.
998
+ await backend.withTransaction(async (tx) => {
999
+ const user = await tx.users.findByEmail("recovery-tokens@example.com");
1000
+ await tx.recovery.create({ id: "magic_link", userId: user.id, tokenHash: "hash-magic-link", expiresAt: new Date(Date.now() + 3_600_000).toISOString() });
1001
+ });
997
1002
 
998
1003
  const firstResetRequest = await authService.requestPasswordReset({
999
1004
  email: "recovery-tokens@example.com"
@@ -1027,6 +1032,8 @@ test("local auth reset invalidates other outstanding recovery tokens for the use
1027
1032
  }),
1028
1033
  /Recovery token is invalid or expired/
1029
1034
  );
1035
+ const link = await backend.withTransaction((tx) => tx.recovery.findByTokenHash("hash-magic-link"));
1036
+ assert.equal(link.usedAt, "", "the reset leaves the sign-in link open");
1030
1037
  });
1031
1038
 
1032
1039
  test("local auth provider changes password through the account-security contract", async () => {