@gjermundgaraba/clankerauth-sdk 0.26.0 → 0.26.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.
- package/README.md +1 -1
- package/dist/verify.d.ts +1 -1
- package/dist/verify.js +38 -30
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -340,7 +340,7 @@ Tagged errors, all from `@gjermundgaraba/clankerauth-sdk/errors`: `Unauthorized`
|
|
|
340
340
|
|
|
341
341
|
There is one 403. Only the verifier's own check fails with `InsufficientScope`: a credential that is valid but lacks a scope in `requiredScopes`, a resource's `required`. It names that scope and the credential's `actor`, which decides whether its `Forbidden` steps up. A scope asked for after verification, by `requires(scope)`, `admitted(scope)`, `admit(header, scope)` or `watch(header, scope)`, is refused with effect-actions' `Action.Forbidden` directly, decided by the same rule. A credential that is not this resource's at all — the wrong audience, or an API key with no entry in this resource's key list — is `Unauthorized`, because from the resource's side those are the same thing.
|
|
342
342
|
|
|
343
|
-
JWTs are checked against issuer, audience, EdDSA signature, token type, required claims, expiry and scopes. Sender-constrained tokens are rejected. One JWKS read answers every verification for a minute, so a key the issuer starts or stops publishing takes effect within that minute; a `kid` the current document does not publish is `Unauthorized` and never triggers a read, so a token with an invented `kid` cannot become provider traffic. A failed read answers `ProviderUnavailable` (503) for five seconds before the next attempt; a read
|
|
343
|
+
JWTs are checked against issuer, audience, EdDSA signature, token type, required claims, expiry and scopes. Sender-constrained tokens are rejected. One JWKS read answers every verification for a minute, so a key the issuer starts or stops publishing takes effect within that minute; a `kid` the current document does not publish is `Unauthorized` and never triggers a read, so a token with an invented `kid` cannot become provider traffic. A failed read answers `ProviderUnavailable` (503) for five seconds before the next attempt; a read whose verification is interrupted writes nothing, and the next verification reads again. API keys are verified offline, against the resource's key list: one document the issuer signs with its access-token key, holding a sealed entry per key granted on this resource, which only that key can find or open. The list is verified once, when it is read, and one read answers every key it names for a minute, without issuer traffic; so a key disabled, deleted or re-scoped keeps the access it had until the next read, within about a minute. A key the held list does not name may have been created, enabled or granted on this resource since, so the list is read again before the key is refused, unless it was read within the last five seconds: such a key works within seconds, and unknown keys reach the issuer at most once every five seconds. A key's own expiry is checked on every verification, so it applies at once. A failed read, or one whose list or JWKS fetch takes longer than three seconds, is no error while the last list is inside its own lifetime, which the issuer sets to 24 hours: keys keep verifying through an outage, JWKS or not, and the issuer is tried again after five seconds. A read whose verification is interrupted writes nothing, and the next key reads again. Past that lifetime, or before any list has been read, a key is `ProviderUnavailable`. Reads that start failing are logged once as a warning, and their recovery once at info level, so an outage the held list hides still shows in the application's logs without a line per retry. The last list read is the one held, whatever its `iat`: HTTPS to the issuer is what keeps an old list from being replayed, and one could only ever be replayed inside its own day. The issuer does not count key use, so rate limiting is the application's. A key is a bearer secret, and one granted on several resources can be replayed by any of them to the others; a resource you trust less gets its own key. A resource that accepts only OAuth access tokens sets `apiKeys: false`; key-shaped bearers then fail as `Unauthorized` without reading the key list. Admission through a `Resource` has a five-second deadline; the raw `Verifier` has none, so an in-process caller can await a provider call it cannot cancel. The exception is the key list read, which gives each of its fetches three seconds: the list, and the JWKS it is verified with when none is held, so a slow first fetch does not cut the second short. An issuer that stops answering therefore delays a key by three seconds and never fails it while a list is held; one that answers the list late and then stalls on the JWKS can take the read past `Resource`'s deadline.
|
|
344
344
|
|
|
345
345
|
The bundled issuer does not configure automatic signing-key rotation. Immediate-use rotation is supported, but tokens signed by a new key are rejected until the next JWKS read, up to a minute; a key list signed by it is not taken until then either, and the held list keeps deciding. Publish-before-use avoids that window; it is not mandatory. Key lists are re-signed on every read, so issued API keys are unaffected by rotation.
|
|
346
346
|
|
package/dist/verify.d.ts
CHANGED
|
@@ -85,7 +85,7 @@ export declare const store: () => Store;
|
|
|
85
85
|
* request costs nothing: every read it makes runs in the verification that needs it.
|
|
86
86
|
* Verification runs to completion: a deadline is the request edge's policy, which
|
|
87
87
|
* `Resource` applies, so an in-process caller can await a provider call it cannot cancel.
|
|
88
|
-
* The one exception is a key list read, which gives up after three seconds.
|
|
88
|
+
* The one exception is a key list read, which gives up on each of its fetches after three seconds.
|
|
89
89
|
*/
|
|
90
90
|
export declare const make: (options: Options) => Effect.Effect<{
|
|
91
91
|
verifyToken: (token: string) => Effect.Effect<{
|
package/dist/verify.js
CHANGED
|
@@ -37,17 +37,22 @@ const documentLifetime = Duration.minutes(1);
|
|
|
37
37
|
*/
|
|
38
38
|
const cooldown = Duration.seconds(5);
|
|
39
39
|
/**
|
|
40
|
-
* How long a key list read may take
|
|
41
|
-
*
|
|
40
|
+
* How long each fetch of a key list read may take: the list, and the JWKS it is verified
|
|
41
|
+
* with when none is held. Under `Resource`'s five-second deadline, so an issuer that stops
|
|
42
|
+
* answering fails the read, not the request, and a held list still decides.
|
|
42
43
|
*/
|
|
43
|
-
const listReadTimeout =
|
|
44
|
+
const listReadTimeout = Duration.seconds(3);
|
|
45
|
+
const deadline = (operation) => (effect) => Effect.timeoutOrElse(effect, {
|
|
46
|
+
duration: listReadTimeout,
|
|
47
|
+
orElse: () => Effect.fail(new ProviderUnavailable({ operation })),
|
|
48
|
+
});
|
|
44
49
|
/**
|
|
45
50
|
* A verifier over `options.store`; transport is selected by the application, not the SDK.
|
|
46
51
|
* It holds nothing but its options, the store and the HTTP client, so acquiring one per
|
|
47
52
|
* request costs nothing: every read it makes runs in the verification that needs it.
|
|
48
53
|
* Verification runs to completion: a deadline is the request edge's policy, which
|
|
49
54
|
* `Resource` applies, so an in-process caller can await a provider call it cannot cancel.
|
|
50
|
-
* The one exception is a key list read, which gives up after three seconds.
|
|
55
|
+
* The one exception is a key list read, which gives up on each of its fetches after three seconds.
|
|
51
56
|
*/
|
|
52
57
|
export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
53
58
|
const client = yield* HttpClient.HttpClient;
|
|
@@ -72,26 +77,28 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
72
77
|
/** The keys read within their lifetime, a failed read's failure within its cooldown, or a read now. */
|
|
73
78
|
const keys = Effect.gen(function* () {
|
|
74
79
|
const settled = options.store.jwks.get(options.issuer);
|
|
75
|
-
const
|
|
80
|
+
const startedAt = yield* Clock.currentTimeMillis;
|
|
76
81
|
if (settled !== undefined) {
|
|
77
82
|
const answers = "keys" in settled ? documentLifetime : cooldown;
|
|
78
|
-
if (
|
|
83
|
+
if (startedAt - settled.readAt < Duration.toMillis(answers))
|
|
79
84
|
return "keys" in settled
|
|
80
85
|
? settled.keys
|
|
81
86
|
: yield* new ProviderUnavailable({ operation: settled.failure });
|
|
82
87
|
}
|
|
83
88
|
const read = yield* Effect.result(document());
|
|
84
89
|
const readAt = yield* Clock.currentTimeMillis;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
if (Result.isSuccess(read)) {
|
|
91
|
+
options.store.jwks.set(options.issuer, { readAt, keys: read.success });
|
|
92
|
+
return read.success;
|
|
93
|
+
}
|
|
94
|
+
// why: a failure never replaces an outcome that ended after it began.
|
|
95
|
+
if ((options.store.jwks.get(options.issuer)?.readAt ?? -Infinity) >= startedAt)
|
|
96
|
+
return yield* read.failure;
|
|
97
|
+
options.store.jwks.set(options.issuer, { readAt, failure: read.failure.operation });
|
|
98
|
+
return yield* read.failure;
|
|
92
99
|
});
|
|
93
|
-
const signingKey = Effect.fn("Verifier.signingKey")(function* (kid) {
|
|
94
|
-
const jwks = { keys: [...(yield*
|
|
100
|
+
const signingKey = Effect.fn("Verifier.signingKey")(function* (kid, held) {
|
|
101
|
+
const jwks = { keys: [...(yield* held)] };
|
|
95
102
|
return yield* Effect.tryPromise({
|
|
96
103
|
try: () => createLocalJWKSet(jwks)({ alg: "EdDSA", kid }),
|
|
97
104
|
catch: unauthorized,
|
|
@@ -101,9 +108,9 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
101
108
|
* A document this issuer signed for this resource: EdDSA under a key it names, with the
|
|
102
109
|
* expected `typ`, issuer and audience, and not expired. Which claims it must carry is
|
|
103
110
|
* its schema's to say. `Unauthorized` means it is not to be trusted; what that amounts
|
|
104
|
-
* to is the caller's to say.
|
|
111
|
+
* to is the caller's to say. Its signing keys are `held`'s, the store's JWKS by default.
|
|
105
112
|
*/
|
|
106
|
-
const verifySigned = Effect.fn("Verifier.signed")(function* (jws, typ) {
|
|
113
|
+
const verifySigned = Effect.fn("Verifier.signed")(function* (jws, typ, held = keys) {
|
|
107
114
|
const header = yield* Effect.try({
|
|
108
115
|
try: () => decodeProtectedHeader(jws),
|
|
109
116
|
catch: unauthorized,
|
|
@@ -111,7 +118,7 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
111
118
|
// why: refused before any JWKS read, so an unauthenticated request causes no issuer traffic.
|
|
112
119
|
if (header.alg !== "EdDSA" || header.kid === undefined)
|
|
113
120
|
return yield* unauthorized();
|
|
114
|
-
const key = yield* signingKey(header.kid);
|
|
121
|
+
const key = yield* signingKey(header.kid, held);
|
|
115
122
|
const now = yield* Clock.currentTimeMillis;
|
|
116
123
|
const { payload } = yield* Effect.tryPromise({
|
|
117
124
|
try: () => jwtVerify(jws, key, {
|
|
@@ -139,15 +146,19 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
139
146
|
});
|
|
140
147
|
const listDocument = Effect.fn("Verifier.keyList")(function* () {
|
|
141
148
|
const request = HttpClientRequest.post(endpoints.keyList).pipe(HttpClientRequest.bodyJsonUnsafe({ resource: options.resource }));
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
+
const fetched = Effect.gen(function* () {
|
|
150
|
+
const response = yield* execute(client, request);
|
|
151
|
+
if (response.status !== 200)
|
|
152
|
+
return yield* new ProviderUnavailable({
|
|
153
|
+
operation: "key-list.fetch",
|
|
154
|
+
cause: { status: response.status },
|
|
155
|
+
});
|
|
156
|
+
return yield* HttpClientResponse.schemaBodyJson(KeyListResponse)(response).pipe(Effect.mapError((cause) => new ProviderUnavailable({ operation: "key-list.decode", cause })));
|
|
157
|
+
});
|
|
158
|
+
const { list } = yield* fetched.pipe(deadline("key-list.timeout"));
|
|
149
159
|
const rejected = (cause) => new ProviderUnavailable({ operation: "key-list.verify", cause });
|
|
150
|
-
|
|
160
|
+
// why: a JWKS read on its own deadline, so a cold one does not spend the list's.
|
|
161
|
+
const payload = yield* verifySigned(list, KeyList.type, keys.pipe(deadline("jwks.timeout"))).pipe(Effect.catchTag("Unauthorized", (cause) => Effect.fail(rejected(cause))));
|
|
151
162
|
const claims = yield* Schema.decodeUnknownEffect(KeyListClaims)(payload).pipe(Effect.mapError(rejected));
|
|
152
163
|
return {
|
|
153
164
|
readAt: yield* Clock.currentTimeMillis,
|
|
@@ -167,10 +178,7 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
167
178
|
return settled.failure === undefined
|
|
168
179
|
? undefined
|
|
169
180
|
: yield* new ProviderUnavailable({ operation: settled.failure });
|
|
170
|
-
const read = yield* Effect.result(listDocument()
|
|
171
|
-
duration: listReadTimeout,
|
|
172
|
-
orElse: () => Effect.fail(new ProviderUnavailable({ operation: "key-list.timeout" })),
|
|
173
|
-
})));
|
|
181
|
+
const read = yield* Effect.result(listDocument());
|
|
174
182
|
const readAt = yield* Clock.currentTimeMillis;
|
|
175
183
|
const before = options.store.keyLists.get(listed);
|
|
176
184
|
const failing = before?.failure !== undefined;
|
package/package.json
CHANGED