@o3co/create-auth-provider 0.9.0 → 0.11.0

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 (37) hide show
  1. package/dist/index.d.mts +20 -0
  2. package/dist/index.d.mts.map +1 -1
  3. package/dist/index.mjs +110 -7
  4. package/package.json +4 -4
  5. package/templates/standalone/.env.example +49 -5
  6. package/templates/standalone/Dockerfile +59 -16
  7. package/templates/standalone/README.ja.md +92 -13
  8. package/templates/standalone/README.md +358 -18
  9. package/templates/standalone/config/application.conf +132 -4
  10. package/templates/standalone/config/clients.yaml.example +56 -2
  11. package/templates/standalone/config/production.conf +9 -3
  12. package/templates/standalone/docker-compose.production.yml +132 -0
  13. package/templates/standalone/docker-compose.yml +7 -0
  14. package/templates/standalone/gitignore +21 -0
  15. package/templates/standalone/package.json +15 -20
  16. package/templates/standalone/src/__tests__/audit-sink.test.mts +343 -0
  17. package/templates/standalone/src/__tests__/documented-env-overrides.test.mts +423 -0
  18. package/templates/standalone/src/__tests__/google-federation-config-module.test.mts +114 -0
  19. package/templates/standalone/src/__tests__/metrics.test.mts +231 -0
  20. package/templates/standalone/src/__tests__/refresh-token-family-client-module.test.mts +76 -1
  21. package/templates/standalone/src/__tests__/scaffold-assets.test.mts +117 -0
  22. package/templates/standalone/src/__tests__/shutdown.test.mts +244 -0
  23. package/templates/standalone/src/__tests__/smoke.test.mts +213 -11
  24. package/templates/standalone/src/__tests__/terminal-error.test.mts +96 -0
  25. package/templates/standalone/src/__tests__/three-tier-resolution.test.mts +117 -7
  26. package/templates/standalone/src/app.mts +78 -10
  27. package/templates/standalone/src/buildModules.mts +43 -1
  28. package/templates/standalone/src/configPath.mts +2 -2
  29. package/templates/standalone/src/logger.mts +90 -2
  30. package/templates/standalone/src/metrics.mts +190 -0
  31. package/templates/standalone/src/modules.mts +319 -33
  32. package/templates/standalone/src/shutdown.mts +162 -0
  33. package/templates/standalone/src/terminalError.mts +60 -0
  34. package/templates/standalone/tests/index.test.js +69 -5
  35. package/templates/standalone/tsconfig.json +9 -1
  36. package/templates/standalone/vitest.config.mts +8 -0
  37. package/templates/versions.json +12 -11
@@ -4,15 +4,58 @@ Deployable server template for auth.provider. This is the composition root — i
4
4
 
5
5
  ## Requirements
6
6
 
7
- - **Node.js** `>=18.19.0`
8
- - **`bcrypt` native binary**: this template depends transitively on `bcrypt@6.x`, which ships **prebuilt N-API binaries** for `darwin-arm64`, `darwin-x64`, `linux-x64` (glibc and musl), `linux-arm64` (glibc and musl), `linux-arm`, `win32-x64`, and `win32-arm64`. On these platforms `pnpm install` succeeds without compiling from source, and no extra toolchain is required (the `darwin-*`, `node:*-alpine`, and `node:*-bookworm` images all match a shipped prebuild). On platforms or libc/arch combinations that have no matching prebuild, `pnpm install` falls back to compiling, which then requires a C++ compiler and Python (the standard Node.js native-addon toolchain): Debian/Ubuntu `apt-get install build-essential python3`, Alpine `apk add make g++ python3`, macOS `xcode-select --install`. The pnpm 10 `onlyBuiltDependencies` allowlist for `bcrypt` is set in this template's `package.json` so a fresh `pnpm install` does not silently skip the install hook on those platforms.
7
+ - **Node.js** `>=22.0.0`
8
+ - **`bcrypt` native binary**: this template depends transitively on `bcrypt@6.x`, which ships **prebuilt N-API binaries** for `darwin-arm64`, `darwin-x64`, `linux-x64` (glibc and musl), `linux-arm64` (glibc and musl), `linux-arm`, `win32-x64`, and `win32-arm64`. On these platforms `pnpm install` succeeds without compiling from source, and no extra toolchain is required (the `darwin-*`, `node:*-alpine`, and `node:*-bookworm` images all match a shipped prebuild). On platforms or libc/arch combinations that have no matching prebuild, `pnpm install` falls back to compiling, which then requires a C++ compiler and Python (the standard Node.js native-addon toolchain): Debian/Ubuntu `apt-get install build-essential python3`, Alpine `apk add make g++ python3`, macOS `xcode-select --install`. The pnpm 10 `onlyBuiltDependencies` allowlist for `bcrypt` lives in `pnpm-workspace.yaml` — pnpm ≥10.29 reads it only from there, in single-package projects too. `create-auth-provider` writes that file into the project it scaffolds (inside this monorepo, the allowlist sits in the workspace root's `pnpm-workspace.yaml`); without it a fresh `pnpm install` silently skips the install hook on those platforms.
9
9
  - **Redis 7.2 LTS or later** for any Redis-backed adapter (refresh token family store, code repository, federation token store, session store). The `pExpireGT` flag pair on which several adapters depend was introduced in Redis 7.0+; 7.2 LTS is the tested floor. Tested against AWS ElastiCache for Redis 7.2, Upstash Redis, Redis Cloud 7.2, and self-managed `redis:7.2-alpine`.
10
10
  - **ioredis** `^5.4.1` (direct runtime dependency, used by the refresh-token-family client). Other Redis-backed adapters in this template still use the `redis` npm package; ioredis is added for the RT family store specifically.
11
11
 
12
+ ## First-party clients
13
+
14
+ `GET /authorize` mints an authorization code as soon as the user's session is authenticated. There is **no consent step**: the user is never asked whether this client may have their identity.
15
+
16
+ That is a coherent model for a **first-party** OP — one where every registered client is operated by you — and it is coherent nowhere else. A page you do not control can force a top-level navigation to `/authorize?client_id=X&redirect_uri=...&code_challenge=<theirs>`, a logged-in user's browser mints a code bound to *their* session, it is delivered to X's registered `redirect_uri`, and whoever chose the `code_challenge` redeems it for tokens carrying that user's identity.
17
+
18
+ So the assumption is enforced rather than assumed. A client must carry `firstParty: true` in its registration to use `/authorize`; anything else is refused with `unauthorized_client`. Mark a client first-party only if you would be comfortable with it receiving a user's identity **without the user being asked**.
19
+
20
+ ```yaml
21
+ # config/clients.yaml
22
+ my-app:
23
+ tokenEndpointAuthMethod: "client_secret_basic"
24
+ firstParty: true
25
+ allowedRedirectUris: ["https://app.example/callback"]
26
+ ```
27
+
28
+ **What this does not do.** It does not make `/authorize` safe against forced navigation for a client that *is* first-party — that remains the accepted model here. What it prevents is a client that should never have been trusted with a silent code being registered into that position by accident. The step that changes the former is a consent screen, tracked separately.
29
+
30
+ **Migrating.** Registrations that predate the field are refused at `/authorize` until you mark them — a client with no `firstParty` field and one carrying an explicit `false` are treated alike. Mark every client you operate before pointing it at `/authorize`; there is no admit-with-warning window. The one-time `oauth.authorize.allowUnmarkedClients` migration flag was removed before it ever shipped in a release, and a config or environment still setting it (`OAUTH_AUTHORIZE_ALLOW_UNMARKED_CLIENTS`, any value) fails at boot with migration instructions rather than being silently ignored.
31
+
12
32
  ## Multi-replica deployments
13
33
 
14
34
  When running multiple instances of the standalone server behind a load balancer, point `REFRESH_TOKEN_FAMILY_STORE_REDIS_URL` at a **shared** Redis 7.2+ instance. Without a shared Redis URL, every replica holds refresh-token families in its own connection (and any local-only Redis), so a refresh request that lands on a replica that did not issue the token returns `invalid_grant` on every other request in a round-robin LB.
15
35
 
36
+ **Set `HTTP_TRUST_PROXY` to your proxy's address behind a load balancer.** It defaults to `false`, which makes `req.ip` the *load balancer's* address rather than the client's. Every rate limit keyed by IP — the OAuth endpoint limiters and the `POST /session/login` brute-force guard — then shares **one bucket across all users**: the first 20 login attempts from anyone exhaust the window and every subsequent user gets `429`. The failure reads like an attack rather than a misconfiguration, which is what makes it expensive to diagnose at 3am. Set it whenever anything terminates TLS or proxies in front of this service, and make sure that hop is the one setting `X-Forwarded-For`.
37
+
38
+ Since #292 it takes four shapes, all of them Express's own:
39
+
40
+ | Value | Meaning |
41
+ |---|---|
42
+ | `false` (default) | Trust nothing; `req.ip` is the socket peer |
43
+ | `10.0.0.0/8,192.168.0.0/16` | Trust exactly these hops. IP literals, CIDR ranges, or the named ranges `loopback` / `linklocal` / `uniquelocal`. Comma-separate for the env var |
44
+ | `1` | Trust that many hops back from the socket peer |
45
+ | `true` | Trust every hop — the leftmost `X-Forwarded-For` entry, from whoever opened the connection |
46
+
47
+ **Prefer the address list.** `true` believes a forwarded client address from anyone who can reach this process; a client that routes to it directly, or through one hop more than you accounted for, then chooses its own `req.ip` and with it any rate-limit identity keyed on it. Entries are validated at boot, so a hostname or a typo'd range fails loudly instead of becoming a rule that never matches.
48
+
49
+ An allowlist is a network control, not a cryptographic one. The edge must also **strip** inbound `X-Forwarded-*` headers rather than appending to them, and the hop between it and this process must not be reachable by anyone able to spoof a source address.
50
+
51
+ **Point `rateLimiter.adapter` at Redis.** It defaults to `"memory"`, which is per-process: with N replicas every configured limit is effectively N times larger and resets on every deploy. The memory adapter is also **evadable under bucket exhaustion**: it caps itself at 10,000 buckets and, at the cap, admitting a new key evicts the bucket closest to reset — so an attacker who can present many source IPs (and `req.ip` is client-influenced whenever `HTTP_TRUST_PROXY` is broader than your actual hops) can churn the table until a target's counter is evicted and starts over. That is acceptable for one dev process; it is not a production rate limit. Since #270 the login guard runs on this same shared component, so one setting covers both the OAuth endpoints and `/session/login`. The login window and limit stay configured at `rateLimit.login`; both adapters seed their own `limits.login` from it, so there is nothing to restate.
52
+
53
+ **Set `DEPLOYMENT_MODE=multi` once you run more than one replica.** Boot then *fails* if any in-memory store that has to be shared is still wired, naming every offender and what it costs — user sessions forking (back-channel logout reaches one replica, a logged-out session stays valid on the others), rate-limit counters multiplying, access-token revocation not propagating, DPoP proof-replay detection forking. With the mode unset you get one `replica_unsafe_adapters` warning at boot instead; with `DEPLOYMENT_MODE=single` the check is silent, because you have said there is one replica.
54
+
55
+ Be aware of what this check *cannot* do: if you scale to N replicas without ever setting `DEPLOYMENT_MODE`, nothing fails. A process holding all its state in its own memory has no shared medium through which to notice peers — the condition is undetectable from inside exactly when it is true. Set the variable as part of scaling, not after something breaks.
56
+
57
+ **Access-token revocation needs a denylist, and the template ships one.** `POST /oauth/revoke` revokes an access token by writing its `jti` to the `accessTokenDenylist`, which token verification and introspection consult. Boot *fails* if the endpoint is mounted with no denylist behind it: RFC 7009 obliges the endpoint to answer `200`, so an unwired denylist would tell you a token is revoked while it keeps working until it expires. `accessTokenDenylist.adapter` defaults to `"redis"` here (`ACCESS_TOKEN_DENYLIST_ADAPTER=memory` for single-instance local work) and shares the ioredis socket configured by `REFRESH_TOKEN_FAMILY_STORE_REDIS_URL`, so it costs no extra connection. A deployment that genuinely does not revoke access tokens sets `OAUTH_REVOCATION_ACCESS_TOKEN=unsupported` instead; the endpoint then answers `unsupported_token_type` for `token_type_hint=access_token`. **Refresh-token revocation works in either case and never needed a denylist.**
58
+
16
59
  Other multi-replica considerations covered by the default modules:
17
60
 
18
61
  - The session store is wired via `redisSessionStoresModule`; set the corresponding session-store Redis URL (per `session.storage.redis.url`).
@@ -59,27 +102,55 @@ fail fast rather than silently falling back to defaults.
59
102
  | Variable | Default | Description |
60
103
  |---|---|---|
61
104
  | `HTTP_PORT` | `3000` | Port the server listens on |
62
- | `HTTP_TRUST_PROXY` | `false` | Set to `true` when running behind a reverse proxy |
105
+ | `HTTP_TRUST_PROXY` | `false` | Express `trust proxy`: `false`, an address/CIDR list (`10.0.0.0/8,loopback`), a hop count (`1`), or `true`. **Required behind a load balancer** — otherwise every IP-keyed rate limit shares one bucket across all users. Prefer naming the proxy over `true`. See [Multi-replica deployments](#multi-replica-deployments) |
106
+ | `HTTP_READINESS_TIMEOUT_MS` | `1000` | Per-probe deadline for `/readyz` (see [Health endpoints](#health-endpoints)) |
107
+ | `LOG_LEVEL` | `info` | Minimum level emitted: `trace`\|`debug`\|`info`\|`warn`\|`error`\|`fatal`\|`silent`. Does **not** gate the audit trail — see [Audit trail](#audit-trail) |
108
+ | `AUDIT_SINK_TYPE` | `logger` | Where security events go: `logger` (pino-enveloped NDJSON on stdout) or `console` (bare event JSON). There is no `none`. See [Audit trail](#audit-trail) |
63
109
 
64
110
  ### OAuth JWT
65
111
 
66
112
  | Variable | Default | Description |
67
113
  |---|---|---|
68
- | `OAUTH_JWT_ALGORITHM` | `HS256` | JWT signing algorithm (e.g. `RS256`, `ES256`) |
69
- | `OAUTH_JWT_SECRET` | — | Signing secret (HMAC algorithms) |
70
- | `OAUTH_JWT_ISSUER` | — | JWT `iss` claim value |
114
+ | `OAUTH_JWT_ALGORITHM` | `EdDSA` | JWT signing algorithm: `EdDSA`, `ES256`, `RS256` or `HS256`. The default is asymmetric so `/.well-known/jwks.json` publishes a real verification key. |
115
+ | `OAUTH_JWT_SECRET` | — | Signing secret, **HMAC (`HS256`) only**. At least 32 bytes (256 bits) of random material — `openssl rand -hex 32`. Hex/base64 values are measured **decoded**, so a 32-character hex string counts as 16 bytes and is refused. |
116
+ | `OAUTH_JWT_ISSUER` | **(required)** | Canonical issuer URL stamped as `iss` on every token. Must be absolute `https` (`http` only for a loopback host), with no query or fragment. Boot fails when unset — it is never derived from the `Host` header. |
117
+ | `OAUTH_REQUIRE_EMAIL_VERIFIED` | `false` | Refuse to issue tokens for a user until the Store publishes `emailVerified: true`. Enforced at `/authorize` and on the `session` grant. Verification itself is the Store's job — this only reads the result. |
71
118
  | `OAUTH_JWT_KID` | `v0` | Key ID included in the JWT header |
72
119
  | `OAUTH_JWT_PRIVATE_KEY` | — | PEM-encoded private key (asymmetric algorithms) |
73
120
  | `OAUTH_JWT_PRIVATE_KEY_PATH` | — | Path to PEM private key file |
74
121
  | `OAUTH_JWT_PUBLIC_KEY` | — | PEM-encoded public key |
75
122
  | `OAUTH_JWT_PUBLIC_KEY_PATH` | — | Path to PEM public key file |
123
+ | `OAUTH_JWT_LEGACY_TYP_ACCEPT` | `false` | Accept tokens whose `typ` header is absent. `false` rejects them, treating a typ-less token as the misconfiguration or downgrade attempt it usually is. Set `true` only for a bounded migration window while v0.4.x tokens are still in circulation. |
124
+
125
+ **Signing keys are required.** The default algorithm is `EdDSA` and there is no
126
+ key-material default: a deployment that sets none fails at boot naming the keys
127
+ to set, rather than quietly signing with something guessable. Generate an
128
+ Ed25519 pair:
129
+
130
+ ```bash
131
+ openssl genpkey -algorithm ed25519 -out jwt-private.pem
132
+ openssl pkey -in jwt-private.pem -pubout -out jwt-public.pem
133
+ ```
134
+
135
+ then point `OAUTH_JWT_PRIVATE_KEY_PATH` / `OAUTH_JWT_PUBLIC_KEY_PATH` at them
136
+ (or inline the PEMs via `OAUTH_JWT_PRIVATE_KEY` / `OAUTH_JWT_PUBLIC_KEY`).
137
+
138
+ `HS256` is still selectable (`OAUTH_JWT_ALGORITHM=HS256` plus a ≥32-byte
139
+ `OAUTH_JWT_SECRET`), but understand what it costs: a symmetric key has no
140
+ public half, so `/.well-known/jwks.json` answers `404 jwks_not_published` and
141
+ every relying party must be handed the shared secret — which also lets it
142
+ **mint** tokens, not merely verify them.
76
143
 
77
144
  ### Token Expiry
78
145
 
79
146
  | Variable | Default | Description |
80
147
  |---|---|---|
81
- | `OAUTH_ACCESS_TOKEN_EXPIRES_IN` | `3600` | Access token lifetime in seconds |
82
- | `OAUTH_REFRESH_TOKEN_EXPIRES_IN` | `86400` | Refresh token lifetime in seconds |
148
+ | `OAUTH_ACCESS_TOKEN_EXPIRES_IN` | `3600` | Access token lifetime in seconds. Whole positive number, at most one year (`31536000`). |
149
+ | `OAUTH_REFRESH_TOKEN_EXPIRES_IN` | `86400` | Refresh token lifetime in seconds. Whole positive number, at most one year (`31536000`). |
150
+
151
+ Exporting one of these as an empty string is a boot failure, not a fallback:
152
+ HOCON resolves `FOO=` to `""`, which coerces to `0`, and a zero lifetime mints
153
+ tokens that have already expired.
83
154
 
84
155
  ### Grant Types
85
156
 
@@ -93,12 +164,13 @@ fail fast rather than silently falling back to defaults.
93
164
 
94
165
  | Variable | Default | Description |
95
166
  |---|---|---|
96
- | `SESSION_SECRET` | — | **Required.** Session signing secret |
167
+ | `SESSION_SECRET` | — | **Required.** Signs the cookie that *is* the authenticated session, so guessing it forges logins. At least 32 bytes (256 bits) — `openssl rand -hex 32`. Measured **decoded**, same rule as `OAUTH_JWT_SECRET`. |
97
168
  | `SESSION_NAME` | `__Host-auth.session` | Session cookie name. The default uses the `__Host-` prefix and therefore requires `SESSION_SECURE=true` and no `SESSION_DOMAIN`. |
98
- | `SESSION_MAX_AGE` | `3600000` | Session cookie max age in milliseconds |
169
+ | `SESSION_MAX_AGE` | `3600000` | Session cookie max age in milliseconds. Whole positive number, at most one year (`31536000000`). |
99
170
  | `SESSION_SECURE` | `true` | Set `Secure` flag on session cookie |
100
- | `SESSION_SAME_SITE` | `lax` | `SameSite` attribute (`lax`, `strict`, `none`) |
171
+ | `SESSION_SAME_SITE` | `lax` | `SameSite` attribute (`lax`, `strict`, `none`). `none` **requires** `SESSION_SECURE=true` — browsers drop a `SameSite=None` cookie that is not `Secure`, so boot refuses the combination rather than letting every login fail silently in the client. |
101
172
  | `SESSION_DOMAIN` | — | Cookie domain (unset by default) |
173
+ | `SESSION_CSRF_TTL_SECONDS` | `7200` | Lifetime of an issued CSRF token, in seconds. Integer, 1–86400; boot fails otherwise (an *empty* value coerces to `0` and would silently disable the token arm). |
102
174
  | `SESSION_STORAGE_TYPE` | `redis` | Session store backend: `redis` or `memory` |
103
175
  | `SESSION_STORAGE_REDIS_URL` | `redis://localhost:6379` | Redis connection URL for session storage |
104
176
  | `SESSION_STORAGE_REDIS_PASSWORD` | — | Redis password for session storage |
@@ -108,6 +180,25 @@ for shared-domain cookies, also set `SESSION_NAME` to a non-`__Host-` value such
108
180
  `auth.sid`. The server fails fast when a `__Host-` cookie name is combined with
109
181
  attributes that browsers reject for that prefix.
110
182
 
183
+ #### CSRF on `/session/login` and `/session/logout`
184
+
185
+ Both routes accept a request carrying **either** a same-origin (or trusted)
186
+ `Origin` / `Referer`, **or** a valid double-submit CSRF token, and reject one
187
+ carrying neither (#272). Browsers satisfy this automatically; a scripted client
188
+ calls `GET /session/csrf` first and sends the returned `csrf_token` back as
189
+ both the `<SESSION_NAME>.csrf` cookie and an `x-csrf-token` header (or a
190
+ `csrf_token` form field).
191
+
192
+ Two configuration notes:
193
+
194
+ - Behind a TLS-terminating proxy, set `HTTP_TRUST_PROXY` (to the proxy's
195
+ address or CIDR range, not `true`). Without it `req.protocol` reads `http`
196
+ while the browser sends `Origin: https://…`, and the origin arm rejects every
197
+ request.
198
+ - If your login UI is served from a **different origin** than the provider,
199
+ list that origin under `session.csrf.trustedOrigins` in your HOCON config.
200
+ `cors.allowedOrigins` no longer confers CSRF trust.
201
+
111
202
  ### Google Federation
112
203
 
113
204
  | Variable | Default | Description |
@@ -129,9 +220,19 @@ attributes that browsers reject for that prefix.
129
220
  | Variable | Default | Description |
130
221
  |---|---|---|
131
222
  | `CLIENT_USER_TYPE` | `http` | User repository backend: `http` |
132
- | `CLIENT_USER_AUTHENTICATE_URL` | — | URL for password-based user authentication |
133
- | `CLIENT_USER_AUTHENTICATE_BY_TOKEN_URL` | — | URL for token-based user authentication |
134
- | `CLIENT_USER_TIMEOUT` | `5000` | HTTP request timeout in milliseconds |
223
+ | `CLIENT_USER_AUTHENTICATE_URL` | — | URL for password-based user authentication. **https required** (see below) |
224
+ | `CLIENT_USER_AUTHENTICATE_BY_TOKEN_URL` | — | URL for token-based user authentication. **https required** (see below) |
225
+ | `CLIENT_USER_TIMEOUT` | `5000` | HTTP request timeout in milliseconds. Positive integer ≤ `2147483647` |
226
+ | `CLIENT_USER_MAX_RESPONSE_BYTES` | `1048576` | Largest upstream response body accepted, in bytes |
227
+
228
+ Both user-authentication URLs carry **plaintext user credentials** to the
229
+ upstream store, so both must be absolute `https://` URLs. `http://` is accepted
230
+ only for a loopback host — `localhost`, an address in `127.0.0.0/8`, or `[::1]`
231
+ — so that local development needs no certificate. A private-range address
232
+ (`http://10.0.0.5/…`) or a container-network service name
233
+ (`http://user-service/…`) still requires `https://`: those cross a network the
234
+ deployment does not control end to end. Boot fails if either URL, the timeout,
235
+ or the response cap is unusable, rather than at the first login attempt.
135
236
 
136
237
  ### Code Repository
137
238
 
@@ -160,9 +261,12 @@ Use values that include the deployment name, for example `tenant-a:ss:`,
160
261
 
161
262
  | Variable | Default | Description |
162
263
  |---|---|---|
163
- | `ENDPOINTS_LOGIN_URL` | — | URL of the login page (for redirects) |
164
- | `ENDPOINTS_CLIENT_URL` | — | URL of the client application |
165
- | `ENDPOINTS_AUTH_CALLBACK_URL` | — | URL of the authorization callback |
264
+ | `ENDPOINTS_LOGIN_URL` | `/login` | URL of the login page (for redirects) |
265
+
266
+ `ENDPOINTS_CLIENT_URL` and `ENDPOINTS_AUTH_CALLBACK_URL` were removed (IH-10) —
267
+ no production consumer read them, and the config lines leaked values into
268
+ `AppConfig` that nothing consumed. Federation callback URLs are configured
269
+ per-provider (`FEDERATIONS_GOOGLE_CALLBACK_URL`).
166
270
 
167
271
  ## Module Composition Order
168
272
 
@@ -205,6 +309,37 @@ make test
205
309
 
206
310
  The `docker-compose.yml` starts the auth server together with a Redis container. Configure environment variables in `.env`.
207
311
 
312
+ `docker-compose.yml` is a **development** file: it builds the `develop` target and bind-mounts `./src` and `./config`, so deploying it ships a hot-reload server running your working tree. The deployable shape is
313
+ [`docker-compose.production.yml`](docker-compose.production.yml) — `runtime` target, no source mounts, restart policies, a network-internal persistent Redis, and a **required** `.env` (a boot without a real `SESSION_SECRET` must fail loudly). Its header comments state what it deliberately leaves to you: TLS termination in front plus `HTTP_TRUST_PROXY`, and the [multi-replica](#multi-replica-deployments) steps before any `--scale`.
314
+
315
+ ```bash
316
+ # The signing keys are a required input: EdDSA is the default and there is no
317
+ # key-material default, so a deployment that generates none fails at boot.
318
+ openssl genpkey -algorithm ed25519 -out jwt-private.pem
319
+ openssl pkey -in jwt-private.pem -pubout -out jwt-public.pem
320
+ chmod 600 jwt-private.pem
321
+
322
+ docker compose -f docker-compose.production.yml up -d --build
323
+ ```
324
+
325
+ **Keys in containers.** The pem pair reaches the container as a compose
326
+ **secret**, mounted read-only at `/run/secrets/`, and the compose file points
327
+ `OAUTH_JWT_*_KEY_PATH` there. Do not park them in `config/`: the `Dockerfile`
328
+ `COPY`s that directory, so a key placed there is baked into an image layer and
329
+ travels with every push — a key you then have to rotate rather than one you
330
+ merely stored badly. The same applies to `.env`; keep both out of git.
331
+
332
+ The production file also pins `SESSION_SECURE=true` and the `__Host-` cookie
333
+ name in its `environment:` block, which wins over `env_file`. `.env.example`
334
+ ships `SESSION_SECURE=false` so a plain-HTTP local run works, and this file
335
+ requires that same `.env` — behind the TLS it assumes, a non-Secure session
336
+ cookie is the session handed to anyone who can read one plaintext hop.
337
+
338
+ The image installs with `pnpm install --frozen-lockfile`, so a committed
339
+ `pnpm-lock.yaml` is a required build input — `create-auth-provider` generates
340
+ one at scaffold time; if yours is missing, run `pnpm install` once and commit
341
+ the result. Rebuilding the same source then produces the same dependency tree.
342
+
208
343
  The production image sets `ENV HTTP_PORT=3000` and uses it for both `EXPOSE`
209
344
  and the Docker-native healthcheck against `/_healthcheck`. The HOCON config
210
345
  also reads `${?HTTP_PORT}` for `http.port`, so `app.listen()` and the
@@ -217,6 +352,78 @@ docker run -e HTTP_PORT=8080 -p 8080:8080 my-auth-provider
217
352
  `EXPOSE` is image metadata and does not publish ports by itself, so the
218
353
  explicit `-p` mapping is still required.
219
354
 
355
+ ### Health endpoints
356
+
357
+ Two routes, answering two different questions. Both are mounted on the host
358
+ app ahead of the auth router, so they keep answering while the auth pipeline
359
+ is degraded.
360
+
361
+ | Route | Question | Answer |
362
+ |---|---|---|
363
+ | `GET /_healthcheck` | Is the process up? | Always `200 {"status":"ok"}` |
364
+ | `GET /readyz` | Can this replica serve right now? | `200 {"status":"ready", …}` or `503 {"status":"unready", …}` |
365
+
366
+ Use them for different probes:
367
+
368
+ ```yaml
369
+ livenessProbe:
370
+ httpGet: { path: /_healthcheck, port: 3000 }
371
+ readinessProbe:
372
+ httpGet: { path: /readyz, port: 3000 }
373
+ ```
374
+
375
+ Pointing liveness at `/readyz` is the mistake worth avoiding: a Redis
376
+ partition would then look like a broken container and Kubernetes would
377
+ restart every replica in a loop, which reconnects nothing and adds cold
378
+ starts to an incident. Losing Redis is a reason to stop *routing* to a
379
+ replica, not to kill it.
380
+
381
+ The image's `HEALTHCHECK` probes `/_healthcheck` for the same reason. Docker
382
+ has exactly one health signal, and on Swarm and ECS an unhealthy container is
383
+ stopped and replaced — so wiring it to `/readyz` would reintroduce the restart
384
+ loop for every non-Kubernetes orchestrator. If you want
385
+ `depends_on: condition: service_healthy` to mean "ready" rather than "alive",
386
+ override `healthcheck:` for that one service in your compose file instead of
387
+ changing the image.
388
+
389
+ `/readyz` runs one probe per backing connection and reports each by name:
390
+
391
+ ```json
392
+ {
393
+ "status": "unready",
394
+ "checks": [
395
+ { "name": "redis", "ok": true, "durationMs": 2 },
396
+ { "name": "session-store", "ok": false, "durationMs": 1001 }
397
+ ]
398
+ }
399
+ ```
400
+
401
+ The failure's *reason* is deliberately absent from the body and goes to the log
402
+ instead (`readiness_probe_failed`, with the full per-check detail). A driver
403
+ error reads `connect ECONNREFUSED 10.0.3.14:6379` — an internal host and port —
404
+ and this endpoint is unauthenticated because an orchestrator has no credentials
405
+ to present. Set `includeErrorDetail: true` on `createReadinessRouter` only when
406
+ the endpoint is reachable solely from inside the deployment.
407
+
408
+ For the same reason, keep `/readyz` off the public listener. It sits ahead of
409
+ the auth router, so it is outside that router's rate limiter, and each request
410
+ issues one command per probe against the very dependency it reports on —
411
+ enough for an anonymous request loop to add load during an incident. Bind it to
412
+ the internal network, an admin listener, or your ingress' internal-only path
413
+ rules.
414
+
415
+ Probes are registered by whichever builder opened the connection, so the list
416
+ reflects what this deployment actually wired: the shared ioredis client
417
+ (`redis`) and the connect-redis session client (`session-store`) when the
418
+ Redis adapters are selected, and **nothing at all** for a memory-only
419
+ deployment — which is therefore always ready, because it has no dependency to
420
+ be unready for.
421
+
422
+ `http.readinessTimeoutMs` (env `HTTP_READINESS_TIMEOUT_MS`, default `1000`)
423
+ is the per-probe deadline. Keep it below the orchestrator's own probe timeout;
424
+ otherwise an unreachable dependency reads as a slow replica rather than an
425
+ unready one.
426
+
220
427
  ## Adding Custom Modules
221
428
 
222
429
  To add a custom module, import it in `src/buildModules.mts` and include it in the `modules` array passed to `createApp`:
@@ -253,9 +460,21 @@ const handle = await createApp({
253
460
 
254
461
  app.use(handle.router);
255
462
  const server = app.listen(config.http.port);
256
- gracefulShutdown(server, () => handle.dispose());
463
+ installGracefulShutdown(server, { logger, cleanup: () => handle.dispose() });
257
464
  ```
258
465
 
466
+ ### Shutdown guarantees
467
+
468
+ `src/shutdown.mts` is deliberately part of the scaffold rather than a dependency (#290): for the component that terminates every user session, *"does SIGTERM wait for in-flight requests, and for how long?"* has to be answerable from the code you deploy.
469
+
470
+ 1. **`SIGTERM` and `SIGINT`** both start it. A second signal is ignored rather than starting a second dispose over the first one's stores.
471
+ 2. **New connections stop immediately**, and idle keep-alive sockets are released — they hold the server open with no request behind them, so a quiet server would otherwise wait out the whole deadline for nothing.
472
+ 3. **In-flight requests get `drainTimeoutMs`** (default **10s**) to finish.
473
+ 4. **Past the deadline the remaining connections are cut and the process exits non-zero.** An orchestrator that only ever sees `0` cannot tell a clean drain from one that ran out of time.
474
+ 5. **`cleanup` runs after draining, before exit** — `handle.dispose()`, i.e. reverse-topological component cleanup plus the Redis/timer drain. A failure there is logged through this service's own logger (NDJSON, like every other line) and reflected in the exit code. A dispose that throws still exits; it never wedges the process.
475
+
476
+ **Size `drainTimeoutMs` below your orchestrator's kill grace period** — Kubernetes `terminationGracePeriodSeconds` and compose `stop_grace_period` are both 30s by default. The point is to close on your terms before `SIGKILL` arrives on someone else's.
477
+
259
478
  ## npm Scripts
260
479
 
261
480
  | Script | Description |
@@ -272,3 +491,124 @@ gracefulShutdown(server, () => handle.dispose());
272
491
  - [`@o3co/auth-provider-session`](../../packages/session) — Session module
273
492
  - [`@o3co/auth-provider-foundation`](../../packages/foundation) — Built-in adapter registration
274
493
  - [`@o3co/create-auth-provider`](../../create-app) — CLI scaffolder that generates this template
494
+
495
+ ## Observability
496
+
497
+ ### Logging
498
+
499
+ The provider logs newline-delimited JSON on stdout via [pino](https://getpino.io),
500
+ which is what a log aggregator ingests without a parser. `LOG_LEVEL` (HOCON
501
+ `logging.level`) sets the threshold; `trace` and `debug` carry request-shaped
502
+ detail and are off by default, and pino drops sub-threshold calls before
503
+ formatting, so they cost nothing in production rather than being emitted and
504
+ filtered downstream.
505
+
506
+ The logger is wired into the boot planner's `logger` component slot, so every
507
+ module that declares `optional: ["logger"]` — oauth, session, dpop, mtls,
508
+ token-exchange — logs through this one instance. Filling that slot is what makes
509
+ `LOG_LEVEL` reach them; a composition root that leaves it empty gets each
510
+ module's own `consoleLogger` default instead, and configuring a level changes
511
+ nothing.
512
+
513
+ Events are structured and named: `logger.error({ err }, "session_store_redis_error")`.
514
+ Alert on the name, not on the message text.
515
+
516
+ To swap the backend, replace `createAppLogger` in `src/logger.mts`. Anything
517
+ satisfying core's `Logger` works — the interface has pino's two-overload call
518
+ shape, so a pino-compatible logger needs no adapter.
519
+
520
+ ### Audit trail
521
+
522
+ Security-relevant events — who authenticated, what was issued, what was refused,
523
+ and when the rate limiter could not answer — go to the sink named by
524
+ `audit.sink.type` (env `AUDIT_SINK_TYPE`). The template ships `"logger"`, which
525
+ writes one event per line through the same pino stream as everything else:
526
+
527
+ ```json
528
+ {"level":30,"time":1787841564013,"name":"audit","audit":{"timestamp":"2026-08-27T14:39:24.013Z","type":"token.issued.failure","ip":"203.0.113.7","details":{"reason":"unsupported_grant_type"}},"msg":"token.issued.failure"}
529
+ ```
530
+
531
+ One output style, so an aggregator ingests application logs and audit events
532
+ through one parser and separates them on `name` (`"provider"` vs `"audit"`).
533
+ The event type is also the message, so alert on the name — `authorize.rejected`,
534
+ `token.issued.failure`, `rate_limit.unavailable` — the same way you would on
535
+ `session_store_redis_error`.
536
+
537
+ `AUDIT_SINK_TYPE=console` selects core's built-in sink instead: the bare event
538
+ as one JSON object per line, with no log envelope, for a pipeline that wants the
539
+ event and nothing else.
540
+
541
+ **`LOG_LEVEL` does not gate the audit trail.** The audit stream's level is fixed
542
+ at `info`. `warn` is an ordinary production setting and `silent` a legitimate
543
+ one, and either silencing this would delete the record of every authentication
544
+ and issuance while looking like a logging preference. Where audit events go is
545
+ chosen by `audit.sink.type` — and that selector has **no `"none"`**. An unknown
546
+ type fails boot naming the sinks that are registered, rather than producing a
547
+ deployment with no audit trail.
548
+
549
+ To point it at a real sink — SIEM, log pipeline, message bus — register a
550
+ builder in `auditSinkModule` (`src/modules.mts`) and name it in config; its
551
+ options ride along in the same block:
552
+
553
+ ```ts
554
+ factory.register("splunk-hec", (cfg) => createSplunkSink(cfg));
555
+ ```
556
+
557
+ ```hocon
558
+ audit {
559
+ sink {
560
+ type = "splunk-hec"
561
+ splunk-hec { endpoint = ${?SPLUNK_HEC_URL}, token = ${?SPLUNK_HEC_TOKEN} }
562
+ }
563
+ }
564
+ ```
565
+
566
+ Sinks are fire-and-forget by contract: core dispatches without awaiting and
567
+ swallows rejections, so a slow or failing sink cannot add latency to — or
568
+ fail — an auth flow. The flip side is that a sink which cannot deliver drops
569
+ events silently, and those drops are not counted yet — see **Not published
570
+ yet** under Metrics below.
571
+
572
+ ### Metrics
573
+
574
+ `GET /metrics` serves the Prometheus text exposition format.
575
+
576
+ | Metric | Type | What it answers |
577
+ |---|---|---|
578
+ | `http_request_duration_seconds` | histogram | Request rate, error rate and latency, by `method` / `route` / `status` |
579
+ | `auth_dependency_up` | gauge | Whether each backing dependency answered its readiness probe (`dependency="redis"`, `"session-store"`) |
580
+ | `auth_provider_*` | various | Node process defaults — event-loop lag, heap, GC, handles |
581
+
582
+ `auth_dependency_up` is the series that separates "Redis is gone" from "the app
583
+ is slow", which is otherwise only distinguishable by grepping container logs. It
584
+ reuses the probes the adapter builders already registered, so it reflects what
585
+ this deployment actually wired rather than a second, drifting list. Dependencies
586
+ are re-sampled on every scrape — a cached verdict would report healthy for
587
+ exactly as long as an incident lasts — and in-flight probes are joined rather
588
+ than re-issued, so a scrape loop does not stack commands on a dependency that is
589
+ already in trouble.
590
+
591
+ Route labels use the Express route **pattern** (`/oauth/token`,
592
+ `/api/widgets/:id`), never the URL, and unmatched requests collapse to
593
+ `route="unmatched"`. Labelling by path would mint a series per distinct URL, and
594
+ this server's paths carry opaque values — that is how a metrics endpoint takes
595
+ down the monitoring that was supposed to watch it.
596
+
597
+ A scrape example:
598
+
599
+ ```yaml
600
+ scrape_configs:
601
+ - job_name: auth-provider
602
+ static_configs:
603
+ - targets: ["auth-provider:3000"]
604
+ ```
605
+
606
+ Keep `/metrics` off the public listener, for the same reason as `/readyz`: it is
607
+ unauthenticated, it sits ahead of the auth router and so outside its rate
608
+ limiter, and each scrape samples the readiness probes.
609
+
610
+ **Not published yet:** rate-limiter fail-closed counts and audit-sink drops.
611
+ Both happen inside `@o3co/auth-provider-core` and are not observable from the
612
+ composition root; counting them needs a metrics hook in core rather than
613
+ something this template can reach. Until then, alert on the
614
+ `rate_limiter_failed_closed` and `rate_limit.unavailable` log events.