@jskit-ai/auth-provider-local-db-core 0.1.205 → 0.1.207

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-local-db-core",
3
- "version": "0.1.205",
3
+ "version": "0.1.207",
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.46",
14
+ "@jskit-ai/auth-provider-local-core": "0.2.47",
15
15
  "@jskit-ai/resource-core": "0.1.226"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@jskit-ai/auth-core": "0.2.40",
19
- "@jskit-ai/database-runtime": "0.1.287",
19
+ "@jskit-ai/database-runtime": "0.1.289",
20
20
  "@jskit-ai/kernel": "0.2.35",
21
- "@jskit-ai/resource-runtime": "0.2.52"
21
+ "@jskit-ai/resource-runtime": "0.2.54"
22
22
  },
23
23
  "description": "Database-backed local auth storage backend for JSKIT local auth.",
24
24
  "jskit": {
@@ -641,7 +641,8 @@ function createRecords(resources, identities, options) {
641
641
 
642
642
  /**
643
643
  * The `auth.local.backend` that keeps local auth's records in the database: each withTransaction opens one
644
- * transaction on the records' handles, runs it again whole on a deadlock, and hands the callback the records in it.
644
+ * transaction on the records' handles, runs it again whole on a deadlock, and hands the callback the records in it;
645
+ * withRecords hands it the records outside a transaction, each call a statement of its own.
645
646
  * A failed callback rejects with the callback's own error (an AppError stays one); a failed commit rejects with
646
647
  * json-rest-api's RestApiWriteError. Every call runs in the feature's system context.
647
648
  * @param {object} dependencies
@@ -666,9 +667,15 @@ function createLocalDbBackend({ resources, identities, context }) {
666
667
  }
667
668
  }
668
669
 
670
+ // A request's reads, each on its own with no transaction around them.
671
+ function withRecords(callback) {
672
+ return callback(createRecords(resources, identities, { context }));
673
+ }
674
+
669
675
  return Object.freeze({
670
676
  tables: TABLES,
671
- withTransaction
677
+ withTransaction,
678
+ withRecords
672
679
  });
673
680
  }
674
681
 
@@ -37,7 +37,7 @@ const account = (id, email, extra = {}) => ({ id, email, displayName: `Name ${id
37
37
  test("accounts: create answers the account it wrote, reads find it by id and email, and an email or id is taken once", async (context) => {
38
38
  requireTestDatabase(process.env);
39
39
  const { backend } = await openLocalAuthDatabase(context, "da_local_accounts");
40
- assert.deepEqual(Object.keys(backend), ["tables", "withTransaction"]);
40
+ assert.deepEqual(Object.keys(backend), ["tables", "withTransaction", "withRecords"]);
41
41
  assert.deepEqual(backend.tables, {
42
42
  users: "auth_local_users",
43
43
  sessions: "auth_local_sessions",
@@ -0,0 +1,49 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { createLocalAuthService, hashPassword } from "@jskit-ai/auth-provider-local-core/server/lib/index";
4
+ import { localServiceOptions } from "../../auth-provider-local-core/test-support/localService.js";
5
+ import { openSqliteLocalAuth } from "./support/localAuthRuntime.js";
6
+
7
+ const PASSWORD = "a long enough password value";
8
+
9
+ // Every request reads its session and its user, so a revoked session or a disabled account is refused from the next
10
+ // request on. The two reads need no transaction around them, and the session is noted as seen at most every five
11
+ // minutes, so an authenticated request costs two statements however many come.
12
+ test("an authenticated request costs two statements, its session and its user, and a revoked session or disabled account is refused from the next", async (t) => {
13
+ t.mock.timers.enable({ apis: ["Date"], now: Date.now() });
14
+ const { accounts, backend, knex, sessions } = await openSqliteLocalAuth(t);
15
+ const service = createLocalAuthService({ ...localServiceOptions(), backend });
16
+ await accounts.create({ id: "usr_req", email: "req@example.com", displayName: "Req", password: await hashPassword(PASSWORD) });
17
+ const signIn = async () => (await service.login({ email: "req@example.com", password: PASSWORD }, null)).session;
18
+ const session = await signIn();
19
+ const statements = [];
20
+ const onQuery = (query) => { statements.push(query.sql); };
21
+ knex.on("query", onQuery);
22
+ t.after(() => knex.off("query", onQuery));
23
+ const requests = async (cookies, count) => {
24
+ statements.length = 0;
25
+ const answers = [];
26
+ for (let index = 0; index < count; index += 1) answers.push(await service.authenticateRequest({ cookies }));
27
+ return { authenticated: answers.map(({ authenticated }) => authenticated), statements: statements.length };
28
+ };
29
+ const byAccess = { jskit_local_access_token: session.access_token };
30
+ const byRefresh = { jskit_local_refresh_token: session.refresh_token };
31
+ for (const cookies of [byAccess, byRefresh]) {
32
+ assert.deepEqual(await requests(cookies, 1), { authenticated: [true], statements: 2 });
33
+ assert.deepEqual(await requests(cookies, 20), { authenticated: Array(20).fill(true), statements: 40 });
34
+ }
35
+ // Five minutes on, the first request notes the session as seen: one UPDATE, in a transaction of its own, once.
36
+ t.mock.timers.tick(5 * 60 * 1000 + 1000);
37
+ assert.deepEqual(await requests(byAccess, 2), { authenticated: [true, true], statements: 7 });
38
+ assert.deepEqual(statements.map((sql) => sql.split(" ")[0].toLowerCase().replace(/;$/u, "")),
39
+ ["select", "select", "begin", "update", "commit", "select", "select"]);
40
+
41
+ const { sessionId } = await service.authenticateRequest({ cookies: byAccess });
42
+ await sessions.revoke(sessionId);
43
+ assert.deepEqual((await requests(byAccess, 1)).authenticated, [false]);
44
+ assert.deepEqual((await requests(byRefresh, 1)).authenticated, [false]);
45
+ const again = await signIn();
46
+ assert.deepEqual((await requests({ jskit_local_access_token: again.access_token }, 1)).authenticated, [true]);
47
+ await accounts.disable("usr_req");
48
+ assert.deepEqual((await requests({ jskit_local_access_token: again.access_token }, 1)).authenticated, [false]);
49
+ });