@jskit-ai/auth-provider-local-db-core 0.1.175 → 0.1.177
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.
|
|
3
|
+
"version": "0.1.177",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -11,11 +11,11 @@
|
|
|
11
11
|
"./server/types": "./src/server/types.js"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@jskit-ai/auth-provider-local-core": "0.2.
|
|
14
|
+
"@jskit-ai/auth-provider-local-core": "0.2.28",
|
|
15
15
|
"@jskit-ai/resource-core": "0.1.213"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@jskit-ai/auth-core": "0.2.
|
|
18
|
+
"@jskit-ai/auth-core": "0.2.23",
|
|
19
19
|
"@jskit-ai/database-runtime": "0.1.271",
|
|
20
20
|
"@jskit-ai/kernel": "0.2.22",
|
|
21
21
|
"@jskit-ai/resource-runtime": "0.2.29"
|
|
@@ -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({
|
|
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
|
|