@jskit-ai/auth-provider-oidc-core 0.2.20 → 0.2.21

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
+ - `./server/lib/index` exports `OIDC_CLIENT_INFRASTRUCTURE_TABLES` (`auth_oidc_clients`, `auth_oidc_sessions`, `auth_oidc_logout_tokens`), the protocol state the relying party keeps outside table resources, for an app test's `expectEveryTableOwned({ infrastructureTables })`.
5
6
  - An ordinary request synchronizes the app profile only when it is missing or no longer matches the identity its session keeps from sign-in (`onlyWhenChanged`); sign-in synchronizes in full. The session is still read on every request. Visible difference: a personal workspace, owner membership or settings row deleted while someone is signed in comes back at their next sign-in instead of their next request.
6
7
  - `confirmRegistration` answers `AUTH_OPERATION_UNSUPPORTED`: the hub confirms emails itself.
7
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-oidc-core",
3
- "version": "0.2.20",
3
+ "version": "0.2.21",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -20,8 +20,8 @@
20
20
  "openid-client": "6.8.8"
21
21
  },
22
22
  "devDependencies": {
23
- "@jskit-ai/auth-provider-local-core": "0.2.16",
24
- "@jskit-ai/auth-web": "0.2.18",
23
+ "@jskit-ai/auth-provider-local-core": "0.2.17",
24
+ "@jskit-ai/auth-web": "0.2.19",
25
25
  "oidc-provider": "9.12.2"
26
26
  },
27
27
  "description": "Sign in through any OpenID Connect issuer: self-registration or a static client, app-owned sessions, and back-channel logout.",
@@ -91,7 +91,7 @@
91
91
  }
92
92
  },
93
93
  "peerDependencies": {
94
- "@jskit-ai/auth-core": "0.2.13",
95
- "@jskit-ai/kernel": "0.2.13"
94
+ "@jskit-ai/auth-core": "0.2.14",
95
+ "@jskit-ai/kernel": "0.2.14"
96
96
  }
97
97
  }
@@ -2,7 +2,7 @@
2
2
  "private": true,
3
3
  "type": "module",
4
4
  "dependencies": {
5
- "@jskit-ai/auth-provider-oidc-core": "0.2.20",
6
- "@jskit-ai/auth-web": "0.2.18"
5
+ "@jskit-ai/auth-provider-oidc-core": "0.2.21",
6
+ "@jskit-ai/auth-web": "0.2.19"
7
7
  }
8
8
  }
@@ -3,4 +3,4 @@ export { resolveOidcConfig } from "./config.js";
3
3
  export { createIssuerClient, registrationMetadata } from "./issuer.js";
4
4
  export { adoptOidcAccounts, exportLocalAccounts } from "./localAccounts.js";
5
5
  export { createOidcAuthService } from "./service.js";
6
- export { createOidcStore } from "./store.js";
6
+ export { OIDC_CLIENT_INFRASTRUCTURE_TABLES, createOidcStore } from "./store.js";
@@ -1,3 +1,5 @@
1
+ // readable-check: allow no-direct-knex - infrastructure: an operator's one-off command, run with the app's knexfile and no running app, that exports local sign-in's accounts for an accounts hub and moves the adopted users rows to the hub's provider
2
+
1
3
  import { open, readFile } from "node:fs/promises";
2
4
  import { AUTH_OIDC_PROVIDER_ID } from "../../shared/index.js";
3
5
 
@@ -1,3 +1,5 @@
1
+ // readable-check: allow no-direct-knex - infrastructure: the OIDC relying party's protocol state (the clients it registered at each issuer, the sessions the issuer's tokens opened and the back-channel logout token ids it has seen) is kept with Knex, as the data-access spec keeps protocol state
2
+
1
3
  const TABLES = Object.freeze({
2
4
  clients: "auth_oidc_clients",
3
5
  sessions: "auth_oidc_sessions",
@@ -48,14 +50,22 @@ function isDuplicateError(error) {
48
50
  return code === "ER_DUP_ENTRY" || code === "23505" || code.startsWith("SQLITE_CONSTRAINT");
49
51
  }
50
52
 
53
+ /**
54
+ * @typedef {object} OidcKnownUsers The accounts the app already knows: users-core's `users.profiles`.
55
+ * @property {(identity: { provider: string, providerUserId: string }) => Promise<{ authProvider: string, authProviderUserSid: string, email: string, displayName: string } | null>} findByIdentity
56
+ * @property {(email: string) => Promise<{ authProvider: string, authProviderUserSid: string, email: string, displayName: string } | null>} findByEmail
57
+ */
58
+
51
59
  /**
52
60
  * Knex storage for registered clients (one per issuer and app origin), app
53
61
  * sessions, and seen back-channel logout token ids.
54
62
  * @param {object} dependencies
55
- * @param {import("@jskit-ai/database-runtime/server/types").RuntimeKnex} dependencies.knex
63
+ * @param {import("@jskit-ai/database-runtime/server/types").RuntimeDatabase} dependencies.database
64
+ * @param {OidcKnownUsers | null} dependencies.profiles users-core's `users.profiles`, or null when the app keeps no users.
56
65
  * @param {() => number} [dependencies.now]
57
66
  */
58
- function createOidcStore({ knex, now = Date.now }) {
67
+ function createOidcStore({ database, profiles, now = Date.now }) {
68
+ const { knex } = database;
59
69
  const stamp = () => new Date(now());
60
70
 
61
71
  async function findClient(issuer, origin) {
@@ -134,18 +144,20 @@ function createOidcStore({ knex, now = Date.now }) {
134
144
  }
135
145
  }
136
146
 
137
- /** Preview identity signs in as an account the app already knows. */
147
+ /** Preview identity signs in as an account the app already knows, which users-core's records answer. */
138
148
  async function findKnownUser(provider, { userId = "", email = "" } = {}) {
139
- const query = knex("users").where({ auth_provider: provider });
140
- if (userId) query.andWhere({ auth_provider_user_sid: userId });
141
- else query.andWhere({ email });
142
- let row;
149
+ if (!profiles) return null;
150
+ let profile;
143
151
  try {
144
- row = await query.first("auth_provider_user_sid", "email", "display_name");
152
+ profile = userId
153
+ ? await profiles.findByIdentity({ provider, providerUserId: userId })
154
+ : await profiles.findByEmail(email);
145
155
  } catch {
146
156
  return null;
147
157
  }
148
- return row ? { sub: row.auth_provider_user_sid, email: row.email, displayName: row.display_name } : null;
158
+ return profile && profile.authProvider === provider
159
+ ? { sub: profile.authProviderUserSid, email: profile.email, displayName: profile.displayName }
160
+ : null;
149
161
  }
150
162
 
151
163
  return Object.freeze({
@@ -161,4 +173,7 @@ function createOidcStore({ knex, now = Date.now }) {
161
173
  });
162
174
  }
163
175
 
164
- export { TABLES, createOidcStore };
176
+ // The tables the relying party keeps outside table resources, for an app test's expectEveryTableOwned infrastructureTables.
177
+ const OIDC_CLIENT_INFRASTRUCTURE_TABLES = Object.freeze(Object.values(TABLES));
178
+
179
+ export { OIDC_CLIENT_INFRASTRUCTURE_TABLES, TABLES, createOidcStore };
@@ -22,7 +22,8 @@ const AuthOidcProvider = defineProvider({
22
22
  logger: "runtime.logger"
23
23
  },
24
24
  optional: {
25
- profileProjector: "auth.profile.projector"
25
+ profileProjector: "auth.profile.projector",
26
+ profiles: "users.profiles"
26
27
  },
27
28
  provides: {
28
29
  authService: "auth.service"
@@ -39,10 +40,11 @@ const AuthOidcProvider = defineProvider({
39
40
  * @param {import("@jskit-ai/kernel/server/types").RuntimeFastify} dependencies.fastify
40
41
  * @param {import("@jskit-ai/kernel/server/types").RuntimeLogger} dependencies.logger
41
42
  * @param {import("@jskit-ai/auth-core/server/types").AuthProfileProjector | null} dependencies.profileProjector The `auth.profile.projector`, when one is installed.
43
+ * @param {import("../lib/store.js").OidcKnownUsers | null} dependencies.profiles users-core's `users.profiles`, when it is installed.
42
44
  */
43
- async boot({ appRoot, database, env, extensions, fastify, logger, profileProjector }, { outputs }) {
45
+ async boot({ appRoot, database, env, extensions, fastify, logger, profileProjector, profiles }, { outputs }) {
44
46
  const config = resolveOidcConfig({ env, appRoot });
45
- const store = createOidcStore({ knex: database.knex });
47
+ const store = createOidcStore({ database, profiles: profiles ?? null });
46
48
  const issuer = createIssuerClient({ config, store, logger });
47
49
  const baseService = createOidcAuthService({
48
50
  config,
@@ -0,0 +1,13 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import createKnex from "knex";
4
+ import migration from "../migrations/auth_oidc_initial.cjs";
5
+ import { OIDC_CLIENT_INFRASTRUCTURE_TABLES } from "../src/server/lib/index.js";
6
+
7
+ test("OIDC_CLIENT_INFRASTRUCTURE_TABLES names every table the relying party's migration creates", async (t) => {
8
+ const knex = createKnex({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
9
+ t.after(() => knex.destroy());
10
+ await migration.up(knex);
11
+ const tables = await knex.raw("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'");
12
+ assert.deepEqual(tables.map(({ name }) => name).sort(), [...OIDC_CLIENT_INFRASTRUCTURE_TABLES].sort());
13
+ });
@@ -53,6 +53,21 @@ function createProfileProjector(knex) {
53
53
  };
54
54
  }
55
55
 
56
+ // Stands in for users-core's users.profiles: the users rows as its profile records.
57
+ function createUsersProfiles(knex) {
58
+ const toProfile = (row) => row
59
+ ? { authProvider: row.auth_provider, authProviderUserSid: row.auth_provider_user_sid, email: row.email, displayName: row.display_name }
60
+ : null;
61
+ return {
62
+ async findByIdentity({ provider, providerUserId }) {
63
+ return toProfile(await knex("users").where({ auth_provider: provider, auth_provider_user_sid: providerUserId }).first());
64
+ },
65
+ async findByEmail(email) {
66
+ return toProfile(await knex("users").where({ email }).first());
67
+ }
68
+ };
69
+ }
70
+
56
71
  /**
57
72
  * A relying party over an in-memory database, signing in at the given issuer.
58
73
  * @param {object} options
@@ -73,7 +88,7 @@ async function createRelyingParty({ issuer, env = {}, fetchImpl = globalThis.fet
73
88
  ...env
74
89
  }
75
90
  });
76
- const store = createOidcStore({ knex, now });
91
+ const store = createOidcStore({ database: { knex }, profiles: createUsersProfiles(knex), now });
77
92
  const logger = { info() {}, warn() {} };
78
93
  const issuerClient = createIssuerClient({ config, store, logger, fetchImpl, now });
79
94
  const service = createOidcAuthService({