@softistx/alxia-clavix 0.0.0-stage → 0.3.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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +122 -2
  3. package/dist/clavix.d.ts +85 -0
  4. package/dist/clavix.d.ts.map +1 -0
  5. package/dist/dpop/htu.d.ts +11 -0
  6. package/dist/dpop/htu.d.ts.map +1 -0
  7. package/dist/dpop/index.d.ts +6 -0
  8. package/dist/dpop/index.d.ts.map +1 -0
  9. package/dist/dpop/jwk.d.ts +20 -0
  10. package/dist/dpop/jwk.d.ts.map +1 -0
  11. package/dist/dpop/nonce.d.ts +29 -0
  12. package/dist/dpop/nonce.d.ts.map +1 -0
  13. package/dist/dpop/options.d.ts +33 -0
  14. package/dist/dpop/options.d.ts.map +1 -0
  15. package/dist/dpop/proof.d.ts +32 -0
  16. package/dist/dpop/proof.d.ts.map +1 -0
  17. package/dist/dpop/replay-store.d.ts +24 -0
  18. package/dist/dpop/replay-store.d.ts.map +1 -0
  19. package/dist/guard.d.ts +19 -0
  20. package/dist/guard.d.ts.map +1 -0
  21. package/dist/index.d.ts +6 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +615 -0
  24. package/dist/index.js.map +22 -0
  25. package/dist/issuers.d.ts +19 -0
  26. package/dist/issuers.d.ts.map +1 -0
  27. package/dist/lru.d.ts +10 -0
  28. package/dist/lru.d.ts.map +1 -0
  29. package/dist/principal.d.ts +25 -0
  30. package/dist/principal.d.ts.map +1 -0
  31. package/dist/scope.d.ts +30 -0
  32. package/dist/scope.d.ts.map +1 -0
  33. package/dist/unverified-issuer.d.ts +3 -0
  34. package/dist/unverified-issuer.d.ts.map +1 -0
  35. package/docs/README.md +14 -0
  36. package/docs/guide/issuers-and-tenants.md +70 -0
  37. package/docs/guide/principal-and-scopes.md +59 -0
  38. package/docs/guide/readiness.md +31 -0
  39. package/docs/guide/security.md +128 -0
  40. package/docs/roadmap.md +38 -0
  41. package/docs/troubleshooting.md +269 -0
  42. package/package.json +52 -5
@@ -0,0 +1,269 @@
1
+ # Troubleshooting
2
+
3
+ Each entry is headed by the text you see: an exception thrown by `clavix` or `requireScope` at
4
+ startup (`verify` never throws), or the body of a 401 or 403.
5
+
6
+ **Startup**
7
+
8
+ - [`TypeError: clavix: give domain, issuers, or both`](#typeerror-clavix-give-domain-issuers-or-both)
9
+ - [`TypeError: clavix: audience is required`](#typeerror-clavix-audience-is-required)
10
+ - [`TypeError: clavix: maxIssuers must be a positive integer`](#typeerror-clavix-maxissuers-must-be-a-positive-integer)
11
+ - [`TypeError: clavix: domain must be an ASCII DNS name such as "clavix.example", got …`](#typeerror-clavix-domain-must-be-an-ascii-dns-name-such-as-clavixexample-got-)
12
+ - [`TypeError: clavix: issuer is not a URL: …`](#typeerror-clavix-issuer-is-not-a-url-)
13
+ - [`TypeError: clavix: issuer must be an https URL: …`](#typeerror-clavix-issuer-must-be-an-https-url-)
14
+ - [`TypeError: clavix: probation must be a positive integer`](#typeerror-clavix-probation-must-be-a-positive-integer)
15
+ - [`TypeError: clavix: the tenant of … is empty`](#typeerror-clavix-the-tenant-of--is-empty)
16
+ - [`TypeError: clavix: warm issuer … is not allowed`](#typeerror-clavix-warm-issuer--is-not-allowed)
17
+ - [`TypeError: clavix: dpop.origin must be an origin such as "https://api.example.com", got …`](#typeerror-clavix-dpoporigin-must-be-an-origin-such-as-httpsapiexamplecom-got-)
18
+ - [`TypeError: clavix: dpop.replay is required`](#typeerror-clavix-dpopreplay-is-required)
19
+ - [`TypeError: clavix: dpop.algorithms takes ES256, EdDSA or both`](#typeerror-clavix-dpopalgorithms-takes-es256-eddsa-or-both)
20
+ - [`TypeError: clavix: dpop.maxAgeSec must be a finite number above 0`](#typeerror-clavix-dpopmaxagesec-must-be-a-finite-number-above-0)
21
+ - [`TypeError: clavix: dpop.clockSkewSec must be a finite number, 0 or more`](#typeerror-clavix-dpopclockskewsec-must-be-a-finite-number-0-or-more)
22
+ - [`TypeError: clavix: the dpop nonce secret needs 16 characters`](#typeerror-clavix-the-dpop-nonce-secret-needs-16-characters)
23
+ - [`TypeError: clavix: the dpop nonce ttlMs must be a finite number above 0`](#typeerror-clavix-the-dpop-nonce-ttlms-must-be-a-finite-number-above-0)
24
+ - [`TypeError: requireScope: give scopes, each a non-empty token without spaces or quotes`](#typeerror-requirescope-give-scopes-each-a-non-empty-token-without-spaces-or-quotes)
25
+
26
+ **Responses**
27
+
28
+ - [`401 { "error": "unauthorized", "reason": "issuer" }`](#401-reason-issuer)
29
+ - [`401 … "reason": "audience"`](#401-reason-audience)
30
+ - [`401 … "reason": "type"`](#401-reason-type)
31
+ - [`401 … "reason": "missing"`](#401-reason-missing)
32
+ - [`401 … "reason": "signature"` or `"algorithm"`](#401-reason-signature-or-algorithm)
33
+ - [`401 … "reason": "malformed"`](#401-reason-malformed)
34
+ - [`401 … "reason": "claims"`](#401-reason-claims)
35
+ - [`401 … "reason": "expired"` or `"not_yet_valid"`](#401-reason-expired-or-not_yet_valid)
36
+ - [`401 … "reason": "key"` or `"keys_unavailable"`](#401-reason-key-or-keys_unavailable)
37
+ - [`401 … "reason": "dpop_required"`, `"dpop_unsupported"`, `"dpop_malformed"` or `"dpop_unbound"`](#401-reason-dpop_required-dpop_unsupported-dpop_malformed-or-dpop_unbound)
38
+ - [`401 … "reason": "dpop_proof_*"`](#401-reason-dpop_proof_)
39
+ - [`401 … "reason": "dpop_nonce"`](#401-reason-dpop_nonce)
40
+ - [`500` on a request with a DPoP proof](#500-on-a-request-with-a-dpop-proof)
41
+ - [`403 { "error": "forbidden", "reason": "insufficient_scope" }`](#403-insufficient_scope)
42
+
43
+ **Traps**
44
+
45
+ - [`/ready` stays 503 or flips back](#ready-stays-503-or-flips-back)
46
+
47
+ ## Startup
48
+
49
+ ### `TypeError: clavix: give domain, issuers, or both`
50
+
51
+ Neither `domain` nor `issuers` was given, or `issuers` is empty (`[]` or `{}`) with no `domain`, so no
52
+ issuer could ever be allowed.
53
+
54
+ ```ts
55
+ import { clavix } from '@softistx/alxia-clavix';
56
+
57
+ clavix({ domain: 'clavix.example.com', audience: 'https://api.example.com' });
58
+ ```
59
+
60
+ ### `TypeError: clavix: audience is required`
61
+
62
+ `audience` is missing or empty. Give the API's identifier, the one clients request tokens for (the
63
+ "Resource identifier" in Clavix). A token's `aud` must contain it exactly.
64
+
65
+ ```ts
66
+ import { clavix } from '@softistx/alxia-clavix';
67
+
68
+ clavix({ domain: 'clavix.example.com', audience: 'https://api.example.com' });
69
+ ```
70
+
71
+ ### `TypeError: clavix: maxIssuers must be a positive integer`
72
+
73
+ `maxIssuers` was `0`, negative, fractional or `NaN`. Leave it out for 100, or give an integer of 1 or more.
74
+
75
+ ### `TypeError: clavix: domain must be an ASCII DNS name such as "clavix.example", got …`
76
+
77
+ `domain` has fewer than two labels, a label that is empty, too long or has characters other than
78
+ letters, digits and hyphens, or an `xn--` label. Give the bare domain, with no scheme, port or path
79
+ (a leading or trailing dot is dropped).
80
+
81
+ ```ts
82
+ import { clavix } from '@softistx/alxia-clavix';
83
+
84
+ clavix({ domain: 'clavix.example.com', audience: 'https://api.example.com' }); // not 'https://clavix.example.com'
85
+ ```
86
+
87
+ ### `TypeError: clavix: issuer is not a URL: …`
88
+
89
+ An entry of `issuers` does not parse as a URL. Give the full issuer, as the token carries it.
90
+
91
+ ### `TypeError: clavix: issuer must be an https URL: …`
92
+
93
+ An entry of `issuers` is not `https`. Only `http` on `localhost`, `127.0.0.1` or `[::1]` is accepted,
94
+ for tests.
95
+
96
+ ### `TypeError: clavix: probation must be a positive integer`
97
+
98
+ `probation` was `0`, negative, fractional or `NaN`. Leave it out for `min(maxIssuers, 16)`, or give an
99
+ integer of 1 or more.
100
+
101
+ ### `TypeError: clavix: the tenant of … is empty`
102
+
103
+ In the `{ issuer: tenant }` form a tenant is `''`. Name it, or use the array form for an issuer with no tenant.
104
+
105
+ ### `TypeError: clavix: warm issuer … is not allowed`
106
+
107
+ A `warm` entry fails the rules a token's issuer would: it is neither listed in `issuers` nor
108
+ `https://<one label>.<domain>`. Check for a trailing slash or a port.
109
+
110
+ ```ts
111
+ import { clavix } from '@softistx/alxia-clavix';
112
+
113
+ clavix({
114
+ domain: 'clavix.example.com',
115
+ audience: 'https://api.example.com',
116
+ warm: ['https://acme.clavix.example.com'], // no trailing slash
117
+ });
118
+ ```
119
+
120
+ ### `TypeError: clavix: dpop.origin must be an origin such as "https://api.example.com", got …`
121
+
122
+ `dpop.origin` is a scheme, host and optional port, no path, query, fragment or userinfo
123
+ (`https://api.example.com`).
124
+
125
+ ### `TypeError: clavix: dpop.replay is required`
126
+
127
+ Give `dpop.replay`: `redisDpopReplayStore(connection.client, { name: 'dpop' })` from
128
+ `@softistx/alxia-redis`, or `memoryDpopReplayStore()` for tests and one instance.
129
+
130
+ ### `TypeError: clavix: dpop.algorithms takes ES256, EdDSA or both`
131
+
132
+ `dpop.algorithms` is a non-empty list of `'ES256'` and `'EdDSA'`.
133
+
134
+ ### `TypeError: clavix: dpop.maxAgeSec must be a finite number above 0`
135
+
136
+ `maxAgeSec` is in seconds, finite and above zero (60 by default).
137
+
138
+ ### `TypeError: clavix: dpop.clockSkewSec must be a finite number, 0 or more`
139
+
140
+ `clockSkewSec` is in seconds, finite, zero or more (5 by default).
141
+
142
+ ### `TypeError: clavix: the dpop nonce secret needs 16 characters`
143
+
144
+ `hmacDpopNonce({ secret })` takes a secret of at least 16 characters, shared by every instance
145
+ (`Bun.env['DPOP_NONCE_SECRET']!`).
146
+
147
+ ### `TypeError: clavix: the dpop nonce ttlMs must be a finite number above 0`
148
+
149
+ `hmacDpopNonce({ ttlMs })` takes a finite number of milliseconds above 0 (default 300000, 5 minutes).
150
+
151
+ ### `TypeError: requireScope: give scopes, each a non-empty token without spaces or quotes`
152
+
153
+ `requireScope()` got no scope, or one with a space, a quote or a backslash. One argument per scope:
154
+ `requireScope('invoices:read', 'invoices:write')`, not `requireScope('invoices:read invoices:write')`.
155
+
156
+ ## Responses
157
+
158
+ ### 401 reason `issuer`
159
+
160
+ The token's `iss` is readable but not allowed. Compare the token's `iss` (decode its payload)
161
+ with your options, byte for byte: a trailing slash (`https://acme.clavix.example.com/`), a port, a
162
+ different host or a second label (`a.b.clavix.example.com`) all refuse. A custom domain needs an entry
163
+ in `issuers`. See [Issuers and tenants](guide/issuers-and-tenants.md). Nothing is fetched for a refused issuer.
164
+
165
+ ### 401 reason `audience`
166
+
167
+ The token's `aud` does not contain `audience`. The client asked Clavix for a token for another
168
+ resource. `audience` must equal the Resource identifier the client requested, character for character
169
+ (scheme, host, no extra trailing slash). It is not the request's `Host` header: the API's own host name
170
+ plays no part.
171
+
172
+ ### 401 reason `type`
173
+
174
+ The token's header `typ` is not `at+jwt`: it is an ID token or another kind of JWT, not an RFC 9068
175
+ access token. The client must send the access token, not the ID token.
176
+
177
+ ### 401 reason `missing`
178
+
179
+ No `Authorization: Bearer <token>` header (and no cookie, when `bearer({ cookie })` is set), or an `Authorization: Bearer`/`DPoP` header with no parsable token (the cookie is then not read). The client must send the access token.
180
+
181
+ ### 401 reason `signature` or `algorithm`
182
+
183
+ `signature`: the token was not signed by a key the issuer publishes (tampered, or signed by another issuer). `algorithm`: its `alg` is not one the verifier accepts (`none` and `HS*` are always refused; see `algorithms`). Ask the client to request a fresh token from Clavix.
184
+
185
+ ### 401 reason `malformed`
186
+
187
+ The value is not a three-part JWT, its payload is not JSON with a string `iss`, an RFC 9068 claim
188
+ (`iss`, `aud`, `exp`, `iat`, `sub`, `client_id`, `jti`) is missing or empty, or `scope`, `roles`,
189
+ `permissions` or `cnf` has the wrong shape (the profile refuses these first, so they are never `claims`). Check the header sends
190
+ `Authorization: Bearer <token>` with nothing else around the token.
191
+
192
+ ### 401 reason `claims`
193
+
194
+ The token is valid, but its issuer has no tenant: it is listed in `issuers` as an array. The body's
195
+ `issues` points at `iss`. Use `{ issuer: tenant }` for such an issuer. Missing or badly shaped claims
196
+ are `malformed`, not `claims`.
197
+
198
+ ### 401 reason `expired` or `not_yet_valid`
199
+
200
+ The token's `exp` has passed, or its `nbf` is in the future. Refresh the token, or check the
201
+ clocks; `clockTolerance` (seconds) allows for small skew.
202
+
203
+ ### 401 reason `key` or `keys_unavailable`
204
+
205
+ `key`: no published key matches the token's `kid` (after one refetch). `keys_unavailable`: the issuer's
206
+ keys could not be fetched and none is cached. Check that the issuer's discovery document is reachable
207
+ from the API (egress, DNS, `timeoutMs`). See
208
+ [`@softistx/alxia-jwt`'s JWKS guide](https://github.com/softistx/alxia/blob/develop/packages/jwt/docs/guide/jwks.md).
209
+
210
+ ### 401 reason `dpop_required`, `dpop_unsupported`, `dpop_malformed` or `dpop_unbound`
211
+
212
+ `WWW-Authenticate: DPoP error="invalid_token"` (with `dpop`). `dpop_required`: the token carries
213
+ `cnf.jkt` and was sent as `Bearer` or in the cookie (no proof), also when `clavix()` has no `dpop`
214
+ option. Send `Authorization: DPoP <token>` with a `DPoP` proof header, and give `clavix()` its `dpop`
215
+ option. `dpop_unsupported`: the `DPoP` scheme was used while `clavix()` has no `dpop` option; add it.
216
+ `dpop_malformed`: the token's `cnf` is not an object, or its `jkt` is present but not a non-empty string; ask Clavix for a new token.
217
+ `dpop_unbound`: the `DPoP` scheme with a token that has no `cnf.jkt`: ask Clavix for a DPoP-bound
218
+ token (send the proof at the token endpoint) or use `Bearer`.
219
+
220
+ ### 401 reason `dpop_proof_*`
221
+
222
+ `WWW-Authenticate: DPoP error="invalid_dpop_proof"`. The suffix names the check that failed:
223
+
224
+ | Reason | Cause and fix |
225
+ | --- | --- |
226
+ | `dpop_proof_missing` | no `DPoP` header: send one proof per request |
227
+ | `dpop_proof_malformed` | not three dot-separated parts of JSON, several proofs in one header, or a `crit` header |
228
+ | `dpop_proof_type` | header `typ` is not exactly `dpop+jwt` (case matters) |
229
+ | `dpop_proof_algorithm` | `alg` is not in `dpop.algorithms` (`ES256`, `EdDSA` by default) |
230
+ | `dpop_proof_key` | the header `jwk` is absent, not a P-256 or Ed25519 public key, or has a private member |
231
+ | `dpop_proof_signature` | the proof is not signed by its own `jwk` |
232
+ | `dpop_proof_method` | `htm` is not the request's method |
233
+ | `dpop_proof_url` | `htu` is not `dpop.origin` plus the path (userinfo, another host or a trailing slash differ); check `origin`, and a proxy that rewrites the path ([#993](https://github.com/softistx/alxia/issues/993)) |
234
+ | `dpop_proof_iat` | `iat` is more than `maxAgeSec` old or more than `clockSkewSec` ahead: fix the client's clock, or make one proof per request |
235
+ | `dpop_proof_jti` | no `jti`, or one over 256 characters |
236
+ | `dpop_proof_ath` | `ath` is not the SHA-256 of this access token: the proof was made for another token, or for none |
237
+ | `dpop_proof_thumbprint` | the proof key is not the key the token is bound to (`cnf.jkt`) |
238
+ | `dpop_proof_replay` | this proof was already used: sign a new one (new `jti`) for every request, retries included |
239
+
240
+ ### 401 reason `dpop_nonce`
241
+
242
+ `WWW-Authenticate: DPoP error="use_dpop_nonce"` and a `DPoP-Nonce` header. `dpop.nonce` is set and the
243
+ proof carries no valid nonce. Retry once with a new proof whose `nonce` claim is that header's value;
244
+ keep the latest `DPoP-Nonce` a response gives (a successful response carries one too when the nonce is
245
+ near its end). An expired nonce (older than `ttlMs`) is answered the same way with a fresh one.
246
+
247
+ ### 500 on a request with a DPoP proof
248
+
249
+ The replay store threw (`claim` rejected): Redis is down, or `redisDpopReplayStore` has no connection.
250
+ Every DPoP request fails with a 500 until it answers, on purpose: a proof is never accepted unchecked.
251
+ Check the Redis connection (`/ready`, the `connectRedis` handle); `Bearer` tokens without `cnf.jkt`
252
+ are not affected. `memoryDpopReplayStore` throws `memoryDpopReplayStore: full (use a shared store)`
253
+ past `maxEntries` live proofs: use a shared store.
254
+
255
+ ### 403 `insufficient_scope`
256
+
257
+ ```json
258
+ { "error": "forbidden", "reason": "insufficient_scope", "scope": "invoices:write" }
259
+ ```
260
+
261
+ The token is valid but lacks a scope `requireScope` asks for (all are required); `scope` lists them
262
+ and `WWW-Authenticate` repeats them. The client must request the scope from Clavix.
263
+
264
+ ## Traps
265
+
266
+ ### `/ready` stays 503 or flips back
267
+
268
+ `ready` is `false` while a `warm` issuer's keys are not loaded. Warm issuers are pinned and never
269
+ evicted, so check the issuer's discovery URL is reachable. See [Readiness and warm-up](guide/readiness.md).
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
- "name": "@softistx/alxia-clavix",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@softistx/alxia-clavix",
3
+ "version": "0.3.0",
4
+ "description": "Verify the access tokens Clavix issues in an alxia API: a multi-tenant issuer allow-list on the RFC 9068 profile of @softistx/alxia-jwt",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "docs",
12
+ "README.md",
13
+ "package.json",
14
+ "LICENSE"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/softistx/alxia.git",
27
+ "directory": "packages/clavix"
28
+ },
29
+ "publishConfig": {
30
+ "registry": "https://registry.npmjs.org",
31
+ "access": "restricted"
32
+ },
33
+ "scripts": {
34
+ "build": "bun run ../../build.ts",
35
+ "test": "bun test src",
36
+ "typecheck": "tsc --noEmit"
37
+ },
38
+ "alxia": {
39
+ "entrypoints": [
40
+ "src/index.ts"
41
+ ]
42
+ },
43
+ "devDependencies": {
44
+ "@softistx/alxia": "^0.28.0",
45
+ "@softistx/alxia-jwt": "^0.10.0",
46
+ "@types/bun": "^1.4.5"
47
+ },
48
+ "peerDependencies": {
49
+ "@softistx/alxia": "^0.28.0",
50
+ "@softistx/alxia-jwt": "^0.10.0",
51
+ "typescript": "^6.0.3 || ^7.0.0"
52
+ }
53
+ }