@mostra/runtime 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +39 -2
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-CzBkUyHu.js +15920 -0
- package/dist/instance-CzBkUyHu.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -3
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +760 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/identity.ts +101 -0
- package/src/gateway/index.ts +1299 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +979 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1013 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +187 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +596 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +518 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability tokens, sessions and frame tickets (D97, D98, HY-03): the gateway's authority over
|
|
3
|
+
* who may use an artifact. Tokens are minted with secrets derived from the home's capability key
|
|
4
|
+
* and stored only as hashes; sessions are stored as hashes of their cookies and bound to the one
|
|
5
|
+
* origin they were issued for; tickets live in memory for 60 seconds, work once and only while the
|
|
6
|
+
* shell session that asked for them lives. A link works only at the shell address it was minted
|
|
7
|
+
* for. Every check reads the ledger, so a revocation applies to the very next request.
|
|
8
|
+
*/
|
|
9
|
+
import { createHash, createHmac, randomBytes } from "node:crypto";
|
|
10
|
+
import {
|
|
11
|
+
chmodSync,
|
|
12
|
+
closeSync,
|
|
13
|
+
fsyncSync,
|
|
14
|
+
mkdirSync,
|
|
15
|
+
openSync,
|
|
16
|
+
readFileSync,
|
|
17
|
+
renameSync,
|
|
18
|
+
writeSync,
|
|
19
|
+
} from "node:fs";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
import {
|
|
22
|
+
ALL_SCOPES,
|
|
23
|
+
type CapabilityScope,
|
|
24
|
+
normalizeScopes,
|
|
25
|
+
type Refusal,
|
|
26
|
+
refusal,
|
|
27
|
+
safeEqual,
|
|
28
|
+
TokenId,
|
|
29
|
+
} from "@mostra/protocol";
|
|
30
|
+
import { appendEvent } from "../interaction/index.ts";
|
|
31
|
+
import type { Ledger, LedgerDb } from "../ledger/index.ts";
|
|
32
|
+
import type { Log } from "../log.ts";
|
|
33
|
+
|
|
34
|
+
/** Crockford base32, the alphabet of every Mostra ID body (D77). */
|
|
35
|
+
const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
36
|
+
|
|
37
|
+
/** The length of every secret: 26 Crockford characters, 130 bits. */
|
|
38
|
+
export const SECRET_LENGTH = 26;
|
|
39
|
+
|
|
40
|
+
/** How long a frame ticket works (D98). */
|
|
41
|
+
export const TICKET_TTL_MS = 60_000;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Failed capability tokens tolerated per artifact and minute (D97 item 8). Tickets don't count:
|
|
45
|
+
* they are single-use 130-bit secrets that live for a minute, and any page on the host could spend
|
|
46
|
+
* a count by loading an entry URL.
|
|
47
|
+
*/
|
|
48
|
+
export const FAILURE_LIMIT = 30;
|
|
49
|
+
const FAILURE_WINDOW_MS = 60_000;
|
|
50
|
+
|
|
51
|
+
/** How long a session the browser no longer presents is kept: the cookie's own lifetime. */
|
|
52
|
+
export const SESSION_MAX_AGE_S: number = 400 * 24 * 60 * 60;
|
|
53
|
+
|
|
54
|
+
/** How often a session's `last_seen_at` is written at most. */
|
|
55
|
+
const TOUCH_INTERVAL_MS = 60 * 60 * 1000;
|
|
56
|
+
|
|
57
|
+
/** Encodes the first 130 bits of `bytes` as 26 Crockford base32 characters. */
|
|
58
|
+
function crockford(bytes: Uint8Array): string {
|
|
59
|
+
let bits = 0;
|
|
60
|
+
let value = 0;
|
|
61
|
+
let out = "";
|
|
62
|
+
for (const byte of bytes) {
|
|
63
|
+
value = ((value << 8) | byte) & 0xffff;
|
|
64
|
+
bits += 8;
|
|
65
|
+
while (bits >= 5 && out.length < SECRET_LENGTH) {
|
|
66
|
+
out += CROCKFORD[(value >>> (bits - 5)) & 31];
|
|
67
|
+
bits -= 5;
|
|
68
|
+
}
|
|
69
|
+
if (out.length === SECRET_LENGTH) break;
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A fresh random secret: a session cookie, a CSRF token or a ticket (130 bits). */
|
|
75
|
+
export function randomSecret(): string {
|
|
76
|
+
return crockford(randomBytes(17));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The SHA-256 of a secret, as stored (lowercase hex). */
|
|
80
|
+
export function hashSecret(secret: string): string {
|
|
81
|
+
return createHash("sha256").update(secret, "utf8").digest("hex");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A session's CSRF token, derived from its cookie (D98 item 2). The ledger holds neither: only
|
|
86
|
+
* the SHA-256 of the cookie (the session's ID) and of the CSRF token, and neither hash leads to
|
|
87
|
+
* the other. Whoever holds the cookie already holds the session, so deriving costs nothing.
|
|
88
|
+
*/
|
|
89
|
+
export function csrfOf(cookie: string): string {
|
|
90
|
+
return crockford(createHash("sha256").update(`mostra csrf ${cookie}`, "utf8").digest());
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Whether a value has the shape of a secret, checked before any lookup. */
|
|
94
|
+
export function looksLikeSecret(value: unknown): value is string {
|
|
95
|
+
return (
|
|
96
|
+
typeof value === "string" &&
|
|
97
|
+
value.length === SECRET_LENGTH &&
|
|
98
|
+
/^[0-9A-HJKMNP-TV-Z]+$/.test(value)
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Reads the home's capability key (`<home>/keys/capability`, 32 random bytes, mode 0600),
|
|
104
|
+
* creating it when it doesn't exist. A key that can't be read is replaced: links minted under it
|
|
105
|
+
* still verify by their hashes; only showing them again needs the key (D97 item 2).
|
|
106
|
+
*/
|
|
107
|
+
export function capabilityKey(home: string, log: Log): Buffer {
|
|
108
|
+
const dir = join(home, "keys");
|
|
109
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
110
|
+
chmodSync(dir, 0o700);
|
|
111
|
+
const file = join(dir, "capability");
|
|
112
|
+
try {
|
|
113
|
+
const key = readFileSync(file);
|
|
114
|
+
if (key.length === 32) return key;
|
|
115
|
+
log.warn("gateway.key_replaced", { reason: `the key file has ${key.length} bytes` });
|
|
116
|
+
} catch (error) {
|
|
117
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
118
|
+
if (code !== "ENOENT") log.warn("gateway.key_replaced", { reason: code ?? "unreadable" });
|
|
119
|
+
}
|
|
120
|
+
const key = randomBytes(32);
|
|
121
|
+
// Written aside, synced and renamed, so a crash never leaves a short key behind.
|
|
122
|
+
const aside = `${file}.${process.pid}.tmp`;
|
|
123
|
+
const fd = openSync(aside, "w", 0o600);
|
|
124
|
+
try {
|
|
125
|
+
writeSync(fd, key);
|
|
126
|
+
fsyncSync(fd);
|
|
127
|
+
} finally {
|
|
128
|
+
closeSync(fd);
|
|
129
|
+
}
|
|
130
|
+
renameSync(aside, file);
|
|
131
|
+
chmodSync(file, 0o600);
|
|
132
|
+
return key;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** What a token grants (DATA_MODEL §3 `token`). */
|
|
136
|
+
export interface Grant {
|
|
137
|
+
readonly tokenId: string;
|
|
138
|
+
readonly artifactId: string;
|
|
139
|
+
readonly scopes: readonly CapabilityScope[];
|
|
140
|
+
/** When it stops working, if it does. */
|
|
141
|
+
readonly expiresAt?: string;
|
|
142
|
+
/** The version number its link names, if it pins one. */
|
|
143
|
+
readonly pinnedVersion?: number;
|
|
144
|
+
/** Whether it is the artifact's default link. */
|
|
145
|
+
readonly isDefault: boolean;
|
|
146
|
+
/** The shell origin the link was minted for; it works only there. */
|
|
147
|
+
readonly shellOrigin: string;
|
|
148
|
+
/** The viewer the link acts for (D104); absent: the home's local user. */
|
|
149
|
+
readonly viewerId?: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A token with its secret, just minted or derived again. */
|
|
153
|
+
export interface MintedToken extends Grant {
|
|
154
|
+
readonly secret: string;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** A session as a request presents it, resolved. */
|
|
158
|
+
export interface ResolvedSession {
|
|
159
|
+
/** The session's ID (the SHA-256 of its cookie). */
|
|
160
|
+
readonly id: string;
|
|
161
|
+
readonly csrf: string;
|
|
162
|
+
/** The version a frame session entered (D98 item 5). */
|
|
163
|
+
readonly versionId?: string;
|
|
164
|
+
readonly grant: Grant;
|
|
165
|
+
/**
|
|
166
|
+
* The viewer the session acts as: its link's viewer (D104), else the one the host's identity
|
|
167
|
+
* port named when the link was exchanged (D143 item 2); absent: the home's local user.
|
|
168
|
+
*/
|
|
169
|
+
readonly viewerId?: string;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** What a redeemed frame ticket carries. */
|
|
173
|
+
export interface TicketGrant {
|
|
174
|
+
readonly tokenId: string;
|
|
175
|
+
readonly artifactId: string;
|
|
176
|
+
readonly versionId: string;
|
|
177
|
+
/** The artifact origin the ticket is good for. */
|
|
178
|
+
readonly origin: string;
|
|
179
|
+
/** The shell origin that minted it, which the frame's bridge talks to. */
|
|
180
|
+
readonly shellOrigin: string;
|
|
181
|
+
/** The shell session that asked for it: the ticket works only while that session lives. */
|
|
182
|
+
readonly shellSessionId: string;
|
|
183
|
+
/** The viewer that shell session acts as, which the frame's session acts as too (D143). */
|
|
184
|
+
readonly viewerId?: string | undefined;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
interface TokenRow {
|
|
188
|
+
id: string;
|
|
189
|
+
secret_hash: string;
|
|
190
|
+
artifact_id: string;
|
|
191
|
+
scopes_json: string;
|
|
192
|
+
expires_at: string | null;
|
|
193
|
+
revoked_at: string | null;
|
|
194
|
+
is_default: number;
|
|
195
|
+
pinned_version: number | null;
|
|
196
|
+
shell_origin: string;
|
|
197
|
+
viewer_id: string | null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface SessionRow {
|
|
201
|
+
id: string;
|
|
202
|
+
token_id: string;
|
|
203
|
+
origin: string;
|
|
204
|
+
csrf: string;
|
|
205
|
+
version_id: string | null;
|
|
206
|
+
viewer_id: string | null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const grantOf = (row: TokenRow): Grant => ({
|
|
210
|
+
tokenId: row.id,
|
|
211
|
+
artifactId: row.artifact_id,
|
|
212
|
+
scopes: JSON.parse(row.scopes_json) as CapabilityScope[],
|
|
213
|
+
...(row.expires_at !== null ? { expiresAt: row.expires_at } : {}),
|
|
214
|
+
...(row.pinned_version !== null ? { pinnedVersion: row.pinned_version } : {}),
|
|
215
|
+
isDefault: row.is_default === 1,
|
|
216
|
+
shellOrigin: row.shell_origin,
|
|
217
|
+
...(row.viewer_id !== null ? { viewerId: row.viewer_id } : {}),
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
/** Options for {@link Capabilities}. */
|
|
221
|
+
export interface CapabilitiesOptions {
|
|
222
|
+
readonly ledger: Ledger;
|
|
223
|
+
readonly log: Log;
|
|
224
|
+
/** The home's capability key. */
|
|
225
|
+
readonly key: Buffer;
|
|
226
|
+
/** The clock, in milliseconds since the epoch. */
|
|
227
|
+
readonly now: () => number;
|
|
228
|
+
/** The shell's origin, once the gateway listens: links are minted for it and work only there. */
|
|
229
|
+
readonly shellOrigin: () => string;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The capability authority of one instance. */
|
|
233
|
+
export class Capabilities {
|
|
234
|
+
readonly #ledger: Ledger;
|
|
235
|
+
readonly #log: Log;
|
|
236
|
+
readonly #key: Buffer;
|
|
237
|
+
readonly #now: () => number;
|
|
238
|
+
readonly #shellOrigin: () => string;
|
|
239
|
+
readonly #tickets = new Map<string, TicketGrant & { readonly expires: number }>();
|
|
240
|
+
/** One-time launch tickets (D140 item 15), by the SHA-256 of their secret. */
|
|
241
|
+
readonly #launches = new Map<string, { readonly tokenId: string; readonly expires: number }>();
|
|
242
|
+
/** Failure times per artifact (`""` for tokens of no artifact in this home). */
|
|
243
|
+
readonly #failures = new Map<string, number[]>();
|
|
244
|
+
readonly #touched = new Map<string, number>();
|
|
245
|
+
|
|
246
|
+
constructor(options: CapabilitiesOptions) {
|
|
247
|
+
this.#ledger = options.ledger;
|
|
248
|
+
this.#log = options.log;
|
|
249
|
+
this.#key = options.key;
|
|
250
|
+
this.#now = options.now;
|
|
251
|
+
this.#shellOrigin = options.shellOrigin;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Runs `fn` in the caller's transaction, or in a new one. */
|
|
255
|
+
#tx<T>(db: LedgerDb | undefined, fn: (db: LedgerDb) => T): T {
|
|
256
|
+
return db ? fn(db) : this.#ledger.write(fn);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** The secret of a token, derived from its ID under the home's key (D97 item 2). */
|
|
260
|
+
secretOf(tokenId: string): string {
|
|
261
|
+
return crockford(
|
|
262
|
+
createHmac("sha256", this.#key).update(`mostra capability ${tokenId}`).digest(),
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
#iso(ms: number): string {
|
|
267
|
+
return new Date(ms).toISOString();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
#live(row: TokenRow): boolean {
|
|
271
|
+
return (
|
|
272
|
+
row.revoked_at === null &&
|
|
273
|
+
(row.expires_at === null || Date.parse(row.expires_at) > this.#now())
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
#insert(
|
|
278
|
+
db: LedgerDb,
|
|
279
|
+
input: {
|
|
280
|
+
readonly artifactId: string;
|
|
281
|
+
readonly scopes: readonly CapabilityScope[];
|
|
282
|
+
readonly expiresAt?: string | undefined;
|
|
283
|
+
readonly pinnedVersion?: number | undefined;
|
|
284
|
+
readonly isDefault: boolean;
|
|
285
|
+
readonly tokenId?: string;
|
|
286
|
+
readonly viewerId?: string | undefined;
|
|
287
|
+
},
|
|
288
|
+
): MintedToken {
|
|
289
|
+
const tokenId = input.tokenId ?? TokenId.create();
|
|
290
|
+
const secret = this.secretOf(tokenId);
|
|
291
|
+
const scopes = normalizeScopes(input.scopes);
|
|
292
|
+
const shellOrigin = this.#shellOrigin();
|
|
293
|
+
db.run(
|
|
294
|
+
"INSERT INTO token (id, secret_hash, artifact_id, scopes_json, viewer_id, expires_at, revoked_at, created_at, is_default, pinned_version, shell_origin) VALUES (?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?)",
|
|
295
|
+
tokenId,
|
|
296
|
+
hashSecret(secret),
|
|
297
|
+
input.artifactId,
|
|
298
|
+
JSON.stringify(scopes),
|
|
299
|
+
input.viewerId ?? null,
|
|
300
|
+
input.expiresAt ?? null,
|
|
301
|
+
this.#iso(this.#now()),
|
|
302
|
+
input.isDefault ? 1 : 0,
|
|
303
|
+
input.pinnedVersion ?? null,
|
|
304
|
+
shellOrigin,
|
|
305
|
+
);
|
|
306
|
+
return {
|
|
307
|
+
tokenId,
|
|
308
|
+
secret,
|
|
309
|
+
artifactId: input.artifactId,
|
|
310
|
+
scopes,
|
|
311
|
+
...(input.expiresAt !== undefined ? { expiresAt: input.expiresAt } : {}),
|
|
312
|
+
...(input.pinnedVersion !== undefined ? { pinnedVersion: input.pinnedVersion } : {}),
|
|
313
|
+
isDefault: input.isDefault,
|
|
314
|
+
shellOrigin,
|
|
315
|
+
...(input.viewerId !== undefined ? { viewerId: input.viewerId } : {}),
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Mints a new token (`view/url`), in the caller's transaction when it passes one. */
|
|
320
|
+
mint(
|
|
321
|
+
input: {
|
|
322
|
+
readonly artifactId: string;
|
|
323
|
+
readonly scopes?: readonly CapabilityScope[] | undefined;
|
|
324
|
+
readonly ttlMs?: number | undefined;
|
|
325
|
+
readonly pinnedVersion?: number | undefined;
|
|
326
|
+
/** The viewer the link acts for (D104). */
|
|
327
|
+
readonly viewerId?: string | undefined;
|
|
328
|
+
},
|
|
329
|
+
within?: LedgerDb,
|
|
330
|
+
): MintedToken {
|
|
331
|
+
return this.#tx(within, (db) =>
|
|
332
|
+
this.#insert(db, {
|
|
333
|
+
artifactId: input.artifactId,
|
|
334
|
+
scopes: input.scopes ?? ALL_SCOPES,
|
|
335
|
+
expiresAt: input.ttlMs === undefined ? undefined : this.#iso(this.#now() + input.ttlMs),
|
|
336
|
+
pinnedVersion: input.pinnedVersion,
|
|
337
|
+
isDefault: false,
|
|
338
|
+
viewerId: input.viewerId,
|
|
339
|
+
}),
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The artifact's default link (D97 item 3): the live default token, shown again from its
|
|
345
|
+
* derived secret, or a new one. A default whose secret the current key no longer derives (the
|
|
346
|
+
* key was replaced) stays valid for whoever holds it, and a new default takes its place; so does
|
|
347
|
+
* one minted for another shell address, which works again if the gateway returns there.
|
|
348
|
+
*/
|
|
349
|
+
defaultLink(artifactId: string): MintedToken {
|
|
350
|
+
return this.#ledger.write((db) => {
|
|
351
|
+
const row = db.get<TokenRow>(
|
|
352
|
+
"SELECT * FROM token WHERE artifact_id = ? AND is_default = 1",
|
|
353
|
+
artifactId,
|
|
354
|
+
);
|
|
355
|
+
if (row) {
|
|
356
|
+
const secret = this.secretOf(row.id);
|
|
357
|
+
const here = row.shell_origin === this.#shellOrigin();
|
|
358
|
+
if (here && safeEqual(hashSecret(secret), row.secret_hash)) {
|
|
359
|
+
return { ...grantOf(row), secret };
|
|
360
|
+
}
|
|
361
|
+
db.run("UPDATE token SET is_default = 0 WHERE id = ?", row.id);
|
|
362
|
+
this.#log.warn("gateway.default_link_replaced", {
|
|
363
|
+
artifactId,
|
|
364
|
+
tokenId: row.id,
|
|
365
|
+
reason: here ? "the capability key changed" : "the shell's address changed",
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
return this.#insert(db, { artifactId, scopes: ALL_SCOPES, isDefault: true });
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** A token's grant, live or not; `undefined` when no such token exists. */
|
|
373
|
+
grant(tokenId: string): (Grant & { readonly live: boolean }) | undefined {
|
|
374
|
+
const row = this.#ledger.read((db) =>
|
|
375
|
+
db.get<TokenRow>("SELECT * FROM token WHERE id = ?", tokenId),
|
|
376
|
+
);
|
|
377
|
+
return row ? { ...grantOf(row), live: this.#live(row) } : undefined;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** The token's link again, from its derived secret, when the current key still derives it. */
|
|
381
|
+
reveal(tokenId: string): MintedToken | undefined {
|
|
382
|
+
const row = this.#ledger.read((db) =>
|
|
383
|
+
db.get<TokenRow>("SELECT * FROM token WHERE id = ?", tokenId),
|
|
384
|
+
);
|
|
385
|
+
if (!row) return undefined;
|
|
386
|
+
const secret = this.secretOf(row.id);
|
|
387
|
+
return safeEqual(hashSecret(secret), row.secret_hash) ? { ...grantOf(row), secret } : undefined;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Revokes a token and its sessions (`view/revoke`); revoking a revoked token is a no-op. */
|
|
391
|
+
revoke(tokenId: string, within?: LedgerDb): "revoked" | "already" | "unknown" {
|
|
392
|
+
return this.#tx(within, (db) => {
|
|
393
|
+
const row = db.get<TokenRow>("SELECT * FROM token WHERE id = ?", tokenId);
|
|
394
|
+
if (!row) return "unknown";
|
|
395
|
+
if (row.revoked_at !== null) return "already";
|
|
396
|
+
// Its sessions stay, so they keep answering 410 rather than 401 (FAILURE_MODEL §8).
|
|
397
|
+
db.run(
|
|
398
|
+
"UPDATE token SET revoked_at = ?, is_default = 0 WHERE id = ?",
|
|
399
|
+
this.#iso(this.#now()),
|
|
400
|
+
tokenId,
|
|
401
|
+
);
|
|
402
|
+
appendEvent(db, {
|
|
403
|
+
artifactId: row.artifact_id,
|
|
404
|
+
type: "token.revoked",
|
|
405
|
+
payload: { tokenId, reason: "revoked" },
|
|
406
|
+
});
|
|
407
|
+
return "revoked";
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Replaces a live token with a new one of the same artifact, scopes, expiry, pinned version and viewer,
|
|
413
|
+
* in one transaction (`view/rotate`, D97 item 6). Returns `undefined` for an unknown token and
|
|
414
|
+
* `"dead"` for a revoked or expired one.
|
|
415
|
+
*/
|
|
416
|
+
rotate(tokenId: string, within?: LedgerDb): MintedToken | "dead" | undefined {
|
|
417
|
+
return this.#tx(within, (db) => {
|
|
418
|
+
const row = db.get<TokenRow>("SELECT * FROM token WHERE id = ?", tokenId);
|
|
419
|
+
if (!row) return undefined;
|
|
420
|
+
if (!this.#live(row)) return "dead";
|
|
421
|
+
const grant = grantOf(row);
|
|
422
|
+
const replacement = TokenId.create();
|
|
423
|
+
// The old token stops being the default first (one default per artifact), in the only update
|
|
424
|
+
// a revocation makes: a revoked token never changes again (the ledger's trigger).
|
|
425
|
+
db.run(
|
|
426
|
+
"UPDATE token SET revoked_at = ?, is_default = 0, replaced_by = ? WHERE id = ?",
|
|
427
|
+
this.#iso(this.#now()),
|
|
428
|
+
replacement,
|
|
429
|
+
tokenId,
|
|
430
|
+
);
|
|
431
|
+
const minted = this.#insert(db, {
|
|
432
|
+
tokenId: replacement,
|
|
433
|
+
artifactId: grant.artifactId,
|
|
434
|
+
scopes: grant.scopes,
|
|
435
|
+
expiresAt: grant.expiresAt,
|
|
436
|
+
pinnedVersion: grant.pinnedVersion,
|
|
437
|
+
isDefault: grant.isDefault,
|
|
438
|
+
viewerId: grant.viewerId,
|
|
439
|
+
});
|
|
440
|
+
appendEvent(db, {
|
|
441
|
+
artifactId: grant.artifactId,
|
|
442
|
+
type: "token.revoked",
|
|
443
|
+
payload: { tokenId, reason: "rotated", replacedBy: minted.tokenId },
|
|
444
|
+
});
|
|
445
|
+
return minted;
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* A refusal, with its `Retry-After` seconds, while too many tokens for `artifactId` failed in the
|
|
451
|
+
* last minute (D97 item 8). One artifact's limit never holds another's links back.
|
|
452
|
+
*/
|
|
453
|
+
limited(artifactId: string): (Refusal & { readonly retryAfter: number }) | undefined {
|
|
454
|
+
const now = this.#now();
|
|
455
|
+
const failures = this.#failures.get(artifactId);
|
|
456
|
+
if (!failures) return undefined;
|
|
457
|
+
while (failures.length > 0 && (failures[0] ?? 0) <= now - FAILURE_WINDOW_MS) failures.shift();
|
|
458
|
+
if (failures.length === 0) {
|
|
459
|
+
this.#failures.delete(artifactId);
|
|
460
|
+
return undefined;
|
|
461
|
+
}
|
|
462
|
+
if (failures.length < FAILURE_LIMIT) return undefined;
|
|
463
|
+
const retry = Math.max(1, Math.ceil(((failures[0] ?? now) + FAILURE_WINDOW_MS - now) / 1000));
|
|
464
|
+
return {
|
|
465
|
+
...refusal("MOSTRA_HYGIENE_RATE_LIMITED", `Too many failed attempts; retry in ${retry} s.`),
|
|
466
|
+
retryAfter: retry,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** Counts a failed token for `artifactId`; a token for no artifact of this home counts once, under `""`. */
|
|
471
|
+
#fail(artifactId: string): void {
|
|
472
|
+
const known =
|
|
473
|
+
artifactId !== "" &&
|
|
474
|
+
this.#ledger.read((db) => db.get("SELECT 1 FROM artifact WHERE id = ?", artifactId)) !==
|
|
475
|
+
undefined;
|
|
476
|
+
const key = known ? artifactId : "";
|
|
477
|
+
const now = this.#now();
|
|
478
|
+
const failures = (this.#failures.get(key) ?? []).filter((at) => at > now - FAILURE_WINDOW_MS);
|
|
479
|
+
failures.push(now);
|
|
480
|
+
this.#failures.set(key, failures);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Checks a capability token presented for `artifactId`. While the artifact's guessing limit
|
|
485
|
+
* holds, every token is refused, a right one too, and the limit is checked here, at the moment
|
|
486
|
+
* of the check, so concurrent guesses can't slip past it. An unknown secret, or one for another
|
|
487
|
+
* artifact, is `MOSTRA_HYGIENE_TOKEN_INVALID`; a revoked or expired one, or one minted for
|
|
488
|
+
* another shell address, `_TOKEN_REVOKED`. Unknown secrets count towards the limit.
|
|
489
|
+
*/
|
|
490
|
+
presentToken(
|
|
491
|
+
secret: unknown,
|
|
492
|
+
artifactId: string,
|
|
493
|
+
): Grant | (Refusal & { readonly retryAfter?: number }) {
|
|
494
|
+
const limited = this.limited(artifactId);
|
|
495
|
+
if (limited) return limited;
|
|
496
|
+
if (!looksLikeSecret(secret)) {
|
|
497
|
+
this.#fail(artifactId);
|
|
498
|
+
return refusal(
|
|
499
|
+
"MOSTRA_HYGIENE_TOKEN_INVALID",
|
|
500
|
+
"The capability token isn't one Mostra minted.",
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
const row = this.#ledger.read((db) =>
|
|
504
|
+
db.get<TokenRow>("SELECT * FROM token WHERE secret_hash = ?", hashSecret(secret)),
|
|
505
|
+
);
|
|
506
|
+
if (!row || row.artifact_id !== artifactId) {
|
|
507
|
+
this.#fail(artifactId);
|
|
508
|
+
return refusal(
|
|
509
|
+
"MOSTRA_HYGIENE_TOKEN_INVALID",
|
|
510
|
+
"No capability token in this home matches this link.",
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
if (!this.#live(row)) {
|
|
514
|
+
return refusal(
|
|
515
|
+
"MOSTRA_HYGIENE_TOKEN_REVOKED",
|
|
516
|
+
row.revoked_at !== null ? "This link was revoked." : "This link has expired.",
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
if (row.shell_origin !== this.#shellOrigin()) {
|
|
520
|
+
return refusal(
|
|
521
|
+
"MOSTRA_HYGIENE_TOKEN_REVOKED",
|
|
522
|
+
`This link was made for ${row.shell_origin}, an address this Mostra no longer serves.`,
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
return grantOf(row);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/** Starts a session for a grant on one origin; returns the cookie secret and CSRF token. */
|
|
529
|
+
createSession(input: {
|
|
530
|
+
readonly tokenId: string;
|
|
531
|
+
readonly origin: string;
|
|
532
|
+
readonly versionId?: string;
|
|
533
|
+
/** The person the host's identity port named, for a link without a viewer (D143 item 2). */
|
|
534
|
+
readonly viewerId?: string | undefined;
|
|
535
|
+
}): {
|
|
536
|
+
readonly cookie: string;
|
|
537
|
+
readonly csrf: string;
|
|
538
|
+
readonly id: string;
|
|
539
|
+
} {
|
|
540
|
+
const cookie = randomSecret();
|
|
541
|
+
const csrf = csrfOf(cookie);
|
|
542
|
+
const id = hashSecret(cookie);
|
|
543
|
+
const now = this.#iso(this.#now());
|
|
544
|
+
this.#ledger.write((db) =>
|
|
545
|
+
db.run(
|
|
546
|
+
"INSERT INTO session (id, token_id, origin, mode, csrf, version_id, created_at, last_seen_at, viewer_id) VALUES (?, ?, ?, 'cookie', ?, ?, ?, ?, ?)",
|
|
547
|
+
id,
|
|
548
|
+
input.tokenId,
|
|
549
|
+
input.origin,
|
|
550
|
+
hashSecret(csrf),
|
|
551
|
+
input.versionId ?? null,
|
|
552
|
+
now,
|
|
553
|
+
now,
|
|
554
|
+
input.viewerId ?? null,
|
|
555
|
+
),
|
|
556
|
+
);
|
|
557
|
+
return { cookie, csrf, id };
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Resolves the session a request presents on `origin` for `artifactId`. Every candidate cookie
|
|
562
|
+
* value is tried, since another page of the host may have set a cookie of the same name (D98):
|
|
563
|
+
* only a session issued for this origin, whose token is live and grants this artifact, counts.
|
|
564
|
+
*/
|
|
565
|
+
session(
|
|
566
|
+
cookies: readonly string[],
|
|
567
|
+
origin: string,
|
|
568
|
+
artifactId: string,
|
|
569
|
+
): ResolvedSession | Refusal {
|
|
570
|
+
let revoked = false;
|
|
571
|
+
for (const value of cookies) {
|
|
572
|
+
if (!looksLikeSecret(value)) continue;
|
|
573
|
+
const found = this.#ledger.read((db) => {
|
|
574
|
+
const session = db.get<SessionRow>("SELECT * FROM session WHERE id = ?", hashSecret(value));
|
|
575
|
+
if (!session || session.origin !== origin) return undefined;
|
|
576
|
+
const token = db.get<TokenRow>("SELECT * FROM token WHERE id = ?", session.token_id);
|
|
577
|
+
return token && token.artifact_id === artifactId ? { session, token } : undefined;
|
|
578
|
+
});
|
|
579
|
+
if (!found) continue;
|
|
580
|
+
if (!this.#live(found.token)) {
|
|
581
|
+
revoked = true;
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
const csrf = csrfOf(value);
|
|
585
|
+
// A row whose CSRF hash doesn't match was not made by this code: it doesn't count.
|
|
586
|
+
if (!safeEqual(hashSecret(csrf), found.session.csrf)) continue;
|
|
587
|
+
this.#touch(found.session.id);
|
|
588
|
+
const grant = grantOf(found.token);
|
|
589
|
+
// A link's own viewer always wins: a token's viewer never changes (D104).
|
|
590
|
+
const viewerId = grant.viewerId ?? found.session.viewer_id ?? undefined;
|
|
591
|
+
return {
|
|
592
|
+
id: found.session.id,
|
|
593
|
+
csrf,
|
|
594
|
+
...(found.session.version_id !== null ? { versionId: found.session.version_id } : {}),
|
|
595
|
+
grant,
|
|
596
|
+
...(viewerId !== undefined ? { viewerId } : {}),
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
return revoked
|
|
600
|
+
? refusal(
|
|
601
|
+
"MOSTRA_HYGIENE_TOKEN_REVOKED",
|
|
602
|
+
"The link this session came from was revoked or has expired.",
|
|
603
|
+
)
|
|
604
|
+
: refusal(
|
|
605
|
+
"MOSTRA_HYGIENE_SESSION_MISSING",
|
|
606
|
+
"The request carries no session for this origin.",
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** Records the version a frame session entered (D98 item 5). */
|
|
611
|
+
enterVersion(sessionId: string, versionId: string): void {
|
|
612
|
+
this.#ledger.write((db) =>
|
|
613
|
+
db.run(
|
|
614
|
+
"UPDATE session SET version_id = ?, last_seen_at = ? WHERE id = ?",
|
|
615
|
+
versionId,
|
|
616
|
+
this.#iso(this.#now()),
|
|
617
|
+
sessionId,
|
|
618
|
+
),
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** Writes `last_seen_at` at most hourly, so serving files doesn't write the ledger per request. */
|
|
623
|
+
#touch(sessionId: string): void {
|
|
624
|
+
const now = this.#now();
|
|
625
|
+
if (now - (this.#touched.get(sessionId) ?? 0) < TOUCH_INTERVAL_MS) return;
|
|
626
|
+
this.#touched.set(sessionId, now);
|
|
627
|
+
this.#ledger.write((db) =>
|
|
628
|
+
db.run("UPDATE session SET last_seen_at = ? WHERE id = ?", this.#iso(now), sessionId),
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/** Mints a single-use frame ticket (D98 item 3). */
|
|
633
|
+
mintTicket(grant: Omit<TicketGrant, never>): string {
|
|
634
|
+
const ticket = randomSecret();
|
|
635
|
+
this.#sweepTickets();
|
|
636
|
+
this.#tickets.set(hashSecret(ticket), { ...grant, expires: this.#now() + TICKET_TTL_MS });
|
|
637
|
+
return ticket;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Redeems a ticket on `origin`, once, while the shell session that asked for it lives and its
|
|
642
|
+
* link is live (D98 item 3).
|
|
643
|
+
*/
|
|
644
|
+
redeemTicket(ticket: unknown, origin: string): TicketGrant | Refusal {
|
|
645
|
+
this.#sweepTickets();
|
|
646
|
+
const key = looksLikeSecret(ticket) ? hashSecret(ticket) : undefined;
|
|
647
|
+
const entry = key === undefined ? undefined : this.#tickets.get(key);
|
|
648
|
+
if (key !== undefined) this.#tickets.delete(key);
|
|
649
|
+
if (!entry || entry.origin !== origin) {
|
|
650
|
+
return refusal(
|
|
651
|
+
"MOSTRA_HYGIENE_TOKEN_INVALID",
|
|
652
|
+
"The frame ticket is unknown, used or expired.",
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
const shellSession = this.#ledger.read((db) =>
|
|
656
|
+
db.get<{ token_id: string }>(
|
|
657
|
+
"SELECT token_id FROM session WHERE id = ?",
|
|
658
|
+
entry.shellSessionId,
|
|
659
|
+
),
|
|
660
|
+
);
|
|
661
|
+
if (shellSession?.token_id !== entry.tokenId) {
|
|
662
|
+
return refusal(
|
|
663
|
+
"MOSTRA_HYGIENE_SESSION_MISSING",
|
|
664
|
+
"The shell session that asked for this frame ticket has ended.",
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
const grant = this.grant(entry.tokenId);
|
|
668
|
+
if (!grant?.live) {
|
|
669
|
+
return refusal(
|
|
670
|
+
"MOSTRA_HYGIENE_TOKEN_REVOKED",
|
|
671
|
+
"The link behind this frame was revoked or has expired.",
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
const { expires: _expires, ...rest } = entry;
|
|
675
|
+
return rest;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
#sweepTickets(): void {
|
|
679
|
+
const now = this.#now();
|
|
680
|
+
for (const [key, entry] of this.#tickets) if (entry.expires <= now) this.#tickets.delete(key);
|
|
681
|
+
for (const [key, entry] of this.#launches) if (entry.expires <= now) this.#launches.delete(key);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Mints a one-time launch ticket for a live link (D140 item 15): a browser opened from a command
|
|
686
|
+
* line gets it instead of the link's secret, which other local users could read there. It lives
|
|
687
|
+
* 60 s in memory and is never recorded. `undefined` for an unknown token, `"dead"` for a
|
|
688
|
+
* revoked or expired one, or one minted for another shell address.
|
|
689
|
+
*/
|
|
690
|
+
mintLaunch(
|
|
691
|
+
tokenId: string,
|
|
692
|
+
):
|
|
693
|
+
| { readonly ticket: string; readonly grant: Grant; readonly expiresAt: string }
|
|
694
|
+
| "dead"
|
|
695
|
+
| undefined {
|
|
696
|
+
const grant = this.grant(tokenId);
|
|
697
|
+
if (!grant) return undefined;
|
|
698
|
+
if (!grant.live || grant.shellOrigin !== this.#shellOrigin()) return "dead";
|
|
699
|
+
this.#sweepTickets();
|
|
700
|
+
const ticket = randomSecret();
|
|
701
|
+
const expires = this.#now() + TICKET_TTL_MS;
|
|
702
|
+
this.#launches.set(hashSecret(ticket), { tokenId, expires });
|
|
703
|
+
const { live: _live, ...rest } = grant;
|
|
704
|
+
return { ticket, grant: rest, expiresAt: this.#iso(expires) };
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Checks a launch ticket presented for `artifactId` like a token (the guessing limit applies,
|
|
709
|
+
* and unknown tickets count), and spends it when `spend` is set. The exchange spends it only
|
|
710
|
+
* once a session exists, so a viewer who chooses between two links (D98 item 8) can still use it
|
|
711
|
+
* within its minute.
|
|
712
|
+
*/
|
|
713
|
+
presentLaunch(
|
|
714
|
+
ticket: unknown,
|
|
715
|
+
artifactId: string,
|
|
716
|
+
spend: boolean,
|
|
717
|
+
): Grant | (Refusal & { readonly retryAfter?: number }) {
|
|
718
|
+
const limited = this.limited(artifactId);
|
|
719
|
+
if (limited) return limited;
|
|
720
|
+
this.#sweepTickets();
|
|
721
|
+
const key = looksLikeSecret(ticket) ? hashSecret(ticket) : undefined;
|
|
722
|
+
const entry = key === undefined ? undefined : this.#launches.get(key);
|
|
723
|
+
const grant = entry ? this.grant(entry.tokenId) : undefined;
|
|
724
|
+
if (!entry || !grant || grant.artifactId !== artifactId) {
|
|
725
|
+
this.#fail(artifactId);
|
|
726
|
+
return refusal(
|
|
727
|
+
"MOSTRA_HYGIENE_TOKEN_INVALID",
|
|
728
|
+
"The launch ticket is unknown, used or expired.",
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
if (!grant.live || grant.shellOrigin !== this.#shellOrigin()) {
|
|
732
|
+
return refusal(
|
|
733
|
+
"MOSTRA_HYGIENE_TOKEN_REVOKED",
|
|
734
|
+
"The link behind this launch ticket was revoked or has expired.",
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
if (spend && key !== undefined) this.#launches.delete(key);
|
|
738
|
+
const { live: _live, ...rest } = grant;
|
|
739
|
+
return rest;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* Spends a launch ticket that started a session: `true` only for the caller that spent it, so
|
|
744
|
+
* two exchanges that both presented it while a host's identity port was answering get one
|
|
745
|
+
* session between them (GPT review of WP-12, finding 1).
|
|
746
|
+
*/
|
|
747
|
+
spendLaunch(ticket: unknown): boolean {
|
|
748
|
+
// An expired ticket is swept first, so it can't be spent (Opus review of WP-12, finding 3).
|
|
749
|
+
this.#sweepTickets();
|
|
750
|
+
return looksLikeSecret(ticket) && this.#launches.delete(hashSecret(ticket));
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/** Removes sessions unseen for longer than their cookie lives; no browser holds them any more. */
|
|
754
|
+
prune(): number {
|
|
755
|
+
const cutoff = this.#iso(this.#now() - SESSION_MAX_AGE_S * 1000);
|
|
756
|
+
return this.#ledger.write(
|
|
757
|
+
(db) => db.run("DELETE FROM session WHERE last_seen_at < ?", cutoff).changes,
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
}
|