pico-engine 1.9.4 → 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 +13 -5
- package/demos/bootstrap/README.md +12 -0
- package/demos/bootstrap/com.example.visitors.bootstrap.krl +101 -0
- package/demos/bootstrap/com.example.visitors.format.krl +25 -0
- package/demos/bootstrap/com.example.visitors.krl +228 -0
- package/demos/bootstrap/try-it.md +5 -0
- package/demos/child_tasks/README.md +12 -0
- package/demos/child_tasks/com.example.task_runner.bootstrap.krl +88 -0
- package/demos/child_tasks/com.example.task_runner.krl +139 -0
- package/demos/child_tasks/com.example.task_worker.krl +45 -0
- package/demos/child_tasks/try-it.md +5 -0
- package/demos/collection/README.md +13 -0
- package/demos/collection/com.example.collection.bootstrap.krl +148 -0
- package/demos/collection/com.example.collection.krl +95 -0
- package/demos/collection/com.example.collection.report.krl +93 -0
- package/demos/collection/com.example.collection.room.krl +101 -0
- package/demos/collection/try-it.md +6 -0
- package/demos/echo/README.md +11 -0
- package/demos/echo/com.example.echo.bootstrap.krl +84 -0
- package/demos/echo/com.example.echo.krl +73 -0
- package/demos/echo/try-it.md +5 -0
- package/demos/guard_rules/README.md +12 -0
- package/demos/guard_rules/com.example.archive.bootstrap.krl +98 -0
- package/demos/guard_rules/com.example.archive.krl +213 -0
- package/demos/guard_rules/com.example.archive_store.krl +36 -0
- package/demos/guard_rules/try-it.md +5 -0
- 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/README.md +14 -0
- package/demos/kblog/com.example.kblog.bootstrap.krl +135 -0
- package/demos/kblog/com.example.kblog.config.krl +50 -0
- package/demos/kblog/com.example.kblog.data.krl +75 -0
- package/demos/kblog/com.example.kblog.krl +184 -0
- package/demos/kblog/com.example.kblog.posting.krl +109 -0
- package/demos/kblog/try-it.md +6 -0
- package/demos/layered_rulesets/README.md +13 -0
- package/demos/layered_rulesets/com.example.invitations.bootstrap.krl +87 -0
- package/demos/layered_rulesets/com.example.invitations.krl +164 -0
- package/demos/layered_rulesets/com.example.invitations.short_links.krl +45 -0
- package/demos/layered_rulesets/com.example.invitations.welcome.krl +39 -0
- package/demos/layered_rulesets/try-it.md +5 -0
- package/demos/moving_picos/README.md +15 -0
- package/demos/moving_picos/io.picolabs.club_gear_demo.krl +650 -0
- package/demos/moving_picos/io.picolabs.ride_coordinator_demo.krl +331 -0
- package/demos/score_tracker/README.md +11 -0
- package/demos/score_tracker/com.example.score_tracker.bootstrap.krl +107 -0
- package/demos/score_tracker/com.example.score_tracker.krl +162 -0
- package/demos/score_tracker/try-it.md +5 -0
- package/demos/secrets/README.md +12 -0
- package/demos/secrets/com.example.hook_service.krl +120 -0
- package/demos/secrets/com.example.notifier.bootstrap.krl +130 -0
- package/demos/secrets/com.example.notifier.krl +138 -0
- package/demos/secrets/try-it.md +5 -0
- package/demos/try-it-order.json +1 -0
- package/demos/two_picos/README.md +12 -0
- package/demos/two_picos/com.example.two_picos.answerer.krl +125 -0
- package/demos/two_picos/com.example.two_picos.asker.krl +124 -0
- package/demos/two_picos/com.example.two_picos.bootstrap.krl +134 -0
- package/demos/two_picos/try-it.md +5 -0
- package/demos/xmlui/README.md +17 -0
- package/demos/xmlui/com.example.contacts.krl +132 -0
- package/demos/xmlui/com.example.fav_color.krl +39 -0
- package/demos/xmlui/com.example.guestbook.krl +61 -0
- package/demos/xmlui/com.example.hello.krl +19 -0
- package/demos/xmlui/com.example.temperature_sensors_view.krl +105 -0
- package/demos/xmlui/com.example.thermostat.bootstrap.krl +95 -0
- package/demos/xmlui/com.example.thermostat.krl +124 -0
- package/demos/xmlui/temperature_sensors.krl +56 -0
- package/demos/xmlui/try-it.md +5 -0
- package/dist/bundledKrl.js +97 -12
- package/dist/cli.js +7 -1
- package/dist/engineConfig.js +20 -1
- package/dist/identity/portableMove.js +5 -1
- package/dist/index.js +45 -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 +210 -47
- 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/krl/io.picolabs.xmlui.krl +3 -2
- package/mcp/devResources.js +52 -0
- package/mcp/devTools.js +40 -9
- package/mcp/docs/CLAUDE.md +6 -4
- 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 +10 -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 +18 -9
- package/public/.DS_Store +0 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +26 -25
- package/src/bundledKrl.ts +118 -12
- package/src/cli.ts +8 -2
- package/src/engineConfig.ts +33 -2
- package/src/identity/portableMove.ts +6 -1
- package/src/index.ts +469 -422
- 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 +266 -51
- package/src/subtreeWrangler.ts +17 -5
- package/src/uiChannelPolicies.ts +2 -0
|
@@ -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
|
}
|
package/dist/oauth/acg.js
CHANGED
|
@@ -7,7 +7,8 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
|
7
7
|
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8
8
|
};
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.REFRESH_TOKEN_TTL_MS = exports.AUTH_CODE_TTL_MS = exports.DEFAULT_ACG_SCOPE = exports.OAUTH_REFRESH_PREFIX = exports.OAUTH_AUTH_CODE_PREFIX = exports.OAUTH_APP_PREFIX = void 0;
|
|
10
|
+
exports.PUBLIC_ACCESS_TTL_MS = exports.REFRESH_TOKEN_TTL_MS = exports.AUTH_CODE_TTL_MS = exports.DEFAULT_ACG_SCOPE = exports.OAUTH_REFRESH_USED_PREFIX = exports.OAUTH_REFRESH_PREFIX = exports.OAUTH_AUTH_CODE_PREFIX = exports.OAUTH_APP_PREFIX = void 0;
|
|
11
|
+
exports.readResource = readResource;
|
|
11
12
|
exports.parseApproveBody = parseApproveBody;
|
|
12
13
|
exports.parseAuthorizeQuery = parseAuthorizeQuery;
|
|
13
14
|
exports.listApps = listApps;
|
|
@@ -20,14 +21,29 @@ exports.authorizationCodeGrant = authorizationCodeGrant;
|
|
|
20
21
|
exports.refreshTokenGrant = refreshTokenGrant;
|
|
21
22
|
exports.mintAccessToken = mintAccessToken;
|
|
22
23
|
const crypto = require("crypto");
|
|
24
|
+
const cimd_1 = require("./cimd");
|
|
23
25
|
const errors_1 = require("./errors");
|
|
24
26
|
const meshOAuth_1 = require("./meshOAuth");
|
|
25
27
|
exports.OAUTH_APP_PREFIX = "oauth-app";
|
|
26
28
|
exports.OAUTH_AUTH_CODE_PREFIX = "oauth-auth-code";
|
|
27
29
|
exports.OAUTH_REFRESH_PREFIX = "oauth-refresh";
|
|
30
|
+
exports.OAUTH_REFRESH_USED_PREFIX = "oauth-refresh-used";
|
|
28
31
|
exports.DEFAULT_ACG_SCOPE = "mesh";
|
|
29
32
|
exports.AUTH_CODE_TTL_MS = 10 * 60 * 1000;
|
|
30
33
|
exports.REFRESH_TOKEN_TTL_MS = 90 * 24 * 60 * 60 * 1000;
|
|
34
|
+
/** Public clients (no secret) get a short access token and refresh with the rotated token. */
|
|
35
|
+
exports.PUBLIC_ACCESS_TTL_MS = 60 * 60 * 1000;
|
|
36
|
+
/** One resource parameter, or none. An empty value is the same as omitting it. */
|
|
37
|
+
function readResource(value) {
|
|
38
|
+
if (value === undefined || value === null || value === "") {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(value)) {
|
|
42
|
+
throw new errors_1.OAuthError("resource must be a single absolute URI", "invalid_target", 400);
|
|
43
|
+
}
|
|
44
|
+
const resource = String(value).trim();
|
|
45
|
+
return resource || undefined;
|
|
46
|
+
}
|
|
31
47
|
function parseApproveBody(body) {
|
|
32
48
|
var _a;
|
|
33
49
|
return {
|
|
@@ -37,6 +53,7 @@ function parseApproveBody(body) {
|
|
|
37
53
|
scope: body.scope !== undefined ? String(body.scope) : undefined,
|
|
38
54
|
code_challenge: String(body.code_challenge || "").trim(),
|
|
39
55
|
code_challenge_method: String(body.code_challenge_method || "S256").trim(),
|
|
56
|
+
resource: readResource(body.resource),
|
|
40
57
|
approved: body.approved === true ||
|
|
41
58
|
String((_a = body.approved) !== null && _a !== void 0 ? _a : "")
|
|
42
59
|
.trim()
|
|
@@ -57,8 +74,8 @@ function parseAuthorizeQuery(query) {
|
|
|
57
74
|
if (response_type !== "code") {
|
|
58
75
|
throw new errors_1.OAuthError("Unsupported response_type", "unsupported_response_type", 400);
|
|
59
76
|
}
|
|
60
|
-
if (
|
|
61
|
-
throw new errors_1.OAuthError("
|
|
77
|
+
if (code_challenge_method !== "S256") {
|
|
78
|
+
throw new errors_1.OAuthError("PKCE S256 is required", "invalid_request", 400);
|
|
62
79
|
}
|
|
63
80
|
return {
|
|
64
81
|
client_id,
|
|
@@ -68,6 +85,7 @@ function parseAuthorizeQuery(query) {
|
|
|
68
85
|
scope,
|
|
69
86
|
code_challenge,
|
|
70
87
|
code_challenge_method,
|
|
88
|
+
resource: readResource(query.resource),
|
|
71
89
|
};
|
|
72
90
|
}
|
|
73
91
|
async function listApps(ctx, rootPicoId) {
|
|
@@ -125,6 +143,12 @@ async function revokeApp(ctx, rootPicoId, clientId) {
|
|
|
125
143
|
await revokeAppTokens(ctx, clientId);
|
|
126
144
|
}
|
|
127
145
|
async function getAppForAuthorize(ctx, params) {
|
|
146
|
+
if ((0, cimd_1.isClientMetadataUrl)(params.client_id)) {
|
|
147
|
+
return resolveCimdClient(ctx, params);
|
|
148
|
+
}
|
|
149
|
+
if ((0, cimd_1.rejectsAsMetadataUrl)(params.client_id)) {
|
|
150
|
+
throw new errors_1.OAuthError("client_id must be an https URL without a fragment or userinfo", "invalid_client", 401);
|
|
151
|
+
}
|
|
128
152
|
const app = await getApp(ctx, params.client_id);
|
|
129
153
|
if (!app) {
|
|
130
154
|
throw new errors_1.OAuthError("Unknown client", "invalid_client", 401);
|
|
@@ -136,9 +160,9 @@ async function getAppForAuthorize(ctx, params) {
|
|
|
136
160
|
if (app.publicClient && !params.code_challenge) {
|
|
137
161
|
throw new errors_1.OAuthError("PKCE code_challenge is required", "invalid_request", 400);
|
|
138
162
|
}
|
|
139
|
-
return app;
|
|
163
|
+
return { app, cimd: false, consent: { name: app.name } };
|
|
140
164
|
}
|
|
141
|
-
function renderConsentHtml(app, params) {
|
|
165
|
+
function renderConsentHtml(app, params, client) {
|
|
142
166
|
const esc = escapeHtml;
|
|
143
167
|
const fields = [
|
|
144
168
|
["client_id", params.client_id],
|
|
@@ -146,6 +170,7 @@ function renderConsentHtml(app, params) {
|
|
|
146
170
|
["scope", params.scope || exports.DEFAULT_ACG_SCOPE],
|
|
147
171
|
["code_challenge", params.code_challenge],
|
|
148
172
|
["code_challenge_method", params.code_challenge_method],
|
|
173
|
+
...(params.resource ? [["resource", params.resource]] : []),
|
|
149
174
|
...(params.state !== undefined ? [["state", params.state]] : []),
|
|
150
175
|
];
|
|
151
176
|
const hidden = fields
|
|
@@ -155,7 +180,7 @@ function renderConsentHtml(app, params) {
|
|
|
155
180
|
<html lang="en">
|
|
156
181
|
<head>
|
|
157
182
|
<meta charset="utf-8"/>
|
|
158
|
-
<title>Authorize ${esc(app.name)}</title>
|
|
183
|
+
<title>Authorize ${esc((client === null || client === void 0 ? void 0 : client.name) || app.name)}</title>
|
|
159
184
|
<style>
|
|
160
185
|
body { font-family: system-ui, sans-serif; max-width: 36rem; margin: 3rem auto; padding: 0 1rem; }
|
|
161
186
|
.card { border: 1px solid #ddd; border-radius: 8px; padding: 1.25rem; }
|
|
@@ -168,8 +193,12 @@ function renderConsentHtml(app, params) {
|
|
|
168
193
|
<body>
|
|
169
194
|
<div class="card">
|
|
170
195
|
<h1>Authorize application</h1>
|
|
171
|
-
|
|
172
|
-
<p
|
|
196
|
+
${consentLogo(client)}
|
|
197
|
+
<p><strong>${esc((client === null || client === void 0 ? void 0 : client.name) || app.name)}</strong> is requesting access to <strong>${esc((client === null || client === void 0 ? void 0 : client.meshName) || "this mesh")}</strong>.</p>
|
|
198
|
+
<p class="muted">Client: <code>${esc((client === null || client === void 0 ? void 0 : client.clientIdHost) || app.clientId)}</code></p>
|
|
199
|
+
${consentLink(client)}
|
|
200
|
+
<p class="muted">Scopes: ${scopeCodes(params.scope)}</p>
|
|
201
|
+
${params.resource ? `<p class="muted">Resource: <code>${esc(params.resource)}</code></p>` : ""}
|
|
173
202
|
<p class="muted">Redirect: <code>${esc(params.redirect_uri)}</code></p>
|
|
174
203
|
<form method="POST" action="/oauth/approve">
|
|
175
204
|
${hidden}
|
|
@@ -188,7 +217,10 @@ function renderConsentHtml(app, params) {
|
|
|
188
217
|
</html>`;
|
|
189
218
|
}
|
|
190
219
|
async function approveAuthorization(ctx, session, params) {
|
|
191
|
-
|
|
220
|
+
if (params.code_challenge_method !== "S256") {
|
|
221
|
+
throw new errors_1.OAuthError("PKCE S256 is required", "invalid_request", 400);
|
|
222
|
+
}
|
|
223
|
+
const resolved = await getAppForAuthorize(ctx, {
|
|
192
224
|
client_id: params.client_id,
|
|
193
225
|
redirect_uri: params.redirect_uri,
|
|
194
226
|
response_type: "code",
|
|
@@ -197,7 +229,12 @@ async function approveAuthorization(ctx, session, params) {
|
|
|
197
229
|
code_challenge: params.code_challenge,
|
|
198
230
|
code_challenge_method: params.code_challenge_method,
|
|
199
231
|
});
|
|
200
|
-
|
|
232
|
+
let app = resolved.app;
|
|
233
|
+
if (resolved.cimd) {
|
|
234
|
+
assertMeshOAuthRoot(ctx, session.rootPicoId);
|
|
235
|
+
app = Object.assign(Object.assign({}, app), { rootPicoId: session.rootPicoId });
|
|
236
|
+
}
|
|
237
|
+
else if (app.rootPicoId !== session.rootPicoId) {
|
|
201
238
|
throw new errors_1.OAuthError("Not authorized for this mesh", "access_denied", 403);
|
|
202
239
|
}
|
|
203
240
|
if (!params.approved) {
|
|
@@ -213,6 +250,7 @@ async function approveAuthorization(ctx, session, params) {
|
|
|
213
250
|
scope: normalizeScope(params.scope || exports.DEFAULT_ACG_SCOPE),
|
|
214
251
|
codeChallenge: params.code_challenge,
|
|
215
252
|
codeChallengeMethod: params.code_challenge_method,
|
|
253
|
+
resource: params.resource,
|
|
216
254
|
expiresAt: Date.now() + exports.AUTH_CODE_TTL_MS,
|
|
217
255
|
createdAt: new Date().toISOString(),
|
|
218
256
|
};
|
|
@@ -240,19 +278,18 @@ async function authorizationCodeGrant(ctx, input) {
|
|
|
240
278
|
stored.redirectUri !== redirect_uri) {
|
|
241
279
|
throw new errors_1.OAuthError("Authorization code mismatch", "invalid_grant", 400);
|
|
242
280
|
}
|
|
243
|
-
const app = await
|
|
244
|
-
if (!app) {
|
|
245
|
-
throw new errors_1.OAuthError("Invalid client", "invalid_client", 401);
|
|
246
|
-
}
|
|
281
|
+
const app = await clientForGrant(ctx, client_id, redirect_uri, stored.rootPicoId);
|
|
247
282
|
await assertClientAuth(app, client_secret);
|
|
248
283
|
if (!verifyPkce(code_verifier, stored.codeChallenge, stored.codeChallengeMethod)) {
|
|
249
284
|
throw new errors_1.OAuthError("Invalid PKCE verifier", "invalid_grant", 400);
|
|
250
285
|
}
|
|
286
|
+
assertResourceBinding(stored.resource, input.resource);
|
|
251
287
|
await ctx.db.del([exports.OAUTH_AUTH_CODE_PREFIX, code]);
|
|
252
288
|
return mintAcgTokens(ctx, {
|
|
253
289
|
app,
|
|
254
290
|
accountId: stored.accountId,
|
|
255
291
|
scope: stored.scope,
|
|
292
|
+
resource: stored.resource,
|
|
256
293
|
});
|
|
257
294
|
}
|
|
258
295
|
async function refreshTokenGrant(ctx, input) {
|
|
@@ -267,29 +304,82 @@ async function refreshTokenGrant(ctx, input) {
|
|
|
267
304
|
refresh_token,
|
|
268
305
|
]);
|
|
269
306
|
if (!stored || stored.clientId !== client_id) {
|
|
307
|
+
if (!stored) {
|
|
308
|
+
await rejectReusedRefresh(ctx, refresh_token, client_id);
|
|
309
|
+
}
|
|
270
310
|
throw new errors_1.OAuthError("Invalid refresh token", "invalid_grant", 400);
|
|
271
311
|
}
|
|
272
312
|
if (stored.expiresAt < Date.now()) {
|
|
273
313
|
await ctx.db.del([exports.OAUTH_REFRESH_PREFIX, refresh_token]);
|
|
274
314
|
throw new errors_1.OAuthError("Refresh token expired", "invalid_grant", 400);
|
|
275
315
|
}
|
|
276
|
-
const app = await
|
|
277
|
-
if (!app) {
|
|
278
|
-
throw new errors_1.OAuthError("Invalid client", "invalid_client", 401);
|
|
279
|
-
}
|
|
316
|
+
const app = await clientForGrant(ctx, client_id, undefined, stored.rootPicoId);
|
|
280
317
|
await assertClientAuth(app, client_secret);
|
|
318
|
+
assertResourceBinding(stored.resource, input.resource);
|
|
319
|
+
const familyId = stored.familyId || crypto.randomBytes(16).toString("base64url");
|
|
281
320
|
await ctx.db.del([exports.OAUTH_REFRESH_PREFIX, refresh_token]);
|
|
282
321
|
await ctx.db.del(["oauth-token", stored.accessToken]);
|
|
283
|
-
|
|
322
|
+
const minted = await mintAcgTokens(ctx, {
|
|
284
323
|
app,
|
|
285
324
|
accountId: stored.accountId,
|
|
286
325
|
scope: stored.scope,
|
|
326
|
+
resource: stored.resource,
|
|
327
|
+
familyId,
|
|
328
|
+
});
|
|
329
|
+
const refreshExpiresAt = Date.now() + exports.REFRESH_TOKEN_TTL_MS;
|
|
330
|
+
await rememberRefreshUse(ctx, refresh_token, {
|
|
331
|
+
familyId,
|
|
332
|
+
clientId: client_id,
|
|
333
|
+
expiresAt: refreshExpiresAt,
|
|
287
334
|
});
|
|
335
|
+
await extendRefreshUses(ctx, familyId, refreshExpiresAt);
|
|
336
|
+
return minted;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* A refresh token that was already rotated is a replay. Revoke every token in
|
|
340
|
+
* its family so a stolen copy cannot keep the grant alive.
|
|
341
|
+
*/
|
|
342
|
+
async function rejectReusedRefresh(ctx, refreshToken, clientId) {
|
|
343
|
+
const used = await dbGet(ctx.db, [
|
|
344
|
+
exports.OAUTH_REFRESH_USED_PREFIX,
|
|
345
|
+
refreshToken,
|
|
346
|
+
]);
|
|
347
|
+
if (!used || used.clientId !== clientId || used.expiresAt < Date.now()) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
await revokeRefreshFamily(ctx, used.familyId);
|
|
351
|
+
}
|
|
352
|
+
async function revokeRefreshFamily(ctx, familyId) {
|
|
353
|
+
const refreshes = await dbList(ctx.db, [exports.OAUTH_REFRESH_PREFIX]);
|
|
354
|
+
for (const row of refreshes) {
|
|
355
|
+
if (row.value.familyId !== familyId) {
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
await ctx.db.del(row.key);
|
|
359
|
+
if (row.value.accessToken) {
|
|
360
|
+
await ctx.db.del(["oauth-token", row.value.accessToken]);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async function rememberRefreshUse(ctx, refreshToken, used) {
|
|
365
|
+
await ctx.db.put([exports.OAUTH_REFRESH_USED_PREFIX, refreshToken], used);
|
|
366
|
+
}
|
|
367
|
+
async function extendRefreshUses(ctx, familyId, expiresAt) {
|
|
368
|
+
const rows = await dbList(ctx.db, [exports.OAUTH_REFRESH_USED_PREFIX]);
|
|
369
|
+
for (const row of rows) {
|
|
370
|
+
if (row.value.familyId !== familyId || row.value.expiresAt >= expiresAt) {
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
await ctx.db.put(row.key, Object.assign(Object.assign({}, row.value), { expiresAt }));
|
|
374
|
+
}
|
|
288
375
|
}
|
|
289
376
|
async function mintAcgTokens(ctx, input) {
|
|
290
377
|
const access_token = mintAccessToken();
|
|
291
378
|
const refresh_token = mintRefreshToken();
|
|
292
|
-
const
|
|
379
|
+
const accessTtlMs = input.app.publicClient
|
|
380
|
+
? Math.min(ctx.tokenTtlMs, exports.PUBLIC_ACCESS_TTL_MS)
|
|
381
|
+
: ctx.tokenTtlMs;
|
|
382
|
+
const expiresAt = Date.now() + accessTtlMs;
|
|
293
383
|
await ctx.putAccessToken({
|
|
294
384
|
token: access_token,
|
|
295
385
|
rootPicoId: input.app.rootPicoId,
|
|
@@ -297,6 +387,7 @@ async function mintAcgTokens(ctx, input) {
|
|
|
297
387
|
accountId: input.accountId,
|
|
298
388
|
scope: input.scope,
|
|
299
389
|
expiresAt,
|
|
390
|
+
resource: input.resource,
|
|
300
391
|
});
|
|
301
392
|
const refreshRecord = {
|
|
302
393
|
refreshToken: refresh_token,
|
|
@@ -306,6 +397,8 @@ async function mintAcgTokens(ctx, input) {
|
|
|
306
397
|
appId: input.app.clientId,
|
|
307
398
|
accountId: input.accountId,
|
|
308
399
|
scope: input.scope,
|
|
400
|
+
resource: input.resource,
|
|
401
|
+
familyId: input.familyId || crypto.randomBytes(16).toString("base64url"),
|
|
309
402
|
expiresAt: Date.now() + exports.REFRESH_TOKEN_TTL_MS,
|
|
310
403
|
createdAt: new Date().toISOString(),
|
|
311
404
|
};
|
|
@@ -313,7 +406,7 @@ async function mintAcgTokens(ctx, input) {
|
|
|
313
406
|
return {
|
|
314
407
|
access_token,
|
|
315
408
|
token_type: "Bearer",
|
|
316
|
-
expires_in: Math.floor(
|
|
409
|
+
expires_in: Math.floor(accessTtlMs / 1000),
|
|
317
410
|
refresh_token,
|
|
318
411
|
scope: input.scope,
|
|
319
412
|
};
|
|
@@ -323,6 +416,81 @@ function assertMeshOAuthRoot(ctx, rootPicoId) {
|
|
|
323
416
|
throw new errors_1.OAuthError("Install io.picolabs.oauth on the root pico first", "invalid_request", 400);
|
|
324
417
|
}
|
|
325
418
|
}
|
|
419
|
+
async function resolveCimdClient(ctx, params) {
|
|
420
|
+
if (!ctx.loadClientMetadata) {
|
|
421
|
+
throw new errors_1.OAuthError("Invalid client", "invalid_client", 401);
|
|
422
|
+
}
|
|
423
|
+
if (!params.code_challenge) {
|
|
424
|
+
throw new errors_1.OAuthError("PKCE code_challenge is required", "invalid_request", 400);
|
|
425
|
+
}
|
|
426
|
+
const doc = await ctx.loadClientMetadata(params.client_id);
|
|
427
|
+
if (!doc.redirectUris.includes(params.redirect_uri)) {
|
|
428
|
+
throw new errors_1.OAuthError("Invalid redirect_uri", "invalid_request", 400);
|
|
429
|
+
}
|
|
430
|
+
const app = cimdApp(doc);
|
|
431
|
+
return {
|
|
432
|
+
app,
|
|
433
|
+
cimd: true,
|
|
434
|
+
consent: {
|
|
435
|
+
name: doc.clientName,
|
|
436
|
+
clientIdHost: new URL(doc.clientId).host,
|
|
437
|
+
logoUri: doc.logoUri,
|
|
438
|
+
clientUri: doc.clientUri,
|
|
439
|
+
},
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
async function clientForGrant(ctx, clientId, redirectUri, rootPicoId) {
|
|
443
|
+
if ((0, cimd_1.isClientMetadataUrl)(clientId)) {
|
|
444
|
+
if (!ctx.loadClientMetadata) {
|
|
445
|
+
throw new errors_1.OAuthError("Invalid client", "invalid_client", 401);
|
|
446
|
+
}
|
|
447
|
+
const doc = await ctx.loadClientMetadata(clientId);
|
|
448
|
+
if (redirectUri !== undefined && !doc.redirectUris.includes(redirectUri)) {
|
|
449
|
+
throw new errors_1.OAuthError("Invalid redirect_uri", "invalid_grant", 400);
|
|
450
|
+
}
|
|
451
|
+
return Object.assign(Object.assign({}, cimdApp(doc)), { rootPicoId });
|
|
452
|
+
}
|
|
453
|
+
if ((0, cimd_1.rejectsAsMetadataUrl)(clientId)) {
|
|
454
|
+
throw new errors_1.OAuthError("client_id must be an https URL without a fragment or userinfo", "invalid_client", 401);
|
|
455
|
+
}
|
|
456
|
+
const app = await getApp(ctx, clientId);
|
|
457
|
+
if (!app) {
|
|
458
|
+
throw new errors_1.OAuthError("Invalid client", "invalid_client", 401);
|
|
459
|
+
}
|
|
460
|
+
return app;
|
|
461
|
+
}
|
|
462
|
+
function cimdApp(doc) {
|
|
463
|
+
return {
|
|
464
|
+
clientId: doc.clientId,
|
|
465
|
+
rootPicoId: "",
|
|
466
|
+
name: doc.clientName,
|
|
467
|
+
redirectUris: doc.redirectUris.slice(),
|
|
468
|
+
publicClient: true,
|
|
469
|
+
scopes: [exports.DEFAULT_ACG_SCOPE],
|
|
470
|
+
createdAt: new Date().toISOString(),
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function consentLogo(client) {
|
|
474
|
+
const logo = (0, cimd_1.safeDisplayUrl)(client === null || client === void 0 ? void 0 : client.logoUri);
|
|
475
|
+
if (!logo) {
|
|
476
|
+
return "";
|
|
477
|
+
}
|
|
478
|
+
return `<img src="${escapeHtml(logo)}" alt="" referrerpolicy="no-referrer" style="max-height:48px;max-width:48px"/>`;
|
|
479
|
+
}
|
|
480
|
+
function consentLink(client) {
|
|
481
|
+
const uri = (0, cimd_1.safeDisplayUrl)(client === null || client === void 0 ? void 0 : client.clientUri);
|
|
482
|
+
if (!uri) {
|
|
483
|
+
return "";
|
|
484
|
+
}
|
|
485
|
+
return `<p class="muted">Client page: <a href="${escapeHtml(uri)}" rel="noopener noreferrer">${escapeHtml(uri)}</a></p>`;
|
|
486
|
+
}
|
|
487
|
+
function scopeCodes(scope) {
|
|
488
|
+
const raw = (scope || exports.DEFAULT_ACG_SCOPE).trim() || exports.DEFAULT_ACG_SCOPE;
|
|
489
|
+
return raw
|
|
490
|
+
.split(/\s+/)
|
|
491
|
+
.map((item) => `<code>${escapeHtml(item)}</code>`)
|
|
492
|
+
.join(" ");
|
|
493
|
+
}
|
|
326
494
|
async function getApp(ctx, clientId) {
|
|
327
495
|
return dbGet(ctx.db, [exports.OAUTH_APP_PREFIX, clientId]);
|
|
328
496
|
}
|
|
@@ -367,9 +535,6 @@ async function revokeAppTokens(ctx, clientId) {
|
|
|
367
535
|
}
|
|
368
536
|
}
|
|
369
537
|
function verifyPkce(verifier, challenge, method) {
|
|
370
|
-
if (method === "plain") {
|
|
371
|
-
return verifier === challenge;
|
|
372
|
-
}
|
|
373
538
|
if (method === "S256") {
|
|
374
539
|
const digest = crypto
|
|
375
540
|
.createHash("sha256")
|
|
@@ -395,6 +560,11 @@ function redirectWithError(redirectUri, error, state) {
|
|
|
395
560
|
}
|
|
396
561
|
return url.toString();
|
|
397
562
|
}
|
|
563
|
+
function assertResourceBinding(stored, presented) {
|
|
564
|
+
if ((stored || undefined) !== (presented || undefined)) {
|
|
565
|
+
throw new errors_1.OAuthError("resource does not match the authorization", "invalid_target", 400);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
398
568
|
function normalizeScope(scope) {
|
|
399
569
|
const trimmed = (scope || exports.DEFAULT_ACG_SCOPE).trim();
|
|
400
570
|
return trimmed || exports.DEFAULT_ACG_SCOPE;
|