@jskit-ai/auth-provider-local-db-core 0.1.176 → 0.1.178

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
+ - The package manifest no longer requires `runtime.actions`: its feature declares no actions, so since the kernel's 9646ecf21 it does not require the action catalogue, and the catalog's manifest check reported the requirement as one no server provider has.
5
6
  - Second factors in the database: the table `auth_local_second_factors` (one row per account, deleted with it) and the sessions' `second_factor_at` column, created by the new migration `auth_local_db_users_second_factors`, behind the backend contract's `secondFactors` collection and `sessions.markSecondFactorProved` / `sessions.clearSecondFactorForUser`. `LOCAL_AUTH_DB_TABLES.secondFactors` names the table, which the package owns.
6
7
  - `auth.local.recovery.consumeForUser(userId, { ...options, idPrefix })` consumes only the user's open tokens whose id starts with `idPrefix`, such as `"rec_"` for a password reset's, and requires it (a `TypeError` without one). It consumed every open token of the user, so an app's new sign-in link (its own `magic_…` tokens) cancelled an outstanding password reset, and a reset its links. The prefix is compared exactly, never as a LIKE pattern, through the recovery table's new server-only `idPrefix` filter. The backend's `recovery.consumeForUser(userId, { idPrefix })` takes it too.
7
8
  - `auth.local.sessions.update(sessionId, changes, { ...options, onlyIf })` changes the session only while it still has the `onlyIf` values (`userId`, `purpose`, `revokedAt`; others are refused), compared in the one UPDATE, and answers null, writing nothing, when it is missing or no longer matches. An app promotes a sign-in link's session with `onlyIf: { userId, purpose: "recovery", revokedAt: null }` instead of updating it unconditionally and checking afterwards. Its tests run on SQLite, MariaDB/MySQL and PostgreSQL (`tokenKinds*.test.js`; the PostgreSQL one in CI's PostgreSQL job).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-local-db-core",
3
- "version": "0.1.176",
3
+ "version": "0.1.178",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -11,14 +11,14 @@
11
11
  "./server/types": "./src/server/types.js"
12
12
  },
13
13
  "dependencies": {
14
- "@jskit-ai/auth-provider-local-core": "0.2.27",
15
- "@jskit-ai/resource-core": "0.1.213"
14
+ "@jskit-ai/auth-provider-local-core": "0.2.29",
15
+ "@jskit-ai/resource-core": "0.1.214"
16
16
  },
17
17
  "peerDependencies": {
18
- "@jskit-ai/auth-core": "0.2.23",
19
- "@jskit-ai/database-runtime": "0.1.271",
20
- "@jskit-ai/kernel": "0.2.22",
21
- "@jskit-ai/resource-runtime": "0.2.29"
18
+ "@jskit-ai/auth-core": "0.2.24",
19
+ "@jskit-ai/database-runtime": "0.1.272",
20
+ "@jskit-ai/kernel": "0.2.23",
21
+ "@jskit-ai/resource-runtime": "0.2.30"
22
22
  },
23
23
  "description": "Database-backed local auth storage backend for JSKIT local auth.",
24
24
  "jskit": {
@@ -31,7 +31,6 @@
31
31
  "auth.local.recovery"
32
32
  ],
33
33
  "requires": [
34
- "runtime.actions",
35
34
  "runtime.database",
36
35
  "runtime.resources"
37
36
  ]
@@ -30,8 +30,8 @@ function createReplyFixture() {
30
30
  }
31
31
 
32
32
  // The database backend on a migrated SQLite database, counting the transactions it opens.
33
- async function createDbBackendFixture(t) {
34
- const records = await openSqliteLocalAuth(t);
33
+ async function createDbBackendFixture(t, options) {
34
+ const records = await openSqliteLocalAuth(t, options);
35
35
  const transactions = { count: 0 };
36
36
  records.knex.on("query", ({ sql }) => {
37
37
  if (/^begin/iu.test(String(sql))) {
@@ -270,6 +270,55 @@ test("local database auth uses the shared native login-as session contract", asy
270
270
  assert.equal(authenticated.sessionPurpose, "dev-auth");
271
271
  });
272
272
 
273
+ // The application's profiles, read and synchronized on the database's own pool outside local auth's transactions, as
274
+ // users-core's profile projector does.
275
+ function poolProfileProjector(knex) {
276
+ async function profileOf(identity) {
277
+ await knex.raw("select 1");
278
+ return { ...identity, id: `profile-${identity.id}`, profileSource: "users" };
279
+ }
280
+ return { findByIdentity: profileOf, syncIdentityProfile: profileOf };
281
+ }
282
+
283
+ // Before, each of these read or synchronized the app profile inside its transaction, on a second connection, so on a
284
+ // pool of N connections N of them at once each held one and waited for ever for another; on a pool of one, a single
285
+ // sign-in did. A query waits 2 s for the connection, so such a sign-in fails with knex's timeout instead of hanging.
286
+ test("local sign-in reads the app profile outside its transactions, so sign-ins at once on a one-connection pool finish", async (t) => {
287
+ const { backend, knex } = await createDbBackendFixture(t, { acquireConnectionTimeout: 2000 });
288
+ assert.equal(knex.client.pool.max, 1, "SQLite's pool has one connection.");
289
+ const password = await hashPassword("stored password value");
290
+ await backend.withTransaction(async (tx) => {
291
+ for (const name of ["ada", "grace"]) {
292
+ await tx.users.create({ id: `usr_${name}`, email: `${name}@example.com`, displayName: name, password });
293
+ }
294
+ });
295
+ const authService = createService(backend, {
296
+ env: { AUTH_DEV_BYPASS_ENABLED: "true", AUTH_DEV_BYPASS_SECRET: DEV_AUTH_SECRET },
297
+ profileProjector: poolProfileProjector(knex)
298
+ });
299
+
300
+ const devSignIns = await Promise.all(["ada", "grace"].map((name) =>
301
+ authService.devLoginAs({ email: `${name}@example.com` }, createLocalRequest({ exchange: true }))));
302
+ assert.deepEqual(devSignIns.map((result) => [result.actor.appUserId, result.session.purpose]),
303
+ [["profile-usr_ada", "dev-auth"], ["profile-usr_grace", "dev-auth"]]);
304
+
305
+ const signIns = await Promise.all(["ada", "grace"].map((name) =>
306
+ authService.login({ email: `${name}@example.com`, password: "stored password value" })));
307
+ assert.deepEqual(signIns.map((result) => result.actor.appUserId), ["profile-usr_ada", "profile-usr_grace"]);
308
+
309
+ const reset = await authService.requestPasswordReset({ email: "ada@example.com" });
310
+ const recovered = await authService.completePasswordRecovery({
311
+ code: new URL(reset.recoveryUrl).searchParams.get("token"),
312
+ type: "recovery"
313
+ });
314
+ assert.equal(recovered.session.purpose, "recovery");
315
+
316
+ const reply = createReplyFixture();
317
+ authService.writeSessionCookies(reply, signIns[1].session);
318
+ const renamed = await authService.updateDisplayName({ cookies: reply.cookies }, { displayName: "Grace H." });
319
+ assert.equal(renamed.profile.displayName, "Grace H.");
320
+ });
321
+
273
322
  test("local auth DB backend works with custom password strategy", async (t) => {
274
323
  const { backend } = await createDbBackendFixture(t);
275
324
  const authService = createService(backend, {
@@ -90,9 +90,15 @@ async function startLocalAuthRecords(knex) {
90
90
  * A migrated in-memory SQLite database of the test's own, which enforces its foreign keys and is closed after the
91
91
  * test.
92
92
  * @param {import("node:test").TestContext} context
93
+ * @param {{ acquireConnectionTimeout?: number }} [options] how long a query waits for the pool's one connection
93
94
  */
94
- async function openMigratedSqlite(context) {
95
- const knex = createKnex({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
95
+ async function openMigratedSqlite(context, { acquireConnectionTimeout = 60_000 } = {}) {
96
+ const knex = createKnex({
97
+ client: "better-sqlite3",
98
+ connection: { filename: ":memory:" },
99
+ useNullAsDefault: true,
100
+ acquireConnectionTimeout
101
+ });
96
102
  context.after(() => knex.destroy());
97
103
  await knex.raw("PRAGMA foreign_keys = ON");
98
104
  for (const migration of LOCAL_AUTH_MIGRATIONS) {
@@ -104,9 +110,10 @@ async function openMigratedSqlite(context) {
104
110
  /**
105
111
  * Local auth's records in a migrated in-memory SQLite database of the test's own.
106
112
  * @param {import("node:test").TestContext} context
113
+ * @param {Parameters<typeof openMigratedSqlite>[1]} [options]
107
114
  */
108
- async function openSqliteLocalAuth(context) {
109
- const knex = await openMigratedSqlite(context);
115
+ async function openSqliteLocalAuth(context, options) {
116
+ const knex = await openMigratedSqlite(context, options);
110
117
  return Object.freeze({ knex, ...(await startLocalAuthRecords(knex)) });
111
118
  }
112
119