@o3co/create-auth-provider 0.15.0 → 0.17.0-rc.1

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 (92) hide show
  1. package/README.ja.md +139 -48
  2. package/README.md +138 -47
  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 +69 -24
  12. package/templates/standalone/Dockerfile +7 -3
  13. package/templates/standalone/README.ja.md +677 -184
  14. package/templates/standalone/README.md +699 -238
  15. package/templates/standalone/config/application.conf +203 -325
  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 +399 -0
  22. package/templates/standalone/docker-compose.mailpit.yml +36 -0
  23. package/templates/standalone/docker-compose.production.yml +58 -30
  24. package/templates/standalone/docker-compose.yml +13 -3
  25. package/templates/standalone/gitignore +4 -2
  26. package/templates/standalone/package.json +9 -6
  27. package/templates/standalone/src/__tests__/access-token-lifetime-alias.test.mts +65 -32
  28. package/templates/standalone/src/__tests__/adapters.test.mts +358 -0
  29. package/templates/standalone/src/__tests__/all-modules-composition.fixture.mts +1178 -0
  30. package/templates/standalone/src/__tests__/all-modules-composition.multi.test.mts +256 -0
  31. package/templates/standalone/src/__tests__/all-modules-composition.test.mts +1119 -0
  32. package/templates/standalone/src/__tests__/audit-sink.test.mts +78 -76
  33. package/templates/standalone/src/__tests__/configPath.test.mts +13 -11
  34. package/templates/standalone/src/__tests__/core-federations.test.mts +226 -0
  35. package/templates/standalone/src/__tests__/core-sections.test.mts +190 -0
  36. package/templates/standalone/src/__tests__/device-code-store-client-module.test.mts +9 -14
  37. package/templates/standalone/src/__tests__/documented-env-overrides.test.mts +672 -271
  38. package/templates/standalone/src/__tests__/federation-grants-composition.test.mts +480 -78
  39. package/templates/standalone/src/__tests__/federation-grants-survive-logout.test.mts +102 -66
  40. package/templates/standalone/src/__tests__/federation-token-refresh-outage.test.mts +135 -0
  41. package/templates/standalone/src/__tests__/google-federation-config-module.test.mts +49 -18
  42. package/templates/standalone/src/__tests__/google-session.fixture.mts +315 -0
  43. package/templates/standalone/src/__tests__/host-routes.test.mts +134 -0
  44. package/templates/standalone/src/__tests__/library-references.fixture.mts +183 -0
  45. package/templates/standalone/src/__tests__/listen.test.mts +121 -0
  46. package/templates/standalone/src/__tests__/logger-projection.test.mts +77 -0
  47. package/templates/standalone/src/__tests__/mail-sender.test.mts +134 -0
  48. package/templates/standalone/src/__tests__/mailpit-compose.test.mts +328 -0
  49. package/templates/standalone/src/__tests__/metrics.test.mts +17 -7
  50. package/templates/standalone/src/__tests__/mfa-switch.redis.test.mts +123 -0
  51. package/templates/standalone/src/__tests__/mfa-switch.test.mts +780 -0
  52. package/templates/standalone/src/__tests__/oidc-federation-config-module.test.mts +52 -46
  53. package/templates/standalone/src/__tests__/own-modules.test.mts +1457 -0
  54. package/templates/standalone/src/__tests__/post-logout-redirect.test.mts +183 -0
  55. package/templates/standalone/src/__tests__/redis-connection-error-log.test.mts +135 -0
  56. package/templates/standalone/src/__tests__/redis-rate-limiter-config-survives.test.mts +112 -81
  57. package/templates/standalone/src/__tests__/refresh-token-family-client-module.test.mts +25 -31
  58. package/templates/standalone/src/__tests__/replica-safety.test.mts +166 -145
  59. package/templates/standalone/src/__tests__/router-error-answers.test.mts +146 -0
  60. package/templates/standalone/src/__tests__/scaffold-assets.test.mts +275 -95
  61. package/templates/standalone/src/__tests__/sections.test.mts +480 -0
  62. package/templates/standalone/src/__tests__/session-logout-invalidates-token.test.mts +67 -55
  63. package/templates/standalone/src/__tests__/session-requirements.test.mts +299 -0
  64. package/templates/standalone/src/__tests__/shutdown.test.mts +319 -115
  65. package/templates/standalone/src/__tests__/smoke.test.mts +179 -233
  66. package/templates/standalone/src/__tests__/store-credential.test.mts +317 -0
  67. package/templates/standalone/src/__tests__/supertest-loopback.test.mts +182 -11
  68. package/templates/standalone/src/__tests__/three-tier-resolution.test.mts +108 -95
  69. package/templates/standalone/src/__tests__/two-phase-config.test.mts +438 -0
  70. package/templates/standalone/src/__tests__/upstream-amr.acceptance.test.mts +390 -0
  71. package/templates/standalone/src/adapters.mts +149 -0
  72. package/templates/standalone/src/app.mts +71 -115
  73. package/templates/standalone/src/buildModules.mts +229 -251
  74. package/templates/standalone/src/configPath.mts +422 -29
  75. package/templates/standalone/src/listen.mts +60 -0
  76. package/templates/standalone/src/logger.mts +31 -57
  77. package/templates/standalone/src/metrics.mts +24 -47
  78. package/templates/standalone/src/mfaSwitch.mts +262 -0
  79. package/templates/standalone/src/modules.mts +480 -464
  80. package/templates/standalone/src/rootRenames.mts +79 -0
  81. package/templates/standalone/src/routes.mts +89 -0
  82. package/templates/standalone/src/sections.mts +366 -0
  83. package/templates/standalone/src/shutdown.mts +54 -126
  84. package/templates/standalone/tests/index.test.js +2 -2
  85. package/templates/standalone/tests/package-lock.json +1615 -2223
  86. package/templates/standalone/tests/package.json +2 -2
  87. package/templates/standalone/tests/vitest.config.mjs +26 -0
  88. package/templates/standalone/vitest.config.mts +8 -7
  89. package/templates/standalone/vitest.supertest-loopback.mts +97 -69
  90. package/templates/versions.json +18 -15
  91. package/templates/standalone/src/__tests__/terminal-error.test.mts +0 -96
  92. package/templates/standalone/src/terminalError.mts +0 -60
@@ -0,0 +1,358 @@
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 { 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`. */
64
+ function refusal(env: Record<string, string> = {}, hocon?: string): Error {
65
+ try {
66
+ adaptersFrom(env, hocon);
67
+ } catch (err) {
68
+ return err as Error;
69
+ }
70
+ throw new Error("phase one read the adapters");
71
+ }
72
+
73
+ /** Each selection: its key, the variable that sets it, the path and variable it moved from, a value it takes. */
74
+ const SELECTIONS = [
75
+ ["rateLimiter", "RATE_LIMITER", "rateLimiter.adapter", "RATE_LIMITER_ADAPTER", "redis"],
76
+ [
77
+ "userSessionStores",
78
+ "USER_SESSION_STORES",
79
+ "userSessionStores.adapter",
80
+ "USER_SESSION_STORES_ADAPTER",
81
+ "redis",
82
+ ],
83
+ [
84
+ "accessTokenDenylist",
85
+ "ACCESS_TOKEN_DENYLIST",
86
+ "accessTokenDenylist.adapter",
87
+ "ACCESS_TOKEN_DENYLIST_ADAPTER",
88
+ "memory",
89
+ ],
90
+ [
91
+ "replaySeenSet",
92
+ "REPLAY_SEEN_SET",
93
+ "replaySeenSet.adapter",
94
+ "REPLAY_SEEN_SET_ADAPTER",
95
+ "memory",
96
+ ],
97
+ ["consentStore", "CONSENT_STORE", "consentStore.adapter", "CONSENT_STORE_ADAPTER", "memory"],
98
+ [
99
+ "federationTokenStore",
100
+ "FEDERATION_TOKEN_STORE",
101
+ "federationTokenStore.type",
102
+ "FEDERATION_TOKEN_STORE_TYPE",
103
+ "redis",
104
+ ],
105
+ [
106
+ "federationGrantStore",
107
+ "FEDERATION_GRANT_STORE",
108
+ "federationGrantStore.adapter",
109
+ "FEDERATION_GRANT_STORE_ADAPTER",
110
+ "memory",
111
+ ],
112
+ [
113
+ "federationGrantIntentStore",
114
+ "FEDERATION_GRANT_INTENT_STORE",
115
+ "federationGrantIntentStore.adapter",
116
+ "FEDERATION_GRANT_INTENT_STORE_ADAPTER",
117
+ "memory",
118
+ ],
119
+ [
120
+ "mfaFactorStore",
121
+ "MFA_FACTOR_STORE",
122
+ "mfaFactorStore.adapter",
123
+ "MFA_FACTOR_STORE_ADAPTER",
124
+ "store",
125
+ ],
126
+ [
127
+ "mfaTransactionStore",
128
+ "MFA_TRANSACTION_STORE",
129
+ "mfaTransactionStore.adapter",
130
+ "MFA_TRANSACTION_STORE_ADAPTER",
131
+ "redis",
132
+ ],
133
+ ["codeRepository", "CODE_REPOSITORY", "oauth.code.adapter", "OAUTH_CODE_ADAPTER", "memory"],
134
+ ["codeRepository", "CODE_REPOSITORY", "repositories.code.type", "CLIENT_CODE_TYPE", "memory"],
135
+ ["clientRepository", "CLIENT_REPOSITORY", "repositories.client.type", "CLIENT_TYPE", "yaml"],
136
+ ["userRepository", "USER_REPOSITORY", "repositories.user.type", "CLIENT_USER_TYPE", "yaml"],
137
+ ["auditSink", "AUDIT_SINK", "audit.sink.type", "AUDIT_SINK_TYPE", "console"],
138
+ ] as const;
139
+
140
+ describe("the shipped selections", () => {
141
+ it("reads every selection from the template's reference.conf, at the values the template ships", () => {
142
+ expect(adaptersFrom()).toEqual({
143
+ rateLimiter: "memory",
144
+ userSessionStores: "memory",
145
+ accessTokenDenylist: "redis",
146
+ replaySeenSet: "redis",
147
+ consentStore: "none",
148
+ federationTokenStore: "memory",
149
+ federationGrantStore: "none",
150
+ federationGrantIntentStore: "none",
151
+ mfaFactorStore: "memory",
152
+ mfaTransactionStore: "memory",
153
+ codeRepository: "redis",
154
+ clientRepository: "yaml",
155
+ userRepository: "http",
156
+ auditSink: "logger",
157
+ });
158
+ });
159
+
160
+ it.each(SELECTIONS)(
161
+ "reads adapters.%s from ADAPTERS_%s",
162
+ (key, name, _oldPath, _oldName, value) => {
163
+ expect(adaptersFrom({ [`ADAPTERS_${name}`]: value })[key]).toBe(value);
164
+ },
165
+ );
166
+
167
+ it("reads a selection an operator writes in HOCON", () => {
168
+ expect(adaptersFrom({}, 'adapters.rateLimiter = "redis"\n').rateLimiter).toBe("redis");
169
+ });
170
+
171
+ it("refuses a value its schema does not know, naming the key", () => {
172
+ expect(refusal({ ADAPTERS_RATE_LIMITER: "memcached" }).message).toMatch(
173
+ /adapters\.rateLimiter/,
174
+ );
175
+ });
176
+
177
+ it("refuses the Store for the MFA transaction store, which holds verification state, and accepts it for the factor store", () => {
178
+ expect(refusal({ ADAPTERS_MFA_TRANSACTION_STORE: "store" }).message).toMatch(
179
+ /adapters\.mfaTransactionStore/,
180
+ );
181
+ expect(adaptersFrom({ ADAPTERS_MFA_FACTOR_STORE: "store" }).mfaFactorStore).toBe("store");
182
+ });
183
+
184
+ it.each([
185
+ ["ADAPTERS_CLIENT_REPOSITORY", "clientRepository"],
186
+ ["ADAPTERS_USER_REPOSITORY", "userRepository"],
187
+ ] as const)("reads static, core's alias of yaml, from %s", (variable, key) => {
188
+ expect(adaptersFrom({ [variable]: "static" })[key]).toBe("static");
189
+ });
190
+
191
+ it("refuses a key the section does not declare, naming it", () => {
192
+ expect(refusal({}, 'adapters.sessionStore = "redis"\n').message).toMatch(/sessionStore/);
193
+ });
194
+ });
195
+
196
+ describe("a path a selection moved from, written in the operator's own layer", () => {
197
+ it.each(SELECTIONS)(
198
+ "adapters.%s: refused where it was, %s's old path, naming the new path and ADAPTERS_%s",
199
+ (key, name, oldPath, _oldName, value) => {
200
+ const err = refusal({}, `${oldPath} = "${value}"\n`);
201
+ expect(err).toBeInstanceOf(RangeError);
202
+ expect(err.message).toContain(`${oldPath} has moved to adapters.${key}`);
203
+ expect(err.message).toContain(`ADAPTERS_${name}`);
204
+ },
205
+ );
206
+
207
+ it("names every old path it finds, in one refusal", () => {
208
+ const err = refusal(
209
+ {},
210
+ 'rateLimiter.adapter = "redis"\nconsentStore.adapter = "memory"\naudit.sink.type = "console"\n',
211
+ );
212
+ for (const path of ["rateLimiter.adapter", "consentStore.adapter", "audit.sink.type"]) {
213
+ expect(err.message).toContain(path);
214
+ }
215
+ });
216
+ });
217
+
218
+ describe("a variable renamed with a selection", () => {
219
+ it.each(SELECTIONS)(
220
+ "adapters.%s: %s's old variable set alone is refused, naming ADAPTERS_%s",
221
+ (key, name, _oldPath, oldName, value) => {
222
+ const err = refusal({ [oldName]: value });
223
+ expect(err).toBeInstanceOf(RangeError);
224
+ expect(err.message).toContain(`${oldName} was renamed ADAPTERS_${name}`);
225
+ expect(err.message).toContain(`adapters.${key}`);
226
+ },
227
+ );
228
+
229
+ it.each(SELECTIONS)(
230
+ "adapters.%s: the old variable beside ADAPTERS_%s at a different value is refused, quoting neither",
231
+ (_key, name, _oldPath, oldName) => {
232
+ const err = refusal({ [oldName]: "old-value-5e2d", [`ADAPTERS_${name}`]: "new-value-c81a" });
233
+ expect(err.message).toContain(oldName);
234
+ expect(err.message).not.toContain("old-value-5e2d");
235
+ expect(err.message).not.toContain("new-value-c81a");
236
+ },
237
+ );
238
+
239
+ it.each(SELECTIONS)(
240
+ "adapters.%s: the old variable beside ADAPTERS_%s at the same value is read",
241
+ (key, name, _oldPath, oldName, value) => {
242
+ expect(adaptersFrom({ [oldName]: value, [`ADAPTERS_${name}`]: value })[key]).toBe(value);
243
+ },
244
+ );
245
+ });
246
+
247
+ describe("the modules phase one chooses by the selections", () => {
248
+ const names = (env: Record<string, string>) =>
249
+ buildModules(readSwitches(readOwnLayers(ownFiles(), { env: { ...MFA_OFF, ...env } })), {
250
+ environment: "production",
251
+ }).map((module) => module.name);
252
+
253
+ it.each([
254
+ ["ADAPTERS_RATE_LIMITER", "redis", "redis-rate-limiter", "core-rate-limiter-memory"],
255
+ ["ADAPTERS_RATE_LIMITER", "memory", "core-rate-limiter-memory", "redis-rate-limiter"],
256
+ [
257
+ "ADAPTERS_CODE_REPOSITORY",
258
+ "memory",
259
+ "standalone-in-memory-code-repository",
260
+ "redis-code-repository",
261
+ ],
262
+ [
263
+ "ADAPTERS_CODE_REPOSITORY",
264
+ "redis",
265
+ "redis-code-repository",
266
+ "standalone-in-memory-code-repository",
267
+ ],
268
+ [
269
+ "ADAPTERS_USER_SESSION_STORES",
270
+ "redis",
271
+ "redis-session-stores",
272
+ "standalone-in-memory-session-stores",
273
+ ],
274
+ ["ADAPTERS_CONSENT_STORE", "memory", "core-consent-store-memory", "redis-consent-store"],
275
+ ])("%s=%s installs %s, not %s", (variable, value, installed, absent) => {
276
+ const modules = names({ [variable]: value });
277
+ expect(modules).toContain(installed);
278
+ expect(modules).not.toContain(absent);
279
+ });
280
+ });
281
+
282
+ describe("the federation-grant stores", () => {
283
+ const namesWith = (env: Record<string, string>) =>
284
+ buildModules(
285
+ readSwitches(
286
+ readOwnLayers(ownFiles(), {
287
+ env: { ...MFA_OFF, FEDERATION_GRANTS_ENABLED: "true", ...env },
288
+ }),
289
+ ),
290
+ { environment: "production" },
291
+ ).map((module) => module.name);
292
+ const STORES = [
293
+ "core-federation-grant-store-memory",
294
+ "redis-federation-grant-store",
295
+ "core-federation-grant-intent-store-memory",
296
+ "redis-federation-grant-intent-store",
297
+ ];
298
+
299
+ it("default to none: with the feature on, neither store is installed until one is selected", () => {
300
+ expect(namesWith({}).filter((name) => STORES.includes(name))).toEqual([]);
301
+ });
302
+
303
+ it("read none from ADAPTERS_FEDERATION_GRANT_STORE and ADAPTERS_FEDERATION_GRANT_INTENT_STORE", () => {
304
+ expect(
305
+ adaptersFrom({
306
+ ADAPTERS_FEDERATION_GRANT_STORE: "none",
307
+ ADAPTERS_FEDERATION_GRANT_INTENT_STORE: "none",
308
+ }),
309
+ ).toMatchObject({ federationGrantStore: "none", federationGrantIntentStore: "none" });
310
+ });
311
+
312
+ it.each([
313
+ ["memory", "core-federation-grant-store-memory", "core-federation-grant-intent-store-memory"],
314
+ ["redis", "redis-federation-grant-store", "redis-federation-grant-intent-store"],
315
+ ])("install the %s stores once selected", (value, grants, intents) => {
316
+ const names = namesWith({
317
+ ADAPTERS_FEDERATION_GRANT_STORE: value,
318
+ ADAPTERS_FEDERATION_GRANT_INTENT_STORE: value,
319
+ });
320
+ expect(names).toContain(grants);
321
+ expect(names).toContain(intents);
322
+ });
323
+ });
324
+
325
+ describe("boot and the section", () => {
326
+ it("hands boot no adapters section: the composition root consumed it", () => {
327
+ const own = readOwnLayers(ownFiles(), { env: MFA_OFF });
328
+ const switches = readSwitches(own);
329
+ const resolved = resolveForBoot(
330
+ own,
331
+ buildModules(switches, { environment: "production" }),
332
+ switches,
333
+ );
334
+ expect(resolved).not.toHaveProperty("adapters");
335
+ });
336
+
337
+ it.each([
338
+ ["named adapters", "adapters", undefined],
339
+ ["with its section at adapters.custom", "custom-thing", "adapters.custom"],
340
+ ] as const)("refuses a module %s: its section would never reach boot", (_label, name, at) => {
341
+ const own = readOwnLayers(ownFiles(), { env: MFA_OFF });
342
+ const switches = readSwitches(own);
343
+ const mine = defineModule({
344
+ name,
345
+ section: {
346
+ schema: z.object({}).passthrough().optional(),
347
+ ...(at === undefined ? {} : { at }),
348
+ },
349
+ });
350
+ expect(() =>
351
+ resolveForBoot(
352
+ own,
353
+ [...buildModules(switches, { environment: "production" }), mine],
354
+ switches,
355
+ ),
356
+ ).toThrow(new RegExp(`"${name}".*adapters`));
357
+ });
358
+ });