@jskit-ai/auth-provider-local-core 0.2.27 → 0.2.29

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
+ - A dev sign-in (`devLoginAs`), a password sign-in, a recovery link's sign-in and a display-name change hold one database connection at a time. Each read or synchronized the application's profile inside local auth's transaction, and the profile projector (users-core's) reads and writes on a connection of its own, so one request held two pool connections, and as many such requests at once as the pool has connections each held one and waited for ever for another. The profile is now read or synchronized after local auth's transaction, as registration, OAuth sign-in and email confirmation already did; a dev sign-in finds its user in one transaction, reads the profile, and only then creates its session in another, so a user without a profile still gets none. Should synchronizing the profile fail after a sign-in's transaction committed, the sign-in still fails and its session is never handed out (it expires), as for a registration; a recovery link is then used up, and a changed display name stays changed until the next request's profile check synchronizes it.
5
6
  - `AUTH_COOKIE_SAME_SITE=strict` makes every local auth cookie SameSite=Strict, and its clearing: the access, refresh, recovery and OAuth attempt cookies. Unset, blank or `lax` keeps them Lax, as before; any other value stops startup, naming the setting. OAuth sign-in works under `strict`: the provider's cross-site return only loads the login page, and the attempt cookie is read by that page's same-origin `POST /api/oauth/complete`. `resolveLocalAuthConfig()` returns `cookieSameSite`.
6
7
 
7
8
  - **Second factor: authenticator-app codes (TOTP, RFC 6238) with recovery codes**, one per account, in the file backend and through the backend contract. Setting up shows the secret once (as text and in an `otpauth://` address for a QR code, whose issuer is `APP_PUBLIC_URL`'s host) and a first code confirms it, answering ten single-use recovery codes once. A code is six digits of a 30-second step, accepted within one step either side of the server's clock and only once: no code of an accepted step or an earlier one passes again. The secret is sealed with AES-256-GCM, bound to its account, under a key derived from `AUTH_LOCAL_SECOND_FACTOR_KEY`; recovery codes are kept as keyed hashes. An accepted code or recovery code marks the request's session with the time (`secondFactorAt`, which `authenticate` returns for the auth policy); removing the authenticator clears it from every session of the account. Wrong codes make the account wait as wrong passwords do (five, then 30 seconds doubling to an hour), refused with 429 `auth.local.secondFactor.locked`. New refusals, each with its message key under `jskit.auth.local.secondFactor`: `notSetUp`, `alreadySetUp`, `setupNotStarted`, `proofRequired`, `codeInvalid`, `codeUsed`, `recoveryCodeInvalid` and `locked`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-local-core",
3
- "version": "0.2.27",
3
+ "version": "0.2.29",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -69,7 +69,7 @@
69
69
  }
70
70
  },
71
71
  "peerDependencies": {
72
- "@jskit-ai/auth-core": "0.2.23",
73
- "@jskit-ai/kernel": "0.2.22"
72
+ "@jskit-ai/auth-core": "0.2.24",
73
+ "@jskit-ai/kernel": "0.2.23"
74
74
  }
75
75
  }
@@ -240,8 +240,7 @@ function createLocalAccounts({
240
240
  if (failures) {
241
241
  await tx.signInFailures.clear(email);
242
242
  }
243
- const session = await sessions.createSessionForUser(tx, user);
244
- return { payload: await buildAuthPayload({ user, session, profileProjector }) };
243
+ return { user, session: await sessions.createSessionForUser(tx, user) };
245
244
  });
246
245
  // Refused after the transaction, so the failure it records is kept.
247
246
  if (outcome.waitMs) {
@@ -253,7 +252,9 @@ function createLocalAccounts({
253
252
  if (outcome.unconfirmed) {
254
253
  throw createAuthError(AUTH_TEXTS.emailUnconfirmed);
255
254
  }
256
- return outcome.payload;
255
+ // After the transaction, as register does: the profile projector works in transactions of its own, so a sign-in
256
+ // that synchronized the profile inside this one held two database connections at once.
257
+ return buildAuthPayload({ user: outcome.user, session: outcome.session, profileProjector });
257
258
  }
258
259
 
259
260
  function devAuthBootstrapEnabled() {
@@ -271,23 +272,24 @@ function createLocalAccounts({
271
272
  });
272
273
  }
273
274
 
274
- return backend.withTransaction(async (tx) => {
275
- let user = userId ? await tx.users.findById(userId) : null;
276
- if (!user && email) {
277
- user = await tx.users.findByEmail(email);
278
- }
279
- if (!user || user.disabled) {
280
- throw devLoginAsUserNotFound({ email, userId });
281
- }
282
- const appProfile = await findExistingAppProfile(user, profileProjector);
283
- if (appProfileProjection && !appProfile) {
284
- throw devLoginAsUserNotFound({ email, userId });
285
- }
286
- const session = await sessions.createSessionForUser(tx, user, {
287
- purpose: DEV_AUTH_SESSION_PURPOSE
288
- });
289
- return buildAuthResult({ user, session, appProfile });
275
+ // The user is found, its app profile read and its session created one after the other, never the profile inside a
276
+ // transaction: the profile projector reads on a connection of its own, so a sign-in that read it inside one held
277
+ // two database connections at once, and as many sign-ins at once as the pool has connections waited for ever.
278
+ const user = await backend.withTransaction(async (tx) => {
279
+ const userById = userId ? await tx.users.findById(userId) : null;
280
+ return userById || (email ? tx.users.findByEmail(email) : null);
290
281
  });
282
+ if (!user || user.disabled) {
283
+ throw devLoginAsUserNotFound({ email, userId });
284
+ }
285
+ const appProfile = await findExistingAppProfile(user, profileProjector);
286
+ if (appProfileProjection && !appProfile) {
287
+ throw devLoginAsUserNotFound({ email, userId });
288
+ }
289
+ const session = await backend.withTransaction((tx) => sessions.createSessionForUser(tx, user, {
290
+ purpose: DEV_AUTH_SESSION_PURPOSE
291
+ }));
292
+ return buildAuthResult({ user, session, appProfile });
291
293
  }
292
294
 
293
295
  async function updateDisplayName(request, input) {
@@ -299,13 +301,12 @@ function createLocalAccounts({
299
301
  if (!displayName) {
300
302
  throw createAuthError(AUTH_TEXTS.displayNameRequired);
301
303
  }
302
- const changed = await backend.withTransaction(async (tx) => {
303
- const user = await tx.users.updateProfile(authResult.actor.providerUserId, { displayName });
304
- if (!user) {
305
- throw createAuthError(AUTH_TEXTS.sessionRequired);
306
- }
307
- return buildProfileChangePayload({ user, profileProjector });
308
- });
304
+ const user = await backend.withTransaction((tx) => tx.users.updateProfile(authResult.actor.providerUserId, { displayName }));
305
+ if (!user) {
306
+ throw createAuthError(AUTH_TEXTS.sessionRequired);
307
+ }
308
+ // After the transaction, so the request holds one database connection at a time (see login).
309
+ const changed = await buildProfileChangePayload({ user, profileProjector });
309
310
  sessions.updateRequestActor(request, changed.actor);
310
311
  return changed;
311
312
  }
@@ -98,7 +98,7 @@ function createLocalPasswordFlows({ backend, config, passwords, profileProjector
98
98
  throw createAuthError(AUTH_TEXTS.recoveryTokenRequired);
99
99
  }
100
100
  const tokenInvalid = () => new AppError(401, "Recovery token is invalid or expired.", { code: "auth.local.recovery.tokenInvalid" });
101
- return backend.withTransaction(async (tx) => {
101
+ const { user, session } = await backend.withTransaction(async (tx) => {
102
102
  const recovery = await tx.recovery.findByTokenHash(sha256Base64url(token));
103
103
  if (!recovery || recovery.usedAt || isExpiredIso(recovery.expiresAt)) {
104
104
  throw tokenInvalid();
@@ -111,12 +111,17 @@ function createLocalPasswordFlows({ backend, config, passwords, profileProjector
111
111
  if (!user || user.disabled) {
112
112
  throw tokenInvalid();
113
113
  }
114
- const session = await sessions.createSessionForUser(tx, user, {
115
- purpose: "recovery",
116
- ttlSeconds: RECOVERY_SESSION_TTL_SECONDS
117
- });
118
- return buildAuthPayload({ user, session, profileProjector });
114
+ return {
115
+ user,
116
+ session: await sessions.createSessionForUser(tx, user, {
117
+ purpose: "recovery",
118
+ ttlSeconds: RECOVERY_SESSION_TTL_SECONDS
119
+ })
120
+ };
119
121
  });
122
+ // After the transaction, so the request holds one database connection at a time: the profile projector works in
123
+ // transactions of its own.
124
+ return buildAuthPayload({ user, session, profileProjector });
120
125
  }
121
126
 
122
127
  async function resetPassword(input, request) {