@mostra/runtime 0.10.0 → 0.11.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 +4058 -310
- package/dist/bin.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/{instance-FExcyENK.js → instance-DCiUHvgZ.js} +682 -47
- package/dist/instance-DCiUHvgZ.js.map +1 -0
- package/dist/reference/cli.md +1322 -0
- package/dist/reference/diagnostics.json +1552 -0
- package/dist/reference/errors/MOSTRA_A11Y_MINOR.md +23 -0
- package/dist/reference/errors/MOSTRA_A11Y_SERIOUS.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_CANCELLED.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_EVENT_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_EXPORT_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_INTERRUPTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_PAUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_RUN_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_THREW.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_UNKNOWN_FUNCTION.md +23 -0
- package/dist/reference/errors/MOSTRA_ACTION_UNSERIALIZABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_BUNDLE_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_DIR_NOT_EMPTY.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_ENTRY_AMBIGUOUS.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_ID_DROPPED.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_NO_ENTRY.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_NO_PAGE.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_PATCH_REJECTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_PATH_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_SOURCE_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_SOURCE_UNREADABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_TEMPLATE_UNKNOWN.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_VERSION_COLLECTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ARTIFACT_VERSION_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_CRASHED.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_TIMEOUT.md +23 -0
- package/dist/reference/errors/MOSTRA_BUILD_UNRESOLVED.md +23 -0
- package/dist/reference/errors/MOSTRA_CLI_CONFIG_UNWRITABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_CLI_UNKNOWN_COMMAND.md +23 -0
- package/dist/reference/errors/MOSTRA_CLI_USAGE.md +23 -0
- package/dist/reference/errors/MOSTRA_CONSOLE_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_CONSOLE_WARNING.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_INSTALL_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_OFFLINE.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_SPEC_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_TIMEOUT.md +23 -0
- package/dist/reference/errors/MOSTRA_DEP_WARM_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BIND_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BROWSER_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BUNDLE_DOWNLOAD_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BUNDLE_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_BUNDLE_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_HELPER_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_NODE_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_PORT_TAKEN.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_RUNTIME_INSTALLING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_RUNTIME_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_SERVICE_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_SHELL_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_ENV_WARM_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_DUPLICATE_ID.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_ID_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_ID_UNSTABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_MUTATION_ID_REUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_SHARED.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_FIELD_UNBOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_DISK_FULL.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_GC_INCOMPLETE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_GONE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_INTEGRITY.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_LEDGER_UNREADABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_LOCKED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_MISMATCH.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_NETWORK_FILESYSTEM.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_NOT_ALWAYS_ON.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_NOT_OWNED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_SOCKET_TOO_LONG.md +23 -0
- package/dist/reference/errors/MOSTRA_HOME_TOO_NEW.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_CONNECTION_CLOSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_CURSOR_COMPACTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_IDENTITY_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_INITIALIZE_ORDER.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_INTERNAL_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_INVALID_PARAMS.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_MALFORMED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_MESSAGE_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_METHOD_UNKNOWN.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_NOTIFICATION_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_PORT_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_REQUEST_ID_REUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_REVISION_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_SHUTDOWN_REFUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_SUBSCRIPTION_UNKNOWN.md +23 -0
- package/dist/reference/errors/MOSTRA_HOST_TIMEOUT.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_CONTENT_TYPE.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_CSRF_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_FRAME_SESSION.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_HEADERS_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_HOST_REFUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_NOT_SERVED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_ORIGIN_REFUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_RATE_LIMITED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_REQUEST_MALFORMED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_SCOPE_DENIED.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_SESSION_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_SESSION_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_TOKEN_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_HYGIENE_TOKEN_REVOKED.md +23 -0
- package/dist/reference/errors/MOSTRA_LIVE_SOURCE_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_LIVE_TOO_LARGE.md +23 -0
- package/dist/reference/errors/MOSTRA_MANIFEST_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_MANIFEST_NOT_LITERAL.md +23 -0
- package/dist/reference/errors/MOSTRA_MANIFEST_UNKNOWN_KEY.md +23 -0
- package/dist/reference/errors/MOSTRA_NETWORK_EXTERNAL_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_NETWORK_REQUEST_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_PLATFORM_UNSUPPORTED.md +23 -0
- package/dist/reference/errors/MOSTRA_PUBLISH_ID_CONFLICT.md +23 -0
- package/dist/reference/errors/MOSTRA_PUBLISH_SOURCE_CHANGED.md +23 -0
- package/dist/reference/errors/MOSTRA_PUBLISH_VERIFY_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_ACTION_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_ARIA.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_FIELD_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_LIVE_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_PAGE_REMOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_REGRESSION_PIXELS.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_CONFIG_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_HOME_MOVING.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_HOME_SUSPENDED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_LIMIT_REACHED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_LOGIN_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_NOT_OWNER_INSTANCE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_NO_INSTANCE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_OPERATOR_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_OWNER_OFFLINE.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_PAUSED_BY_OPERATOR.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_RATE_LIMITED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_SIGN_IN_REQUIRED.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_UNDER_REVIEW.md +23 -0
- package/dist/reference/errors/MOSTRA_REMOTE_UNREACHABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_BLANK.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_NOT_READY.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_PAGE_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_WORKER_CRASHED.md +23 -0
- package/dist/reference/errors/MOSTRA_RUNTIME_WORKER_PARKED.md +23 -0
- package/dist/reference/errors/MOSTRA_SECRET_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_NOT_ENABLED.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_PORTS_EXHAUSTED.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TOOL_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TOOL_NOT_READY.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TTL_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_SHARE_TUNNEL_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_SMOKE_BUDGET.md +23 -0
- package/dist/reference/errors/MOSTRA_SMOKE_NO_EFFECT.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_ALREADY.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_BLOCKED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_CHANNEL_DENIED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_NOT_FOUND.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_PAUSED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_POLICY_NONE.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_REVISION_MOVED.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_SIGNAL_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_SUBMIT_VIEW_INVALID.md +23 -0
- package/dist/reference/errors/MOSTRA_TARGET_UNAVAILABLE.md +23 -0
- package/dist/reference/errors/MOSTRA_TYPE_ERROR.md +23 -0
- package/dist/reference/errors/MOSTRA_TYPE_UNTYPED_PACKAGE.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_ACTION_SKIPPED.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_BASELINE_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_CRASHED.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_LIVE_FAILED.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_NO_BASELINE.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_SECRET_MISSING.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_SKIP_CONSEQUENCE.md +23 -0
- package/dist/reference/errors/MOSTRA_VERIFY_TIMEOUT.md +23 -0
- package/dist/reference/self-hosting.md +130 -0
- package/dist/reference/telemetry.md +84 -0
- package/dist/skill/SKILL.md +122 -0
- package/dist/skill/assets/examples/dashboard/index.tsx +161 -0
- package/dist/skill/assets/examples/dashboard/live.ts +25 -0
- package/dist/skill/assets/examples/dashboard/styles.css +125 -0
- package/dist/skill/assets/examples/decision/index.tsx +95 -0
- package/dist/skill/assets/examples/decision/styles.css +125 -0
- package/dist/skill/assets/examples/inbox/actions.ts +23 -0
- package/dist/skill/assets/examples/inbox/index.tsx +109 -0
- package/dist/skill/assets/examples/inbox/styles.css +125 -0
- package/dist/skill/assets/studio.css +125 -0
- package/dist/skill/references/answers.md +217 -0
- package/dist/skill/references/design.md +154 -0
- package/dist/skill/references/libraries.md +167 -0
- package/dist/skill/references/long-lived.md +148 -0
- package/dist/skill/references/mcp.md +68 -0
- package/dist/skill/references/page.md +131 -0
- package/dist/skill/references/server.md +146 -0
- package/dist/skill/references/sharing.md +123 -0
- package/dist/skill/references/verify.md +71 -0
- package/package.json +10 -5
- package/src/gateway/actions.ts +20 -0
- package/src/gateway/browser.ts +44 -0
- package/src/gateway/capabilities.ts +8 -0
- package/src/gateway/http.ts +12 -1
- package/src/gateway/index.ts +170 -5
- package/src/gateway/listener.ts +31 -6
- package/src/gateway/routes.ts +8 -0
- package/src/gc/index.ts +5 -0
- package/src/instance/index.ts +20 -1
- package/src/interaction/index.ts +60 -0
- package/src/interaction/pause.ts +106 -0
- package/src/interaction/submissions.ts +42 -1
- package/src/interaction/viewers.ts +34 -1
- package/src/ledger/index.ts +20 -1
- package/src/ledger/migrations.ts +51 -0
- package/src/link/author.ts +215 -0
- package/src/link/hosted.ts +37 -1
- package/src/link/http.ts +6 -1
- package/src/link/index.ts +3 -1
- package/src/link/manager.ts +138 -34
- package/src/protocol-server/client.ts +53 -0
- package/src/protocol-server/index.ts +1 -1
- package/src/server/api.ts +514 -72
- package/src/server/auth.ts +600 -74
- package/src/server/authorize.ts +816 -0
- package/src/server/clients.ts +235 -0
- package/src/server/config.ts +7 -0
- package/src/server/data.ts +59 -1
- package/src/server/directory.ts +69 -0
- package/src/server/feed.ts +191 -0
- package/src/server/handle.ts +172 -0
- package/src/server/hosted-pages.ts +532 -0
- package/src/server/hosted.ts +519 -18
- package/src/server/index.ts +343 -61
- package/src/server/mcp.ts +330 -0
- package/src/server/metering.ts +156 -0
- package/src/server/oauth-clients.ts +510 -0
- package/src/server/operator.ts +498 -0
- package/src/server/providers.ts +284 -0
- package/src/server/remove.ts +148 -0
- package/src/server/routing.ts +275 -0
- package/src/verify/runner.ts +23 -2
- package/dist/instance-FExcyENK.js.map +0 -1
package/src/server/auth.ts
CHANGED
|
@@ -13,9 +13,11 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { createHash, randomBytes, randomInt, timingSafeEqual } from "node:crypto";
|
|
15
15
|
import { AccountId, HomeId, LinkId } from "@mostra/protocol";
|
|
16
|
+
import type { SignInIdentity } from "@mostra/protocol/operator";
|
|
16
17
|
import type { Ledger, LedgerDb } from "../ledger/index.ts";
|
|
17
18
|
import type { Log } from "../log.ts";
|
|
18
19
|
import type { RemoteServerConfig } from "./config.ts";
|
|
20
|
+
import type { Operator } from "./operator.ts";
|
|
19
21
|
|
|
20
22
|
/** The CLI's client ID: a first-party public client (§18.5). */
|
|
21
23
|
export const CLI_CLIENT_ID = "mostra-cli";
|
|
@@ -28,12 +30,22 @@ const REFRESH_TTL_MS = 90 * 24 * 60 * 60 * 1000;
|
|
|
28
30
|
const DEVICE_TTL_MS = 10 * 60 * 1000;
|
|
29
31
|
const DEVICE_INTERVAL_S = 5;
|
|
30
32
|
const ROTATION_GRACE_MS = 30_000;
|
|
33
|
+
/** An authorization code lives a minute (OAuth 2.1 §4.1.2: short-lived). */
|
|
34
|
+
const CODE_TTL_MS = 60_000;
|
|
35
|
+
/** How long a used or expired code is kept to recognize its reuse (an hour). */
|
|
36
|
+
const CODE_KEPT_MS = 60 * 60 * 1000;
|
|
31
37
|
|
|
32
38
|
/** User codes: eight letters a person can't confuse (RFC 8628 §6.1), shown as `ABCD-EFGH`. */
|
|
33
39
|
const USER_CODE_LETTERS = "BCDFGHJKLMNPQRSTVWXZ";
|
|
34
40
|
|
|
35
41
|
/** What a valid access token names. */
|
|
36
42
|
export interface Authenticated {
|
|
43
|
+
/** The resource (audience) the token was issued for: `<apiOrigin>/host` or `<apiOrigin>/mcp`. */
|
|
44
|
+
readonly resource: string;
|
|
45
|
+
/** The client the grant is for. */
|
|
46
|
+
readonly clientId: string;
|
|
47
|
+
/** The grant's scopes, space-separated. */
|
|
48
|
+
readonly scope: string;
|
|
37
49
|
readonly accountId: string;
|
|
38
50
|
readonly accountName: string | undefined;
|
|
39
51
|
readonly grantId: string;
|
|
@@ -57,13 +69,38 @@ export interface TokenAnswer {
|
|
|
57
69
|
readonly refresh_token: string;
|
|
58
70
|
readonly scope: string;
|
|
59
71
|
readonly mostra_account: { readonly id: string; readonly name?: string };
|
|
60
|
-
readonly mostra_instance
|
|
72
|
+
readonly mostra_instance?: string;
|
|
61
73
|
/** Whether this instance is the account's default instance (D165 item 1b). */
|
|
62
|
-
readonly mostra_default
|
|
74
|
+
readonly mostra_default?: boolean;
|
|
63
75
|
/** The account's hosted home. */
|
|
64
|
-
readonly mostra_home
|
|
76
|
+
readonly mostra_home?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What an authorization code carries, kept with its hash (one minute, single use). */
|
|
80
|
+
export interface CodeGrant {
|
|
81
|
+
readonly clientId: string;
|
|
82
|
+
readonly redirectUri: string;
|
|
83
|
+
readonly challenge: string;
|
|
84
|
+
readonly resource: string;
|
|
85
|
+
readonly scope: string;
|
|
86
|
+
readonly accountId: string;
|
|
87
|
+
/** A hosted shell's viewer sign-in this code finishes (INTERFACES §18.4). */
|
|
88
|
+
readonly viewerSignin?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Whether the authorization request named its redirect URI; the token request must name it
|
|
91
|
+
* too then (OAuth 2.1 §4.1.3). Absent: it did.
|
|
92
|
+
*/
|
|
93
|
+
readonly redirectGiven?: boolean;
|
|
65
94
|
}
|
|
66
95
|
|
|
96
|
+
/** A sign-in identity resolved to the server's account, or why not. */
|
|
97
|
+
export type ResolvedAccount =
|
|
98
|
+
| { readonly kind: "account"; readonly accountId: string; readonly name: string | undefined }
|
|
99
|
+
| { readonly kind: "step"; readonly url: string }
|
|
100
|
+
| { readonly kind: "refused"; readonly message: string }
|
|
101
|
+
| { readonly kind: "suspended" }
|
|
102
|
+
| { readonly kind: "unavailable" };
|
|
103
|
+
|
|
67
104
|
/** The SHA-256 of a secret, as the directory stores it. */
|
|
68
105
|
export function hashSecret(secret: string): string {
|
|
69
106
|
return createHash("sha256").update(secret).digest("hex");
|
|
@@ -80,6 +117,31 @@ function userCode(): string {
|
|
|
80
117
|
return code;
|
|
81
118
|
}
|
|
82
119
|
|
|
120
|
+
/** Whether a PKCE verifier hashes to the S256 challenge (RFC 7636 §4.6), in constant time. */
|
|
121
|
+
export function pkceMatches(verifier: string, challenge: string): boolean {
|
|
122
|
+
if (!/^[A-Za-z0-9._~-]{43,128}$/.test(verifier)) return false;
|
|
123
|
+
const computed = createHash("sha256").update(verifier).digest("base64url");
|
|
124
|
+
return (
|
|
125
|
+
computed.length === challenge.length &&
|
|
126
|
+
timingSafeEqual(Buffer.from(computed), Buffer.from(challenge))
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A resource indicator in its canonical form (RFC 8707; the MCP specification): scheme and host in
|
|
132
|
+
* lower case, no trailing slash, no fragment. An unusable one answers `""`.
|
|
133
|
+
*/
|
|
134
|
+
export function canonicalResource(resource: string): string {
|
|
135
|
+
try {
|
|
136
|
+
// WHATWG URL parsing already lower-cases the scheme and host.
|
|
137
|
+
const url = new URL(resource);
|
|
138
|
+
if (url.hash !== "" || url.search !== "") return "";
|
|
139
|
+
return `${url.protocol}//${url.host}${url.pathname.replace(/\/+$/, "")}`;
|
|
140
|
+
} catch {
|
|
141
|
+
return "";
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
83
145
|
/** `abcd-efgh`, `ABCDEFGH` → `ABCDEFGH`. */
|
|
84
146
|
function normalizeUserCode(input: string): string {
|
|
85
147
|
return input.toUpperCase().replace(/[^A-Z]/g, "");
|
|
@@ -102,9 +164,13 @@ export interface AuthorizationOptions {
|
|
|
102
164
|
readonly directory: Ledger;
|
|
103
165
|
readonly config: RemoteServerConfig;
|
|
104
166
|
readonly log: Log;
|
|
167
|
+
/** The operator module, for `accounts.resolve` and `accounts.status` (D165 item 9). */
|
|
168
|
+
readonly operator?: Operator;
|
|
105
169
|
readonly now?: () => number;
|
|
106
170
|
/** How long an access token lasts; an hour unless tests shorten it. */
|
|
107
171
|
readonly accessTtlMs?: number;
|
|
172
|
+
/** How long a used refresh token returns its successor; 30 s unless tests shorten it. */
|
|
173
|
+
readonly rotationGraceMs?: number;
|
|
108
174
|
/**
|
|
109
175
|
* Called when an instance link was made or bound again (`instance.linked`, §18.5): the server
|
|
110
176
|
* appends the event in the hosted home.
|
|
@@ -147,6 +213,61 @@ export interface Authorization {
|
|
|
147
213
|
}): { readonly ok: true } | { readonly ok: false; readonly reason: string };
|
|
148
214
|
/** The verification address people open. */
|
|
149
215
|
readonly verificationUri: string;
|
|
216
|
+
/** Approves or denies a device code for an account a browser session signed in as. */
|
|
217
|
+
decideFor(input: {
|
|
218
|
+
readonly code: string;
|
|
219
|
+
readonly approve: boolean;
|
|
220
|
+
readonly accountId: string;
|
|
221
|
+
}): { readonly ok: true } | { readonly ok: false; readonly reason: string };
|
|
222
|
+
/**
|
|
223
|
+
* Resolves a sign-in identity to an account (INTERFACES §18.5): through the operator's
|
|
224
|
+
* `accounts.resolve` when it has one, else the server's own accounts; `steps` counts the first
|
|
225
|
+
* sign-in steps already taken.
|
|
226
|
+
*/
|
|
227
|
+
resolveAccount(identity: SignInIdentity, steps: number): Promise<ResolvedAccount>;
|
|
228
|
+
/** An account's display name, and the provider that verified it (its first identity's). */
|
|
229
|
+
accountName(accountId: string): { readonly name?: string; readonly provider?: string };
|
|
230
|
+
/** Mints an authorization code (one minute, single use; only its hash is kept). */
|
|
231
|
+
issueCode(grant: CodeGrant): string;
|
|
232
|
+
/**
|
|
233
|
+
* Redeems a hosted shell's viewer sign-in code (never at the token endpoint): its client, PKCE
|
|
234
|
+
* verifier and sign-in must match. Answers the account, or why not.
|
|
235
|
+
*/
|
|
236
|
+
redeemViewerCode(input: {
|
|
237
|
+
readonly code: string;
|
|
238
|
+
readonly verifier: string;
|
|
239
|
+
readonly clientId: string;
|
|
240
|
+
readonly viewerSignin: string;
|
|
241
|
+
}): { readonly accountId: string } | { readonly refused: string };
|
|
242
|
+
/**
|
|
243
|
+
* A session for a credential the operator issued (`accounts.authenticate`, §18.6): the account
|
|
244
|
+
* (kept under the operator's ID), its hosted home, and one instance link per credential subject,
|
|
245
|
+
* so a program holding an operator-issued key runs an always-on owner instance.
|
|
246
|
+
*/
|
|
247
|
+
credentialSession(input: {
|
|
248
|
+
readonly account: string;
|
|
249
|
+
readonly subject: string;
|
|
250
|
+
readonly resource: string;
|
|
251
|
+
/** Whether this use links an instance (the binding's, `/host`); `/mcp` and `/files` don't. */
|
|
252
|
+
readonly link: boolean;
|
|
253
|
+
}): Authenticated;
|
|
254
|
+
/** The account a refresh token belongs to, so the operator's word on it is asked first. */
|
|
255
|
+
refreshAccount(refreshToken: string): string | undefined;
|
|
256
|
+
/** An account's standing in the directory. */
|
|
257
|
+
accountStatus(accountId: string): "active" | "suspended";
|
|
258
|
+
/** Records the operator's word on an account (`accounts.status`, §18.6). */
|
|
259
|
+
setAccountStatus(accountId: string, status: "active" | "suspended"): void;
|
|
260
|
+
/**
|
|
261
|
+
* Whether an instance link is its account's default, after making it so when `makeDefault`
|
|
262
|
+
* (`POST /link`, D167; one default per account, as `mostra login --default` makes it).
|
|
263
|
+
*/
|
|
264
|
+
linkState(linkId: string, makeDefault: boolean): { readonly default: boolean };
|
|
265
|
+
/** The account's hosted home, made on its first use (one home per owner per server). */
|
|
266
|
+
ensureHome(accountId: string): string;
|
|
267
|
+
/** Whether an account has consented to a client. */
|
|
268
|
+
consented(accountId: string, clientId: string): boolean;
|
|
269
|
+
/** Records an account's consent to a client. */
|
|
270
|
+
consent(accountId: string, clientId: string): void;
|
|
150
271
|
}
|
|
151
272
|
|
|
152
273
|
function oauthError(status: number, error: string, description: string): OAuthError {
|
|
@@ -158,6 +279,7 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
158
279
|
const { directory, config, log } = options;
|
|
159
280
|
const now = options.now ?? (() => Date.now());
|
|
160
281
|
const accessTtlMs = options.accessTtlMs ?? ACCESS_TTL_MS;
|
|
282
|
+
const rotationGraceMs = options.rotationGraceMs ?? ROTATION_GRACE_MS;
|
|
161
283
|
const iso = (ms: number): string => new Date(ms).toISOString();
|
|
162
284
|
const verificationUri = `${config.apiOrigin}/device`;
|
|
163
285
|
|
|
@@ -176,6 +298,35 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
176
298
|
hash,
|
|
177
299
|
);
|
|
178
300
|
|
|
301
|
+
/** An account the operator names: kept in the directory under the operator's own ID. */
|
|
302
|
+
const operatorAccount = (
|
|
303
|
+
db: LedgerDb,
|
|
304
|
+
identity: { provider: string; subject: string; name?: string; email?: string },
|
|
305
|
+
account: string,
|
|
306
|
+
name: string | undefined,
|
|
307
|
+
status: "active" | "suspended",
|
|
308
|
+
): void => {
|
|
309
|
+
db.run(
|
|
310
|
+
`INSERT INTO account (id, name, status, created_at) VALUES (?, ?, ?, ?)
|
|
311
|
+
ON CONFLICT (id) DO UPDATE SET name = COALESCE(excluded.name, name), status = excluded.status`,
|
|
312
|
+
account,
|
|
313
|
+
name ?? identity.name ?? null,
|
|
314
|
+
status,
|
|
315
|
+
iso(now()),
|
|
316
|
+
);
|
|
317
|
+
db.run(
|
|
318
|
+
`INSERT INTO identity (provider, subject, account_id, email, name, linked_at) VALUES (?, ?, ?, ?, ?, ?)
|
|
319
|
+
ON CONFLICT (provider, subject) DO UPDATE SET account_id = excluded.account_id,
|
|
320
|
+
email = excluded.email, name = excluded.name`,
|
|
321
|
+
identity.provider,
|
|
322
|
+
identity.subject,
|
|
323
|
+
account,
|
|
324
|
+
identity.email ?? null,
|
|
325
|
+
identity.name ?? null,
|
|
326
|
+
iso(now()),
|
|
327
|
+
);
|
|
328
|
+
};
|
|
329
|
+
|
|
179
330
|
/** The account for a sign-in identity, made on the first sign-in. */
|
|
180
331
|
const accountFor = (
|
|
181
332
|
db: LedgerDb,
|
|
@@ -285,8 +436,9 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
285
436
|
name: string | null;
|
|
286
437
|
is_default: number | null;
|
|
287
438
|
home_id: string | null;
|
|
439
|
+
scope: string;
|
|
288
440
|
}>(
|
|
289
|
-
`SELECT g.account_id, g.link_id, a.name, l.is_default, l.home_id
|
|
441
|
+
`SELECT g.account_id, g.link_id, a.name, l.is_default, l.home_id, g.scope
|
|
290
442
|
FROM "grant" g JOIN account a ON a.id = g.account_id
|
|
291
443
|
LEFT JOIN instance_link l ON l.id = g.link_id WHERE g.id = ?`,
|
|
292
444
|
grantId,
|
|
@@ -297,11 +449,16 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
297
449
|
token_type: "Bearer",
|
|
298
450
|
expires_in: Math.max(1, Math.floor(accessTtlMs / 1000)),
|
|
299
451
|
refresh_token: tokens.refresh,
|
|
300
|
-
scope:
|
|
452
|
+
scope: row.scope,
|
|
301
453
|
mostra_account: { id: row.account_id, ...(row.name !== null ? { name: row.name } : {}) },
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
454
|
+
// An instance's link, for the CLI's device grant; an MCP client's grant has none.
|
|
455
|
+
...(row.link_id !== null
|
|
456
|
+
? {
|
|
457
|
+
mostra_instance: row.link_id,
|
|
458
|
+
mostra_default: row.is_default === 1,
|
|
459
|
+
mostra_home: row.home_id ?? "",
|
|
460
|
+
}
|
|
461
|
+
: {}),
|
|
305
462
|
};
|
|
306
463
|
};
|
|
307
464
|
|
|
@@ -315,16 +472,98 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
315
472
|
{ readonly tokens: { access: string; refresh: string }; readonly at: number }
|
|
316
473
|
>();
|
|
317
474
|
const forgetOld = (): void => {
|
|
318
|
-
for (const [hash, entry] of recent)
|
|
319
|
-
if (now() - entry.at > ROTATION_GRACE_MS) recent.delete(hash);
|
|
475
|
+
for (const [hash, entry] of recent) if (now() - entry.at > rotationGraceMs) recent.delete(hash);
|
|
320
476
|
};
|
|
321
477
|
|
|
322
|
-
|
|
478
|
+
/** `grant_type=refresh_token`: rotation with a 30 s grace, reuse after it revokes the grant. */
|
|
479
|
+
const refresh = (form: URLSearchParams, clientId: string): TokenAnswer | OAuthError => {
|
|
480
|
+
const presented = form.get("refresh_token") ?? "";
|
|
481
|
+
const hash = hashSecret(presented);
|
|
482
|
+
return directory.write((db): TokenAnswer | OAuthError => {
|
|
483
|
+
const row = db.get<{
|
|
484
|
+
grant_id: string;
|
|
485
|
+
expires_at: string;
|
|
486
|
+
used_at: string | null;
|
|
487
|
+
}>(
|
|
488
|
+
"SELECT grant_id, expires_at, used_at FROM token WHERE hash = ? AND kind = 'refresh'",
|
|
489
|
+
hash,
|
|
490
|
+
);
|
|
491
|
+
if (
|
|
492
|
+
!row ||
|
|
493
|
+
!grantLiveIn(db, row.grant_id, { anyStatus: true }) ||
|
|
494
|
+
grantClient(db, row.grant_id) !== clientId
|
|
495
|
+
) {
|
|
496
|
+
return oauthError(400, "invalid_grant", "The refresh token isn't valid; sign in again.");
|
|
497
|
+
}
|
|
498
|
+
// A suspended account's refresh token stays good: the refresh is refused for now, without
|
|
499
|
+
// touching the token, so the instance or client keeps it and refreshes once the operator
|
|
500
|
+
// lifts the suspension. `invalid_grant` would tell it to sign in again (second Fable
|
|
501
|
+
// review of WP-30, 1).
|
|
502
|
+
if (!grantLiveIn(db, row.grant_id)) {
|
|
503
|
+
return oauthError(
|
|
504
|
+
403,
|
|
505
|
+
"temporarily_unavailable",
|
|
506
|
+
"This account is suspended; its sign-in works again once the server's operator lifts the suspension.",
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
if (Date.parse(row.expires_at) <= now()) {
|
|
510
|
+
return oauthError(400, "invalid_grant", "The refresh token expired; sign in again.");
|
|
511
|
+
}
|
|
512
|
+
if (row.used_at !== null) {
|
|
513
|
+
const used = Date.parse(row.used_at);
|
|
514
|
+
forgetOld();
|
|
515
|
+
if (now() - used <= rotationGraceMs) {
|
|
516
|
+
// A parallel refresh within the grace: the same successor (§18.5).
|
|
517
|
+
const kept = recent.get(hash);
|
|
518
|
+
if (kept) return answer(db, row.grant_id, kept.tokens);
|
|
519
|
+
return oauthError(
|
|
520
|
+
400,
|
|
521
|
+
"invalid_grant",
|
|
522
|
+
"This refresh token was just used; use its successor.",
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
// A refresh token used again after its grace: someone else holds it. The grant goes.
|
|
526
|
+
db.run('UPDATE "grant" SET revoked_at = ? WHERE id = ?', iso(now()), row.grant_id);
|
|
527
|
+
log.warn("auth.refresh_reused", { grant: row.grant_id });
|
|
528
|
+
return oauthError(
|
|
529
|
+
400,
|
|
530
|
+
"invalid_grant",
|
|
531
|
+
"The refresh token was used before; sign in again.",
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
const tokens = issue(db, row.grant_id);
|
|
535
|
+
db.run(
|
|
536
|
+
"UPDATE token SET used_at = ?, successor_hash = ? WHERE hash = ?",
|
|
537
|
+
iso(now()),
|
|
538
|
+
hashSecret(tokens.refresh),
|
|
539
|
+
hash,
|
|
540
|
+
);
|
|
541
|
+
forgetOld();
|
|
542
|
+
recent.set(hash, { tokens, at: now() });
|
|
543
|
+
return answer(db, row.grant_id, tokens);
|
|
544
|
+
});
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
/** The client a grant is for. */
|
|
548
|
+
const grantClient = (db: LedgerDb, grantId: string): string | undefined =>
|
|
549
|
+
db.get<{ client_id: string }>('SELECT client_id FROM "grant" WHERE id = ?', grantId)?.client_id;
|
|
550
|
+
|
|
551
|
+
const grantLiveIn = (
|
|
552
|
+
db: LedgerDb,
|
|
553
|
+
grantId: string,
|
|
554
|
+
options: { readonly anyStatus?: boolean } = {},
|
|
555
|
+
): boolean => {
|
|
556
|
+
// An operator's credential is checked by asking the operator, by the binding's watch.
|
|
557
|
+
if (grantId.startsWith("credential:")) return true;
|
|
323
558
|
const row = db.get<{ revoked_at: string | null; status: string }>(
|
|
324
559
|
'SELECT g.revoked_at, a.status FROM "grant" g JOIN account a ON a.id = g.account_id WHERE g.id = ?',
|
|
325
560
|
grantId,
|
|
326
561
|
);
|
|
327
|
-
return
|
|
562
|
+
return (
|
|
563
|
+
row !== undefined &&
|
|
564
|
+
row.revoked_at === null &&
|
|
565
|
+
(options.anyStatus === true || row.status === "active")
|
|
566
|
+
);
|
|
328
567
|
};
|
|
329
568
|
|
|
330
569
|
return {
|
|
@@ -371,7 +610,91 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
371
610
|
|
|
372
611
|
token(form) {
|
|
373
612
|
const grantType = form.get("grant_type");
|
|
374
|
-
|
|
613
|
+
const clientId = form.get("client_id") ?? "";
|
|
614
|
+
if (grantType === "authorization_code") {
|
|
615
|
+
const code = form.get("code") ?? "";
|
|
616
|
+
const hash = hashSecret(code);
|
|
617
|
+
return directory.write((db): TokenAnswer | OAuthError => {
|
|
618
|
+
const row = db.get<{ data_json: string; expires_at: string; used_at: string | null }>(
|
|
619
|
+
"SELECT data_json, expires_at, used_at FROM token WHERE hash = ? AND kind = 'code'",
|
|
620
|
+
hash,
|
|
621
|
+
);
|
|
622
|
+
if (row?.used_at != null) {
|
|
623
|
+
// A code presented again: someone else may hold it, so what it gave is revoked
|
|
624
|
+
// (OAuth 2.1 §4.1.3, RFC 9700 §4.5.3).
|
|
625
|
+
const spent = JSON.parse(row.data_json) as CodeGrant & { readonly grantId?: string };
|
|
626
|
+
if (spent.grantId !== undefined) {
|
|
627
|
+
db.run(
|
|
628
|
+
'UPDATE "grant" SET revoked_at = ? WHERE id = ? AND revoked_at IS NULL',
|
|
629
|
+
iso(now()),
|
|
630
|
+
spent.grantId,
|
|
631
|
+
);
|
|
632
|
+
log.warn("auth.code_reused", { grant: spent.grantId });
|
|
633
|
+
}
|
|
634
|
+
return oauthError(400, "invalid_grant", "The code is unknown, used or expired.");
|
|
635
|
+
}
|
|
636
|
+
// Spent at its first presentation, whatever comes of it: a code works once. The row is
|
|
637
|
+
// kept (as a hash) until it is long expired, so a second presentation is recognized.
|
|
638
|
+
// Code rows only: a refresh token sent as a code is no code, and is left alone.
|
|
639
|
+
db.run("UPDATE token SET used_at = ? WHERE hash = ? AND kind = 'code'", iso(now()), hash);
|
|
640
|
+
if (!row || Date.parse(row.expires_at) <= now()) {
|
|
641
|
+
return oauthError(400, "invalid_grant", "The code is unknown, used or expired.");
|
|
642
|
+
}
|
|
643
|
+
const grant = JSON.parse(row.data_json) as CodeGrant;
|
|
644
|
+
if (grant.viewerSignin !== undefined) {
|
|
645
|
+
return oauthError(
|
|
646
|
+
400,
|
|
647
|
+
"invalid_grant",
|
|
648
|
+
"This code is a page's sign-in, not a client's.",
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
if (grant.clientId !== clientId) {
|
|
652
|
+
return oauthError(400, "invalid_grant", "The code was issued to another client.");
|
|
653
|
+
}
|
|
654
|
+
// Required when the authorization request named it (OAuth 2.1 §4.1.3), and the same.
|
|
655
|
+
const redirectUri = form.get("redirect_uri");
|
|
656
|
+
if (
|
|
657
|
+
(redirectUri === null && grant.redirectGiven !== false) ||
|
|
658
|
+
(redirectUri !== null && redirectUri !== grant.redirectUri)
|
|
659
|
+
) {
|
|
660
|
+
return oauthError(
|
|
661
|
+
400,
|
|
662
|
+
"invalid_grant",
|
|
663
|
+
"redirect_uri isn't the one the code was issued for.",
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
if (!pkceMatches(form.get("code_verifier") ?? "", grant.challenge)) {
|
|
667
|
+
return oauthError(400, "invalid_grant", "The PKCE code_verifier doesn't match.");
|
|
668
|
+
}
|
|
669
|
+
const resource = form.get("resource");
|
|
670
|
+
if (resource !== null && canonicalResource(resource) !== grant.resource) {
|
|
671
|
+
return oauthError(
|
|
672
|
+
400,
|
|
673
|
+
"invalid_target",
|
|
674
|
+
"resource isn't the one the code was issued for.",
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
const grantId = `grt_${randomBytes(16).toString("hex")}`;
|
|
678
|
+
db.run(
|
|
679
|
+
'INSERT INTO "grant" (id, account_id, client_id, scope, resource, link_id, created_at) VALUES (?, ?, ?, ?, ?, NULL, ?)',
|
|
680
|
+
grantId,
|
|
681
|
+
grant.accountId,
|
|
682
|
+
grant.clientId,
|
|
683
|
+
grant.scope,
|
|
684
|
+
grant.resource,
|
|
685
|
+
iso(now()),
|
|
686
|
+
);
|
|
687
|
+
db.run(
|
|
688
|
+
"UPDATE token SET data_json = ? WHERE hash = ? AND kind = 'code'",
|
|
689
|
+
JSON.stringify({ ...grant, grantId }),
|
|
690
|
+
hash,
|
|
691
|
+
);
|
|
692
|
+
log.info("auth.code_redeemed", { client: grant.clientId, grant: grantId });
|
|
693
|
+
return answer(db, grantId, issue(db, grantId));
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
if (grantType === "refresh_token") return refresh(form, clientId);
|
|
697
|
+
if (clientId !== CLI_CLIENT_ID) {
|
|
375
698
|
return oauthError(401, "invalid_client", "Unknown client.");
|
|
376
699
|
}
|
|
377
700
|
if (grantType === DEVICE_GRANT) {
|
|
@@ -434,63 +757,11 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
434
757
|
return answer(db, grantId, tokens);
|
|
435
758
|
});
|
|
436
759
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
grant_id: string;
|
|
443
|
-
expires_at: string;
|
|
444
|
-
used_at: string | null;
|
|
445
|
-
}>(
|
|
446
|
-
"SELECT grant_id, expires_at, used_at FROM token WHERE hash = ? AND kind = 'refresh'",
|
|
447
|
-
hash,
|
|
448
|
-
);
|
|
449
|
-
if (!row || !grantLiveIn(db, row.grant_id)) {
|
|
450
|
-
return oauthError(
|
|
451
|
-
400,
|
|
452
|
-
"invalid_grant",
|
|
453
|
-
"The refresh token isn't valid; sign in again.",
|
|
454
|
-
);
|
|
455
|
-
}
|
|
456
|
-
if (Date.parse(row.expires_at) <= now()) {
|
|
457
|
-
return oauthError(400, "invalid_grant", "The refresh token expired; sign in again.");
|
|
458
|
-
}
|
|
459
|
-
if (row.used_at !== null) {
|
|
460
|
-
const used = Date.parse(row.used_at);
|
|
461
|
-
forgetOld();
|
|
462
|
-
if (now() - used <= ROTATION_GRACE_MS) {
|
|
463
|
-
// A parallel refresh within the grace: the same successor (§18.5).
|
|
464
|
-
const kept = recent.get(hash);
|
|
465
|
-
if (kept) return answer(db, row.grant_id, kept.tokens);
|
|
466
|
-
return oauthError(
|
|
467
|
-
400,
|
|
468
|
-
"invalid_grant",
|
|
469
|
-
"This refresh token was just used; use its successor.",
|
|
470
|
-
);
|
|
471
|
-
}
|
|
472
|
-
// A refresh token used again after its grace: someone else holds it. The grant goes.
|
|
473
|
-
db.run('UPDATE "grant" SET revoked_at = ? WHERE id = ?', iso(now()), row.grant_id);
|
|
474
|
-
log.warn("auth.refresh_reused", { grant: row.grant_id });
|
|
475
|
-
return oauthError(
|
|
476
|
-
400,
|
|
477
|
-
"invalid_grant",
|
|
478
|
-
"The refresh token was used before; sign in again.",
|
|
479
|
-
);
|
|
480
|
-
}
|
|
481
|
-
const tokens = issue(db, row.grant_id);
|
|
482
|
-
db.run(
|
|
483
|
-
"UPDATE token SET used_at = ?, successor_hash = ? WHERE hash = ?",
|
|
484
|
-
iso(now()),
|
|
485
|
-
hashSecret(tokens.refresh),
|
|
486
|
-
hash,
|
|
487
|
-
);
|
|
488
|
-
forgetOld();
|
|
489
|
-
recent.set(hash, { tokens, at: now() });
|
|
490
|
-
return answer(db, row.grant_id, tokens);
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
return oauthError(400, "unsupported_grant_type", "Use the device grant or a refresh token.");
|
|
760
|
+
return oauthError(
|
|
761
|
+
400,
|
|
762
|
+
"unsupported_grant_type",
|
|
763
|
+
"Use authorization_code, refresh_token or the device grant.",
|
|
764
|
+
);
|
|
494
765
|
},
|
|
495
766
|
|
|
496
767
|
revoke(form) {
|
|
@@ -523,8 +794,11 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
523
794
|
account_id: string;
|
|
524
795
|
link_id: string | null;
|
|
525
796
|
name: string | null;
|
|
797
|
+
resource: string;
|
|
798
|
+
client_id: string;
|
|
799
|
+
scope: string;
|
|
526
800
|
}>(
|
|
527
|
-
`SELECT t.hash, t.expires_at, t.grant_id, g.account_id, g.link_id, a.name
|
|
801
|
+
`SELECT t.hash, t.expires_at, t.grant_id, g.account_id, g.link_id, a.name, g.resource, g.client_id, g.scope
|
|
528
802
|
FROM token t JOIN "grant" g ON g.id = t.grant_id JOIN account a ON a.id = g.account_id
|
|
529
803
|
WHERE t.hash = ? AND t.kind = 'access'`,
|
|
530
804
|
hash,
|
|
@@ -533,23 +807,275 @@ export function createAuthorization(options: AuthorizationOptions): Authorizatio
|
|
|
533
807
|
// Constant-time on what was found, though the lookup was by hash already.
|
|
534
808
|
if (!timingSafeEqual(Buffer.from(row.hash), Buffer.from(hash))) return undefined;
|
|
535
809
|
const expiresAt = Date.parse(row.expires_at);
|
|
536
|
-
|
|
810
|
+
// A suspended account's token still names it, so its caller hears "suspended" (403)
|
|
811
|
+
// rather than "sign in again", and the operator's later word can lift it (D165 item 9).
|
|
812
|
+
if (expiresAt <= now() || !grantLiveIn(db, row.grant_id, { anyStatus: true }))
|
|
813
|
+
return undefined;
|
|
537
814
|
const home = db.get<{ id: string }>(
|
|
538
815
|
"SELECT id FROM home WHERE owner_account_id = ?",
|
|
539
816
|
row.account_id,
|
|
540
817
|
);
|
|
541
|
-
|
|
818
|
+
// An MCP client's account may have no home yet: it gets one at its first hosted call.
|
|
819
|
+
if (!home && row.resource.endsWith("/host")) return undefined;
|
|
542
820
|
return {
|
|
821
|
+
resource: row.resource,
|
|
822
|
+
clientId: row.client_id,
|
|
823
|
+
scope: row.scope,
|
|
543
824
|
accountId: row.account_id,
|
|
544
825
|
accountName: row.name ?? undefined,
|
|
545
826
|
grantId: row.grant_id,
|
|
546
|
-
homeId: home
|
|
827
|
+
homeId: home?.id ?? "",
|
|
547
828
|
linkId: row.link_id ?? undefined,
|
|
548
829
|
expiresAt,
|
|
549
830
|
};
|
|
550
831
|
});
|
|
551
832
|
},
|
|
552
833
|
|
|
834
|
+
decideFor(input) {
|
|
835
|
+
const wanted = normalizeUserCode(input.code);
|
|
836
|
+
return directory.write((db) => {
|
|
837
|
+
const rows = db.all<{ hash: string; data_json: string }>(
|
|
838
|
+
"SELECT hash, data_json FROM token WHERE kind = 'device' AND expires_at > ?",
|
|
839
|
+
iso(now()),
|
|
840
|
+
);
|
|
841
|
+
const row = rows.find(
|
|
842
|
+
(candidate) => (JSON.parse(candidate.data_json) as DeviceState).userCode === wanted,
|
|
843
|
+
);
|
|
844
|
+
if (!row) return { ok: false as const, reason: "That code is unknown or has expired." };
|
|
845
|
+
const state = JSON.parse(row.data_json) as DeviceState;
|
|
846
|
+
if (state.status !== "pending")
|
|
847
|
+
return { ok: false as const, reason: "That code was already used." };
|
|
848
|
+
db.run(
|
|
849
|
+
"UPDATE token SET data_json = ? WHERE hash = ?",
|
|
850
|
+
JSON.stringify(
|
|
851
|
+
input.approve
|
|
852
|
+
? { ...state, status: "approved", accountId: input.accountId }
|
|
853
|
+
: { ...state, status: "denied" },
|
|
854
|
+
),
|
|
855
|
+
row.hash,
|
|
856
|
+
);
|
|
857
|
+
if (input.approve) {
|
|
858
|
+
db.run(
|
|
859
|
+
"INSERT INTO consent (account_id, client_id, granted_at) VALUES (?, ?, ?) ON CONFLICT (account_id, client_id) DO UPDATE SET revoked_at = NULL",
|
|
860
|
+
input.accountId,
|
|
861
|
+
CLI_CLIENT_ID,
|
|
862
|
+
iso(now()),
|
|
863
|
+
);
|
|
864
|
+
}
|
|
865
|
+
return { ok: true as const };
|
|
866
|
+
});
|
|
867
|
+
},
|
|
868
|
+
|
|
869
|
+
async resolveAccount(identity, steps) {
|
|
870
|
+
const verdict = options.operator
|
|
871
|
+
? await options.operator.resolve(identity, steps)
|
|
872
|
+
: ({ default: true } as const);
|
|
873
|
+
if (verdict === "unavailable") return { kind: "unavailable" };
|
|
874
|
+
if ("default" in verdict) {
|
|
875
|
+
const accountId = directory.write((db) => accountFor(db, identity));
|
|
876
|
+
const status = directory.read(
|
|
877
|
+
(db) =>
|
|
878
|
+
db.get<{ status: string }>("SELECT status FROM account WHERE id = ?", accountId)
|
|
879
|
+
?.status,
|
|
880
|
+
);
|
|
881
|
+
if (status === "suspended") return { kind: "suspended" };
|
|
882
|
+
return { kind: "account", accountId, name: identity.name };
|
|
883
|
+
}
|
|
884
|
+
if ("step" in verdict) return { kind: "step", url: verdict.step.url };
|
|
885
|
+
if ("refused" in verdict) return { kind: "refused", message: verdict.refused.message };
|
|
886
|
+
directory.write((db) =>
|
|
887
|
+
operatorAccount(db, identity, verdict.account, verdict.name, verdict.status),
|
|
888
|
+
);
|
|
889
|
+
if (verdict.status === "suspended") return { kind: "suspended" };
|
|
890
|
+
return { kind: "account", accountId: verdict.account, name: verdict.name ?? identity.name };
|
|
891
|
+
},
|
|
892
|
+
|
|
893
|
+
accountName(accountId) {
|
|
894
|
+
return directory.read((db) => {
|
|
895
|
+
const row = db.get<{ name: string | null; provider: string | null }>(
|
|
896
|
+
`SELECT a.name, (SELECT i.provider FROM identity i WHERE i.account_id = a.id
|
|
897
|
+
ORDER BY i.linked_at LIMIT 1) AS provider FROM account a WHERE a.id = ?`,
|
|
898
|
+
accountId,
|
|
899
|
+
);
|
|
900
|
+
return {
|
|
901
|
+
...(row?.name ? { name: row.name } : {}),
|
|
902
|
+
...(row?.provider ? { provider: row.provider } : {}),
|
|
903
|
+
};
|
|
904
|
+
});
|
|
905
|
+
},
|
|
906
|
+
|
|
907
|
+
issueCode(grant) {
|
|
908
|
+
const code = secret();
|
|
909
|
+
directory.write((db) => {
|
|
910
|
+
// Codes are kept a while after use to recognize a second presentation, then dropped.
|
|
911
|
+
db.run(
|
|
912
|
+
"DELETE FROM token WHERE kind = 'code' AND expires_at < ?",
|
|
913
|
+
iso(now() - CODE_KEPT_MS),
|
|
914
|
+
);
|
|
915
|
+
db.run(
|
|
916
|
+
"INSERT INTO token (hash, kind, expires_at, data_json) VALUES (?, 'code', ?, ?)",
|
|
917
|
+
hashSecret(code),
|
|
918
|
+
iso(now() + CODE_TTL_MS),
|
|
919
|
+
JSON.stringify(grant),
|
|
920
|
+
);
|
|
921
|
+
});
|
|
922
|
+
return code;
|
|
923
|
+
},
|
|
924
|
+
|
|
925
|
+
redeemViewerCode(input) {
|
|
926
|
+
const hash = hashSecret(input.code);
|
|
927
|
+
return directory.write((db) => {
|
|
928
|
+
const row = db.get<{ data_json: string; expires_at: string }>(
|
|
929
|
+
"SELECT data_json, expires_at FROM token WHERE hash = ? AND kind = 'code'",
|
|
930
|
+
hash,
|
|
931
|
+
);
|
|
932
|
+
db.run("DELETE FROM token WHERE hash = ?", hash);
|
|
933
|
+
if (!row || Date.parse(row.expires_at) <= now()) {
|
|
934
|
+
return { refused: "The sign-in code is unknown, used or expired." };
|
|
935
|
+
}
|
|
936
|
+
const grant = JSON.parse(row.data_json) as CodeGrant;
|
|
937
|
+
if (grant.viewerSignin === undefined || grant.viewerSignin !== input.viewerSignin) {
|
|
938
|
+
return { refused: "This sign-in code was started for another page." };
|
|
939
|
+
}
|
|
940
|
+
if (grant.clientId !== input.clientId) {
|
|
941
|
+
return { refused: "This sign-in code was issued to another shell." };
|
|
942
|
+
}
|
|
943
|
+
if (!pkceMatches(input.verifier, grant.challenge)) {
|
|
944
|
+
return { refused: "The sign-in's PKCE verifier doesn't match." };
|
|
945
|
+
}
|
|
946
|
+
return { accountId: grant.accountId };
|
|
947
|
+
});
|
|
948
|
+
},
|
|
949
|
+
|
|
950
|
+
credentialSession(input) {
|
|
951
|
+
return directory.write((db) => {
|
|
952
|
+
db.run(
|
|
953
|
+
"INSERT INTO account (id, name, status, created_at) VALUES (?, NULL, 'active', ?) ON CONFLICT (id) DO NOTHING",
|
|
954
|
+
input.account,
|
|
955
|
+
iso(now()),
|
|
956
|
+
);
|
|
957
|
+
const home = homeFor(db, input.account);
|
|
958
|
+
let link = db.get<{ link_id: string }>(
|
|
959
|
+
"SELECT link_id FROM credential_link WHERE account_id = ? AND subject = ?",
|
|
960
|
+
input.account,
|
|
961
|
+
input.subject,
|
|
962
|
+
)?.link_id;
|
|
963
|
+
// Only a binding makes the key's instance link: a key used on /mcp or /files first names
|
|
964
|
+
// none, so no link that never binds takes the account's default (Fable review, 3).
|
|
965
|
+
if (link === undefined && input.link) {
|
|
966
|
+
link = linkFor(
|
|
967
|
+
db,
|
|
968
|
+
input.account,
|
|
969
|
+
home,
|
|
970
|
+
undefined,
|
|
971
|
+
`Service key ${input.subject}`.slice(0, 128),
|
|
972
|
+
);
|
|
973
|
+
db.run(
|
|
974
|
+
"INSERT INTO credential_link (account_id, subject, link_id) VALUES (?, ?, ?)",
|
|
975
|
+
input.account,
|
|
976
|
+
input.subject,
|
|
977
|
+
link,
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
const name = db.get<{ name: string | null }>(
|
|
981
|
+
"SELECT name FROM account WHERE id = ?",
|
|
982
|
+
input.account,
|
|
983
|
+
)?.name;
|
|
984
|
+
return {
|
|
985
|
+
resource: input.resource,
|
|
986
|
+
clientId: "operator",
|
|
987
|
+
scope: "mostra",
|
|
988
|
+
accountId: input.account,
|
|
989
|
+
accountName: name ?? undefined,
|
|
990
|
+
// Not a grant of this server: the operator is asked again while the binding stays open.
|
|
991
|
+
grantId: `credential:${input.account}:${input.subject}`,
|
|
992
|
+
homeId: home,
|
|
993
|
+
linkId: link,
|
|
994
|
+
expiresAt: now() + accessTtlMs,
|
|
995
|
+
};
|
|
996
|
+
});
|
|
997
|
+
},
|
|
998
|
+
|
|
999
|
+
refreshAccount(refreshToken) {
|
|
1000
|
+
return directory.read(
|
|
1001
|
+
(db) =>
|
|
1002
|
+
db.get<{ account_id: string }>(
|
|
1003
|
+
"SELECT g.account_id FROM token t JOIN \"grant\" g ON g.id = t.grant_id WHERE t.hash = ? AND t.kind = 'refresh'",
|
|
1004
|
+
hashSecret(refreshToken),
|
|
1005
|
+
)?.account_id,
|
|
1006
|
+
);
|
|
1007
|
+
},
|
|
1008
|
+
|
|
1009
|
+
accountStatus(accountId) {
|
|
1010
|
+
return directory.read((db) =>
|
|
1011
|
+
db.get<{ status: string }>("SELECT status FROM account WHERE id = ?", accountId)?.status ===
|
|
1012
|
+
"suspended"
|
|
1013
|
+
? "suspended"
|
|
1014
|
+
: "active",
|
|
1015
|
+
);
|
|
1016
|
+
},
|
|
1017
|
+
|
|
1018
|
+
setAccountStatus(accountId, status) {
|
|
1019
|
+
directory.write((db) =>
|
|
1020
|
+
db.run(
|
|
1021
|
+
"UPDATE account SET status = ? WHERE id = ? AND status != ?",
|
|
1022
|
+
status,
|
|
1023
|
+
accountId,
|
|
1024
|
+
status,
|
|
1025
|
+
),
|
|
1026
|
+
);
|
|
1027
|
+
},
|
|
1028
|
+
|
|
1029
|
+
linkState(linkId, makeDefault) {
|
|
1030
|
+
return directory.write((db) => {
|
|
1031
|
+
if (makeDefault) {
|
|
1032
|
+
const owner = db.get<{ account_id: string }>(
|
|
1033
|
+
"SELECT account_id FROM instance_link WHERE id = ?",
|
|
1034
|
+
linkId,
|
|
1035
|
+
);
|
|
1036
|
+
if (owner) {
|
|
1037
|
+
db.run(
|
|
1038
|
+
"UPDATE instance_link SET is_default = 0 WHERE account_id = ? AND id != ?",
|
|
1039
|
+
owner.account_id,
|
|
1040
|
+
linkId,
|
|
1041
|
+
);
|
|
1042
|
+
db.run("UPDATE instance_link SET is_default = 1 WHERE id = ?", linkId);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
const row = db.get<{ is_default: number }>(
|
|
1046
|
+
"SELECT is_default FROM instance_link WHERE id = ?",
|
|
1047
|
+
linkId,
|
|
1048
|
+
);
|
|
1049
|
+
return { default: row?.is_default === 1 };
|
|
1050
|
+
});
|
|
1051
|
+
},
|
|
1052
|
+
|
|
1053
|
+
ensureHome(accountId) {
|
|
1054
|
+
return directory.write((db) => homeFor(db, accountId));
|
|
1055
|
+
},
|
|
1056
|
+
|
|
1057
|
+
consented(accountId, clientId) {
|
|
1058
|
+
return directory.read(
|
|
1059
|
+
(db) =>
|
|
1060
|
+
db.get(
|
|
1061
|
+
"SELECT 1 FROM consent WHERE account_id = ? AND client_id = ? AND revoked_at IS NULL",
|
|
1062
|
+
accountId,
|
|
1063
|
+
clientId,
|
|
1064
|
+
) !== undefined,
|
|
1065
|
+
);
|
|
1066
|
+
},
|
|
1067
|
+
|
|
1068
|
+
consent(accountId, clientId) {
|
|
1069
|
+
directory.write((db) =>
|
|
1070
|
+
db.run(
|
|
1071
|
+
"INSERT INTO consent (account_id, client_id, granted_at) VALUES (?, ?, ?) ON CONFLICT (account_id, client_id) DO UPDATE SET revoked_at = NULL, granted_at = excluded.granted_at",
|
|
1072
|
+
accountId,
|
|
1073
|
+
clientId,
|
|
1074
|
+
iso(now()),
|
|
1075
|
+
),
|
|
1076
|
+
);
|
|
1077
|
+
},
|
|
1078
|
+
|
|
553
1079
|
grantLive(grantId) {
|
|
554
1080
|
return directory.read((db) => grantLiveIn(db, grantId));
|
|
555
1081
|
},
|