@softistx/alxia-clavix 0.2.0 → 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.
- package/README.md +39 -2
- package/dist/clavix.d.ts +10 -4
- package/dist/clavix.d.ts.map +1 -1
- package/dist/dpop/htu.d.ts +11 -0
- package/dist/dpop/htu.d.ts.map +1 -0
- package/dist/dpop/index.d.ts +6 -0
- package/dist/dpop/index.d.ts.map +1 -0
- package/dist/dpop/jwk.d.ts +20 -0
- package/dist/dpop/jwk.d.ts.map +1 -0
- package/dist/dpop/nonce.d.ts +29 -0
- package/dist/dpop/nonce.d.ts.map +1 -0
- package/dist/dpop/options.d.ts +33 -0
- package/dist/dpop/options.d.ts.map +1 -0
- package/dist/dpop/proof.d.ts +32 -0
- package/dist/dpop/proof.d.ts.map +1 -0
- package/dist/dpop/replay-store.d.ts +24 -0
- package/dist/dpop/replay-store.d.ts.map +1 -0
- package/dist/guard.d.ts +19 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +350 -18
- package/dist/index.js.map +11 -4
- package/docs/guide/principal-and-scopes.md +5 -1
- package/docs/guide/security.md +89 -3
- package/docs/roadmap.md +7 -0
- package/docs/troubleshooting.md +88 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -54,6 +54,41 @@ clavix({
|
|
|
54
54
|
});
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
+
## DPoP
|
|
58
|
+
|
|
59
|
+
Tokens bound to a client key (`cnf.jkt`, RFC 9449) are checked per call when `dpop` is given: the
|
|
60
|
+
`DPoP` scheme, the proof (`typ`, ES256 or EdDSA, `htm`, `htu`, `iat`, single-use `jti`, `ath`, key
|
|
61
|
+
thumbprint equal to `cnf.jkt`) and an optional `DPoP-Nonce`. A bound token sent as `Bearer` (or in the
|
|
62
|
+
cookie) is refused.
|
|
63
|
+
|
|
64
|
+
**Behaviour change:** a token with `cnf.jkt` used to pass as a bearer token and is now a 401
|
|
65
|
+
`dpop_required`. Clients of such tokens must send `Authorization: DPoP <token>` and a `DPoP` proof
|
|
66
|
+
header, and the app must add `dpop` to `clavix()`; tokens without `cnf.jkt` need nothing. With `dpop`,
|
|
67
|
+
the `WWW-Authenticate` of a request with no token becomes `Bearer, DPoP algs="ES256 EdDSA"`.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { connectRedis } from '@softistx/redis';
|
|
71
|
+
import { clavix, hmacDpopNonce } from '@softistx/alxia-clavix';
|
|
72
|
+
import { redisDpopReplayStore } from '@softistx/alxia-redis';
|
|
73
|
+
|
|
74
|
+
const connection = await connectRedis(Bun.env['REDIS_URL']!);
|
|
75
|
+
|
|
76
|
+
clavix({
|
|
77
|
+
domain: 'clavix.example.com',
|
|
78
|
+
audience: 'https://api.example.com',
|
|
79
|
+
dpop: {
|
|
80
|
+
origin: 'https://api.example.com', // the public origin, behind a proxy too
|
|
81
|
+
replay: redisDpopReplayStore(connection.client, { name: 'dpop' }), // shared by every instance
|
|
82
|
+
nonce: hmacDpopNonce({ secret: Bun.env['DPOP_NONCE_SECRET']! }), // optional
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`memoryDpopReplayStore()` is the replay store for tests and one instance only. No peer on
|
|
88
|
+
`@softistx/alxia-redis` is needed: its store is typed by shape.
|
|
89
|
+
|
|
90
|
+
See [Security notes](https://github.com/softistx/alxia/blob/develop/packages/clavix/docs/guide/security.md#dpop).
|
|
91
|
+
|
|
57
92
|
## API
|
|
58
93
|
|
|
59
94
|
| Export | What it is |
|
|
@@ -66,17 +101,19 @@ clavix({
|
|
|
66
101
|
| `options.probation` | issuers not yet verified, kept in a cache of their own and promoted after one verified token (default `min(maxIssuers, 16)`) |
|
|
67
102
|
| `options.warm` | issuers whose keys `ready` waits for; each must be allowed; pinned, never evicted, not counted in `maxIssuers` |
|
|
68
103
|
| `cacheMs`, `staleMs`, `refetchMs`, `timeoutMs`, `clockTolerance`, `algorithms` | passed to `createJwt`; defaults as in [`@softistx/alxia-jwt`](https://github.com/softistx/alxia/blob/develop/packages/jwt/docs/guide/jwks.md); nothing else reaches it |
|
|
104
|
+
| `options.dpop` | `{ origin, replay, algorithms?, maxAgeSec?, clockSkewSec?, nonce? }`: verifies DPoP proofs and accepts the `DPoP` scheme; without it a token with `cnf.jkt` is refused |
|
|
105
|
+
| `hmacDpopNonce({ secret, ttlMs? })`, `memoryDpopReplayStore({ maxEntries? })`, `jwkThumbprint(jwk)` | a stateless server nonce; an in-process replay store (tests, one instance, bounded); the RFC 7638 thumbprint. The shared replay store is `redisDpopReplayStore` in `@softistx/alxia-redis` |
|
|
69
106
|
| `auth.bearer({ cookie? })` | the guard: routes after it read a `ClavixPrincipal` as `user`; a malformed token is a 401 `malformed`, and `claims` only means the issuer has no tenant |
|
|
70
107
|
| `auth.ready()` | `Promise<boolean>`, never throws: `true` once every `warm` issuer's keys are loaded (always `true` with none) |
|
|
71
108
|
| `auth.tenantOf(issuer)` | the tenant of an allowed issuer; `undefined` if refused or listed without a tenant |
|
|
72
109
|
| `requireScope(...scopes)` | a guard after `bearer()`: every scope required, else a 403 `insufficient_scope` |
|
|
73
110
|
| `ClavixPrincipal` | `subject`, `tenant`, `issuer`, `clientId`, `scopes`, `roles`, `claims` |
|
|
74
|
-
| `ClavixOptions`, `ClavixVerifier`, `RequireScope`, `InsufficientScopeBody`, `InsufficientScopeProblem` | the types |
|
|
111
|
+
| `ClavixOptions`, `ClavixVerifier`, `DpopOptions`, `DpopReplayStore`, `DpopNonce`, `DpopReason`, `ClavixGuard`, `RequireScope`, `InsufficientScopeBody`, `InsufficientScopeProblem` | the types |
|
|
75
112
|
|
|
76
113
|
Why a token is refused (`verify` result or the 401 `reason`): `malformed` (not a JWT, no readable
|
|
77
114
|
`iss`, a required RFC 9068 claim missing or empty, or a `scope`, `roles`, `permissions` or `cnf` of the wrong shape), `issuer` (not allowed; nothing is fetched),
|
|
78
115
|
`audience`, `type` (header `typ` is not `at+jwt`), `signature`, `algorithm`, `expired`,
|
|
79
|
-
`not_yet_valid`, `key`, `keys_unavailable`, and from the guard `missing` and `
|
|
116
|
+
`not_yet_valid`, `key`, `keys_unavailable`, and from the guard `missing`, `claims` and the DPoP ones (`dpop_required`, `dpop_unsupported`, `dpop_malformed`, `dpop_unbound`, `dpop_proof_*`, `dpop_nonce`: see [Troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/clavix/docs/troubleshooting.md)).
|
|
80
117
|
|
|
81
118
|
## Documentation
|
|
82
119
|
|
package/dist/clavix.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { type
|
|
3
|
-
import { type
|
|
1
|
+
import { type JwksOptions, type Verifier } from '@softistx/alxia-jwt';
|
|
2
|
+
import { type DpopOptions } from './dpop';
|
|
3
|
+
import { type ClavixGuard } from './guard';
|
|
4
4
|
export interface ClavixOptions extends Pick<JwksOptions, 'audience' | 'cacheMs' | 'staleMs' | 'refetchMs' | 'timeoutMs' | 'clockTolerance' | 'algorithms'> {
|
|
5
5
|
/**
|
|
6
6
|
* The deployment's domain: an issuer `https://<tenant>.<domain>` is
|
|
@@ -35,6 +35,12 @@ export interface ClavixOptions extends Pick<JwksOptions, 'audience' | 'cacheMs'
|
|
|
35
35
|
* always ready.
|
|
36
36
|
*/
|
|
37
37
|
readonly warm?: readonly string[];
|
|
38
|
+
/**
|
|
39
|
+
* Verifies the DPoP proof of sender-constrained tokens (RFC 9449) and
|
|
40
|
+
* accepts the `DPoP` scheme. Left out, a token bound to a key (`cnf.jkt`)
|
|
41
|
+
* is refused outright, as a bearer or as `DPoP`.
|
|
42
|
+
*/
|
|
43
|
+
readonly dpop?: DpopOptions;
|
|
38
44
|
}
|
|
39
45
|
export interface ClavixVerifier extends Verifier {
|
|
40
46
|
/**
|
|
@@ -63,7 +69,7 @@ export interface ClavixVerifier extends Verifier {
|
|
|
63
69
|
bearer(options?: {
|
|
64
70
|
/** Reads the token from this cookie when no `Authorization` header carries one. */
|
|
65
71
|
readonly cookie?: string;
|
|
66
|
-
}):
|
|
72
|
+
}): ClavixGuard;
|
|
67
73
|
}
|
|
68
74
|
/**
|
|
69
75
|
* A verifier of the access tokens Clavix issues, for any tenant: a token is
|
package/dist/clavix.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clavix.d.ts","sourceRoot":"","sources":["../src/clavix.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"clavix.d.ts","sourceRoot":"","sources":["../src/clavix.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,KAAK,WAAW,EAAe,MAAM,QAAQ,CAAC;AACvD,OAAO,EAAE,KAAK,WAAW,EAAS,MAAM,SAAS,CAAC;AAWlD,MAAM,WAAW,aAChB,SAAQ,IAAI,CACX,WAAW,EACT,UAAU,GACV,SAAS,GACT,SAAS,GACT,WAAW,GACX,WAAW,GACX,gBAAgB,GAChB,YAAY,CACd;IACD;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACxE,gEAAgE;IAChE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B;;;;OAIG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B;;;;;OAKG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC;CAC5B;AAED,MAAM,WAAW,cAAe,SAAQ,QAAQ;IAC/C;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC7C;;;;;;;OAOG;IACH,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B;;;;;;;OAOG;IACH,MAAM,CAAC,OAAO,CAAC,EAAE;QAChB,mFAAmF;QACnF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;KACzB,GAAG,WAAW,CAAC;CAChB;AAuDD;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,cAAc,CA+D7D"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a proof's `htu` names this request: the configured public origin
|
|
3
|
+
* (already normalised by `resolveDpop`) and the request's path, the query and the fragment ignored (RFC 9449
|
|
4
|
+
* section 4.3). The scheme and host compare without case and without a
|
|
5
|
+
* default port (`:443`, `:80`) as URL parsing normalises them; the path
|
|
6
|
+
* compares exactly (a trailing slash matters; percent-escapes compare as
|
|
7
|
+
* written, RFC 3986 6.2.2 normalisation is not applied, which can only refuse); a `htu` with userinfo never
|
|
8
|
+
* matches.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sameUrl(htu: string, origin: string, path: string): boolean;
|
|
11
|
+
//# sourceMappingURL=htu.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"htu.d.ts","sourceRoot":"","sources":["../../src/dpop/htu.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAS1E"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { jwkThumbprint } from './jwk';
|
|
2
|
+
export { type DpopNonce, hmacDpopNonce } from './nonce';
|
|
3
|
+
export { type DpopAlgorithm, type DpopOptions, type ResolvedDpop, resolveDpop, } from './options';
|
|
4
|
+
export { type DpopReason, verifyProof } from './proof';
|
|
5
|
+
export { type DpopReplayStore, memoryDpopReplayStore } from './replay-store';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dpop/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EAAE,KAAK,SAAS,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,WAAW,GACX,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,KAAK,UAAU,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,EAAE,KAAK,eAAe,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DpopAlgorithm } from './options';
|
|
2
|
+
/** The members of a proof's header, claims and key that are read. */
|
|
3
|
+
export type Fields = Partial<Record<'typ' | 'alg' | 'jwk' | 'crit' | 'htm' | 'htu' | 'iat' | 'jti' | 'ath' | 'nonce' | 'd' | 'kty' | 'crv' | 'x' | 'y', unknown>>;
|
|
4
|
+
export declare const encoder: TextEncoder;
|
|
5
|
+
export declare const isObject: (value: unknown) => value is Fields;
|
|
6
|
+
/** A JWT part as an object, or `undefined`. */
|
|
7
|
+
export declare function decode(part: string): Fields | undefined;
|
|
8
|
+
export interface ProofKey {
|
|
9
|
+
readonly jwk: JsonWebKey;
|
|
10
|
+
readonly params: EcKeyImportParams | Algorithm;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The public key of a proof's header, kept to its public members, with its
|
|
14
|
+
* algorithm's import parameters; `undefined` for a private key, another
|
|
15
|
+
* curve, or a key that does not fit `alg`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function publicKey(jwk: Fields, alg: DpopAlgorithm): ProofKey | undefined;
|
|
18
|
+
/** The RFC 7638 thumbprint of a public JWK made by `publicKey`. */
|
|
19
|
+
export declare function jwkThumbprint(jwk: JsonWebKey): Promise<string>;
|
|
20
|
+
//# sourceMappingURL=jwk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwk.d.ts","sourceRoot":"","sources":["../../src/dpop/jwk.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE/C,qEAAqE;AACrE,MAAM,MAAM,MAAM,GAAG,OAAO,CAC3B,MAAM,CACH,KAAK,GACL,KAAK,GACL,KAAK,GACL,MAAM,GACN,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,OAAO,GACP,GAAG,GACH,KAAK,GACL,KAAK,GACL,GAAG,GACH,GAAG,EACL,OAAO,CACP,CACD,CAAC;AAEF,eAAO,MAAM,OAAO,aAAoB,CAAC;AAEzC,eAAO,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MACkB,CAAC;AAEtE,+CAA+C;AAC/C,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CASvD;AAED,MAAM,WAAW,QAAQ;IACxB,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,iBAAiB,GAAG,SAAS,CAAC;CAC/C;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,aAAa,GAChB,QAAQ,GAAG,SAAS,CAgBtB;AAED,mEAAmE;AACnE,wBAAsB,aAAa,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAUpE"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server nonce of RFC 9449 section 8: `issue` makes the nonce a client
|
|
3
|
+
* must put in its next proof, `accepts` tells whether a nonce is still valid.
|
|
4
|
+
*/
|
|
5
|
+
export interface DpopNonce {
|
|
6
|
+
issue(): string | Promise<string>;
|
|
7
|
+
accepts(nonce: string): boolean | Promise<boolean>;
|
|
8
|
+
/**
|
|
9
|
+
* A fresh nonce for a client whose accepted `nonce` is near its end
|
|
10
|
+
* (RFC 9449 section 8.2), sent as `DPoP-Nonce` on the answer so the next
|
|
11
|
+
* proof does not meet a `use_dpop_nonce` refusal; `undefined` while it
|
|
12
|
+
* has time left.
|
|
13
|
+
*/
|
|
14
|
+
renew?(nonce: string): string | Promise<string | undefined> | undefined;
|
|
15
|
+
}
|
|
16
|
+
/** Constant-time equality of two strings. */
|
|
17
|
+
export declare function timingSafeEqual(a: string, b: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* A stateless nonce: a time stamp signed with a key derived from `secret`
|
|
20
|
+
* (HMAC-SHA-256, with a label of its own, so the same secret may serve
|
|
21
|
+
* elsewhere), valid for `ttlMs` (5 minutes by default). Every instance
|
|
22
|
+
* sharing `secret` accepts the nonces of the others, with nothing stored.
|
|
23
|
+
* The secret needs 16 characters.
|
|
24
|
+
*/
|
|
25
|
+
export declare function hmacDpopNonce(options: {
|
|
26
|
+
readonly secret: string;
|
|
27
|
+
readonly ttlMs?: number;
|
|
28
|
+
}): DpopNonce;
|
|
29
|
+
//# sourceMappingURL=nonce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nonce.d.ts","sourceRoot":"","sources":["../../src/dpop/nonce.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,SAAS;IACzB,KAAK,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACnD;;;;;OAKG;IACH,KAAK,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,GAAG,SAAS,CAAC;CACxE;AAID,6CAA6C;AAC7C,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAS7D;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACxB,GAAG,SAAS,CAoDZ"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { DpopNonce } from './nonce';
|
|
2
|
+
import type { DpopReplayStore } from './replay-store';
|
|
3
|
+
/** The signature algorithms a DPoP proof may use (RFC 9449 section 4.2). */
|
|
4
|
+
export type DpopAlgorithm = 'ES256' | 'EdDSA';
|
|
5
|
+
export interface DpopOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The origin clients use to reach the API (`https://api.example.com`), what
|
|
8
|
+
* a proof's `htu` must start with. Behind a proxy the request's own URL is
|
|
9
|
+
* the internal one: this is the public one, never read from a header.
|
|
10
|
+
*/
|
|
11
|
+
readonly origin: string;
|
|
12
|
+
/** Where the `jti` of accepted proofs are remembered: `redisDpopReplayStore` in production, `memoryDpopReplayStore()` for tests. */
|
|
13
|
+
readonly replay: DpopReplayStore;
|
|
14
|
+
/** Proof algorithms accepted, `['ES256', 'EdDSA']` by default. */
|
|
15
|
+
readonly algorithms?: readonly DpopAlgorithm[];
|
|
16
|
+
/** How old a proof's `iat` may be, in seconds. 60 by default. */
|
|
17
|
+
readonly maxAgeSec?: number;
|
|
18
|
+
/** How far ahead of this server's clock an `iat` may be, in seconds. 5 by default. */
|
|
19
|
+
readonly clockSkewSec?: number;
|
|
20
|
+
/** Asks clients for a server nonce (RFC 9449 section 8): `hmacDpopNonce({ secret })`. Off by default. */
|
|
21
|
+
readonly nonce?: DpopNonce;
|
|
22
|
+
}
|
|
23
|
+
export interface ResolvedDpop {
|
|
24
|
+
readonly origin: string;
|
|
25
|
+
readonly replay: DpopReplayStore;
|
|
26
|
+
readonly algorithms: readonly DpopAlgorithm[];
|
|
27
|
+
readonly maxAgeSec: number;
|
|
28
|
+
readonly clockSkewSec: number;
|
|
29
|
+
readonly nonce: DpopNonce | undefined;
|
|
30
|
+
}
|
|
31
|
+
/** The options, checked, with their defaults. */
|
|
32
|
+
export declare function resolveDpop(options: DpopOptions): ResolvedDpop;
|
|
33
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/dpop/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD,4EAA4E;AAC5E,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,OAAO,CAAC;AAE9C,MAAM,WAAW,WAAW;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oIAAoI;IACpI,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC,kEAAkE;IAClE,QAAQ,CAAC,UAAU,CAAC,EAAE,SAAS,aAAa,EAAE,CAAC;IAC/C,iEAAiE;IACjE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,sFAAsF;IACtF,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,yGAAyG;IACzG,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC;CAC3B;AAED,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC,QAAQ,CAAC,UAAU,EAAE,SAAS,aAAa,EAAE,CAAC;IAC9C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,SAAS,GAAG,SAAS,CAAC;CACtC;AAED,iDAAiD;AACjD,wBAAgB,WAAW,CAAC,OAAO,EAAE,WAAW,GAAG,YAAY,CAkD9D"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ResolvedDpop } from './options';
|
|
2
|
+
/** Why a request's DPoP check failed. */
|
|
3
|
+
export type DpopReason = 'dpop_required' | 'dpop_unsupported' | 'dpop_unbound' | 'dpop_proof_missing' | 'dpop_proof_malformed' | 'dpop_proof_type' | 'dpop_proof_algorithm' | 'dpop_proof_key' | 'dpop_proof_signature' | 'dpop_proof_method' | 'dpop_proof_url' | 'dpop_proof_iat' | 'dpop_proof_jti' | 'dpop_proof_ath' | 'dpop_proof_thumbprint' | 'dpop_proof_replay' | 'dpop_malformed' | 'dpop_nonce';
|
|
4
|
+
export type ProofResult = {
|
|
5
|
+
readonly ok: true;
|
|
6
|
+
/** A fresh nonce to send as `DPoP-Nonce`: the accepted one is near its end. */
|
|
7
|
+
readonly nonce?: string;
|
|
8
|
+
} | {
|
|
9
|
+
readonly ok: false;
|
|
10
|
+
readonly reason: DpopReason;
|
|
11
|
+
/** Set with `dpop_nonce`: the nonce the client must use. */
|
|
12
|
+
readonly nonce?: string;
|
|
13
|
+
};
|
|
14
|
+
export interface ProofInput {
|
|
15
|
+
/** The `DPoP` header value. */
|
|
16
|
+
readonly proof: string | null;
|
|
17
|
+
/** The request's method. */
|
|
18
|
+
readonly method: string;
|
|
19
|
+
/** The request's path, as the client sent it. */
|
|
20
|
+
readonly path: string;
|
|
21
|
+
/** The access token the proof is bound to. */
|
|
22
|
+
readonly accessToken: string;
|
|
23
|
+
/** The token's `cnf.jkt`. */
|
|
24
|
+
readonly jkt: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Checks a DPoP proof against RFC 9449 section 4.3, in the cheap-to-costly
|
|
28
|
+
* order, and remembers its `jti` last: a proof refused for anything else is
|
|
29
|
+
* never recorded.
|
|
30
|
+
*/
|
|
31
|
+
export declare function verifyProof(input: ProofInput, options: ResolvedDpop): Promise<ProofResult>;
|
|
32
|
+
//# sourceMappingURL=proof.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proof.d.ts","sourceRoot":"","sources":["../../src/dpop/proof.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAiB,YAAY,EAAE,MAAM,WAAW,CAAC;AAE7D,yCAAyC;AACzC,MAAM,MAAM,UAAU,GACnB,eAAe,GACf,kBAAkB,GAClB,cAAc,GACd,oBAAoB,GACpB,sBAAsB,GACtB,iBAAiB,GACjB,sBAAsB,GACtB,gBAAgB,GAChB,sBAAsB,GACtB,mBAAmB,GACnB,gBAAgB,GAChB,gBAAgB,GAChB,gBAAgB,GAChB,gBAAgB,GAChB,uBAAuB,GACvB,mBAAmB,GACnB,gBAAgB,GAChB,YAAY,CAAC;AAEhB,MAAM,MAAM,WAAW,GACpB;IACA,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAClB,+EAA+E;IAC/E,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACvB,GACD;IACA,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IACnB,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,4DAA4D;IAC5D,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEL,MAAM,WAAW,UAAU;IAC1B,+BAA+B;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,4BAA4B;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iDAAiD;IACjD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,8CAA8C;IAC9C,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,6BAA6B;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACrB;AAwGD;;;;GAIG;AACH,wBAAsB,WAAW,CAChC,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,YAAY,GACnB,OAAO,CAAC,WAAW,CAAC,CA+BtB"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remembers the `jti` of the proofs already accepted, so that a proof is
|
|
3
|
+
* used once. `claim` records `key` for `ttlMs` and answers `true` the first
|
|
4
|
+
* time, `false` while it is still remembered. It must be atomic across the
|
|
5
|
+
* processes that serve the API (Redis `SET NX PX`), and it may throw: a store
|
|
6
|
+
* that cannot answer fails the request (a 500), it never lets a proof
|
|
7
|
+
* through unchecked.
|
|
8
|
+
*/
|
|
9
|
+
export interface DpopReplayStore {
|
|
10
|
+
claim(key: string, ttlMs: number): Promise<boolean>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A replay store in the memory of this process: **for tests and a single
|
|
14
|
+
* instance only**. Two instances behind a load balancer each have their own
|
|
15
|
+
* memory, so a proof replayed on the other one is accepted: use
|
|
16
|
+
* `redisDpopReplayStore` from `@softistx/alxia-redis` there. It holds at
|
|
17
|
+
* most `maxEntries` live proofs (10 000 by default) and throws past that,
|
|
18
|
+
* rather than growing without bound or forgetting a proof. Once full, each
|
|
19
|
+
* claim first sweeps the expired entries, which costs O(`maxEntries`).
|
|
20
|
+
*/
|
|
21
|
+
export declare function memoryDpopReplayStore(options?: {
|
|
22
|
+
readonly maxEntries?: number;
|
|
23
|
+
}): DpopReplayStore;
|
|
24
|
+
//# sourceMappingURL=replay-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay-store.d.ts","sourceRoot":"","sources":["../../src/dpop/replay-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC/B,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,GAAE;IAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAO,GAC5C,eAAe,CAoBjB"}
|
package/dist/guard.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@softistx/alxia';
|
|
2
|
+
import { type Bearer, type UnauthorizedBody, type UnauthorizedProblem, type Verifier } from '@softistx/alxia-jwt';
|
|
3
|
+
import { type DpopReason, type ResolvedDpop } from './dpop';
|
|
4
|
+
import type { ClavixPrincipal } from './principal';
|
|
5
|
+
/** The body of a Clavix 401: the reasons of `@softistx/alxia-jwt`'s, and the DPoP ones. */
|
|
6
|
+
export type ClavixUnauthorizedBody = UnauthorizedBody<DpopReason>;
|
|
7
|
+
/** The 401 under `alxia({ errors: 'problem' })`. */
|
|
8
|
+
export type ClavixUnauthorizedProblem = UnauthorizedProblem<DpopReason>;
|
|
9
|
+
/** What `clavix().bearer()` makes: a middleware that gives `user`, or answers the 401. */
|
|
10
|
+
export type ClavixGuard = Bearer<StandardSchemaV1<unknown, ClavixPrincipal>, DpopReason>;
|
|
11
|
+
export interface GuardOptions {
|
|
12
|
+
readonly jwt: Verifier;
|
|
13
|
+
readonly schema: StandardSchemaV1<unknown, ClavixPrincipal>;
|
|
14
|
+
readonly cookie?: string | undefined;
|
|
15
|
+
readonly dpop?: ResolvedDpop | undefined;
|
|
16
|
+
}
|
|
17
|
+
/** The guard of `clavix().bearer()`: `Bearer` and, with `dpop`, `DPoP` tokens. */
|
|
18
|
+
export declare function guard(options: GuardOptions): ClavixGuard;
|
|
19
|
+
//# sourceMappingURL=guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.d.ts","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EACN,KAAK,MAAM,EAGX,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,QAAQ,EACb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,YAAY,EAAe,MAAM,QAAQ,CAAC;AACzE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,2FAA2F;AAC3F,MAAM,MAAM,sBAAsB,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAC;AAElE,oDAAoD;AACpD,MAAM,MAAM,yBAAyB,GAAG,mBAAmB,CAAC,UAAU,CAAC,CAAC;AAExE,0FAA0F;AAC1F,MAAM,MAAM,WAAW,GAAG,MAAM,CAC/B,gBAAgB,CAAC,OAAO,EAAE,eAAe,CAAC,EAC1C,UAAU,CACV,CAAC;AAEF,MAAM,WAAW,YAAY;IAC5B,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IAC5D,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,IAAI,CAAC,EAAE,YAAY,GAAG,SAAS,CAAC;CACzC;AAyFD,kFAAkF;AAClF,wBAAgB,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,WAAW,CAOxD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { type ClavixOptions, type ClavixVerifier, clavix } from './clavix';
|
|
2
|
+
export { type DpopAlgorithm, type DpopNonce, type DpopOptions, type DpopReason, type DpopReplayStore, hmacDpopNonce, memoryDpopReplayStore, } from './dpop';
|
|
3
|
+
export type { ClavixGuard, ClavixUnauthorizedBody, ClavixUnauthorizedProblem, } from './guard';
|
|
2
4
|
export type { ClavixPrincipal } from './principal';
|
|
3
5
|
export { type InsufficientScopeBody, type InsufficientScopeProblem, type RequireScope, requireScope, } from './scope';
|
|
4
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAC3E,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EACN,KAAK,qBAAqB,EAC1B,KAAK,wBAAwB,EAC7B,KAAK,YAAY,EACjB,YAAY,GACZ,MAAM,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAC3E,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,aAAa,EACb,qBAAqB,GACrB,MAAM,QAAQ,CAAC;AAChB,YAAY,EACX,WAAW,EACX,sBAAsB,EACtB,yBAAyB,GACzB,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EACN,KAAK,qBAAqB,EAC1B,KAAK,wBAAwB,EAC7B,KAAK,YAAY,EACjB,YAAY,GACZ,MAAM,SAAS,CAAC"}
|