pico-engine 1.9.5 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -5
- package/demos/bootstrap/com.example.visitors.bootstrap.krl +9 -5
- package/demos/child_tasks/com.example.task_runner.bootstrap.krl +10 -7
- package/demos/echo/com.example.echo.bootstrap.krl +10 -7
- package/demos/guard_rules/com.example.archive.bootstrap.krl +10 -7
- package/demos/json_query/README.md +11 -0
- package/demos/json_query/com.example.json_query.bootstrap.krl +84 -0
- package/demos/json_query/com.example.json_query.krl +162 -0
- package/demos/json_query/try-it.md +5 -0
- package/demos/kblog/com.example.kblog.bootstrap.krl +10 -7
- package/demos/layered_rulesets/com.example.invitations.bootstrap.krl +10 -7
- package/demos/score_tracker/com.example.score_tracker.bootstrap.krl +10 -7
- package/demos/secrets/com.example.notifier.bootstrap.krl +10 -7
- package/demos/try-it-order.json +1 -1
- package/demos/two_picos/com.example.two_picos.bootstrap.krl +5 -2
- package/demos/xmlui/com.example.thermostat.bootstrap.krl +10 -7
- package/dist/cli.js +5 -1
- package/dist/engineConfig.js +1 -1
- package/dist/identity/portableMove.js +5 -1
- package/dist/index.js +44 -12
- package/dist/internalAccess.js +110 -0
- package/dist/oauth/OAuthService.js +172 -10
- package/dist/oauth/acg.js +194 -24
- package/dist/oauth/cimd.js +381 -0
- package/dist/oauth/discovery.js +175 -0
- package/dist/runMcp.js +1 -1
- package/dist/server.js +197 -48
- package/dist/subtreeWrangler.js +8 -5
- package/dist/uiChannelPolicies.js +2 -0
- package/krl/io.picolabs.pds.krl +109 -21
- package/krl/io.picolabs.pico-engine-ui.krl +13 -7
- package/krl/io.picolabs.wrangler.krl +21 -9
- package/mcp/devResources.js +52 -0
- package/mcp/devTools.js +40 -9
- package/mcp/docs/CLAUDE.md +3 -2
- package/mcp/docs/cookbook/README.md +23 -0
- package/mcp/docs/cookbook/bootstrap/README.md +23 -0
- package/mcp/docs/cookbook/bootstrap/instructions.md +75 -0
- package/mcp/docs/cookbook/child_tasks/README.md +20 -0
- package/mcp/docs/cookbook/child_tasks/instructions.md +60 -0
- package/mcp/docs/cookbook/collection/README.md +21 -0
- package/mcp/docs/cookbook/collection/instructions.md +93 -0
- package/mcp/docs/cookbook/echo/README.md +19 -0
- package/mcp/docs/cookbook/echo/instructions.md +59 -0
- package/mcp/docs/cookbook/guard_rules/README.md +22 -0
- package/mcp/docs/cookbook/guard_rules/instructions.md +73 -0
- package/mcp/docs/cookbook/json_query/README.md +26 -0
- package/mcp/docs/cookbook/json_query/instructions.md +64 -0
- package/mcp/docs/cookbook/kblog/README.md +60 -0
- package/mcp/docs/cookbook/kblog/instructions.md +102 -0
- package/mcp/docs/cookbook/layered_rulesets/README.md +24 -0
- package/mcp/docs/cookbook/layered_rulesets/instructions.md +87 -0
- package/mcp/docs/cookbook/moving_picos/README.md +44 -0
- package/mcp/docs/cookbook/moving_picos/instructions.md +150 -0
- package/mcp/docs/cookbook/score_tracker/README.md +19 -0
- package/mcp/docs/cookbook/score_tracker/instructions.md +73 -0
- package/mcp/docs/cookbook/secrets/README.md +25 -0
- package/mcp/docs/cookbook/secrets/instructions.md +98 -0
- package/mcp/docs/cookbook/two_picos/README.md +20 -0
- package/mcp/docs/cookbook/two_picos/instructions.md +88 -0
- package/mcp/docs/cookbook/xmlui/README.md +23 -0
- package/mcp/docs/cookbook/xmlui/instructions.md +84 -0
- package/mcp/docs/developing/README.md +28 -0
- package/mcp/docs/developing/accepting-relationship-requests.md +65 -0
- package/mcp/docs/developing/arrays-and-maps.md +77 -0
- package/mcp/docs/developing/converting-time-formats.md +28 -0
- package/mcp/docs/developing/cookies-and-redirects.md +81 -0
- package/mcp/docs/developing/decoding-form-data.md +67 -0
- package/mcp/docs/developing/engine-log-files.md +34 -0
- package/mcp/docs/developing/event-loop.md +77 -0
- package/mcp/docs/developing/events-from-email.md +80 -0
- package/mcp/docs/developing/initializing-entity-variables.md +83 -0
- package/mcp/docs/developing/mcp-ai-tools.md +98 -0
- package/mcp/docs/developing/misconceptions.md +467 -0
- package/mcp/docs/developing/names-and-scope.md +44 -0
- package/mcp/docs/developing/postman.md +159 -0
- package/mcp/docs/developing/querying-another-pico.md +47 -0
- package/mcp/docs/developing/singleton-picos.md +36 -0
- package/mcp/docs/developing/tabular-data.md +67 -0
- package/mcp/docs/developing/tips.md +251 -0
- package/mcp/docs/developing/webhook-signatures.md +56 -0
- package/mcp/docs/developing/workflow.md +106 -0
- package/mcp/docs/exp/README.md +46 -0
- package/mcp/docs/exp/event.md +110 -0
- package/mcp/docs/exp/query.md +94 -0
- package/mcp/docs/lessons/README.md +59 -0
- package/mcp/docs/lessons/events-and-queries.md +247 -0
- package/mcp/docs/lessons/gossip.md +28 -0
- package/mcp/docs/lessons/modules-and-apis.md +270 -0
- package/mcp/docs/lessons/pico-based-systems.md +470 -0
- package/mcp/docs/lessons/pico-state.md +267 -0
- package/mcp/docs/lessons/pico-views.md +290 -0
- package/mcp/docs/lessons/quickstart.md +157 -0
- package/mcp/docs/lessons/relationships.md +530 -0
- package/mcp/docs/lessons/scatter-gather.md +362 -0
- package/mcp/docs/pico-engine.md +7 -5
- package/mcp/docs/pico-overview.md +3 -3
- package/mcp/docs/wrangler/README.md +47 -0
- package/mcp/docs/wrangler/channels.md +235 -0
- package/mcp/docs/wrangler/oauth.md +56 -0
- package/mcp/docs/wrangler/pds.md +423 -0
- package/mcp/docs/wrangler/portable-subtree.md +44 -0
- package/mcp/docs/wrangler/relationships.md +297 -0
- package/mcp/docs/wrangler/wrangler.md +253 -0
- package/mcp/docs/wrangler/xmlui.md +60 -0
- package/mcp/engineClient.js +57 -5
- package/mcp/mcpScopes.js +118 -0
- package/mcp/meshHttp.js +151 -45
- package/mcp/runStdio.js +2 -0
- package/mcp/subtreeTools.js +184 -0
- package/mcp/tryItTools.js +202 -0
- package/package.json +14 -8
- package/public/pico-engine-ui.js +26 -26
- package/src/cli.ts +6 -2
- package/src/engineConfig.ts +5 -2
- package/src/identity/portableMove.ts +6 -1
- package/src/index.ts +469 -430
- package/src/internalAccess.ts +127 -0
- package/src/oauth/OAuthService.ts +243 -11
- package/src/oauth/acg.ts +289 -25
- package/src/oauth/cimd.ts +508 -0
- package/src/oauth/discovery.ts +233 -0
- package/src/runMcp.ts +3 -1
- package/src/server.ts +252 -52
- package/src/subtreeWrangler.ts +17 -5
- package/src/uiChannelPolicies.ts +2 -0
package/dist/engineConfig.js
CHANGED
|
@@ -39,7 +39,7 @@ function defaultEngineSettings() {
|
|
|
39
39
|
port: 3000,
|
|
40
40
|
baseUrl: "http://localhost:3000",
|
|
41
41
|
allowSelfSignup: false,
|
|
42
|
-
allowLocalhostChannelBypass:
|
|
42
|
+
allowLocalhostChannelBypass: false,
|
|
43
43
|
wildcardSkyRequireBearer: false,
|
|
44
44
|
logToStdout: false,
|
|
45
45
|
logMirror: true,
|
|
@@ -75,7 +75,11 @@ async function importPortableSubtree(deps, parent, blob, secret, relocateOptions
|
|
|
75
75
|
? { channelEci: importOptions.parentChannelEci }
|
|
76
76
|
: undefined;
|
|
77
77
|
const bootstrapName = resolveImportBootstrapName(blob, secret, importOptions === null || importOptions === void 0 ? void 0 : importOptions.bootstrapDisplayName);
|
|
78
|
-
const { familyEci } = await (0, subtreeWrangler_1.createImportChildViaWrangler)(deps.pf, parent, bootstrapName, deps.rsRegistry, createOpts)
|
|
78
|
+
const { familyEci } = await (0, subtreeWrangler_1.createImportChildViaWrangler)(deps.pf, parent, bootstrapName, deps.rsRegistry, createOpts, async (picoId, ctx) => {
|
|
79
|
+
if (deps.getBaseUrl()) {
|
|
80
|
+
await deps.identity.ensureWebvhDid(picoId, { isRoot: ctx.isRoot });
|
|
81
|
+
}
|
|
82
|
+
});
|
|
79
83
|
const importResult = await (0, pico_engine_core_1.importIntoExistingChild)(services, deps.pf, parent, familyEci, blob, secret, Object.assign(Object.assign({}, importOptions), { async afterImport(result, payload) {
|
|
80
84
|
if (importOptions === null || importOptions === void 0 ? void 0 : importOptions.afterImport) {
|
|
81
85
|
await importOptions.afterImport(result, payload);
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,7 @@ const provisionRoot_1 = require("./provisionRoot");
|
|
|
15
15
|
const RulesetRegistryLoaderFs_1 = require("./RulesetRegistryLoaderFs");
|
|
16
16
|
const bundledKrl_1 = require("./bundledKrl");
|
|
17
17
|
const subtreeWrangler_1 = require("./subtreeWrangler");
|
|
18
|
+
const internalAccess_1 = require("./internalAccess");
|
|
18
19
|
const server_1 = require("./server");
|
|
19
20
|
const engineConfig_1 = require("./engineConfig");
|
|
20
21
|
const charwise = require("charwise");
|
|
@@ -45,6 +46,7 @@ async function startEngine(configuration = {}) {
|
|
|
45
46
|
rpName: configuration.rpName,
|
|
46
47
|
origin: configuration.origin,
|
|
47
48
|
allowSelfSignup: configuration.allowSelfSignup,
|
|
49
|
+
allowLocalhostChannelBypass: configuration.allowLocalhostChannelBypass,
|
|
48
50
|
showTryItMenu: configuration.showTryItMenu,
|
|
49
51
|
defaultRootPolicyCeiling: configuration.defaultRootPolicyCeiling,
|
|
50
52
|
globalEventDeny: configuration.globalEventDeny,
|
|
@@ -85,7 +87,7 @@ async function startEngine(configuration = {}) {
|
|
|
85
87
|
});
|
|
86
88
|
const pf = core.picoFramework;
|
|
87
89
|
core.resolveBundledRulesetUrl = bundledKrl_1.resolveBundledKrlUrl;
|
|
88
|
-
core.subtreeImportServices = (0, subtreeWrangler_1.wranglerSubtreeImportServices)(core.rsRegistry);
|
|
90
|
+
core.subtreeImportServices = (0, subtreeWrangler_1.wranglerSubtreeImportServices)(core.rsRegistry, (picoId, ctx) => onPicoCreated(picoId, ctx));
|
|
89
91
|
const identity = new identity_1.IdentityStore({ db: pf.db });
|
|
90
92
|
let identityService;
|
|
91
93
|
const onPicoCreated = async (picoId, ctx) => {
|
|
@@ -188,25 +190,55 @@ async function startEngine(configuration = {}) {
|
|
|
188
190
|
const oauth = new oauth_1.OAuthService({
|
|
189
191
|
db: pf.db,
|
|
190
192
|
pf,
|
|
193
|
+
getBaseUrl: () => core.base_url,
|
|
191
194
|
});
|
|
192
195
|
core.modules["oauth"] = (0, oauth_1.initOAuthModule)(oauth);
|
|
193
|
-
|
|
196
|
+
await (0, pico_engine_core_1.warmPdsSecretLog)(core);
|
|
197
|
+
const internalToken = (0, internalAccess_1.writeInternalToken)(home);
|
|
198
|
+
const app = (0, server_1.server)(core, uiECI, auth, oauth, identityService, internalToken);
|
|
194
199
|
if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
|
|
195
200
|
port = process.env.NODE_ENV === "test" ? 0 : 3000;
|
|
196
201
|
}
|
|
197
202
|
let httpServer;
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
203
|
+
let listening = false;
|
|
204
|
+
try {
|
|
205
|
+
await new Promise((resolve, reject) => {
|
|
206
|
+
httpServer = app.listen(port, () => {
|
|
207
|
+
listening = true;
|
|
208
|
+
if (httpServer) {
|
|
209
|
+
const addr = httpServer.address();
|
|
210
|
+
if (addr && typeof addr !== "string" && _.isInteger(addr.port)) {
|
|
211
|
+
// Get the actual port i.e. if they set port to 0 nodejs will assign you an available port
|
|
212
|
+
port = addr.port;
|
|
213
|
+
}
|
|
205
214
|
}
|
|
206
|
-
|
|
207
|
-
|
|
215
|
+
resolve(undefined);
|
|
216
|
+
});
|
|
217
|
+
// listen failures are an 'error' event. With no listener, Node throws.
|
|
218
|
+
httpServer.on("error", (err) => {
|
|
219
|
+
if (!listening) {
|
|
220
|
+
reject(err);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
log.error("HTTP server error", { error: err });
|
|
224
|
+
});
|
|
208
225
|
});
|
|
209
|
-
}
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
const coded = err;
|
|
229
|
+
if (coded.code === "EADDRINUSE") {
|
|
230
|
+
log.error(`Port ${port} is already in use`);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
log.error("HTTP server failed to listen", { error: err, port });
|
|
234
|
+
}
|
|
235
|
+
if (httpServer) {
|
|
236
|
+
await new Promise((resolve) => httpServer.close(() => resolve()));
|
|
237
|
+
}
|
|
238
|
+
await rulesetLoaderHandle.close().catch(() => undefined);
|
|
239
|
+
await db.close().catch(() => undefined);
|
|
240
|
+
throw err;
|
|
241
|
+
}
|
|
210
242
|
if (typeof base_url !== "string") {
|
|
211
243
|
base_url = `http://localhost:${port}`;
|
|
212
244
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INTERNAL_TOKEN_HEADER = exports.INTERNAL_TOKEN_FILENAME = void 0;
|
|
4
|
+
exports.internalTokenPath = internalTokenPath;
|
|
5
|
+
exports.writeInternalToken = writeInternalToken;
|
|
6
|
+
exports.readInternalToken = readInternalToken;
|
|
7
|
+
exports.timingSafeEqualString = timingSafeEqualString;
|
|
8
|
+
exports.internalTokenAccepted = internalTokenAccepted;
|
|
9
|
+
exports.requestCarriesProxyClientHeaders = requestCarriesProxyClientHeaders;
|
|
10
|
+
exports.isDirectLoopbackRequest = isDirectLoopbackRequest;
|
|
11
|
+
exports.headersForKrl = headersForKrl;
|
|
12
|
+
const crypto = require("crypto");
|
|
13
|
+
const fs = require("fs");
|
|
14
|
+
const path = require("path");
|
|
15
|
+
/** File name under `PICO_ENGINE_HOME`. The dev MCP client reads the same file. */
|
|
16
|
+
exports.INTERNAL_TOKEN_FILENAME = "internal-token";
|
|
17
|
+
/** HTTP header the dev MCP sends. Express stores it lower-case. */
|
|
18
|
+
exports.INTERNAL_TOKEN_HEADER = "x-pico-internal";
|
|
19
|
+
/**
|
|
20
|
+
* Request headers that must not reach KRL. `x-pico-internal` is the engine's
|
|
21
|
+
* own dev credential; the others are caller credentials (1.10 §5 H1).
|
|
22
|
+
*/
|
|
23
|
+
const CREDENTIAL_HEADERS = new Set([
|
|
24
|
+
"authorization",
|
|
25
|
+
"proxy-authorization",
|
|
26
|
+
"cookie",
|
|
27
|
+
"x-pico-internal",
|
|
28
|
+
]);
|
|
29
|
+
const PROXY_CLIENT_HEADERS = ["x-forwarded-for", "x-real-ip", "forwarded"];
|
|
30
|
+
function internalTokenPath(home) {
|
|
31
|
+
return path.join(home, exports.INTERNAL_TOKEN_FILENAME);
|
|
32
|
+
}
|
|
33
|
+
/** New secret on every startup. Mode 0600 even when umask would loosen the write. */
|
|
34
|
+
function writeInternalToken(home) {
|
|
35
|
+
const token = crypto.randomBytes(32).toString("base64url");
|
|
36
|
+
const file = internalTokenPath(home);
|
|
37
|
+
fs.writeFileSync(file, `${token}\n`, { encoding: "utf8", mode: 0o600 });
|
|
38
|
+
fs.chmodSync(file, 0o600);
|
|
39
|
+
return token;
|
|
40
|
+
}
|
|
41
|
+
function readInternalToken(home) {
|
|
42
|
+
try {
|
|
43
|
+
const token = fs.readFileSync(internalTokenPath(home), "utf8").trim();
|
|
44
|
+
return token.length > 0 ? token : null;
|
|
45
|
+
}
|
|
46
|
+
catch (_err) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function timingSafeEqualString(actual, expected) {
|
|
51
|
+
const a = Buffer.from(actual);
|
|
52
|
+
const b = Buffer.from(expected);
|
|
53
|
+
if (a.length !== b.length) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
return crypto.timingSafeEqual(a, b);
|
|
57
|
+
}
|
|
58
|
+
function internalTokenAccepted(presented, expected) {
|
|
59
|
+
if (!expected || typeof presented !== "string") {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
const token = presented.trim();
|
|
63
|
+
if (!token) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return timingSafeEqualString(token, expected);
|
|
67
|
+
}
|
|
68
|
+
function headerIsPresent(headers, name) {
|
|
69
|
+
const value = headers[name];
|
|
70
|
+
if (typeof value === "string") {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
return Array.isArray(value) && value.length > 0;
|
|
74
|
+
}
|
|
75
|
+
/** A proxy sat in front of this request. Its socket address is the proxy, not the client. */
|
|
76
|
+
function requestCarriesProxyClientHeaders(headers) {
|
|
77
|
+
return PROXY_CLIENT_HEADERS.some((name) => headerIsPresent(headers, name));
|
|
78
|
+
}
|
|
79
|
+
function isLoopbackAddress(addr) {
|
|
80
|
+
if (!addr) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const normalized = addr.replace(/^::ffff:/, "");
|
|
84
|
+
return normalized === "127.0.0.1" || normalized === "::1";
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Direct TCP from this machine. The `Host` header is not consulted: clients set it.
|
|
88
|
+
* `X-Forwarded-For`, `X-Real-IP`, and `Forwarded` mean the peer is a proxy.
|
|
89
|
+
*/
|
|
90
|
+
function isDirectLoopbackRequest(req) {
|
|
91
|
+
var _a;
|
|
92
|
+
if (requestCarriesProxyClientHeaders(req.headers)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (isLoopbackAddress(req.ip)) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
return isLoopbackAddress((_a = req.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress);
|
|
99
|
+
}
|
|
100
|
+
/** Copy of the request headers with credential headers removed. */
|
|
101
|
+
function headersForKrl(headers) {
|
|
102
|
+
const out = {};
|
|
103
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
104
|
+
if (value === undefined || CREDENTIAL_HEADERS.has(key.toLowerCase())) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
out[key] = value;
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
@@ -11,6 +11,8 @@ exports.OAuthService = exports.renderConsentHtml = exports.parseApproveBody = ex
|
|
|
11
11
|
const crypto = require("crypto");
|
|
12
12
|
const acg = require("./acg");
|
|
13
13
|
const channelEligibility_1 = require("./channelEligibility");
|
|
14
|
+
const discovery_1 = require("./discovery");
|
|
15
|
+
const cimd_1 = require("./cimd");
|
|
14
16
|
const errors_1 = require("./errors");
|
|
15
17
|
const meshOAuth_1 = require("./meshOAuth");
|
|
16
18
|
const channelIdentityPolicy_1 = require("../channelIdentityPolicy");
|
|
@@ -28,7 +30,9 @@ Object.defineProperty(exports, "parseApproveBody", { enumerable: true, get: func
|
|
|
28
30
|
Object.defineProperty(exports, "renderConsentHtml", { enumerable: true, get: function () { return acg_1.renderConsentHtml; } });
|
|
29
31
|
class OAuthService {
|
|
30
32
|
constructor(deps) {
|
|
33
|
+
var _a;
|
|
31
34
|
this.deps = deps;
|
|
35
|
+
this.loadClientMetadata = (_a = deps.loadClientMetadata) !== null && _a !== void 0 ? _a : ((url) => (0, cimd_1.loadClientMetadata)(url));
|
|
32
36
|
}
|
|
33
37
|
get tokenTtlMs() {
|
|
34
38
|
var _a;
|
|
@@ -150,11 +154,15 @@ class OAuthService {
|
|
|
150
154
|
const tokenTtlMs = resolveTokenTtlMs(input.expires_in, this.tokenTtlMs);
|
|
151
155
|
const token = acg.mintAccessToken();
|
|
152
156
|
const expiresAt = tokenTtlMs === null ? 0 : Date.now() + tokenTtlMs;
|
|
157
|
+
const resource = this.bindClientCredentialsResource(acg.readResource(input.resource), client_id);
|
|
158
|
+
const scope = this.clientCredentialsScope(resource, input.scope);
|
|
153
159
|
const tokenRecord = {
|
|
154
160
|
token,
|
|
155
161
|
grant: "client_credentials",
|
|
156
162
|
channelEci: client_id,
|
|
157
163
|
rootPicoId: stored.rootPicoId,
|
|
164
|
+
scope,
|
|
165
|
+
resource,
|
|
158
166
|
expiresAt,
|
|
159
167
|
createdAt: new Date().toISOString(),
|
|
160
168
|
};
|
|
@@ -163,6 +171,9 @@ class OAuthService {
|
|
|
163
171
|
access_token: token,
|
|
164
172
|
token_type: "Bearer",
|
|
165
173
|
};
|
|
174
|
+
if (scope) {
|
|
175
|
+
response.scope = scope;
|
|
176
|
+
}
|
|
166
177
|
if (tokenTtlMs !== null) {
|
|
167
178
|
response.expires_in = Math.floor(tokenTtlMs / 1000);
|
|
168
179
|
}
|
|
@@ -210,7 +221,7 @@ class OAuthService {
|
|
|
210
221
|
if ((record.grant || "client_credentials") !== "authorization_code") {
|
|
211
222
|
return null;
|
|
212
223
|
}
|
|
213
|
-
if (!record.rootPicoId) {
|
|
224
|
+
if (!record.rootPicoId || !this.acceptsSkyResource(record.resource)) {
|
|
214
225
|
return null;
|
|
215
226
|
}
|
|
216
227
|
return { rootPicoId: record.rootPicoId, scope: record.scope };
|
|
@@ -228,11 +239,26 @@ class OAuthService {
|
|
|
228
239
|
await this.deps.db.del([OAUTH_TOKEN_PREFIX, trimmed]);
|
|
229
240
|
return false;
|
|
230
241
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return (0, meshOAuth_1.isChannelUnderRoot)(this.deps.pf, channelEci, record.rootPicoId);
|
|
242
|
+
if (!this.acceptsSkyResource(record.resource)) {
|
|
243
|
+
return false;
|
|
234
244
|
}
|
|
235
|
-
return record
|
|
245
|
+
return this.grantAllowsChannel(record, channelEci);
|
|
246
|
+
}
|
|
247
|
+
/** Channel MCP audience only. A Sky token, or a token for another ECI, is rejected. */
|
|
248
|
+
async validateMcpBearerToken(token, channelEci) {
|
|
249
|
+
return (await this.resolveMcpChannelToken(token, channelEci)) !== null;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* A live token whose resource is exactly this channel's MCP URL.
|
|
253
|
+
* Scope is not checked here; the route answers 403 when a call needs a scope
|
|
254
|
+
* the token does not have.
|
|
255
|
+
*/
|
|
256
|
+
async resolveMcpChannelToken(token, channelEci) {
|
|
257
|
+
return this.resolveMcpToken(token, channelEci, "channel");
|
|
258
|
+
}
|
|
259
|
+
/** Authorization-code token whose resource is exactly this mesh MCP URL. */
|
|
260
|
+
async resolveMcpMeshToken(token, channelEci) {
|
|
261
|
+
return this.resolveMcpToken(token, channelEci, "mesh");
|
|
236
262
|
}
|
|
237
263
|
listApps(rootPicoId) {
|
|
238
264
|
return acg.listApps(this.acgContext(), rootPicoId);
|
|
@@ -243,17 +269,29 @@ class OAuthService {
|
|
|
243
269
|
revokeApp(rootPicoId, clientId) {
|
|
244
270
|
return acg.revokeApp(this.acgContext(), rootPicoId, clientId);
|
|
245
271
|
}
|
|
246
|
-
getAppForAuthorize(params) {
|
|
247
|
-
|
|
272
|
+
async getAppForAuthorize(params) {
|
|
273
|
+
const resolved = await acg.getAppForAuthorize(this.acgContext(), params);
|
|
274
|
+
if (!resolved.cimd && params.resource !== undefined) {
|
|
275
|
+
params.resource = this.bindAcgResource(params.resource, resolved.app.rootPicoId);
|
|
276
|
+
}
|
|
277
|
+
return resolved;
|
|
278
|
+
}
|
|
279
|
+
meshOAuthEnabled(rootPicoId) {
|
|
280
|
+
return (0, meshOAuth_1.rootHasOAuthMeshRuleset)(this.deps.pf, rootPicoId);
|
|
281
|
+
}
|
|
282
|
+
/** Canonical resource for the consent form, once the mesh is known. */
|
|
283
|
+
bindAuthorizeResource(resource, rootPicoId) {
|
|
284
|
+
return this.bindAcgResource(resource, rootPicoId);
|
|
248
285
|
}
|
|
249
286
|
approveAuthorization(session, params) {
|
|
250
|
-
|
|
287
|
+
const resource = this.bindAcgResource(params.resource, session.rootPicoId);
|
|
288
|
+
return acg.approveAuthorization(this.acgContext(), session, Object.assign(Object.assign({}, params), { resource }));
|
|
251
289
|
}
|
|
252
290
|
authorizationCodeGrant(input) {
|
|
253
|
-
return acg.authorizationCodeGrant(this.acgContext(), input);
|
|
291
|
+
return acg.authorizationCodeGrant(this.acgContext(), Object.assign(Object.assign({}, input), { resource: this.presentedResource(input.resource) }));
|
|
254
292
|
}
|
|
255
293
|
refreshTokenGrant(input) {
|
|
256
|
-
return acg.refreshTokenGrant(this.acgContext(), input);
|
|
294
|
+
return acg.refreshTokenGrant(this.acgContext(), Object.assign(Object.assign({}, input), { resource: this.presentedResource(input.resource) }));
|
|
257
295
|
}
|
|
258
296
|
tokenGrant(body) {
|
|
259
297
|
const grant_type = String(body.grant_type || "client_credentials").trim();
|
|
@@ -270,6 +308,7 @@ class OAuthService {
|
|
|
270
308
|
db: this.deps.db,
|
|
271
309
|
pf: this.deps.pf,
|
|
272
310
|
tokenTtlMs: this.tokenTtlMs,
|
|
311
|
+
loadClientMetadata: (url) => this.loadClientMetadata(url),
|
|
273
312
|
putAccessToken: async (record) => {
|
|
274
313
|
const tokenRecord = {
|
|
275
314
|
token: record.token,
|
|
@@ -278,6 +317,7 @@ class OAuthService {
|
|
|
278
317
|
appId: record.appId,
|
|
279
318
|
accountId: record.accountId,
|
|
280
319
|
scope: record.scope,
|
|
320
|
+
resource: record.resource,
|
|
281
321
|
expiresAt: record.expiresAt,
|
|
282
322
|
createdAt: new Date().toISOString(),
|
|
283
323
|
};
|
|
@@ -285,6 +325,128 @@ class OAuthService {
|
|
|
285
325
|
},
|
|
286
326
|
};
|
|
287
327
|
}
|
|
328
|
+
grantAllowsChannel(record, channelEci) {
|
|
329
|
+
const grant = record.grant || "client_credentials";
|
|
330
|
+
if (grant === "authorization_code") {
|
|
331
|
+
return (0, meshOAuth_1.isChannelUnderRoot)(this.deps.pf, channelEci, record.rootPicoId);
|
|
332
|
+
}
|
|
333
|
+
return record.channelEci === channelEci;
|
|
334
|
+
}
|
|
335
|
+
/** No resource is a legacy Sky token. A recorded resource must be this engine's /sky. */
|
|
336
|
+
acceptsSkyResource(resource) {
|
|
337
|
+
if (!resource) {
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
const base = this.baseUrl();
|
|
341
|
+
return !!base && resource === (0, discovery_1.skyResourceUrl)(base);
|
|
342
|
+
}
|
|
343
|
+
baseUrl() {
|
|
344
|
+
var _a, _b;
|
|
345
|
+
const raw = (_b = (_a = this.deps).getBaseUrl) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
346
|
+
const base = raw && raw.trim();
|
|
347
|
+
return base || undefined;
|
|
348
|
+
}
|
|
349
|
+
requireBaseUrl() {
|
|
350
|
+
const base = this.baseUrl();
|
|
351
|
+
if (!base) {
|
|
352
|
+
throw new errors_1.OAuthError("Engine base URL is not configured", "invalid_target", 400);
|
|
353
|
+
}
|
|
354
|
+
return base;
|
|
355
|
+
}
|
|
356
|
+
/** Canonical form of a presented resource, or undefined when the client omitted it. */
|
|
357
|
+
presentedResource(resource) {
|
|
358
|
+
const value = acg.readResource(resource);
|
|
359
|
+
if (!value) {
|
|
360
|
+
return undefined;
|
|
361
|
+
}
|
|
362
|
+
return this.canonicalResource(value);
|
|
363
|
+
}
|
|
364
|
+
canonicalResource(resource) {
|
|
365
|
+
const base = this.requireBaseUrl();
|
|
366
|
+
return (0, discovery_1.canonicalResourceUrl)(base, (0, discovery_1.classifyResource)(base, resource));
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* A channel MCP client that omits scope receives query and event access.
|
|
370
|
+
* A named scope must be one of the advertised MCP scopes. Sky tokens do
|
|
371
|
+
* not gain MCP scopes.
|
|
372
|
+
*/
|
|
373
|
+
clientCredentialsScope(resource, requested) {
|
|
374
|
+
const base = this.baseUrl();
|
|
375
|
+
let mcp = false;
|
|
376
|
+
let channel = false;
|
|
377
|
+
if (resource && base) {
|
|
378
|
+
const target = (0, discovery_1.classifyResource)(base, resource);
|
|
379
|
+
if (target.kind === "mcp") {
|
|
380
|
+
mcp = true;
|
|
381
|
+
channel = target.surface === "channel";
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const asked = requested !== undefined ? String(requested).trim() : "";
|
|
385
|
+
if (asked) {
|
|
386
|
+
return mcp ? (0, discovery_1.assertMcpScopes)(asked) : asked.split(/\s+/).filter(Boolean).join(" ") || undefined;
|
|
387
|
+
}
|
|
388
|
+
if (channel) {
|
|
389
|
+
return discovery_1.MCP_CHANNEL_SCOPE;
|
|
390
|
+
}
|
|
391
|
+
return undefined;
|
|
392
|
+
}
|
|
393
|
+
bindClientCredentialsResource(resource, clientId) {
|
|
394
|
+
if (!resource) {
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
const base = this.requireBaseUrl();
|
|
398
|
+
const target = (0, discovery_1.classifyResource)(base, resource);
|
|
399
|
+
if (target.kind === "mcp") {
|
|
400
|
+
if (target.surface !== "channel" || target.eci !== clientId) {
|
|
401
|
+
throw new errors_1.OAuthError("Invalid resource", "invalid_target", 400);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return (0, discovery_1.canonicalResourceUrl)(base, target);
|
|
405
|
+
}
|
|
406
|
+
bindAcgResource(resource, rootPicoId) {
|
|
407
|
+
if (!resource) {
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
const base = this.requireBaseUrl();
|
|
411
|
+
const target = (0, discovery_1.classifyResource)(base, resource);
|
|
412
|
+
if (target.kind === "mcp") {
|
|
413
|
+
try {
|
|
414
|
+
this.lookupChannelReadOnly(target.eci);
|
|
415
|
+
}
|
|
416
|
+
catch (_e) {
|
|
417
|
+
throw new errors_1.OAuthError("Invalid resource", "invalid_target", 400);
|
|
418
|
+
}
|
|
419
|
+
if (!(0, meshOAuth_1.isChannelUnderRoot)(this.deps.pf, target.eci, rootPicoId)) {
|
|
420
|
+
throw new errors_1.OAuthError("Invalid resource", "invalid_target", 400);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return (0, discovery_1.canonicalResourceUrl)(base, target);
|
|
424
|
+
}
|
|
425
|
+
async resolveMcpToken(token, channelEci, surface) {
|
|
426
|
+
const trimmed = (token || "").trim();
|
|
427
|
+
if (!trimmed || !channelEci) {
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
const record = await this.getTokenRecord(trimmed);
|
|
431
|
+
if (!record || !record.resource || !record.rootPicoId) {
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
434
|
+
if (surface === "mesh" && (record.grant || "client_credentials") !== "authorization_code") {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
if (record.expiresAt !== 0 && record.expiresAt < Date.now()) {
|
|
438
|
+
await this.deps.db.del([OAUTH_TOKEN_PREFIX, trimmed]);
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
const base = this.baseUrl();
|
|
442
|
+
if (!base || record.resource !== (0, discovery_1.mcpResourceUrl)(base, channelEci, surface)) {
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
if (!this.grantAllowsChannel(record, channelEci)) {
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
return { scope: record.scope || "", rootPicoId: record.rootPicoId };
|
|
449
|
+
}
|
|
288
450
|
async getTokenRecord(token) {
|
|
289
451
|
return dbGet(this.deps.db, [OAUTH_TOKEN_PREFIX, token]);
|
|
290
452
|
}
|