@gjermundgaraba/clankerauth-sdk 0.25.0 → 0.26.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 +23 -9
- package/dist/resource.d.ts +5 -0
- package/dist/resource.js +1 -0
- package/dist/transport.d.ts +4 -0
- package/dist/transport.js +9 -2
- package/dist/verify.d.ts +47 -1
- package/dist/verify.js +67 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -70,14 +70,26 @@ import { Effect } from "effect";
|
|
|
70
70
|
import { FetchHttpClient } from "effect/http";
|
|
71
71
|
import { Verifier } from "@gjermundgaraba/clankerauth-sdk";
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
// Once, at module scope: what every verification has read.
|
|
74
|
+
const store = Verifier.store();
|
|
75
|
+
|
|
76
|
+
// Per request, in the request that verifies.
|
|
77
|
+
const verify = (token: string) =>
|
|
78
|
+
Verifier.make({
|
|
79
|
+
issuer: "https://clankerauth.internal/api/auth",
|
|
80
|
+
resource: "https://notes.internal/",
|
|
81
|
+
store,
|
|
82
|
+
requiredScopes: ["notes:read"],
|
|
83
|
+
}).pipe(
|
|
84
|
+
Effect.flatMap((verifier) => verifier.verifyToken(token)),
|
|
85
|
+
Effect.provide(FetchHttpClient.layer),
|
|
86
|
+
);
|
|
79
87
|
```
|
|
80
88
|
|
|
89
|
+
The store holds what verifiers have read, decoded and checked: each issuer's JWKS keys, and each resource's verified key list with when it was read and when it expires. It holds values only, never an Effect, a fiber, a Promise, a `Response` or a timer, so it may live as long as the process. A verifier holds nothing but its options, the store and the HTTP client. On a hit it verifies locally; on a miss, or once what it holds has expired, it reads the issuer in the verification that missed and writes the result back. Concurrent misses each read, and none waits on another's: a verification that misses before any read has finished reads for itself. Steady traffic costs one or two extra issuer reads per refresh; a burst arriving at an empty or expired store reads once per verification in it.
|
|
90
|
+
|
|
91
|
+
That split is what Cloudflare Workers requires. A Worker's I/O belongs to the request that started it: a request cannot resume work another request started, and a continuation whose request has ended is dropped ([I/O on behalf of a different request](https://developers.cloudflare.com/workers/observability/errors/#cannot-perform-io-on-behalf-of-a-different-request), [cross-request promise resolution](https://developers.cloudflare.com/workers/configuration/compatibility-flags/#handle-cross-request-promise-resolution-correctly)). Data may live in global scope; in-flight work may not. So keep the store at module scope, as data, and acquire the verifier in each request. On Node a verifier acquired once is just as correct, since it owns no running work either.
|
|
92
|
+
|
|
81
93
|
## One resource per application
|
|
82
94
|
|
|
83
95
|
Register **one** resource in the clankerauth dashboard, identified by the application's public origin root, written with its trailing slash: `https://notes.internal/`. That one resource covers `/api`, `/mcp` and any socket, with one RFC 9728 document at `/.well-known/oauth-protected-resource` and one `forward_auth` block in the proxy.
|
|
@@ -106,11 +118,12 @@ export const Login = Authentication.make("notes.Login", CurrentPrincipal, {
|
|
|
106
118
|
export const Http = ActionHttp.make([List, Write, Whoami], { authentication: Login });
|
|
107
119
|
```
|
|
108
120
|
|
|
109
|
-
The server declares the resource once, at module level, from that descriptor, every scope it has and the one every credential must carry, `required`, one of `scopes` as the types check. It provides its `layer` and its `provider` once at application construction
|
|
121
|
+
The server declares the resource once, at module level, from that descriptor, every scope it has and the one every credential must carry, `required`, one of `scopes` as the types check. It provides its `layer` and its `provider` once at application construction. `Resource.make` takes only a descriptor declaring `ProviderUnavailable`: one that does not is a type error there, naming `error: ProviderUnavailable`.
|
|
110
122
|
|
|
111
123
|
```ts
|
|
112
124
|
import { Layer } from "effect";
|
|
113
125
|
import { FetchHttpClient } from "effect/http";
|
|
126
|
+
import { Verifier } from "@gjermundgaraba/clankerauth-sdk";
|
|
114
127
|
import { Resource } from "@gjermundgaraba/clankerauth-sdk/effect-actions";
|
|
115
128
|
import * as ActionHttp from "@gjermundgaraba/effect-actions/ActionHttp";
|
|
116
129
|
import * as ActionMcp from "@gjermundgaraba/effect-actions/ActionMcp";
|
|
@@ -133,13 +146,14 @@ const routes = Layer.mergeAll(
|
|
|
133
146
|
NotesResource.layer({
|
|
134
147
|
issuer: "https://clankerauth.internal/api/auth",
|
|
135
148
|
publicUrl: new URL("https://notes.internal"),
|
|
149
|
+
store: Verifier.store(),
|
|
136
150
|
}),
|
|
137
151
|
),
|
|
138
152
|
Layer.provide(FetchHttpClient.layer),
|
|
139
153
|
);
|
|
140
154
|
```
|
|
141
155
|
|
|
142
|
-
Serve this layer with Effect's `HttpRouter` and your Node server layer. If using `@effect/platform-node`, install the release that matches your Effect. The scopes are the same in every deployment, so they are declared with the resource; `NotesResource.layer` takes what varies by deployment and builds the resource once: its verifier and refusals. `NotesResource.provider` is the descriptor's provider, effect-actions' `Authentication.layer`: it verifies every protected route and tool call before decoding, and publishes discovery itself, public and outside authentication; there is no discovery route to merge. Discovery cache policy belongs to the host. Every response to a request it authenticates carries `Cache-Control: no-store`. Middleware of your own runs as effect-actions runs it: router middleware provided after the provider, or an HTTP layer's `middleware`.
|
|
156
|
+
Serve this layer with Effect's `HttpRouter` and your Node server layer. If using `@effect/platform-node`, install the release that matches your Effect. The scopes are the same in every deployment, so they are declared with the resource; `NotesResource.layer` takes what varies by deployment and builds the resource once: its verifier and refusals. Its `store` is the verifier's, as above. The resource holds no running work, so built once it still reads the issuer only in the request that verifies; an application that builds its layers per request, as a Worker may, passes a store made at module scope, so what was read outlives each request. `NotesResource.provider` is the descriptor's provider, effect-actions' `Authentication.layer`: it verifies every protected route and tool call before decoding, and publishes discovery itself, public and outside authentication; there is no discovery route to merge. Discovery cache policy belongs to the host. Every response to a request it authenticates carries `Cache-Control: no-store`. Middleware of your own runs as effect-actions runs it: router middleware provided after the provider, or an HTTP layer's `middleware`.
|
|
143
157
|
|
|
144
158
|
A surface whose callers hold API keys and never run an OAuth flow, such as a second listener beside the one that publishes the resource, sets `discovery: false`: it authenticates the same way, publishes nothing, and its challenge is a plain `Bearer`. It is the same registered resource, so it takes the publishing listener's public URL, not its own address: a public machine listener beside a private admin one still passes the admin URL.
|
|
145
159
|
|
|
@@ -147,7 +161,7 @@ Two listeners with different scopes, such as that admin and machine pair, are st
|
|
|
147
161
|
|
|
148
162
|
A declared resource is a `Resource.Declared<"notes.Login">`, and what its `layer` builds a `Resource.Built`; its descriptor is a `Resource.Login<"notes.Login">`. `Declared` and `Login` take what the descriptor declares as an optional second parameter, `ProviderUnavailable` alone when left out. `Declared` and `Built` take the resource's scopes as an optional parameter, last, which `make` infers from `scopes`: an annotation that leaves it out accepts any scope string. The entry point also exports each type a resource's declarations mention under a flat name (`DeclaredResource`, `BuiltResource`, `ResourceService`, `ResourceDeclaration`, `ResourceOptions`, `Login`, `Admission`, `Refusal`, `Principal`, `Verifier`); `CurrentPrincipal` and its `Caller` are `/session`'s, where declarations name them. So a package that exports its descriptor, its resource or its implementations emits declarations without an annotation.
|
|
149
163
|
|
|
150
|
-
The supplied `HttpClient` must not retry credential exchanges or follow redirects. The SDK overrides only FetchHttpClient's redirect policy to
|
|
164
|
+
The supplied `HttpClient` must not retry credential exchanges or follow redirects. The SDK overrides only FetchHttpClient's redirect policy, to `manual`, and fails a read answered with a redirect (any 3xx) as `ProviderUnavailable`; it preserves other caller-provided fetch defaults and never installs retry middleware. `manual`, not `error`, because Cloudflare Workers rejects `redirect: "error"` before sending ([Request](https://developers.cloudflare.com/workers/runtime-apis/request/#options)).
|
|
151
165
|
|
|
152
166
|
## Scope enforcement
|
|
153
167
|
|
package/dist/resource.d.ts
CHANGED
|
@@ -26,6 +26,11 @@ export interface Options {
|
|
|
26
26
|
* logged once as a warning, since an OAuth client discovering the resource there refuses it.
|
|
27
27
|
*/
|
|
28
28
|
readonly publicUrl: URL;
|
|
29
|
+
/**
|
|
30
|
+
* What the resource's verifier keeps of what it reads, `Verifier.store()`: made once where
|
|
31
|
+
* the process starts, so it outlives a layer built per request.
|
|
32
|
+
*/
|
|
33
|
+
readonly store: Verifier.Store;
|
|
29
34
|
/** `false` refuses API keys without reading the issuer's key list; only OAuth access tokens are accepted. */
|
|
30
35
|
readonly apiKeys?: boolean;
|
|
31
36
|
/**
|
package/dist/resource.js
CHANGED
|
@@ -56,6 +56,7 @@ const build = Effect.fn("Resource.layer")(function* (authentication, { scopes, r
|
|
|
56
56
|
const unbounded = yield* Verifier.make({
|
|
57
57
|
issuer: options.issuer,
|
|
58
58
|
resource: identifier,
|
|
59
|
+
store: options.store,
|
|
59
60
|
requiredScopes: [required],
|
|
60
61
|
apiKeys: options.apiKeys,
|
|
61
62
|
});
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { HttpClient, HttpClientRequest } from "effect/http";
|
|
3
3
|
import { ProviderUnavailable } from "./errors.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Send `request` without following a redirect: a redirect fails the read. `manual`, since
|
|
6
|
+
* Cloudflare Workers rejects `redirect: "error"` before sending.
|
|
7
|
+
*/
|
|
4
8
|
export declare const execute: (client: HttpClient.HttpClient, request: HttpClientRequest.HttpClientRequest) => Effect.Effect<import("effect/http/HttpClientResponse").HttpClientResponse, ProviderUnavailable, import("effect/Scope").Scope>;
|
package/dist/transport.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { Context, Effect } from "effect";
|
|
2
2
|
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
|
|
3
3
|
import { ProviderUnavailable } from "./errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* Send `request` without following a redirect: a redirect fails the read. `manual`, since
|
|
6
|
+
* Cloudflare Workers rejects `redirect: "error"` before sending.
|
|
7
|
+
*/
|
|
4
8
|
export const execute = (client, request) => Effect.contextWith((context) => HttpClient.withScope(client)
|
|
5
9
|
.execute(request)
|
|
6
10
|
.pipe(Effect.provideService(FetchHttpClient.RequestInit, {
|
|
7
11
|
...Context.getOrUndefined(context, FetchHttpClient.RequestInit),
|
|
8
|
-
redirect: "
|
|
9
|
-
}), Effect.mapError((cause) => new ProviderUnavailable({ operation: "http.request", cause }))))
|
|
12
|
+
redirect: "manual",
|
|
13
|
+
}), Effect.mapError((cause) => new ProviderUnavailable({ operation: "http.request", cause })), Effect.filterOrFail((response) => response.status < 300 || response.status >= 400, (response) => new ProviderUnavailable({
|
|
14
|
+
operation: "http.redirect",
|
|
15
|
+
cause: { status: response.status },
|
|
16
|
+
}))));
|
package/dist/verify.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
+
import type { JWK } from "jose";
|
|
2
3
|
import { HttpClient } from "effect/http";
|
|
3
4
|
import { ConfigurationError, InsufficientScope, ProviderUnavailable, Unauthorized } from "./errors.ts";
|
|
4
5
|
import type { AuthenticationError } from "./errors.ts";
|
|
@@ -28,6 +29,8 @@ export interface Principal {
|
|
|
28
29
|
export interface Options {
|
|
29
30
|
readonly issuer: string;
|
|
30
31
|
readonly resource: string;
|
|
32
|
+
/** Where this verifier keeps what it reads and finds what was read before: see `Store`. */
|
|
33
|
+
readonly store: Store;
|
|
31
34
|
readonly requiredScopes?: readonly string[];
|
|
32
35
|
/** `false` refuses API keys without reading the issuer's key list; only OAuth access tokens are accepted. */
|
|
33
36
|
readonly apiKeys?: boolean;
|
|
@@ -35,8 +38,51 @@ export interface Options {
|
|
|
35
38
|
export interface Verifier {
|
|
36
39
|
readonly verifyToken: (token: string) => Effect.Effect<Principal, AuthenticationError>;
|
|
37
40
|
}
|
|
41
|
+
/** One verified key list: the entries it holds and how long they may decide. */
|
|
42
|
+
export interface Keys {
|
|
43
|
+
/** Epoch milliseconds, by this host's clock: when the list was read. */
|
|
44
|
+
readonly readAt: number;
|
|
45
|
+
/** Epoch milliseconds: the end of the issuer's outage window for this list. */
|
|
46
|
+
readonly expiresAt: number;
|
|
47
|
+
readonly entries: ReadonlyMap<string, string>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The last JWKS document read from an issuer: its keys, which signing keys are resolved
|
|
51
|
+
* from, or the operation that failed. Either answers until its lifetime or cooldown ends.
|
|
52
|
+
*/
|
|
53
|
+
export type SettledJwks = {
|
|
54
|
+
readonly readAt: number;
|
|
55
|
+
readonly keys: readonly JWK[];
|
|
56
|
+
} | {
|
|
57
|
+
readonly readAt: number;
|
|
58
|
+
readonly failure: string;
|
|
59
|
+
};
|
|
60
|
+
/** The last key list read for one resource of an issuer, and the last one that verified. */
|
|
61
|
+
export interface SettledKeyList {
|
|
62
|
+
/** Epoch milliseconds: when the last read ended, which holds off the next for the cooldown. */
|
|
63
|
+
readonly readAt: number;
|
|
64
|
+
/** The operation the last read failed at; `undefined` when it succeeded. */
|
|
65
|
+
readonly failure: string | undefined;
|
|
66
|
+
/** The last list that verified, which decides through an outage until it expires. */
|
|
67
|
+
readonly held: Keys | undefined;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* What verifiers have read and decoded, for every verifier given it: JWKS documents by
|
|
71
|
+
* issuer, key lists by issuer and resource. It holds values only, never an Effect, fiber,
|
|
72
|
+
* Promise, Response or timer, so it may live as long as the process, at module scope on a
|
|
73
|
+
* runtime whose I/O belongs to one request, such as Cloudflare Workers. Every read runs in
|
|
74
|
+
* the verification that misses, and writes its result back; concurrent misses each read.
|
|
75
|
+
*/
|
|
76
|
+
export interface Store {
|
|
77
|
+
readonly jwks: Map<string, SettledJwks>;
|
|
78
|
+
readonly keyLists: Map<string, SettledKeyList>;
|
|
79
|
+
}
|
|
80
|
+
/** An empty store: `const store = Verifier.store()`, once, where the process starts. */
|
|
81
|
+
export declare const store: () => Store;
|
|
38
82
|
/**
|
|
39
|
-
*
|
|
83
|
+
* A verifier over `options.store`; transport is selected by the application, not the SDK.
|
|
84
|
+
* It holds nothing but its options, the store and the HTTP client, so acquiring one per
|
|
85
|
+
* request costs nothing: every read it makes runs in the verification that needs it.
|
|
40
86
|
* Verification runs to completion: a deadline is the request edge's policy, which
|
|
41
87
|
* `Resource` applies, so an in-process caller can await a provider call it cannot cancel.
|
|
42
88
|
* The one exception is a key list read, which gives up after three seconds.
|
package/dist/verify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Clock, Duration, Effect,
|
|
1
|
+
import { Clock, Duration, Effect, Result, Schema } from "effect";
|
|
2
2
|
import { createLocalJWKSet, decodeProtectedHeader, jwtVerify } from "jose";
|
|
3
3
|
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
|
|
4
4
|
import { ConfigurationError, InsufficientScope, ProviderUnavailable, Unauthorized, } from "./errors.js";
|
|
@@ -26,9 +26,11 @@ const Claims = Schema.Struct({
|
|
|
26
26
|
/** The list document as the issuer's `keyList` action answers it. */
|
|
27
27
|
const KeyListResponse = Schema.Struct({ list: Schema.String });
|
|
28
28
|
const KeyListClaims = KeyList.Claims.pipe(Schema.fieldsAssign({ exp: Schema.Finite }));
|
|
29
|
+
/** An empty store: `const store = Verifier.store()`, once, where the process starts. */
|
|
30
|
+
export const store = () => ({ jwks: new Map(), keyLists: new Map() });
|
|
29
31
|
const unauthorized = (cause) => new Unauthorized({ cause });
|
|
30
32
|
/** How long a JWKS document answers verification before it is read again. */
|
|
31
|
-
const documentLifetime =
|
|
33
|
+
const documentLifetime = Duration.minutes(1);
|
|
32
34
|
/**
|
|
33
35
|
* How long a read holds off the next one: a failed read of either document, and any read
|
|
34
36
|
* of the key list, so a key the held list does not name reaches the issuer at most this often.
|
|
@@ -40,13 +42,16 @@ const cooldown = Duration.seconds(5);
|
|
|
40
42
|
*/
|
|
41
43
|
const listReadTimeout = "3 seconds";
|
|
42
44
|
/**
|
|
43
|
-
*
|
|
45
|
+
* A verifier over `options.store`; transport is selected by the application, not the SDK.
|
|
46
|
+
* It holds nothing but its options, the store and the HTTP client, so acquiring one per
|
|
47
|
+
* request costs nothing: every read it makes runs in the verification that needs it.
|
|
44
48
|
* Verification runs to completion: a deadline is the request edge's policy, which
|
|
45
49
|
* `Resource` applies, so an in-process caller can await a provider call it cannot cancel.
|
|
46
50
|
* The one exception is a key list read, which gives up after three seconds.
|
|
47
51
|
*/
|
|
48
52
|
export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
49
53
|
const client = yield* HttpClient.HttpClient;
|
|
54
|
+
const listed = JSON.stringify([options.issuer, options.resource]);
|
|
50
55
|
const endpoints = yield* Effect.try({
|
|
51
56
|
try: () => ({
|
|
52
57
|
jwks: new URL(`${options.issuer}/jwks`).href,
|
|
@@ -62,15 +67,33 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
62
67
|
cause: { status: response.status },
|
|
63
68
|
});
|
|
64
69
|
const jwks = yield* HttpClientResponse.schemaBodyJson(Jwks)(response).pipe(Effect.mapError((cause) => new ProviderUnavailable({ operation: "jwks.decode", cause })));
|
|
65
|
-
return
|
|
66
|
-
keys: jwks.keys.map(({ key_ops, ...key }) => key_ops ? { ...key, key_ops: [...key_ops] } : key),
|
|
67
|
-
});
|
|
70
|
+
return jwks.keys.map(({ key_ops, ...key }) => key_ops ? { ...key, key_ops: [...key_ops] } : key);
|
|
68
71
|
}, Effect.scoped);
|
|
69
|
-
|
|
72
|
+
/** The keys read within their lifetime, a failed read's failure within its cooldown, or a read now. */
|
|
73
|
+
const keys = Effect.gen(function* () {
|
|
74
|
+
const settled = options.store.jwks.get(options.issuer);
|
|
75
|
+
const now = yield* Clock.currentTimeMillis;
|
|
76
|
+
if (settled !== undefined) {
|
|
77
|
+
const answers = "keys" in settled ? documentLifetime : cooldown;
|
|
78
|
+
if (now - settled.readAt < Duration.toMillis(answers))
|
|
79
|
+
return "keys" in settled
|
|
80
|
+
? settled.keys
|
|
81
|
+
: yield* new ProviderUnavailable({ operation: settled.failure });
|
|
82
|
+
}
|
|
83
|
+
const read = yield* Effect.result(document());
|
|
84
|
+
const readAt = yield* Clock.currentTimeMillis;
|
|
85
|
+
// why: a failure never replaces an outcome that ended after it began; a success always writes.
|
|
86
|
+
if (Result.isSuccess(read) ||
|
|
87
|
+
(options.store.jwks.get(options.issuer)?.readAt ?? -Infinity) < now)
|
|
88
|
+
options.store.jwks.set(options.issuer, Result.isSuccess(read)
|
|
89
|
+
? { readAt, keys: read.success }
|
|
90
|
+
: { readAt, failure: read.failure.operation });
|
|
91
|
+
return Result.isSuccess(read) ? read.success : yield* read.failure;
|
|
92
|
+
});
|
|
70
93
|
const signingKey = Effect.fn("Verifier.signingKey")(function* (kid) {
|
|
71
|
-
const
|
|
94
|
+
const jwks = { keys: [...(yield* keys)] };
|
|
72
95
|
return yield* Effect.tryPromise({
|
|
73
|
-
try: () =>
|
|
96
|
+
try: () => createLocalJWKSet(jwks)({ alg: "EdDSA", kid }),
|
|
74
97
|
catch: unauthorized,
|
|
75
98
|
});
|
|
76
99
|
});
|
|
@@ -132,20 +155,43 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
132
155
|
entries: new Map(claims.keys.map((entry) => [entry.id, entry.sealed])),
|
|
133
156
|
};
|
|
134
157
|
}, Effect.scoped);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
158
|
+
/**
|
|
159
|
+
* Read the key list, unless a read ended within the cooldown: then that read's outcome
|
|
160
|
+
* stands. A list that verified is always written back; a failure is not, over a read that
|
|
161
|
+
* ended after it began. A failure is logged once until a read succeeds again.
|
|
162
|
+
*/
|
|
163
|
+
const readList = Effect.gen(function* () {
|
|
164
|
+
const settled = options.store.keyLists.get(listed);
|
|
165
|
+
const startedAt = yield* Clock.currentTimeMillis;
|
|
166
|
+
if (settled !== undefined && startedAt - settled.readAt < Duration.toMillis(cooldown))
|
|
167
|
+
return settled.failure === undefined
|
|
168
|
+
? undefined
|
|
169
|
+
: yield* new ProviderUnavailable({ operation: settled.failure });
|
|
170
|
+
const read = yield* Effect.result(listDocument().pipe(Effect.timeoutOrElse({
|
|
171
|
+
duration: listReadTimeout,
|
|
172
|
+
orElse: () => Effect.fail(new ProviderUnavailable({ operation: "key-list.timeout" })),
|
|
173
|
+
})));
|
|
174
|
+
const readAt = yield* Clock.currentTimeMillis;
|
|
175
|
+
const before = options.store.keyLists.get(listed);
|
|
176
|
+
const failing = before?.failure !== undefined;
|
|
177
|
+
if (Result.isSuccess(read)) {
|
|
178
|
+
options.store.keyLists.set(listed, { readAt, failure: undefined, held: read.success });
|
|
179
|
+
if (failing)
|
|
180
|
+
yield* Effect.logInfo("clankerauth key list reads have recovered");
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
if ((before?.readAt ?? -Infinity) >= startedAt)
|
|
184
|
+
return yield* read.failure;
|
|
185
|
+
const { operation } = read.failure;
|
|
186
|
+
options.store.keyLists.set(listed, { readAt, failure: operation, held: before?.held });
|
|
187
|
+
if (!failing)
|
|
188
|
+
yield* Effect.logWarning("clankerauth key list reads are failing; a held list decides until it expires", { operation });
|
|
189
|
+
return yield* read.failure;
|
|
190
|
+
});
|
|
143
191
|
/** The list read now, or within the cooldown, or else the held one, while inside its window. */
|
|
144
192
|
const latest = Effect.gen(function* () {
|
|
145
193
|
const fetched = yield* Effect.result(readList);
|
|
146
|
-
|
|
147
|
-
yield* Ref.set(held, fetched.success);
|
|
148
|
-
const list = yield* Ref.get(held);
|
|
194
|
+
const list = options.store.keyLists.get(listed)?.held;
|
|
149
195
|
if (list !== undefined && list.expiresAt > (yield* Clock.currentTimeMillis))
|
|
150
196
|
return list;
|
|
151
197
|
return yield* Result.isFailure(fetched)
|
|
@@ -158,7 +204,7 @@ export const make = Effect.fn("Verifier.make")(function* (options) {
|
|
|
158
204
|
catch: unauthorized,
|
|
159
205
|
});
|
|
160
206
|
const now = yield* Clock.currentTimeMillis;
|
|
161
|
-
const current =
|
|
207
|
+
const current = options.store.keyLists.get(listed)?.held;
|
|
162
208
|
const list = current !== undefined &&
|
|
163
209
|
now - current.readAt < Duration.toMillis(keyListRefresh) &&
|
|
164
210
|
current.expiresAt > now
|
package/package.json
CHANGED