pico-engine 1.4.0 → 1.6.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 +188 -0
- package/dist/RulesetRegistryLoaderFs.js +2 -0
- package/dist/auth/AuthService.js +601 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/bundledKrl.js +42 -0
- package/dist/cli.js +11 -0
- package/dist/identity/IdentityService.js +340 -0
- package/dist/identity/crossPico.js +238 -0
- package/dist/identity/dbHelpers.js +67 -0
- package/dist/identity/didcommIngressRoutes.js +44 -0
- package/dist/identity/didoModule.js +252 -0
- package/dist/identity/errors.js +11 -0
- package/dist/identity/establishSubscription.js +97 -0
- package/dist/identity/index.js +15 -0
- package/dist/identity/peerDid.js +9 -0
- package/dist/identity/skyIntro.js +194 -0
- package/dist/identity/skyProtocol.js +105 -0
- package/dist/identity/store.js +233 -0
- package/dist/identity/types.js +13 -0
- package/dist/identity/veramoDidComm.js +260 -0
- package/dist/identity/webvh.js +266 -0
- package/dist/identity/webvhCrypto.js +128 -0
- package/dist/identity/webvhRoutes.js +61 -0
- package/dist/index.js +100 -44
- package/dist/oauth/OAuthService.js +388 -0
- package/dist/oauth/acg.js +495 -0
- package/dist/oauth/channelEligibility.js +26 -0
- package/dist/oauth/errors.js +12 -0
- package/dist/oauth/index.js +21 -0
- package/dist/oauth/krlModule.js +27 -0
- package/dist/oauth/meshOAuth.js +43 -0
- package/dist/provisionRoot.js +118 -0
- package/dist/server.js +609 -15
- package/dist/uiChannelPolicies.js +48 -0
- package/krl/io.picolabs.did-o.krl +5 -1
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.pico-engine-ui.krl +106 -18
- package/krl/io.picolabs.subscription.krl +242 -23
- package/krl/io.picolabs.wrangler.krl +225 -16
- package/package.json +38 -9
- package/public/docs/krl/_sidebar.md +1 -0
- package/public/docs/krl/discovery.md +144 -0
- package/public/docs/krl/modules/ctx.md +8 -0
- package/public/docs/krl/modules/event.md +41 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +24 -20
- package/public/pico-labs-logo.png +0 -0
- package/src/RulesetRegistryLoaderFs.ts +2 -0
- package/src/auth/AuthService.ts +850 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/bundledKrl.ts +42 -0
- package/src/cli.ts +11 -0
- package/src/identity/IdentityService.ts +571 -0
- package/src/identity/crossPico.ts +371 -0
- package/src/identity/dbHelpers.ts +51 -0
- package/src/identity/didcommIngressRoutes.ts +54 -0
- package/src/identity/didoModule.ts +388 -0
- package/src/identity/errors.ts +8 -0
- package/src/identity/establishSubscription.ts +160 -0
- package/src/identity/index.ts +24 -0
- package/src/identity/peerDid.ts +22 -0
- package/src/identity/skyIntro.ts +321 -0
- package/src/identity/skyProtocol.ts +192 -0
- package/src/identity/store.ts +343 -0
- package/src/identity/types.ts +63 -0
- package/src/identity/veramoDidComm.ts +363 -0
- package/src/identity/webvh.ts +325 -0
- package/src/identity/webvhCrypto.ts +200 -0
- package/src/identity/webvhRoutes.ts +64 -0
- package/src/index.ts +145 -53
- package/src/oauth/OAuthService.ts +510 -0
- package/src/oauth/acg.ts +666 -0
- package/src/oauth/channelEligibility.ts +26 -0
- package/src/oauth/errors.ts +10 -0
- package/src/oauth/index.ts +14 -0
- package/src/oauth/krlModule.ts +31 -0
- package/src/oauth/meshOAuth.ts +50 -0
- package/src/provisionRoot.ts +151 -0
- package/src/server.ts +700 -24
- package/src/uiChannelPolicies.ts +49 -0
package/src/server.ts
CHANGED
|
@@ -1,27 +1,94 @@
|
|
|
1
1
|
import * as bodyParser from "body-parser";
|
|
2
2
|
import * as express from "express";
|
|
3
|
-
import { Express, Request } from "express";
|
|
3
|
+
import { Express, Request, Response } from "express";
|
|
4
4
|
import helmet from "helmet";
|
|
5
5
|
import * as _ from "lodash";
|
|
6
6
|
import * as path from "path";
|
|
7
7
|
import { PicoEngineCore } from "pico-engine-core";
|
|
8
|
+
import { PicoFramework } from "pico-framework";
|
|
9
|
+
import { AuthError, AuthService } from "./auth";
|
|
10
|
+
import { OAuthError, OAuthService, parseApproveBody, parseAuthorizeQuery, renderConsentHtml } from "./oauth";
|
|
11
|
+
import { uiECIForRoot } from "./provisionRoot";
|
|
12
|
+
import { IdentityService } from "./identity/IdentityService";
|
|
13
|
+
import { registerWebvhRoutes } from "./identity/webvhRoutes";
|
|
14
|
+
import { registerDidcommIngressRoutes } from "./identity/didcommIngressRoutes";
|
|
8
15
|
|
|
9
16
|
const engineVersion = require("../package.json").version;
|
|
10
17
|
|
|
18
|
+
const SESSION_COOKIE = "pico-session";
|
|
19
|
+
const CEREMONY_COOKIE = "pico-ceremony";
|
|
20
|
+
|
|
21
|
+
function localhostChannelBypassEnabled(): boolean {
|
|
22
|
+
return process.env.PICO_ENGINE_ALLOW_LOCALHOST_C !== "0";
|
|
23
|
+
}
|
|
24
|
+
|
|
11
25
|
function mergeGetPost(req: Request) {
|
|
12
26
|
// give preference to post body params
|
|
13
27
|
return _.assign({}, req.query, req.body, { _headers: req.headers });
|
|
14
28
|
}
|
|
15
29
|
|
|
16
|
-
|
|
30
|
+
function parseCookies(req: Request): { [name: string]: string } {
|
|
31
|
+
const header = req.headers.cookie;
|
|
32
|
+
const out: { [name: string]: string } = {};
|
|
33
|
+
if (typeof header !== "string") {
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
for (const part of header.split(";")) {
|
|
37
|
+
const idx = part.indexOf("=");
|
|
38
|
+
if (idx < 0) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const name = part.slice(0, idx).trim();
|
|
42
|
+
const value = part.slice(idx + 1).trim();
|
|
43
|
+
if (name) {
|
|
44
|
+
out[name] = decodeURIComponent(value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isLocalhostAddress(addr: string | undefined): boolean {
|
|
51
|
+
if (!addr) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
const normalized = addr.replace(/^::ffff:/, "");
|
|
55
|
+
return normalized === "127.0.0.1" || normalized === "::1";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isLocalhostRequest(req: Request): boolean {
|
|
59
|
+
if (isLocalhostAddress(req.ip)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (isLocalhostAddress(req.socket?.remoteAddress)) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
const host = req.headers.host || "";
|
|
66
|
+
return host.startsWith("localhost:") || host.startsWith("127.0.0.1:");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function server(
|
|
70
|
+
core: PicoEngineCore,
|
|
71
|
+
uiECI: string | null,
|
|
72
|
+
auth?: AuthService,
|
|
73
|
+
oauth?: OAuthService,
|
|
74
|
+
identity?: IdentityService
|
|
75
|
+
): Express {
|
|
76
|
+
const pf = core.picoFramework;
|
|
17
77
|
const app = express();
|
|
18
78
|
|
|
19
79
|
app.use(function(req, res, next) {
|
|
80
|
+
// /auth routes need a specific credentialed CORS origin (below), not "*".
|
|
81
|
+
if (req.path.indexOf("/auth") === 0) {
|
|
82
|
+
return next();
|
|
83
|
+
}
|
|
20
84
|
res.header("Access-Control-Allow-Origin", "*");
|
|
21
|
-
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
85
|
+
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
|
|
22
86
|
next();
|
|
23
87
|
});
|
|
24
88
|
app.use('*',(req,res,next) =>{
|
|
89
|
+
if (req.path.indexOf("/auth") === 0) {
|
|
90
|
+
return next();
|
|
91
|
+
}
|
|
25
92
|
if (req.method == "OPTIONS") {
|
|
26
93
|
res.header("Allow", "POST, GET, OPTIONS");
|
|
27
94
|
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
|
|
@@ -33,23 +100,35 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
33
100
|
});
|
|
34
101
|
// Helmet 7+ CSP defaults include upgrade-insecure-requests, which breaks the
|
|
35
102
|
// UI on plain HTTP (browser upgrades /api/* to https and TLS fails).
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
103
|
+
// OAuth consent pages must not use form-action 'self' — after Allow, the
|
|
104
|
+
// browser follows a redirect to the app's redirect_uri (external origin).
|
|
105
|
+
const defaultHelmet = helmet({
|
|
106
|
+
contentSecurityPolicy: {
|
|
107
|
+
directives: {
|
|
108
|
+
...helmet.contentSecurityPolicy.getDefaultDirectives(),
|
|
109
|
+
"upgrade-insecure-requests": null,
|
|
43
110
|
},
|
|
44
|
-
}
|
|
45
|
-
);
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
app.use(function (req, res, next) {
|
|
114
|
+
if (req.path === "/oauth/authorize" || req.path === "/oauth/approve") {
|
|
115
|
+
return helmet({ contentSecurityPolicy: false })(req, res, next);
|
|
116
|
+
}
|
|
117
|
+
return defaultHelmet(req, res, next);
|
|
118
|
+
});
|
|
46
119
|
app.use(express.static(path.resolve(__dirname, "..", "public")));
|
|
120
|
+
|
|
121
|
+
if (identity) {
|
|
122
|
+
registerWebvhRoutes(app, identity);
|
|
123
|
+
}
|
|
124
|
+
|
|
47
125
|
app.use(
|
|
48
126
|
bodyParser.json({
|
|
49
127
|
type: [
|
|
50
128
|
"application/json",
|
|
51
129
|
"application/octet-stream",
|
|
52
130
|
"application/ssi-agent-wire",
|
|
131
|
+
"application/didcomm-encrypted+json",
|
|
53
132
|
],
|
|
54
133
|
})
|
|
55
134
|
);
|
|
@@ -61,11 +140,191 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
61
140
|
})
|
|
62
141
|
);
|
|
63
142
|
|
|
143
|
+
if (identity) {
|
|
144
|
+
registerDidcommIngressRoutes(app, identity);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (auth) {
|
|
148
|
+
registerAuthRoutes(app, auth);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function isEciInRootTree(rootPicoId: string, eci: string): boolean {
|
|
152
|
+
try {
|
|
153
|
+
let pico = pf.getPico(eci);
|
|
154
|
+
while (pico) {
|
|
155
|
+
if (pico.id === rootPicoId) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
if (!pico.parent) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
pico = pf.getPico(pico.parent);
|
|
162
|
+
}
|
|
163
|
+
} catch (_e) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function requireAuthSession(
|
|
170
|
+
req: Request,
|
|
171
|
+
res: Response,
|
|
172
|
+
next: express.NextFunction
|
|
173
|
+
) {
|
|
174
|
+
if (!auth) {
|
|
175
|
+
next();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
179
|
+
auth
|
|
180
|
+
.getSession(token)
|
|
181
|
+
.then((session) => {
|
|
182
|
+
if (!session) {
|
|
183
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const eci = req.params.eci;
|
|
187
|
+
if (eci && !isEciInRootTree(session.rootPicoId, eci)) {
|
|
188
|
+
res.status(403).json({ error: "Not authorized for this pico" });
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
(req as any).picoSession = session;
|
|
192
|
+
next();
|
|
193
|
+
})
|
|
194
|
+
.catch(next);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** `/c/*` is internal: passkey session, or localhost for in-engine HTTP loops. */
|
|
198
|
+
function requireChannelSession(
|
|
199
|
+
req: Request,
|
|
200
|
+
res: Response,
|
|
201
|
+
next: express.NextFunction
|
|
202
|
+
) {
|
|
203
|
+
if (!auth) {
|
|
204
|
+
next();
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (localhostChannelBypassEnabled() && isLocalhostRequest(req)) {
|
|
208
|
+
next();
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
requireAuthSession(req, res, next);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** External Sky API: Bearer when channel is oauth-webhook or mesh has OAuth ruleset. */
|
|
215
|
+
function requireSkyBearerIfOAuth(
|
|
216
|
+
req: Request,
|
|
217
|
+
res: Response,
|
|
218
|
+
next: express.NextFunction
|
|
219
|
+
) {
|
|
220
|
+
if (!oauth) {
|
|
221
|
+
next();
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const eci = req.params.eci;
|
|
225
|
+
if (!eci) {
|
|
226
|
+
next();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
oauth
|
|
230
|
+
.skyRequiresBearer(eci)
|
|
231
|
+
.then((required) => {
|
|
232
|
+
if (!required) {
|
|
233
|
+
next();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const header = req.headers.authorization;
|
|
237
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) {
|
|
238
|
+
res.status(401).json({ error: "Bearer token required" });
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const token = header.slice("Bearer ".length).trim();
|
|
242
|
+
return oauth.validateSkyBearerToken(token, eci).then((valid) => {
|
|
243
|
+
if (!valid) {
|
|
244
|
+
res.status(401).json({ error: "Invalid or expired token" });
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
next();
|
|
248
|
+
});
|
|
249
|
+
})
|
|
250
|
+
.catch(next);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (oauth) {
|
|
254
|
+
registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
app.get("/api/mesh-context", function (req, res, next) {
|
|
258
|
+
if (!oauth) {
|
|
259
|
+
res.status(503).json({ error: "OAuth not enabled" });
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const header = req.headers.authorization;
|
|
263
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) {
|
|
264
|
+
res.status(401).json({ error: "Bearer token required" });
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const token = header.slice("Bearer ".length).trim();
|
|
268
|
+
oauth
|
|
269
|
+
.resolveMeshBearerToken(token)
|
|
270
|
+
.then((ctx) => {
|
|
271
|
+
if (!ctx) {
|
|
272
|
+
res.status(401).json({ error: "Invalid or expired token" });
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const rootUiEci = uiECIForRoot(pf, ctx.rootPicoId);
|
|
276
|
+
if (!rootUiEci) {
|
|
277
|
+
res.status(503).json({ error: "Mesh UI channel not found" });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
let baseUrl = core.base_url;
|
|
281
|
+
if (!baseUrl) {
|
|
282
|
+
baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
|
|
283
|
+
}
|
|
284
|
+
baseUrl = baseUrl.replace(/\/$/, "");
|
|
285
|
+
res.json({
|
|
286
|
+
baseUrl,
|
|
287
|
+
rootUiEci,
|
|
288
|
+
oauth: {
|
|
289
|
+
authorize: `${baseUrl}/oauth/authorize`,
|
|
290
|
+
token: `${baseUrl}/oauth/token`,
|
|
291
|
+
},
|
|
292
|
+
scope: ctx.scope,
|
|
293
|
+
});
|
|
294
|
+
})
|
|
295
|
+
.catch(next);
|
|
296
|
+
});
|
|
297
|
+
|
|
64
298
|
app.get("/api/ui-context", function (req, res, next) {
|
|
65
|
-
|
|
299
|
+
const hasRoots = pf.rootPicos().length > 0;
|
|
300
|
+
const base: any = {
|
|
301
|
+
version: engineVersion,
|
|
302
|
+
hasRoots,
|
|
303
|
+
allowSelfSignup: auth ? auth.allowSelfSignup() : false,
|
|
304
|
+
};
|
|
305
|
+
if (!auth) {
|
|
306
|
+
base.eci = uiECI;
|
|
307
|
+
res.json(base);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
311
|
+
auth
|
|
312
|
+
.needsAuthMigration()
|
|
313
|
+
.then((needsAuthMigration) => {
|
|
314
|
+
base.needsAuthMigration = needsAuthMigration;
|
|
315
|
+
return auth.whoami(token);
|
|
316
|
+
})
|
|
317
|
+
.then((who) => {
|
|
318
|
+
base.session = who;
|
|
319
|
+
if (who.authenticated && who.uiECI) {
|
|
320
|
+
base.eci = who.uiECI;
|
|
321
|
+
}
|
|
322
|
+
res.json(base);
|
|
323
|
+
})
|
|
324
|
+
.catch(next);
|
|
66
325
|
});
|
|
67
326
|
|
|
68
|
-
app.all("/api/flush", function (req, res, next) {
|
|
327
|
+
app.all("/api/flush", requireAuthSession, function (req, res, next) {
|
|
69
328
|
const attrs = mergeGetPost(req);
|
|
70
329
|
const url = attrs["url"];
|
|
71
330
|
if (typeof url !== "string") {
|
|
@@ -86,7 +345,7 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
86
345
|
}
|
|
87
346
|
});
|
|
88
347
|
|
|
89
|
-
app.all("/c/:eci/event/:domain/:name", function (req, res, next) {
|
|
348
|
+
app.all("/c/:eci/event/:domain/:name", requireChannelSession, function (req, res, next) {
|
|
90
349
|
core
|
|
91
350
|
.event({
|
|
92
351
|
eci: req.params.eci,
|
|
@@ -101,7 +360,7 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
101
360
|
.catch(next);
|
|
102
361
|
});
|
|
103
362
|
|
|
104
|
-
app.all("/c/:eci/event-wait/:domain/:name", function (req, res, next) {
|
|
363
|
+
app.all("/c/:eci/event-wait/:domain/:name", requireChannelSession, function (req, res, next) {
|
|
105
364
|
core
|
|
106
365
|
.eventWait({
|
|
107
366
|
eci: req.params.eci,
|
|
@@ -116,7 +375,7 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
116
375
|
.catch(next);
|
|
117
376
|
});
|
|
118
377
|
|
|
119
|
-
app.all("/sky/event/:eci/:eid/:domain/:type", function (req, res, next) {
|
|
378
|
+
app.all("/sky/event/:eci/:eid/:domain/:type", requireSkyBearerIfOAuth, function (req, res, next) {
|
|
120
379
|
const attrs = mergeGetPost(req);
|
|
121
380
|
if (req.params.eid !== "none" && !attrs.hasOwnProperty("__eid")) {
|
|
122
381
|
attrs.__eid = req.params.eid;
|
|
@@ -155,6 +414,7 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
155
414
|
|
|
156
415
|
app.all(
|
|
157
416
|
"/c/:eci/event/:domain/:name/query/:rid/:qname",
|
|
417
|
+
requireChannelSession,
|
|
158
418
|
function (req, res, next) {
|
|
159
419
|
const attrs = mergeGetPost(req);
|
|
160
420
|
core
|
|
@@ -180,8 +440,8 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
180
440
|
}
|
|
181
441
|
);
|
|
182
442
|
|
|
183
|
-
|
|
184
|
-
const funcPart = req.params.
|
|
443
|
+
function handleSkyQuery(req: Request, res: Response, next: express.NextFunction) {
|
|
444
|
+
const funcPart = req.params["function"].split(".");
|
|
185
445
|
core
|
|
186
446
|
.query({
|
|
187
447
|
eci: req.params.eci,
|
|
@@ -189,7 +449,7 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
189
449
|
name: funcPart[0],
|
|
190
450
|
args: mergeGetPost(req),
|
|
191
451
|
})
|
|
192
|
-
.then((data) => {
|
|
452
|
+
.then((data: any) => {
|
|
193
453
|
if (funcPart[1] === "html") {
|
|
194
454
|
res.header("Content-Type", "text/html");
|
|
195
455
|
res.end(data);
|
|
@@ -201,10 +461,10 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
201
461
|
}
|
|
202
462
|
})
|
|
203
463
|
.catch(next);
|
|
204
|
-
}
|
|
464
|
+
}
|
|
205
465
|
|
|
206
|
-
app.all("/
|
|
207
|
-
const funcPart = req.params
|
|
466
|
+
app.all("/c/:eci/query/:rid/:name", requireChannelSession, function (req, res, next) {
|
|
467
|
+
const funcPart = req.params.name.split(".");
|
|
208
468
|
core
|
|
209
469
|
.query({
|
|
210
470
|
eci: req.params.eci,
|
|
@@ -212,7 +472,7 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
212
472
|
name: funcPart[0],
|
|
213
473
|
args: mergeGetPost(req),
|
|
214
474
|
})
|
|
215
|
-
.then((data) => {
|
|
475
|
+
.then((data: any) => {
|
|
216
476
|
if (funcPart[1] === "html") {
|
|
217
477
|
res.header("Content-Type", "text/html");
|
|
218
478
|
res.end(data);
|
|
@@ -226,12 +486,24 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
226
486
|
.catch(next);
|
|
227
487
|
});
|
|
228
488
|
|
|
489
|
+
app.all("/sky/query/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
|
|
490
|
+
// Legacy alias; prefer /sky/query/ per Sky Query API docs.
|
|
491
|
+
app.all("/sky/cloud/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
|
|
492
|
+
|
|
229
493
|
app.use(function (
|
|
230
494
|
err: any,
|
|
231
495
|
req: express.Request,
|
|
232
496
|
res: express.Response,
|
|
233
497
|
next: express.NextFunction
|
|
234
498
|
) {
|
|
499
|
+
if (err instanceof AuthError) {
|
|
500
|
+
res.status(err.statusCode).json({ error: err.message });
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if (err instanceof OAuthError) {
|
|
504
|
+
res.status(err.statusCode).json({ error: err.oauthError, error_description: err.message });
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
235
507
|
let message = err + "";
|
|
236
508
|
console.error(err);
|
|
237
509
|
if (err?.krl_compiler?.loc?.start) {
|
|
@@ -259,3 +531,407 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
|
|
|
259
531
|
|
|
260
532
|
return app;
|
|
261
533
|
}
|
|
534
|
+
|
|
535
|
+
function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
536
|
+
function setSessionCookie(res: Response, token: string, expiry: number) {
|
|
537
|
+
res.cookie(SESSION_COOKIE, token, {
|
|
538
|
+
httpOnly: true,
|
|
539
|
+
sameSite: "lax",
|
|
540
|
+
secure: auth.isSecure(),
|
|
541
|
+
path: "/",
|
|
542
|
+
expires: new Date(expiry),
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function setCeremonyCookie(res: Response, ceremonyId: string) {
|
|
547
|
+
res.cookie(CEREMONY_COOKIE, ceremonyId, {
|
|
548
|
+
httpOnly: true,
|
|
549
|
+
sameSite: "lax",
|
|
550
|
+
secure: auth.isSecure(),
|
|
551
|
+
path: "/",
|
|
552
|
+
maxAge: 5 * 60 * 1000,
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function clearCeremonyCookie(res: Response) {
|
|
557
|
+
res.clearCookie(CEREMONY_COOKIE, { path: "/" });
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// WebAuthn requires a specific, credentialed origin — never "*".
|
|
561
|
+
app.use("/auth", function (req, res, next) {
|
|
562
|
+
res.header("Access-Control-Allow-Origin", auth.rp().origin);
|
|
563
|
+
res.header("Access-Control-Allow-Credentials", "true");
|
|
564
|
+
res.header(
|
|
565
|
+
"Access-Control-Allow-Headers",
|
|
566
|
+
"Origin, X-Requested-With, Content-Type, Accept"
|
|
567
|
+
);
|
|
568
|
+
res.header(
|
|
569
|
+
"Access-Control-Allow-Methods",
|
|
570
|
+
"POST, GET, DELETE, OPTIONS"
|
|
571
|
+
);
|
|
572
|
+
res.header("Vary", "Origin");
|
|
573
|
+
if (req.method === "OPTIONS") {
|
|
574
|
+
res.status(204).end();
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
next();
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
function requireSession(req: Request, res: Response, next: express.NextFunction) {
|
|
581
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
582
|
+
auth
|
|
583
|
+
.getSession(token)
|
|
584
|
+
.then((session) => {
|
|
585
|
+
if (!session) {
|
|
586
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
(req as any).picoSession = session;
|
|
590
|
+
next();
|
|
591
|
+
})
|
|
592
|
+
.catch(next);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
app.post("/auth/register/options", function (req, res, next) {
|
|
596
|
+
auth
|
|
597
|
+
.registerNewAccountOptions({
|
|
598
|
+
displayName: req.body && req.body.displayName,
|
|
599
|
+
invite: req.body && req.body.invite,
|
|
600
|
+
})
|
|
601
|
+
.then(({ options, ceremonyId }) => {
|
|
602
|
+
setCeremonyCookie(res, ceremonyId);
|
|
603
|
+
res.json(options);
|
|
604
|
+
})
|
|
605
|
+
.catch(next);
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
app.post("/auth/register/verify", function (req, res, next) {
|
|
609
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
610
|
+
auth
|
|
611
|
+
.registerNewAccountVerify({ ceremonyId, response: req.body })
|
|
612
|
+
.then(({ account, session, uiECI }) => {
|
|
613
|
+
clearCeremonyCookie(res);
|
|
614
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
615
|
+
res.json({
|
|
616
|
+
verified: true,
|
|
617
|
+
accountId: account.accountId,
|
|
618
|
+
rootPicoId: account.rootPicoId,
|
|
619
|
+
uiECI,
|
|
620
|
+
});
|
|
621
|
+
})
|
|
622
|
+
.catch(next);
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
app.post("/auth/claim/options", function (req, res, next) {
|
|
626
|
+
auth
|
|
627
|
+
.claimPrimaryRootOptions({ displayName: req.body && req.body.displayName })
|
|
628
|
+
.then(({ options, ceremonyId }) => {
|
|
629
|
+
setCeremonyCookie(res, ceremonyId);
|
|
630
|
+
res.json(options);
|
|
631
|
+
})
|
|
632
|
+
.catch(next);
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
app.post("/auth/claim/verify", function (req, res, next) {
|
|
636
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
637
|
+
auth
|
|
638
|
+
.claimPrimaryRootVerify({ ceremonyId, response: req.body })
|
|
639
|
+
.then(({ account, session, uiECI }) => {
|
|
640
|
+
clearCeremonyCookie(res);
|
|
641
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
642
|
+
res.json({
|
|
643
|
+
verified: true,
|
|
644
|
+
accountId: account.accountId,
|
|
645
|
+
rootPicoId: account.rootPicoId,
|
|
646
|
+
uiECI,
|
|
647
|
+
});
|
|
648
|
+
})
|
|
649
|
+
.catch(next);
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
app.post("/auth/login/options", function (req, res, next) {
|
|
653
|
+
auth
|
|
654
|
+
.loginOptions()
|
|
655
|
+
.then(({ options, ceremonyId }) => {
|
|
656
|
+
setCeremonyCookie(res, ceremonyId);
|
|
657
|
+
res.json(options);
|
|
658
|
+
})
|
|
659
|
+
.catch(next);
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
app.post("/auth/login/verify", function (req, res, next) {
|
|
663
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
664
|
+
auth
|
|
665
|
+
.loginVerify({ ceremonyId, response: req.body })
|
|
666
|
+
.then(({ account, session }) => {
|
|
667
|
+
clearCeremonyCookie(res);
|
|
668
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
669
|
+
res.json({
|
|
670
|
+
verified: true,
|
|
671
|
+
accountId: account.accountId,
|
|
672
|
+
rootPicoId: account.rootPicoId,
|
|
673
|
+
});
|
|
674
|
+
})
|
|
675
|
+
.catch(next);
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
app.post("/auth/logout", function (req, res, next) {
|
|
679
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
680
|
+
auth
|
|
681
|
+
.deleteSession(token)
|
|
682
|
+
.then(() => {
|
|
683
|
+
res.clearCookie(SESSION_COOKIE, { path: "/" });
|
|
684
|
+
res.json({ ok: true });
|
|
685
|
+
})
|
|
686
|
+
.catch(next);
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
app.get("/auth/session", function (req, res, next) {
|
|
690
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
691
|
+
auth
|
|
692
|
+
.whoami(token)
|
|
693
|
+
.then((who) => res.json(who))
|
|
694
|
+
.catch(next);
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
app.post("/auth/credentials/options", requireSession, function (req, res, next) {
|
|
698
|
+
const session = (req as any).picoSession;
|
|
699
|
+
auth
|
|
700
|
+
.addCredentialOptions({ accountId: session.accountId })
|
|
701
|
+
.then(({ options, ceremonyId }) => {
|
|
702
|
+
setCeremonyCookie(res, ceremonyId);
|
|
703
|
+
res.json(options);
|
|
704
|
+
})
|
|
705
|
+
.catch(next);
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
app.post("/auth/credentials/verify", requireSession, function (req, res, next) {
|
|
709
|
+
const session = (req as any).picoSession;
|
|
710
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
711
|
+
auth
|
|
712
|
+
.addCredentialVerify({
|
|
713
|
+
ceremonyId,
|
|
714
|
+
accountId: session.accountId,
|
|
715
|
+
response: req.body,
|
|
716
|
+
label: req.body && req.body.label,
|
|
717
|
+
})
|
|
718
|
+
.then(({ credential }) => {
|
|
719
|
+
clearCeremonyCookie(res);
|
|
720
|
+
res.json({ verified: true, credential });
|
|
721
|
+
})
|
|
722
|
+
.catch(next);
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
app.delete("/auth/credentials/:id", requireSession, function (req, res, next) {
|
|
726
|
+
const session = (req as any).picoSession;
|
|
727
|
+
auth
|
|
728
|
+
.deleteCredential(session.accountId, req.params.id)
|
|
729
|
+
.then(() => res.json({ ok: true }))
|
|
730
|
+
.catch(next);
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
app.get("/auth/invites/:token", function (req, res, next) {
|
|
734
|
+
auth
|
|
735
|
+
.peekInvite(req.params.token)
|
|
736
|
+
.then((peek) => res.json(peek))
|
|
737
|
+
.catch(next);
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
app.post("/auth/invites", requireSession, function (req, res, next) {
|
|
741
|
+
const session = (req as any).picoSession;
|
|
742
|
+
auth
|
|
743
|
+
.createInvite({
|
|
744
|
+
createdByAccountId: session.accountId,
|
|
745
|
+
label: req.body && req.body.label,
|
|
746
|
+
bootstrapUrl: req.body && req.body.bootstrapUrl,
|
|
747
|
+
})
|
|
748
|
+
.then((invite) => res.json(invite))
|
|
749
|
+
.catch(next);
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
function registerOAuthRoutes(
|
|
754
|
+
app: Express,
|
|
755
|
+
oauth: OAuthService,
|
|
756
|
+
auth: AuthService | undefined,
|
|
757
|
+
requireAuthSession: (
|
|
758
|
+
req: Request,
|
|
759
|
+
res: Response,
|
|
760
|
+
next: express.NextFunction
|
|
761
|
+
) => void,
|
|
762
|
+
parseCookiesFn: (req: Request) => { [name: string]: string }
|
|
763
|
+
) {
|
|
764
|
+
function requireOAuthSession(
|
|
765
|
+
req: Request,
|
|
766
|
+
res: Response,
|
|
767
|
+
next: express.NextFunction
|
|
768
|
+
) {
|
|
769
|
+
if (!auth) {
|
|
770
|
+
res.status(503).json({ error: "Authentication is not configured" });
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
774
|
+
auth
|
|
775
|
+
.getSession(token)
|
|
776
|
+
.then((session) => {
|
|
777
|
+
if (!session) {
|
|
778
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
(req as any).picoSession = session;
|
|
782
|
+
next();
|
|
783
|
+
})
|
|
784
|
+
.catch(next);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
app.post("/oauth/token", function (req, res, next) {
|
|
788
|
+
// Prefer body, but accept query params too (manual/browser testing).
|
|
789
|
+
const body = _.assign({}, req.query, req.body);
|
|
790
|
+
oauth
|
|
791
|
+
.tokenGrant(body)
|
|
792
|
+
.then((token) => res.json(token))
|
|
793
|
+
.catch(next);
|
|
794
|
+
});
|
|
795
|
+
|
|
796
|
+
// Browser-friendly alias for manual token exchange during development.
|
|
797
|
+
app.get("/oauth/token", function (req, res, next) {
|
|
798
|
+
oauth
|
|
799
|
+
.tokenGrant(req.query as Record<string, unknown>)
|
|
800
|
+
.then((token) => res.json(token))
|
|
801
|
+
.catch(next);
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
app.get("/oauth/authorize", function (req, res, next) {
|
|
805
|
+
let params;
|
|
806
|
+
try {
|
|
807
|
+
params = parseAuthorizeQuery(req.query as Record<string, unknown>);
|
|
808
|
+
} catch (err) {
|
|
809
|
+
next(err);
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
oauth
|
|
813
|
+
.getAppForAuthorize(params)
|
|
814
|
+
.then(async (appRecord) => {
|
|
815
|
+
if (!auth) {
|
|
816
|
+
res.status(503).send("Authentication is not configured");
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
820
|
+
const session = await auth.getSession(token);
|
|
821
|
+
if (!session) {
|
|
822
|
+
const returnPath = req.originalUrl || "/oauth/authorize";
|
|
823
|
+
res.redirect(
|
|
824
|
+
"/?oauth_return=" + encodeURIComponent(returnPath)
|
|
825
|
+
);
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
if (session.rootPicoId !== appRecord.rootPicoId) {
|
|
829
|
+
res.status(403).send("Signed in to a different mesh");
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
res.type("html").send(renderConsentHtml(appRecord, params));
|
|
833
|
+
})
|
|
834
|
+
.catch(next);
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
app.post("/oauth/approve", function (req, res, next) {
|
|
838
|
+
if (!auth) {
|
|
839
|
+
res.status(503).send("Authentication is not configured");
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
843
|
+
auth
|
|
844
|
+
.getSession(token)
|
|
845
|
+
.then((session) => {
|
|
846
|
+
if (!session) {
|
|
847
|
+
res.status(401).send("Not authenticated");
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
const params = parseApproveBody(req.body || {});
|
|
851
|
+
return oauth
|
|
852
|
+
.approveAuthorization(session, params)
|
|
853
|
+
.then((redirectUrl) => {
|
|
854
|
+
// 303 See Other: browser must follow with GET (OAuth code in query string).
|
|
855
|
+
res.redirect(303, redirectUrl);
|
|
856
|
+
});
|
|
857
|
+
})
|
|
858
|
+
.catch((err) => {
|
|
859
|
+
if (err instanceof OAuthError) {
|
|
860
|
+
const redirect_uri = String(req.body?.redirect_uri || "").trim();
|
|
861
|
+
if (redirect_uri) {
|
|
862
|
+
try {
|
|
863
|
+
const url = new URL(redirect_uri);
|
|
864
|
+
url.searchParams.set("error", err.oauthError);
|
|
865
|
+
url.searchParams.set("error_description", err.message);
|
|
866
|
+
const state = req.body?.state;
|
|
867
|
+
if (state !== undefined && state !== "") {
|
|
868
|
+
url.searchParams.set("state", String(state));
|
|
869
|
+
}
|
|
870
|
+
res.redirect(303, url.toString());
|
|
871
|
+
return;
|
|
872
|
+
} catch (_e) {
|
|
873
|
+
// fall through to default error handler
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
next(err);
|
|
878
|
+
});
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
app.get("/oauth/apps", requireOAuthSession, function (req, res, next) {
|
|
882
|
+
const session = (req as any).picoSession;
|
|
883
|
+
oauth
|
|
884
|
+
.listApps(session.rootPicoId)
|
|
885
|
+
.then((apps) => res.json({ apps }))
|
|
886
|
+
.catch(next);
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
app.post("/oauth/apps", requireOAuthSession, function (req, res, next) {
|
|
890
|
+
const session = (req as any).picoSession;
|
|
891
|
+
const body = req.body || {};
|
|
892
|
+
oauth
|
|
893
|
+
.registerApp(session.rootPicoId, {
|
|
894
|
+
name: body.name,
|
|
895
|
+
redirect_uris: body.redirect_uris,
|
|
896
|
+
public_client: body.public_client,
|
|
897
|
+
})
|
|
898
|
+
.then((app) => res.json(app))
|
|
899
|
+
.catch(next);
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
app.delete("/oauth/apps/:clientId", requireOAuthSession, function (req, res, next) {
|
|
903
|
+
const session = (req as any).picoSession;
|
|
904
|
+
oauth
|
|
905
|
+
.revokeApp(session.rootPicoId, req.params.clientId)
|
|
906
|
+
.then(() => res.json({ ok: true }))
|
|
907
|
+
.catch(next);
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
app.get("/oauth/channels/:eci/status", requireAuthSession, function (req, res, next) {
|
|
911
|
+
oauth
|
|
912
|
+
.channelStatusAsync(req.params.eci)
|
|
913
|
+
.then((status) => res.json(status))
|
|
914
|
+
.catch(next);
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
app.post("/oauth/channels/:eci/credentials", requireAuthSession, function (req, res, next) {
|
|
918
|
+
oauth
|
|
919
|
+
.createChannelSecret(req.params.eci)
|
|
920
|
+
.then((creds) => res.json(creds))
|
|
921
|
+
.catch(next);
|
|
922
|
+
});
|
|
923
|
+
|
|
924
|
+
app.delete("/oauth/channels/:eci/credentials", requireAuthSession, function (req, res, next) {
|
|
925
|
+
oauth
|
|
926
|
+
.revokeChannelSecret(req.params.eci)
|
|
927
|
+
.then(() => res.json({ ok: true }))
|
|
928
|
+
.catch(next);
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
app.delete("/oauth/channels/:eci/tokens", requireAuthSession, function (req, res, next) {
|
|
932
|
+
oauth
|
|
933
|
+
.revokeTokens(req.params.eci)
|
|
934
|
+
.then(() => res.json({ ok: true }))
|
|
935
|
+
.catch(next);
|
|
936
|
+
});
|
|
937
|
+
}
|