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/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;
|