@o3co/create-auth-provider 0.16.0 → 0.17.0-rc.2

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.
Files changed (100) hide show
  1. package/README.ja.md +66 -21
  2. package/README.md +76 -31
  3. package/dist/index.d.mts +33 -10
  4. package/dist/index.d.mts.map +1 -1
  5. package/dist/index.mjs +118 -59
  6. package/dist/internal/template-filter.d.mts +7 -10
  7. package/dist/internal/template-filter.d.mts.map +1 -1
  8. package/dist/internal/template-filter.mjs +7 -10
  9. package/package.json +6 -4
  10. package/templates/standalone/.dockerignore +15 -2
  11. package/templates/standalone/.env.example +59 -29
  12. package/templates/standalone/Dockerfile +6 -2
  13. package/templates/standalone/README.ja.md +324 -169
  14. package/templates/standalone/README.md +618 -208
  15. package/templates/standalone/config/application.conf +211 -352
  16. package/templates/standalone/config/clients.yaml +2 -0
  17. package/templates/standalone/config/clients.yaml.example +4 -0
  18. package/templates/standalone/config/development.conf +13 -4
  19. package/templates/standalone/config/mailpit.conf +8 -0
  20. package/templates/standalone/config/production.conf +5 -6
  21. package/templates/standalone/config/reference.conf +418 -0
  22. package/templates/standalone/docker-compose.mailpit.yml +36 -0
  23. package/templates/standalone/docker-compose.production.yml +63 -32
  24. package/templates/standalone/docker-compose.yml +13 -3
  25. package/templates/standalone/gitignore +1 -0
  26. package/templates/standalone/package.json +9 -6
  27. package/templates/standalone/src/__tests__/adapters.test.mts +419 -0
  28. package/templates/standalone/src/__tests__/admission-lifecycle.test.mts +87 -0
  29. package/templates/standalone/src/__tests__/all-modules-composition.fixture.mts +287 -198
  30. package/templates/standalone/src/__tests__/all-modules-composition.multi.test.mts +71 -35
  31. package/templates/standalone/src/__tests__/all-modules-composition.test.mts +148 -94
  32. package/templates/standalone/src/__tests__/audit-sink.test.mts +84 -81
  33. package/templates/standalone/src/__tests__/config-notices.test.mts +154 -0
  34. package/templates/standalone/src/__tests__/configPath.test.mts +13 -11
  35. package/templates/standalone/src/__tests__/core-federations.test.mts +249 -0
  36. package/templates/standalone/src/__tests__/core-sections.test.mts +190 -0
  37. package/templates/standalone/src/__tests__/device-code-store-client-module.test.mts +10 -15
  38. package/templates/standalone/src/__tests__/documented-env-overrides.test.mts +970 -288
  39. package/templates/standalone/src/__tests__/federation-grants-composition.test.mts +421 -86
  40. package/templates/standalone/src/__tests__/federation-grants-survive-logout.test.mts +87 -72
  41. package/templates/standalone/src/__tests__/federation-token-refresh-outage.test.mts +16 -8
  42. package/templates/standalone/src/__tests__/federation-types.test.mts +239 -0
  43. package/templates/standalone/src/__tests__/google-session.fixture.mts +97 -93
  44. package/templates/standalone/src/__tests__/host-routes.test.mts +4 -11
  45. package/templates/standalone/src/__tests__/library-reference-resolution.smoke.test.mts +1 -1
  46. package/templates/standalone/src/__tests__/library-references.fixture.mts +188 -0
  47. package/templates/standalone/src/__tests__/lifecycle-pending-close.test.mts +216 -0
  48. package/templates/standalone/src/__tests__/listen.test.mts +6 -13
  49. package/templates/standalone/src/__tests__/logger-projection.test.mts +1 -1
  50. package/templates/standalone/src/__tests__/mail-sender.test.mts +134 -0
  51. package/templates/standalone/src/__tests__/mailpit-compose.test.mts +326 -0
  52. package/templates/standalone/src/__tests__/metrics.test.mts +4 -9
  53. package/templates/standalone/src/__tests__/mfa-routes-budget.test.mts +122 -0
  54. package/templates/standalone/src/__tests__/mfa-switch.redis.test.mts +123 -0
  55. package/templates/standalone/src/__tests__/mfa-switch.test.mts +825 -0
  56. package/templates/standalone/src/__tests__/own-modules.test.mts +1486 -0
  57. package/templates/standalone/src/__tests__/post-logout-redirect.test.mts +10 -14
  58. package/templates/standalone/src/__tests__/redis-connection-error-log.test.mts +1 -5
  59. package/templates/standalone/src/__tests__/redis-rate-limiter-config-survives.test.mts +116 -81
  60. package/templates/standalone/src/__tests__/refresh-token-family-client-module.test.mts +25 -31
  61. package/templates/standalone/src/__tests__/replica-safety.test.mts +265 -146
  62. package/templates/standalone/src/__tests__/router-error-answers.test.mts +4 -7
  63. package/templates/standalone/src/__tests__/scaffold-assets.test.mts +278 -103
  64. package/templates/standalone/src/__tests__/sections.test.mts +480 -0
  65. package/templates/standalone/src/__tests__/session-lifecycle.test.mts +215 -0
  66. package/templates/standalone/src/__tests__/session-logout-invalidates-token.test.mts +69 -61
  67. package/templates/standalone/src/__tests__/session-logout-lifecycle.test.mts +130 -0
  68. package/templates/standalone/src/__tests__/session-requirements.test.mts +382 -0
  69. package/templates/standalone/src/__tests__/shipped-config-notices.test.mts +270 -0
  70. package/templates/standalone/src/__tests__/shutdown.test.mts +163 -96
  71. package/templates/standalone/src/__tests__/smoke.test.mts +176 -258
  72. package/templates/standalone/src/__tests__/store-credential.test.mts +88 -52
  73. package/templates/standalone/src/__tests__/supertest-loopback.test.mts +182 -11
  74. package/templates/standalone/src/__tests__/three-tier-resolution.test.mts +131 -102
  75. package/templates/standalone/src/__tests__/two-phase-config.test.mts +604 -0
  76. package/templates/standalone/src/__tests__/upstream-amr.acceptance.test.mts +382 -0
  77. package/templates/standalone/src/adapters.mts +162 -0
  78. package/templates/standalone/src/app.mts +65 -79
  79. package/templates/standalone/src/bootRefusal.mts +117 -0
  80. package/templates/standalone/src/buildModules.mts +220 -309
  81. package/templates/standalone/src/configPath.mts +449 -29
  82. package/templates/standalone/src/listen.mts +12 -18
  83. package/templates/standalone/src/logger.mts +31 -60
  84. package/templates/standalone/src/metrics.mts +24 -47
  85. package/templates/standalone/src/mfaSwitch.mts +285 -0
  86. package/templates/standalone/src/modules.mts +423 -629
  87. package/templates/standalone/src/rootRenames.mts +96 -0
  88. package/templates/standalone/src/routes.mts +8 -14
  89. package/templates/standalone/src/sections.mts +367 -0
  90. package/templates/standalone/src/shutdown.mts +45 -123
  91. package/templates/standalone/tests/index.test.js +6 -5
  92. package/templates/standalone/tests/package-lock.json +1615 -2223
  93. package/templates/standalone/tests/package.json +2 -2
  94. package/templates/standalone/tests/vitest.config.mjs +26 -0
  95. package/templates/standalone/vitest.config.mts +8 -7
  96. package/templates/standalone/vitest.supertest-loopback.mts +97 -69
  97. package/templates/versions.json +18 -15
  98. package/templates/standalone/src/__tests__/access-token-lifetime-alias.test.mts +0 -135
  99. package/templates/standalone/src/__tests__/google-federation-config-module.test.mts +0 -199
  100. package/templates/standalone/src/__tests__/oidc-federation-config-module.test.mts +0 -138
@@ -0,0 +1,419 @@
1
+ /*
2
+ * Copyright 2026 1o1 Co. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * The composition root's own section, `adapters`: which adapter fills each
19
+ * slot. Phase one reads it alone, with the template's own strict schema, over
20
+ * the template's own layers and its `reference.conf`, before it chooses its
21
+ * modules; boot is not handed it. A path a selection moved from, or a
22
+ * variable renamed with it, refuses before any module is chosen, naming the
23
+ * new path and variable; an old variable beside its new name at the same
24
+ * value is accepted.
25
+ */
26
+
27
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
28
+ import { tmpdir } from "node:os";
29
+ import { join } from "node:path";
30
+ import { fileURLToPath } from "node:url";
31
+ import { BootError, defineModule } from "@o3co/auth-provider-core";
32
+ import { afterAll, describe, expect, it } from "vitest";
33
+ import { z } from "zod";
34
+ import { buildModules } from "#/buildModules.mjs";
35
+ import { readOwnLayers, readSwitches, resolveConfigPaths, resolveForBoot } from "#/configPath.mjs";
36
+
37
+ const configDir = fileURLToPath(new URL("../../config", import.meta.url));
38
+
39
+ /** The directories the operator layers are written to, removed after the suite. */
40
+ const operatorDirs: string[] = [];
41
+ afterAll(() => {
42
+ for (const dir of operatorDirs) rmSync(dir, { recursive: true, force: true });
43
+ });
44
+
45
+ /** The template's own files for the production environment, under an operator's layer when given. */
46
+ function ownFiles(hocon?: string): string[] {
47
+ const { applicationConfPath, envConfPath } = resolveConfigPaths(configDir, "production");
48
+ if (hocon === undefined) return [envConfPath, applicationConfPath];
49
+ const dir = mkdtempSync(join(tmpdir(), "adapters-"));
50
+ operatorDirs.push(dir);
51
+ const file = join(dir, "operator.conf");
52
+ writeFileSync(file, hocon);
53
+ return [file, envConfPath, applicationConfPath];
54
+ }
55
+
56
+ /** MFA, on by default, switched off where modules are built: these are about the other adapters. */
57
+ const MFA_OFF: Readonly<Record<string, string>> = { MFA_MODE: "off" };
58
+
59
+ /** Phase one's reading of `adapters`, under `env` and an operator's `hocon`. */
60
+ const adaptersFrom = (env: Record<string, string> = {}, hocon?: string) =>
61
+ readSwitches(readOwnLayers(ownFiles(hocon), { env })).adapters;
62
+
63
+ /** What phase one refuses under `env` and an operator's `hocon`: a `BootError`. */
64
+ function refusal(env: Record<string, string> = {}, hocon?: string): BootError {
65
+ try {
66
+ adaptersFrom(env, hocon);
67
+ } catch (err) {
68
+ expect(err).toBeInstanceOf(BootError);
69
+ return err as BootError;
70
+ }
71
+ throw new Error("phase one read the adapters");
72
+ }
73
+
74
+ /** Each selection: its key, the variable that sets it, the path and variable it moved from, a value it takes. */
75
+ const SELECTIONS = [
76
+ ["rateLimiter", "RATE_LIMITER", "rateLimiter.adapter", "RATE_LIMITER_ADAPTER", "redis"],
77
+ [
78
+ "userSessionStores",
79
+ "USER_SESSION_STORES",
80
+ "userSessionStores.adapter",
81
+ "USER_SESSION_STORES_ADAPTER",
82
+ "redis",
83
+ ],
84
+ [
85
+ "accessTokenDenylist",
86
+ "ACCESS_TOKEN_DENYLIST",
87
+ "accessTokenDenylist.adapter",
88
+ "ACCESS_TOKEN_DENYLIST_ADAPTER",
89
+ "memory",
90
+ ],
91
+ [
92
+ "replaySeenSet",
93
+ "REPLAY_SEEN_SET",
94
+ "replaySeenSet.adapter",
95
+ "REPLAY_SEEN_SET_ADAPTER",
96
+ "memory",
97
+ ],
98
+ ["consentStore", "CONSENT_STORE", "consentStore.adapter", "CONSENT_STORE_ADAPTER", "memory"],
99
+ [
100
+ "federationTokenStore",
101
+ "FEDERATION_TOKEN_STORE",
102
+ "federationTokenStore.type",
103
+ "FEDERATION_TOKEN_STORE_TYPE",
104
+ "redis",
105
+ ],
106
+ [
107
+ "federationGrantStore",
108
+ "FEDERATION_GRANT_STORE",
109
+ "federationGrantStore.adapter",
110
+ "FEDERATION_GRANT_STORE_ADAPTER",
111
+ "memory",
112
+ ],
113
+ [
114
+ "federationGrantIntentStore",
115
+ "FEDERATION_GRANT_INTENT_STORE",
116
+ "federationGrantIntentStore.adapter",
117
+ "FEDERATION_GRANT_INTENT_STORE_ADAPTER",
118
+ "memory",
119
+ ],
120
+ [
121
+ "mfaFactorStore",
122
+ "MFA_FACTOR_STORE",
123
+ "mfaFactorStore.adapter",
124
+ "MFA_FACTOR_STORE_ADAPTER",
125
+ "store",
126
+ ],
127
+ [
128
+ "mfaTransactionStore",
129
+ "MFA_TRANSACTION_STORE",
130
+ "mfaTransactionStore.adapter",
131
+ "MFA_TRANSACTION_STORE_ADAPTER",
132
+ "redis",
133
+ ],
134
+ ["codeRepository", "CODE_REPOSITORY", "oauth.code.adapter", "OAUTH_CODE_ADAPTER", "memory"],
135
+ ["codeRepository", "CODE_REPOSITORY", "repositories.code.type", "CLIENT_CODE_TYPE", "memory"],
136
+ ["clientRepository", "CLIENT_REPOSITORY", "repositories.client.type", "CLIENT_TYPE", "yaml"],
137
+ ["userRepository", "USER_REPOSITORY", "repositories.user.type", "CLIENT_USER_TYPE", "yaml"],
138
+ ["auditSink", "AUDIT_SINK", "audit.sink.type", "AUDIT_SINK_TYPE", "console"],
139
+ ] as const;
140
+
141
+ describe("the shipped selections", () => {
142
+ it("reads every selection from the template's reference.conf, at the values the template ships", () => {
143
+ expect(adaptersFrom()).toEqual({
144
+ rateLimiter: "memory",
145
+ attemptCounter: "memory",
146
+ userSessionStores: "memory",
147
+ accessTokenDenylist: "redis",
148
+ replaySeenSet: "redis",
149
+ consentStore: "none",
150
+ federationTokenStore: "memory",
151
+ federationGrantStore: "none",
152
+ federationGrantIntentStore: "none",
153
+ mfaFactorStore: "memory",
154
+ mfaTransactionStore: "memory",
155
+ codeRepository: "redis",
156
+ clientRepository: "yaml",
157
+ userRepository: "http",
158
+ auditSink: "logger",
159
+ });
160
+ });
161
+
162
+ it.each(SELECTIONS)(
163
+ "reads adapters.%s from ADAPTERS_%s",
164
+ (key, name, _oldPath, _oldName, value) => {
165
+ expect(adaptersFrom({ [`ADAPTERS_${name}`]: value })[key]).toBe(value);
166
+ },
167
+ );
168
+
169
+ it("reads adapters.attemptCounter from ADAPTERS_ATTEMPT_COUNTER", () => {
170
+ expect(adaptersFrom({ ADAPTERS_ATTEMPT_COUNTER: "redis" }).attemptCounter).toBe("redis");
171
+ expect(refusal({ ADAPTERS_ATTEMPT_COUNTER: "memcached" }).message).toMatch(
172
+ /adapters\.attemptCounter/,
173
+ );
174
+ });
175
+
176
+ it("reads a selection an operator writes in HOCON", () => {
177
+ expect(adaptersFrom({}, 'adapters.rateLimiter = "redis"\n').rateLimiter).toBe("redis");
178
+ });
179
+
180
+ it("refuses a value its schema does not know, naming the key", () => {
181
+ const err = refusal({ ADAPTERS_RATE_LIMITER: "memcached" });
182
+ expect(err.message).toMatch(/adapters\.rateLimiter/);
183
+ expect(err.reason).toBe("config-validation-failed");
184
+ expect(err.details).toMatchObject({
185
+ issues: [{ path: ["adapters", "rateLimiter"] }],
186
+ modules: [{ module: "adapters", schemaPath: "adapters" }],
187
+ });
188
+ });
189
+
190
+ it("refuses the Store for the MFA transaction store, which holds verification state, and accepts it for the factor store", () => {
191
+ expect(refusal({ ADAPTERS_MFA_TRANSACTION_STORE: "store" }).message).toMatch(
192
+ /adapters\.mfaTransactionStore/,
193
+ );
194
+ expect(adaptersFrom({ ADAPTERS_MFA_FACTOR_STORE: "store" }).mfaFactorStore).toBe("store");
195
+ });
196
+
197
+ it.each([
198
+ ["ADAPTERS_CLIENT_REPOSITORY", "clientRepository"],
199
+ ["ADAPTERS_USER_REPOSITORY", "userRepository"],
200
+ ] as const)("reads static, core's alias of yaml, from %s", (variable, key) => {
201
+ expect(adaptersFrom({ [variable]: "static" })[key]).toBe("static");
202
+ });
203
+
204
+ it("refuses a key the section does not declare, naming it", () => {
205
+ expect(refusal({}, 'adapters.sessionStore = "redis"\n').message).toMatch(/sessionStore/);
206
+ });
207
+ });
208
+
209
+ describe("a path a selection moved from, written in the operator's own layer", () => {
210
+ it.each(SELECTIONS)(
211
+ "adapters.%s: refused where it was, %s's old path, naming the new path and ADAPTERS_%s",
212
+ (key, name, oldPath, _oldName, value) => {
213
+ const err = refusal({}, `${oldPath} = "${value}"\n`);
214
+ expect(err.message).toContain(`${oldPath} has moved to adapters.${key}`);
215
+ expect(err.message).toContain(`ADAPTERS_${name}`);
216
+ expect(err.reason).toBe("config-path-relocated");
217
+ expect(err.details).toEqual({
218
+ reason: "config-path-relocated",
219
+ relocated: [
220
+ {
221
+ module: "adapters",
222
+ from: oldPath,
223
+ to: `adapters.${key}`,
224
+ environmentVariable: `ADAPTERS_${name}`,
225
+ },
226
+ ],
227
+ });
228
+ },
229
+ );
230
+
231
+ it("names every old path it finds, in one refusal", () => {
232
+ const err = refusal(
233
+ {},
234
+ 'rateLimiter.adapter = "redis"\nconsentStore.adapter = "memory"\naudit.sink.type = "console"\n',
235
+ );
236
+ for (const path of ["rateLimiter.adapter", "consentStore.adapter", "audit.sink.type"]) {
237
+ expect(err.message).toContain(path);
238
+ }
239
+ expect(err.details).toMatchObject({
240
+ relocated: [
241
+ { from: "rateLimiter.adapter" },
242
+ { from: "consentStore.adapter" },
243
+ { from: "audit.sink.type" },
244
+ ],
245
+ });
246
+ });
247
+ });
248
+
249
+ describe("a variable renamed with a selection", () => {
250
+ it.each(SELECTIONS)(
251
+ "adapters.%s: %s's old variable set alone is refused, naming ADAPTERS_%s",
252
+ (key, name, _oldPath, oldName, value) => {
253
+ const err = refusal({ [oldName]: value });
254
+ expect(err.message).toContain(`${oldName} was renamed ADAPTERS_${name}`);
255
+ expect(err.message).toContain(`adapters.${key}`);
256
+ expect(err.reason).toBe("environment-variable-renamed");
257
+ expect(err.details).toEqual({
258
+ reason: "environment-variable-renamed",
259
+ renamed: [
260
+ {
261
+ module: "adapters",
262
+ from: oldName,
263
+ to: `ADAPTERS_${name}`,
264
+ path: `adapters.${key}`,
265
+ state: "unset",
266
+ },
267
+ ],
268
+ });
269
+ },
270
+ );
271
+
272
+ it.each(SELECTIONS)(
273
+ "adapters.%s: the old variable beside ADAPTERS_%s at a different value is refused, quoting neither",
274
+ (_key, name, _oldPath, oldName) => {
275
+ const err = refusal({ [oldName]: "old-value-5e2d", [`ADAPTERS_${name}`]: "new-value-c81a" });
276
+ expect(err.message).toContain(oldName);
277
+ expect(err.message).not.toContain("old-value-5e2d");
278
+ expect(err.message).not.toContain("new-value-c81a");
279
+ expect(err.details).toMatchObject({ renamed: [{ from: oldName, state: "different" }] });
280
+ expect(JSON.stringify(err.details)).not.toMatch(/old-value-5e2d|new-value-c81a/);
281
+ },
282
+ );
283
+
284
+ it.each(SELECTIONS)(
285
+ "adapters.%s: the old variable beside ADAPTERS_%s at the same value is read",
286
+ (key, name, _oldPath, oldName, value) => {
287
+ expect(adaptersFrom({ [oldName]: value, [`ADAPTERS_${name}`]: value })[key]).toBe(value);
288
+ },
289
+ );
290
+ });
291
+
292
+ describe("the modules phase one chooses by the selections", () => {
293
+ const names = (env: Record<string, string>) =>
294
+ buildModules(readSwitches(readOwnLayers(ownFiles(), { env: { ...MFA_OFF, ...env } })), {
295
+ environment: "production",
296
+ }).map((module) => module.name);
297
+
298
+ it.each([
299
+ ["ADAPTERS_RATE_LIMITER", "redis", "redis-rate-limiter", "core-rate-limiter-memory"],
300
+ ["ADAPTERS_RATE_LIMITER", "memory", "core-rate-limiter-memory", "redis-rate-limiter"],
301
+ [
302
+ "ADAPTERS_CODE_REPOSITORY",
303
+ "memory",
304
+ "standalone-in-memory-code-repository",
305
+ "redis-code-repository",
306
+ ],
307
+ [
308
+ "ADAPTERS_CODE_REPOSITORY",
309
+ "redis",
310
+ "redis-code-repository",
311
+ "standalone-in-memory-code-repository",
312
+ ],
313
+ [
314
+ "ADAPTERS_USER_SESSION_STORES",
315
+ "redis",
316
+ "redis-session-stores",
317
+ "standalone-in-memory-session-stores",
318
+ ],
319
+ ["ADAPTERS_CONSENT_STORE", "memory", "core-consent-store-memory", "redis-consent-store"],
320
+ ])("%s=%s installs %s, not %s", (variable, value, installed, absent) => {
321
+ const modules = names({ [variable]: value });
322
+ expect(modules).toContain(installed);
323
+ expect(modules).not.toContain(absent);
324
+ });
325
+
326
+ it("ADAPTERS_ATTEMPT_COUNTER=redis installs redis-attempt-counter over the shared Redis socket; memory installs no counter", () => {
327
+ const onRedis = names({ ADAPTERS_ATTEMPT_COUNTER: "redis" });
328
+ expect(onRedis).toContain("redis-attempt-counter");
329
+ expect(onRedis).toContain("redis-clients");
330
+ expect(names({ ADAPTERS_ATTEMPT_COUNTER: "memory" })).not.toContain("redis-attempt-counter");
331
+ });
332
+ });
333
+
334
+ describe("the federation-grant stores", () => {
335
+ const namesWith = (env: Record<string, string>) =>
336
+ buildModules(
337
+ readSwitches(
338
+ readOwnLayers(ownFiles(), {
339
+ env: { ...MFA_OFF, FEDERATION_GRANTS_ENABLED: "true", ...env },
340
+ }),
341
+ ),
342
+ { environment: "production" },
343
+ ).map((module) => module.name);
344
+ const STORES = [
345
+ "core-federation-grant-store-memory",
346
+ "redis-federation-grant-store",
347
+ "core-federation-grant-intent-store-memory",
348
+ "redis-federation-grant-intent-store",
349
+ ];
350
+
351
+ it("default to none: with the feature on, neither store is installed until one is selected", () => {
352
+ expect(namesWith({}).filter((name) => STORES.includes(name))).toEqual([]);
353
+ });
354
+
355
+ it("read none from ADAPTERS_FEDERATION_GRANT_STORE and ADAPTERS_FEDERATION_GRANT_INTENT_STORE", () => {
356
+ expect(
357
+ adaptersFrom({
358
+ ADAPTERS_FEDERATION_GRANT_STORE: "none",
359
+ ADAPTERS_FEDERATION_GRANT_INTENT_STORE: "none",
360
+ }),
361
+ ).toMatchObject({ federationGrantStore: "none", federationGrantIntentStore: "none" });
362
+ });
363
+
364
+ it.each([
365
+ ["memory", "core-federation-grant-store-memory", "core-federation-grant-intent-store-memory"],
366
+ ["redis", "redis-federation-grant-store", "redis-federation-grant-intent-store"],
367
+ ])("install the %s stores once selected", (value, grants, intents) => {
368
+ const names = namesWith({
369
+ ADAPTERS_FEDERATION_GRANT_STORE: value,
370
+ ADAPTERS_FEDERATION_GRANT_INTENT_STORE: value,
371
+ });
372
+ expect(names).toContain(grants);
373
+ expect(names).toContain(intents);
374
+ });
375
+ });
376
+
377
+ describe("boot and the section", () => {
378
+ it("hands boot no adapters section: the composition root consumed it", () => {
379
+ const own = readOwnLayers(ownFiles(), { env: MFA_OFF });
380
+ const switches = readSwitches(own);
381
+ const resolved = resolveForBoot(
382
+ own,
383
+ buildModules(switches, { environment: "production" }),
384
+ switches,
385
+ );
386
+ expect(resolved).not.toHaveProperty("adapters");
387
+ });
388
+
389
+ it.each([
390
+ ["named adapters", "adapters"],
391
+ ["named mfaMode", "mfaMode"],
392
+ ] as const)("refuses a module %s: its section would never reach boot", (_label, name) => {
393
+ const own = readOwnLayers(ownFiles(), { env: MFA_OFF });
394
+ const switches = readSwitches(own);
395
+ const mine = defineModule({
396
+ name,
397
+ section: { schema: z.object({}).passthrough().optional() },
398
+ });
399
+ const run = () =>
400
+ resolveForBoot(
401
+ own,
402
+ [...buildModules(switches, { environment: "production" }), mine],
403
+ switches,
404
+ );
405
+ expect(run).toThrow(new RegExp(`"${name}".*composition root's own section`));
406
+ const err = (() => {
407
+ try {
408
+ run();
409
+ } catch (thrown) {
410
+ return thrown;
411
+ }
412
+ })();
413
+ expect(err).toBeInstanceOf(BootError);
414
+ expect(err).toMatchObject({
415
+ reason: "module-section-path-invalid",
416
+ details: { module: name, at: name },
417
+ });
418
+ });
419
+ });
@@ -0,0 +1,87 @@
1
+ /*
2
+ * Copyright 2026 1o1 Co. Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * The oauth package's consumers of session admission read the session
19
+ * lifecycle's record the template's session stores provide: a session whose
20
+ * record is closing is refused at `/authorize`, the code exchange and the
21
+ * refresh grant, while its user session is still there.
22
+ */
23
+
24
+ import type { SessionLifecycleStore, UserSessionStore } from "@o3co/auth-provider-core";
25
+ import request from "supertest";
26
+ import { describe, expect, it } from "vitest";
27
+ import {
28
+ authorize,
29
+ basic,
30
+ codeFrom,
31
+ compose,
32
+ login,
33
+ redeem,
34
+ WEB,
35
+ } from "./all-modules-composition.fixture.mjs";
36
+
37
+ /** A JWT's claims, unverified. */
38
+ const claimsOf = (token: string): Record<string, unknown> =>
39
+ JSON.parse(Buffer.from(token.split(".")[1] ?? "", "base64url").toString("utf8"));
40
+
41
+ describe("a session whose lifecycle record is closing", () => {
42
+ it("is refused at /authorize, the code exchange and the refresh grant", async () => {
43
+ const { app, handle } = await compose();
44
+ try {
45
+ const components = handle.components as Record<string, unknown>;
46
+ const store = components.sessionLifecycleStore as SessionLifecycleStore;
47
+ const sessions = components.userSessionStore as UserSessionStore;
48
+ const { cookies } = await login(app);
49
+ const tokens = (await redeem(app, codeFrom(await authorize(app, cookies)))).body as Record<
50
+ string,
51
+ string
52
+ >;
53
+ const sid = String(claimsOf(tokens.id_token ?? "").sid);
54
+ const pendingCode = codeFrom(await authorize(app, cookies));
55
+
56
+ // The session's close commits, its work pending: the user session stays.
57
+ const session = await sessions.get(sid);
58
+ if (session === null) throw new Error("the login wrote no session");
59
+ expect((await store.open(sid, session.sub, session.expiresAt)).outcome).toBe("opened");
60
+ const closing = await store.beginClose(sid, {
61
+ cause: "rp_logout",
62
+ steps: ["held_open"],
63
+ perParticipant: [],
64
+ retainMs: 0,
65
+ });
66
+ expect(closing.outcome).toBe("closing");
67
+ expect(await sessions.get(sid)).not.toBeNull();
68
+
69
+ const again = await authorize(app, cookies);
70
+ // Sent to log in again, with no code.
71
+ expect(again.status).toBe(302);
72
+ expect(String(again.headers.location)).not.toMatch(/[?&]code=/);
73
+
74
+ expect((await redeem(app, pendingCode)).body).toMatchObject({ error: "invalid_grant" });
75
+
76
+ const refreshed = await request(app)
77
+ .post("/oauth/token")
78
+ .set("Authorization", basic(WEB))
79
+ .type("form")
80
+ .send({ grant_type: "refresh_token", refresh_token: tokens.refresh_token });
81
+ expect(refreshed.status).toBe(400);
82
+ expect(refreshed.body).toMatchObject({ error: "invalid_grant" });
83
+ } finally {
84
+ await handle.dispose();
85
+ }
86
+ });
87
+ });