@mostra/runtime 0.12.0 → 0.14.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/dist/bin.js +2912 -2318
- package/dist/bin.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/{instance-CBUlOmzG.js → instance-CFTw3g-c.js} +227 -25
- package/dist/instance-CFTw3g-c.js.map +1 -0
- package/dist/reference/diagnostics.json +2 -2
- package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +2 -2
- package/dist/reference/self-hosting.md +2 -1
- package/dist/skill/SKILL.md +2 -2
- package/dist/skill/assets/examples/dashboard/styles.css +27 -9
- package/dist/skill/assets/examples/decision/styles.css +27 -9
- package/dist/skill/assets/examples/inbox/styles.css +27 -9
- package/dist/skill/assets/studio.css +27 -9
- package/dist/skill/references/mcp.md +2 -2
- package/package.json +7 -6
- package/src/actions/index.ts +5 -2
- package/src/gateway/actions.ts +6 -1
- package/src/gateway/capabilities.ts +5 -1
- package/src/gateway/http.ts +15 -19
- package/src/gateway/index.ts +70 -1
- package/src/gateway/listener.ts +10 -5
- package/src/gateway/routes.ts +6 -1
- package/src/gateway/theme.ts +238 -0
- package/src/ledger/migrations.ts +9 -0
- package/src/server/api.ts +79 -87
- package/src/server/auth.ts +83 -16
- package/src/server/authorize.ts +74 -58
- package/src/server/config.ts +98 -0
- package/src/server/directory.ts +51 -0
- package/src/server/hosted-pages.ts +19 -6
- package/src/server/hosted.ts +59 -27
- package/src/server/index.ts +144 -17
- package/src/server/metering.ts +117 -12
- package/src/server/pages.ts +242 -0
- package/src/server/providers.ts +61 -9
- package/src/server/relay.ts +258 -107
- package/dist/instance-CBUlOmzG.js.map +0 -1
package/src/server/api.ts
CHANGED
|
@@ -12,17 +12,20 @@ import { join } from "node:path";
|
|
|
12
12
|
import type { Duplex } from "node:stream";
|
|
13
13
|
import { createDiagnostic, type Diagnostic } from "@mostra/protocol";
|
|
14
14
|
import type { CredentialResource } from "@mostra/protocol/operator";
|
|
15
|
+
import { appearanceCookie } from "@mostra/themes";
|
|
15
16
|
import { type WebSocket, WebSocketServer } from "ws";
|
|
17
|
+
import { type ServerPages, themePageCsp } from "../gateway/index.ts";
|
|
16
18
|
import type { Log } from "../log.ts";
|
|
17
19
|
import { BINDING_SUBPROTOCOL, MAX_FRAME_BYTES, serveWebSocket } from "../protocol-server/index.ts";
|
|
18
20
|
import type { Authenticated, Authorization } from "./auth.ts";
|
|
19
|
-
import type
|
|
21
|
+
import { type Authorize, pageRequest } from "./authorize.ts";
|
|
20
22
|
import type { ClientAddresses, RateLimiter } from "./clients.ts";
|
|
21
23
|
import type { RemoteServerConfig } from "./config.ts";
|
|
22
24
|
import type { HostedHome } from "./hosted.ts";
|
|
23
25
|
import type { RemoteMcp } from "./mcp.ts";
|
|
24
26
|
import type { Clients } from "./oauth-clients.ts";
|
|
25
27
|
import type { Operator } from "./operator.ts";
|
|
28
|
+
import { API_PAGE_WORDS, APPEARANCE_PATH, createApiPages } from "./pages.ts";
|
|
26
29
|
|
|
27
30
|
/** How often an open binding's grant is checked again: a revocation closes it within 60 s. */
|
|
28
31
|
const GRANT_RECHECK_MS = 30_000;
|
|
@@ -59,20 +62,6 @@ function send(
|
|
|
59
62
|
response.end(text);
|
|
60
63
|
}
|
|
61
64
|
|
|
62
|
-
function page(response: ServerResponse, status: number, html: string): void {
|
|
63
|
-
response.writeHead(status, {
|
|
64
|
-
...BASE_HEADERS,
|
|
65
|
-
"content-type": "text/html; charset=utf-8",
|
|
66
|
-
"content-security-policy":
|
|
67
|
-
"default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'",
|
|
68
|
-
"x-frame-options": "DENY",
|
|
69
|
-
// The device page posts its own form, whose `Origin` the server checks: under `no-referrer`
|
|
70
|
-
// browsers send `Origin: null` for a form's POST, so this page sends origins to itself only.
|
|
71
|
-
"referrer-policy": "same-origin",
|
|
72
|
-
});
|
|
73
|
-
response.end(html);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
65
|
async function readBody(request: IncomingMessage, limit: number): Promise<Buffer | undefined> {
|
|
77
66
|
const chunks: Buffer[] = [];
|
|
78
67
|
let size = 0;
|
|
@@ -84,13 +73,11 @@ async function readBody(request: IncomingMessage, limit: number): Promise<Buffer
|
|
|
84
73
|
return Buffer.concat(chunks);
|
|
85
74
|
}
|
|
86
75
|
|
|
87
|
-
function escapeHtml(text: string): string {
|
|
88
|
-
return text.replace(/[&<>"']/g, (character) => `&#${character.charCodeAt(0)};`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
76
|
/** Options for {@link createApi}. */
|
|
92
77
|
export interface ApiOptions {
|
|
93
78
|
readonly config: RemoteServerConfig;
|
|
79
|
+
/** The server's own pages on the API origin, in its operator's theme and name (D171). */
|
|
80
|
+
readonly server: ServerPages;
|
|
94
81
|
readonly auth: Authorization;
|
|
95
82
|
readonly log: Log;
|
|
96
83
|
readonly filesDir: string;
|
|
@@ -141,63 +128,40 @@ function developmentProvider(
|
|
|
141
128
|
return provider ? { id: provider.id, label: provider.label ?? "Development sign-in" } : undefined;
|
|
142
129
|
}
|
|
143
130
|
|
|
144
|
-
/** The device page: the code, the warning, and the development provider's sign-in. */
|
|
145
|
-
function devicePage(options: {
|
|
146
|
-
readonly code: string;
|
|
147
|
-
readonly provider: { id: string; label: string } | undefined;
|
|
148
|
-
readonly message?: string;
|
|
149
|
-
readonly done?: boolean;
|
|
150
|
-
}): string {
|
|
151
|
-
const body = options.done
|
|
152
|
-
? `<p>${escapeHtml(options.message ?? "Done.")}</p><p>You can close this page.</p>`
|
|
153
|
-
: `${options.message ? `<p role="alert">${escapeHtml(options.message)}</p>` : ""}
|
|
154
|
-
<p><strong>Approving links a machine to your account.</strong> That machine's Mostra can then host
|
|
155
|
-
pages under your account and receives their answers. Continue only if you started this sign-in
|
|
156
|
-
yourself, on that machine.</p>
|
|
157
|
-
${
|
|
158
|
-
options.provider
|
|
159
|
-
? `<form method="post" action="/device">
|
|
160
|
-
<label>Code <input name="user_code" value="${escapeHtml(options.code)}" autocomplete="off" required></label>
|
|
161
|
-
<p>${escapeHtml(options.provider.label)}: this server signs people in with a development provider,
|
|
162
|
-
which trusts the name you type. Use it only on a server you run for yourself or for tests.</p>
|
|
163
|
-
<label>Your name <input name="name" required maxlength="64"></label>
|
|
164
|
-
<input type="hidden" name="provider" value="${escapeHtml(options.provider.id)}">
|
|
165
|
-
<p><button name="decision" value="approve">Approve this machine</button>
|
|
166
|
-
<button name="decision" value="deny">Refuse</button></p>
|
|
167
|
-
</form>`
|
|
168
|
-
: "<p>This server has no identity provider configured, so nobody can sign in yet.</p>"
|
|
169
|
-
}`;
|
|
170
|
-
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="robots" content="noindex">
|
|
171
|
-
<meta name="viewport" content="width=device-width, initial-scale=1"><title>Link a machine to Mostra</title>
|
|
172
|
-
<style>body{font:16px/1.5 system-ui,sans-serif;max-width:34rem;margin:3rem auto;padding:0 1rem}
|
|
173
|
-
input{font:inherit;padding:.3rem}button{font:inherit;padding:.4rem .9rem;margin-right:.5rem}</style></head>
|
|
174
|
-
<body><h1>Link a machine to Mostra</h1>${body}</body></html>`;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/** The device page for a browser already signed in: approving links a machine (§18.5). */
|
|
178
|
-
function deviceApproval(options: {
|
|
179
|
-
readonly code: string;
|
|
180
|
-
readonly name: string;
|
|
181
|
-
readonly csrf: string;
|
|
182
|
-
}): string {
|
|
183
|
-
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="robots" content="noindex">
|
|
184
|
-
<meta name="viewport" content="width=device-width, initial-scale=1"><title>Link a machine to Mostra</title>
|
|
185
|
-
<style>body{font:16px/1.5 system-ui,sans-serif;max-width:34rem;margin:3rem auto;padding:0 1rem}
|
|
186
|
-
input{font:inherit;padding:.3rem}button{font:inherit;padding:.4rem .9rem;margin-right:.5rem}</style></head>
|
|
187
|
-
<body><h1>Link a machine to Mostra</h1>
|
|
188
|
-
<p><strong>Approving links a machine to your account, ${escapeHtml(options.name)}.</strong> That machine's
|
|
189
|
-
Mostra can then host pages under your account and receives their answers. Continue only if you started
|
|
190
|
-
this sign-in yourself, on that machine.</p>
|
|
191
|
-
<form method="post" action="/device"><input type="hidden" name="csrf" value="${escapeHtml(options.csrf)}">
|
|
192
|
-
<label>Code <input name="user_code" value="${escapeHtml(options.code)}" autocomplete="off" required></label>
|
|
193
|
-
<p><button name="session_decision" value="approve">Approve this machine</button>
|
|
194
|
-
<button name="session_decision" value="deny">Refuse</button></p></form></body></html>`;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
131
|
/** Creates the API origin. */
|
|
198
132
|
export function createApi(options: ApiOptions): Api {
|
|
199
133
|
const { config, auth, log } = options;
|
|
200
134
|
const apiOrigin = config.apiOrigin;
|
|
135
|
+
const pages = createApiPages(options.server, options.server.name);
|
|
136
|
+
/** Sends one of the API origin's pages: themed, no script, its forms posting here only. */
|
|
137
|
+
const page = (response: ServerResponse, status: number, text: string): void => {
|
|
138
|
+
response.writeHead(status, {
|
|
139
|
+
...BASE_HEADERS,
|
|
140
|
+
"content-type": "text/html; charset=utf-8",
|
|
141
|
+
"content-security-policy": themePageCsp(options.server.pages, "'none'", "'self'"),
|
|
142
|
+
"x-frame-options": "DENY",
|
|
143
|
+
// The device page posts its own form, whose `Origin` the server checks: under `no-referrer`
|
|
144
|
+
// browsers send `Origin: null` for a form's POST, so this page sends origins to itself only.
|
|
145
|
+
"referrer-policy": "same-origin",
|
|
146
|
+
});
|
|
147
|
+
response.end(text);
|
|
148
|
+
};
|
|
149
|
+
const devicePage = (
|
|
150
|
+
response: ServerResponse,
|
|
151
|
+
options: {
|
|
152
|
+
readonly code: string;
|
|
153
|
+
readonly provider: { id: string; label: string } | undefined;
|
|
154
|
+
readonly message?: string;
|
|
155
|
+
readonly done?: boolean;
|
|
156
|
+
},
|
|
157
|
+
): string =>
|
|
158
|
+
options.done
|
|
159
|
+
? pages.deviceDone(pageRequest(response), options.message ?? "")
|
|
160
|
+
: pages.deviceForm(pageRequest(response), {
|
|
161
|
+
code: options.code,
|
|
162
|
+
message: options.message,
|
|
163
|
+
development: options.provider,
|
|
164
|
+
});
|
|
201
165
|
const sockets = new WebSocketServer({
|
|
202
166
|
noServer: true,
|
|
203
167
|
maxPayload: MAX_FRAME_BYTES,
|
|
@@ -328,11 +292,17 @@ export function createApi(options: ApiOptions): Api {
|
|
|
328
292
|
authorization_response_iss_parameter_supported: true,
|
|
329
293
|
});
|
|
330
294
|
|
|
331
|
-
const sessionDevicePage = (
|
|
332
|
-
|
|
295
|
+
const sessionDevicePage = (
|
|
296
|
+
response: ServerResponse,
|
|
297
|
+
code: string,
|
|
298
|
+
signed: { accountId: string; hash: string },
|
|
299
|
+
): string =>
|
|
300
|
+
pages.deviceForm(pageRequest(response), {
|
|
333
301
|
code,
|
|
334
|
-
|
|
335
|
-
|
|
302
|
+
signedIn: {
|
|
303
|
+
name: auth.accountName(signed.accountId).name ?? API_PAGE_WORDS.yourAccount,
|
|
304
|
+
csrf: options.authorize.csrf(signed.hash, "device"),
|
|
305
|
+
},
|
|
336
306
|
});
|
|
337
307
|
|
|
338
308
|
const form = async (request: IncomingMessage): Promise<URLSearchParams | undefined> => {
|
|
@@ -418,7 +388,7 @@ export function createApi(options: ApiOptions): Api {
|
|
|
418
388
|
}
|
|
419
389
|
if (existsSync(target)) await rm(scratch, { force: true });
|
|
420
390
|
else renameSync(scratch, target);
|
|
421
|
-
home.uploaded(sha);
|
|
391
|
+
home.uploaded(sha, size);
|
|
422
392
|
options.stored?.(who.homeId, size);
|
|
423
393
|
send(response, 201, { sha256: sha, size });
|
|
424
394
|
};
|
|
@@ -577,13 +547,31 @@ export function createApi(options: ApiOptions): Api {
|
|
|
577
547
|
send(response, 200, {});
|
|
578
548
|
return;
|
|
579
549
|
}
|
|
550
|
+
if (method === "POST" && path === APPEARANCE_PATH) {
|
|
551
|
+
// A person's light-or-dark choice (D171): kept in a cookie of this origin, from this
|
|
552
|
+
// origin's own pages only, then back to the page it was made on.
|
|
553
|
+
const body = request.headers.origin === apiOrigin ? await form(request) : undefined;
|
|
554
|
+
const chosen = body?.get("appearance");
|
|
555
|
+
const next = body?.get("next") ?? "/";
|
|
556
|
+
const back =
|
|
557
|
+
next.startsWith("/") && !next.startsWith("//") && !next.startsWith("/\\") ? next : "/";
|
|
558
|
+
response.writeHead(303, {
|
|
559
|
+
...BASE_HEADERS,
|
|
560
|
+
location: back,
|
|
561
|
+
...(chosen === "light" || chosen === "dark" || chosen === "system"
|
|
562
|
+
? { "set-cookie": appearanceCookie(chosen, apiOrigin.startsWith("https:")) }
|
|
563
|
+
: {}),
|
|
564
|
+
});
|
|
565
|
+
response.end();
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
580
568
|
if (path === "/device") {
|
|
581
569
|
const provider = developmentProvider(config);
|
|
582
570
|
const signed = options.authorize.sessionAccount(request);
|
|
583
571
|
if (method === "GET") {
|
|
584
572
|
const code = new URL(request.url ?? "/", apiOrigin).searchParams.get("user_code") ?? "";
|
|
585
573
|
if (signed) {
|
|
586
|
-
page(response, 200, sessionDevicePage(code, signed));
|
|
574
|
+
page(response, 200, sessionDevicePage(response, code, signed));
|
|
587
575
|
return;
|
|
588
576
|
}
|
|
589
577
|
// Real providers sign the person in first; the development one asks on this page, when it
|
|
@@ -595,7 +583,7 @@ export function createApi(options: ApiOptions): Api {
|
|
|
595
583
|
);
|
|
596
584
|
return;
|
|
597
585
|
}
|
|
598
|
-
page(response, 200, devicePage({ code, provider }));
|
|
586
|
+
page(response, 200, devicePage(response, { code, provider }));
|
|
599
587
|
return;
|
|
600
588
|
}
|
|
601
589
|
// The body is read once, for either form.
|
|
@@ -610,7 +598,7 @@ export function createApi(options: ApiOptions): Api {
|
|
|
610
598
|
page(
|
|
611
599
|
response,
|
|
612
600
|
403,
|
|
613
|
-
devicePage({
|
|
601
|
+
devicePage(response, {
|
|
614
602
|
code: "",
|
|
615
603
|
provider,
|
|
616
604
|
message: "This form must be sent from this page.",
|
|
@@ -623,7 +611,7 @@ export function createApi(options: ApiOptions): Api {
|
|
|
623
611
|
page(
|
|
624
612
|
response,
|
|
625
613
|
decided.ok ? 200 : 400,
|
|
626
|
-
devicePage({
|
|
614
|
+
devicePage(response, {
|
|
627
615
|
code,
|
|
628
616
|
provider,
|
|
629
617
|
...(decided.ok
|
|
@@ -645,7 +633,11 @@ export function createApi(options: ApiOptions): Api {
|
|
|
645
633
|
page(
|
|
646
634
|
response,
|
|
647
635
|
403,
|
|
648
|
-
devicePage(
|
|
636
|
+
devicePage(response, {
|
|
637
|
+
code: "",
|
|
638
|
+
provider,
|
|
639
|
+
message: "This form must be sent from this page.",
|
|
640
|
+
}),
|
|
649
641
|
);
|
|
650
642
|
return;
|
|
651
643
|
}
|
|
@@ -664,7 +656,7 @@ export function createApi(options: ApiOptions): Api {
|
|
|
664
656
|
page(
|
|
665
657
|
response,
|
|
666
658
|
400,
|
|
667
|
-
devicePage({ code, provider, message: "Enter the code and your name." }),
|
|
659
|
+
devicePage(response, { code, provider, message: "Enter the code and your name." }),
|
|
668
660
|
);
|
|
669
661
|
return;
|
|
670
662
|
}
|
|
@@ -673,7 +665,7 @@ export function createApi(options: ApiOptions): Api {
|
|
|
673
665
|
// the operator nothing (second Fable review of WP-30, 5).
|
|
674
666
|
const pending = auth.pendingCode(code);
|
|
675
667
|
if (!pending.ok) {
|
|
676
|
-
page(response, 400, devicePage({ code, provider, message: pending.reason }));
|
|
668
|
+
page(response, 400, devicePage(response, { code, provider, message: pending.reason }));
|
|
677
669
|
return;
|
|
678
670
|
}
|
|
679
671
|
// The typed name is an identity like any provider's: the operator's `accounts.resolve`
|
|
@@ -695,20 +687,20 @@ export function createApi(options: ApiOptions): Api {
|
|
|
695
687
|
page(
|
|
696
688
|
response,
|
|
697
689
|
account.kind === "unavailable" ? 503 : 403,
|
|
698
|
-
devicePage({ code, provider, message: reason }),
|
|
690
|
+
devicePage(response, { code, provider, message: reason }),
|
|
699
691
|
);
|
|
700
692
|
return;
|
|
701
693
|
}
|
|
702
694
|
decided = auth.decideFor({ code, approve: true, accountId: account.accountId });
|
|
703
695
|
}
|
|
704
696
|
if (!decided.ok) {
|
|
705
|
-
page(response, 400, devicePage({ code, provider, message: decided.reason }));
|
|
697
|
+
page(response, 400, devicePage(response, { code, provider, message: decided.reason }));
|
|
706
698
|
return;
|
|
707
699
|
}
|
|
708
700
|
page(
|
|
709
701
|
response,
|
|
710
702
|
200,
|
|
711
|
-
devicePage({
|
|
703
|
+
devicePage(response, {
|
|
712
704
|
code,
|
|
713
705
|
provider,
|
|
714
706
|
done: true,
|
package/src/server/auth.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { AccountId, HomeId, LinkId } from "@mostra/protocol";
|
|
|
16
16
|
import type { SignInIdentity } from "@mostra/protocol/operator";
|
|
17
17
|
import type { Ledger, LedgerDb } from "../ledger/index.ts";
|
|
18
18
|
import type { Log } from "../log.ts";
|
|
19
|
-
import type
|
|
19
|
+
import { configuredVerifier, type RemoteServerConfig } from "./config.ts";
|
|
20
20
|
import type { Operator } from "./operator.ts";
|
|
21
21
|
|
|
22
22
|
/** The CLI's client ID: a first-party public client (§18.5). */
|
|
@@ -224,9 +224,24 @@ export interface Authorization {
|
|
|
224
224
|
* `accounts.resolve` when it has one, else the server's own accounts; `steps` counts the first
|
|
225
225
|
* sign-in steps already taken.
|
|
226
226
|
*/
|
|
227
|
-
resolveAccount(
|
|
228
|
-
|
|
229
|
-
|
|
227
|
+
resolveAccount(
|
|
228
|
+
identity: SignInIdentity,
|
|
229
|
+
steps: number,
|
|
230
|
+
/**
|
|
231
|
+
* The label of the provider's verifier (`IdentityProvider.verifies`), recorded on the identity
|
|
232
|
+
* as it links or signs in; absent: the provider verified no name (MT-03).
|
|
233
|
+
*/
|
|
234
|
+
verifiedBy?: string,
|
|
235
|
+
): Promise<ResolvedAccount>;
|
|
236
|
+
/**
|
|
237
|
+
* An account's display name, its first identity's provider, and the label of that identity's
|
|
238
|
+
* verifier as it was recorded when the provider last named the person (MT-03).
|
|
239
|
+
*/
|
|
240
|
+
accountName(accountId: string): {
|
|
241
|
+
readonly name?: string;
|
|
242
|
+
readonly provider?: string;
|
|
243
|
+
readonly verifiedBy?: string;
|
|
244
|
+
};
|
|
230
245
|
/** Mints an authorization code (one minute, single use; only its hash is kept). */
|
|
231
246
|
issueCode(grant: CodeGrant): string;
|
|
232
247
|
/**
|
|
@@ -290,6 +305,29 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
290
305
|
JSON.stringify({ client_name: "Mostra CLI" }),
|
|
291
306
|
iso(now()),
|
|
292
307
|
);
|
|
308
|
+
// Identities linked before the server recorded who verified each (MT-03) take, once, the
|
|
309
|
+
// verifier the configuration names for their provider now, as their sender lines showed it
|
|
310
|
+
// until then; any other (a provider retired, or one an operator module supplied) none. The
|
|
311
|
+
// configuration may since have given a provider's ID to another kind, so a row keeps none
|
|
312
|
+
// unless its shape is that kind's (its Opus review): never one the development provider made
|
|
313
|
+
// (whose subject is the typed name, lower-cased), and for GitHub only a numeric subject (its
|
|
314
|
+
// user ID). From then on the recorded label is the one shown, whatever the configuration says.
|
|
315
|
+
const at = iso(now());
|
|
316
|
+
for (const provider of config.identityProviders ?? []) {
|
|
317
|
+
const label = configuredVerifier(provider);
|
|
318
|
+
if (label === undefined) continue;
|
|
319
|
+
db.run(
|
|
320
|
+
`UPDATE identity SET verified_by = ?, verified_recorded_at = ?
|
|
321
|
+
WHERE provider = ? AND verified_recorded_at IS NULL
|
|
322
|
+
AND (name IS NULL OR subject != lower(name))
|
|
323
|
+
AND (? != 'github' OR (subject != '' AND subject NOT GLOB '*[^0-9]*'))`,
|
|
324
|
+
label,
|
|
325
|
+
at,
|
|
326
|
+
provider.id,
|
|
327
|
+
provider.kind,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
db.run("UPDATE identity SET verified_recorded_at = ? WHERE verified_recorded_at IS NULL", at);
|
|
293
331
|
});
|
|
294
332
|
|
|
295
333
|
const deviceRow = (db: LedgerDb, hash: string) =>
|
|
@@ -305,6 +343,7 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
305
343
|
account: string,
|
|
306
344
|
name: string | undefined,
|
|
307
345
|
status: "active" | "suspended",
|
|
346
|
+
verifiedBy: string | undefined,
|
|
308
347
|
): void => {
|
|
309
348
|
db.run(
|
|
310
349
|
`INSERT INTO account (id, name, status, created_at) VALUES (?, ?, ?, ?)
|
|
@@ -315,29 +354,47 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
315
354
|
iso(now()),
|
|
316
355
|
);
|
|
317
356
|
db.run(
|
|
318
|
-
`INSERT INTO identity (provider, subject, account_id, email, name, linked_at
|
|
357
|
+
`INSERT INTO identity (provider, subject, account_id, email, name, linked_at, verified_by, verified_recorded_at)
|
|
358
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
319
359
|
ON CONFLICT (provider, subject) DO UPDATE SET account_id = excluded.account_id,
|
|
320
|
-
email = excluded.email, name = excluded.name
|
|
360
|
+
email = excluded.email, name = excluded.name, verified_by = excluded.verified_by,
|
|
361
|
+
verified_recorded_at = excluded.verified_recorded_at`,
|
|
321
362
|
identity.provider,
|
|
322
363
|
identity.subject,
|
|
323
364
|
account,
|
|
324
365
|
identity.email ?? null,
|
|
325
366
|
identity.name ?? null,
|
|
326
367
|
iso(now()),
|
|
368
|
+
verifiedBy ?? null,
|
|
369
|
+
iso(now()),
|
|
327
370
|
);
|
|
328
371
|
};
|
|
329
372
|
|
|
330
|
-
/**
|
|
373
|
+
/**
|
|
374
|
+
* The account for a sign-in identity, made on the first sign-in. The identity records who
|
|
375
|
+
* verified the person's name as it signs in (`verifiedBy`, the provider's verifier label, or none),
|
|
376
|
+
* so a provider renamed or retired later changes no sender line (MT-03).
|
|
377
|
+
*/
|
|
331
378
|
const accountFor = (
|
|
332
379
|
db: LedgerDb,
|
|
333
380
|
identity: { provider: string; subject: string; name?: string },
|
|
381
|
+
verifiedBy: string | undefined,
|
|
334
382
|
): string => {
|
|
335
383
|
const found = db.get<{ account_id: string }>(
|
|
336
384
|
"SELECT account_id FROM identity WHERE provider = ? AND subject = ?",
|
|
337
385
|
identity.provider,
|
|
338
386
|
identity.subject,
|
|
339
387
|
);
|
|
340
|
-
if (found)
|
|
388
|
+
if (found) {
|
|
389
|
+
db.run(
|
|
390
|
+
"UPDATE identity SET verified_by = ?, verified_recorded_at = ? WHERE provider = ? AND subject = ?",
|
|
391
|
+
verifiedBy ?? null,
|
|
392
|
+
iso(now()),
|
|
393
|
+
identity.provider,
|
|
394
|
+
identity.subject,
|
|
395
|
+
);
|
|
396
|
+
return found.account_id;
|
|
397
|
+
}
|
|
341
398
|
const account = AccountId.create();
|
|
342
399
|
db.run(
|
|
343
400
|
"INSERT INTO account (id, name, status, created_at) VALUES (?, ?, 'active', ?)",
|
|
@@ -346,12 +403,14 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
346
403
|
iso(now()),
|
|
347
404
|
);
|
|
348
405
|
db.run(
|
|
349
|
-
"INSERT INTO identity (provider, subject, account_id, name, linked_at) VALUES (?, ?, ?, ?, ?)",
|
|
406
|
+
"INSERT INTO identity (provider, subject, account_id, name, linked_at, verified_by, verified_recorded_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
350
407
|
identity.provider,
|
|
351
408
|
identity.subject,
|
|
352
409
|
account,
|
|
353
410
|
identity.name ?? null,
|
|
354
411
|
iso(now()),
|
|
412
|
+
verifiedBy ?? null,
|
|
413
|
+
iso(now()),
|
|
355
414
|
);
|
|
356
415
|
return account;
|
|
357
416
|
};
|
|
@@ -866,13 +925,13 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
866
925
|
});
|
|
867
926
|
},
|
|
868
927
|
|
|
869
|
-
async resolveAccount(identity, steps) {
|
|
928
|
+
async resolveAccount(identity, steps, verifiedBy) {
|
|
870
929
|
const verdict = options.operator
|
|
871
930
|
? await options.operator.resolve(identity, steps)
|
|
872
931
|
: ({ default: true } as const);
|
|
873
932
|
if (verdict === "unavailable") return { kind: "unavailable" };
|
|
874
933
|
if ("default" in verdict) {
|
|
875
|
-
const accountId = directory.write((db) => accountFor(db, identity));
|
|
934
|
+
const accountId = directory.write((db) => accountFor(db, identity, verifiedBy));
|
|
876
935
|
const status = directory.read(
|
|
877
936
|
(db) =>
|
|
878
937
|
db.get<{ status: string }>("SELECT status FROM account WHERE id = ?", accountId)
|
|
@@ -884,7 +943,7 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
884
943
|
if ("step" in verdict) return { kind: "step", url: verdict.step.url };
|
|
885
944
|
if ("refused" in verdict) return { kind: "refused", message: verdict.refused.message };
|
|
886
945
|
directory.write((db) =>
|
|
887
|
-
operatorAccount(db, identity, verdict.account, verdict.name, verdict.status),
|
|
946
|
+
operatorAccount(db, identity, verdict.account, verdict.name, verdict.status, verifiedBy),
|
|
888
947
|
);
|
|
889
948
|
if (verdict.status === "suspended") return { kind: "suspended" };
|
|
890
949
|
return { kind: "account", accountId: verdict.account, name: verdict.name ?? identity.name };
|
|
@@ -892,14 +951,21 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
892
951
|
|
|
893
952
|
accountName(accountId) {
|
|
894
953
|
return directory.read((db) => {
|
|
895
|
-
const row = db.get<{
|
|
896
|
-
|
|
897
|
-
|
|
954
|
+
const row = db.get<{
|
|
955
|
+
name: string | null;
|
|
956
|
+
provider: string | null;
|
|
957
|
+
verified_by: string | null;
|
|
958
|
+
}>(
|
|
959
|
+
`SELECT a.name, i.provider, i.verified_by FROM account a
|
|
960
|
+
LEFT JOIN identity i ON i.rowid = (SELECT rowid FROM identity WHERE account_id = a.id
|
|
961
|
+
ORDER BY linked_at, rowid LIMIT 1)
|
|
962
|
+
WHERE a.id = ?`,
|
|
898
963
|
accountId,
|
|
899
964
|
);
|
|
900
965
|
return {
|
|
901
966
|
...(row?.name ? { name: row.name } : {}),
|
|
902
967
|
...(row?.provider ? { provider: row.provider } : {}),
|
|
968
|
+
...(row?.verified_by ? { verifiedBy: row.verified_by } : {}),
|
|
903
969
|
};
|
|
904
970
|
});
|
|
905
971
|
},
|
|
@@ -1120,7 +1186,8 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
1120
1186
|
);
|
|
1121
1187
|
return { ok: true as const };
|
|
1122
1188
|
}
|
|
1123
|
-
|
|
1189
|
+
// The development sign-in's name is taken on trust: no verifier (D161).
|
|
1190
|
+
const account = accountFor(db, input.identity, undefined);
|
|
1124
1191
|
db.run(
|
|
1125
1192
|
"INSERT INTO consent (account_id, client_id, granted_at) VALUES (?, ?, ?) ON CONFLICT (account_id, client_id) DO UPDATE SET revoked_at = NULL",
|
|
1126
1193
|
account,
|