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/dist/server.js
CHANGED
|
@@ -6,19 +6,74 @@ const express = require("express");
|
|
|
6
6
|
const helmet_1 = require("helmet");
|
|
7
7
|
const _ = require("lodash");
|
|
8
8
|
const path = require("path");
|
|
9
|
+
const auth_1 = require("./auth");
|
|
10
|
+
const oauth_1 = require("./oauth");
|
|
11
|
+
const provisionRoot_1 = require("./provisionRoot");
|
|
12
|
+
const webvhRoutes_1 = require("./identity/webvhRoutes");
|
|
13
|
+
const didcommIngressRoutes_1 = require("./identity/didcommIngressRoutes");
|
|
9
14
|
const engineVersion = require("../package.json").version;
|
|
15
|
+
const SESSION_COOKIE = "pico-session";
|
|
16
|
+
const CEREMONY_COOKIE = "pico-ceremony";
|
|
17
|
+
function localhostChannelBypassEnabled() {
|
|
18
|
+
return process.env.PICO_ENGINE_ALLOW_LOCALHOST_C !== "0";
|
|
19
|
+
}
|
|
10
20
|
function mergeGetPost(req) {
|
|
11
21
|
// give preference to post body params
|
|
12
22
|
return _.assign({}, req.query, req.body, { _headers: req.headers });
|
|
13
23
|
}
|
|
14
|
-
function
|
|
24
|
+
function parseCookies(req) {
|
|
25
|
+
const header = req.headers.cookie;
|
|
26
|
+
const out = {};
|
|
27
|
+
if (typeof header !== "string") {
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
for (const part of header.split(";")) {
|
|
31
|
+
const idx = part.indexOf("=");
|
|
32
|
+
if (idx < 0) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const name = part.slice(0, idx).trim();
|
|
36
|
+
const value = part.slice(idx + 1).trim();
|
|
37
|
+
if (name) {
|
|
38
|
+
out[name] = decodeURIComponent(value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
function isLocalhostAddress(addr) {
|
|
44
|
+
if (!addr) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const normalized = addr.replace(/^::ffff:/, "");
|
|
48
|
+
return normalized === "127.0.0.1" || normalized === "::1";
|
|
49
|
+
}
|
|
50
|
+
function isLocalhostRequest(req) {
|
|
51
|
+
var _a;
|
|
52
|
+
if (isLocalhostAddress(req.ip)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
if (isLocalhostAddress((_a = req.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress)) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
const host = req.headers.host || "";
|
|
59
|
+
return host.startsWith("localhost:") || host.startsWith("127.0.0.1:");
|
|
60
|
+
}
|
|
61
|
+
function server(core, uiECI, auth, oauth, identity) {
|
|
62
|
+
const pf = core.picoFramework;
|
|
15
63
|
const app = express();
|
|
16
64
|
app.use(function (req, res, next) {
|
|
65
|
+
// /auth routes need a specific credentialed CORS origin (below), not "*".
|
|
66
|
+
if (req.path.indexOf("/auth") === 0) {
|
|
67
|
+
return next();
|
|
68
|
+
}
|
|
17
69
|
res.header("Access-Control-Allow-Origin", "*");
|
|
18
|
-
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
70
|
+
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
|
|
19
71
|
next();
|
|
20
72
|
});
|
|
21
73
|
app.use('*', (req, res, next) => {
|
|
74
|
+
if (req.path.indexOf("/auth") === 0) {
|
|
75
|
+
return next();
|
|
76
|
+
}
|
|
22
77
|
if (req.method == "OPTIONS") {
|
|
23
78
|
res.header("Allow", "POST, GET, OPTIONS");
|
|
24
79
|
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
|
|
@@ -31,17 +86,29 @@ function server(core, uiECI) {
|
|
|
31
86
|
});
|
|
32
87
|
// Helmet 7+ CSP defaults include upgrade-insecure-requests, which breaks the
|
|
33
88
|
// UI on plain HTTP (browser upgrades /api/* to https and TLS fails).
|
|
34
|
-
|
|
89
|
+
// OAuth consent pages must not use form-action 'self' — after Allow, the
|
|
90
|
+
// browser follows a redirect to the app's redirect_uri (external origin).
|
|
91
|
+
const defaultHelmet = (0, helmet_1.default)({
|
|
35
92
|
contentSecurityPolicy: {
|
|
36
93
|
directives: Object.assign(Object.assign({}, helmet_1.default.contentSecurityPolicy.getDefaultDirectives()), { "upgrade-insecure-requests": null }),
|
|
37
94
|
},
|
|
38
|
-
})
|
|
95
|
+
});
|
|
96
|
+
app.use(function (req, res, next) {
|
|
97
|
+
if (req.path === "/oauth/authorize" || req.path === "/oauth/approve") {
|
|
98
|
+
return (0, helmet_1.default)({ contentSecurityPolicy: false })(req, res, next);
|
|
99
|
+
}
|
|
100
|
+
return defaultHelmet(req, res, next);
|
|
101
|
+
});
|
|
39
102
|
app.use(express.static(path.resolve(__dirname, "..", "public")));
|
|
103
|
+
if (identity) {
|
|
104
|
+
(0, webvhRoutes_1.registerWebvhRoutes)(app, identity);
|
|
105
|
+
}
|
|
40
106
|
app.use(bodyParser.json({
|
|
41
107
|
type: [
|
|
42
108
|
"application/json",
|
|
43
109
|
"application/octet-stream",
|
|
44
110
|
"application/ssi-agent-wire",
|
|
111
|
+
"application/didcomm-encrypted+json",
|
|
45
112
|
],
|
|
46
113
|
}));
|
|
47
114
|
app.use(bodyParser.urlencoded({
|
|
@@ -49,10 +116,171 @@ function server(core, uiECI) {
|
|
|
49
116
|
type: "application/x-www-form-urlencoded",
|
|
50
117
|
extended: false,
|
|
51
118
|
}));
|
|
119
|
+
if (identity) {
|
|
120
|
+
(0, didcommIngressRoutes_1.registerDidcommIngressRoutes)(app, identity);
|
|
121
|
+
}
|
|
122
|
+
if (auth) {
|
|
123
|
+
registerAuthRoutes(app, auth);
|
|
124
|
+
}
|
|
125
|
+
function isEciInRootTree(rootPicoId, eci) {
|
|
126
|
+
try {
|
|
127
|
+
let pico = pf.getPico(eci);
|
|
128
|
+
while (pico) {
|
|
129
|
+
if (pico.id === rootPicoId) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
if (!pico.parent) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
pico = pf.getPico(pico.parent);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (_e) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
function requireAuthSession(req, res, next) {
|
|
144
|
+
if (!auth) {
|
|
145
|
+
next();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
149
|
+
auth
|
|
150
|
+
.getSession(token)
|
|
151
|
+
.then((session) => {
|
|
152
|
+
if (!session) {
|
|
153
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const eci = req.params.eci;
|
|
157
|
+
if (eci && !isEciInRootTree(session.rootPicoId, eci)) {
|
|
158
|
+
res.status(403).json({ error: "Not authorized for this pico" });
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
req.picoSession = session;
|
|
162
|
+
next();
|
|
163
|
+
})
|
|
164
|
+
.catch(next);
|
|
165
|
+
}
|
|
166
|
+
/** `/c/*` is internal: passkey session, or localhost for in-engine HTTP loops. */
|
|
167
|
+
function requireChannelSession(req, res, next) {
|
|
168
|
+
if (!auth) {
|
|
169
|
+
next();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (localhostChannelBypassEnabled() && isLocalhostRequest(req)) {
|
|
173
|
+
next();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
requireAuthSession(req, res, next);
|
|
177
|
+
}
|
|
178
|
+
/** External Sky API: Bearer when channel is oauth-webhook or mesh has OAuth ruleset. */
|
|
179
|
+
function requireSkyBearerIfOAuth(req, res, next) {
|
|
180
|
+
if (!oauth) {
|
|
181
|
+
next();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const eci = req.params.eci;
|
|
185
|
+
if (!eci) {
|
|
186
|
+
next();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
oauth
|
|
190
|
+
.skyRequiresBearer(eci)
|
|
191
|
+
.then((required) => {
|
|
192
|
+
if (!required) {
|
|
193
|
+
next();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const header = req.headers.authorization;
|
|
197
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) {
|
|
198
|
+
res.status(401).json({ error: "Bearer token required" });
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const token = header.slice("Bearer ".length).trim();
|
|
202
|
+
return oauth.validateSkyBearerToken(token, eci).then((valid) => {
|
|
203
|
+
if (!valid) {
|
|
204
|
+
res.status(401).json({ error: "Invalid or expired token" });
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
next();
|
|
208
|
+
});
|
|
209
|
+
})
|
|
210
|
+
.catch(next);
|
|
211
|
+
}
|
|
212
|
+
if (oauth) {
|
|
213
|
+
registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
|
|
214
|
+
}
|
|
215
|
+
app.get("/api/mesh-context", function (req, res, next) {
|
|
216
|
+
if (!oauth) {
|
|
217
|
+
res.status(503).json({ error: "OAuth not enabled" });
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const header = req.headers.authorization;
|
|
221
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) {
|
|
222
|
+
res.status(401).json({ error: "Bearer token required" });
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const token = header.slice("Bearer ".length).trim();
|
|
226
|
+
oauth
|
|
227
|
+
.resolveMeshBearerToken(token)
|
|
228
|
+
.then((ctx) => {
|
|
229
|
+
if (!ctx) {
|
|
230
|
+
res.status(401).json({ error: "Invalid or expired token" });
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const rootUiEci = (0, provisionRoot_1.uiECIForRoot)(pf, ctx.rootPicoId);
|
|
234
|
+
if (!rootUiEci) {
|
|
235
|
+
res.status(503).json({ error: "Mesh UI channel not found" });
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
let baseUrl = core.base_url;
|
|
239
|
+
if (!baseUrl) {
|
|
240
|
+
baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
|
|
241
|
+
}
|
|
242
|
+
baseUrl = baseUrl.replace(/\/$/, "");
|
|
243
|
+
res.json({
|
|
244
|
+
baseUrl,
|
|
245
|
+
rootUiEci,
|
|
246
|
+
oauth: {
|
|
247
|
+
authorize: `${baseUrl}/oauth/authorize`,
|
|
248
|
+
token: `${baseUrl}/oauth/token`,
|
|
249
|
+
},
|
|
250
|
+
scope: ctx.scope,
|
|
251
|
+
});
|
|
252
|
+
})
|
|
253
|
+
.catch(next);
|
|
254
|
+
});
|
|
52
255
|
app.get("/api/ui-context", function (req, res, next) {
|
|
53
|
-
|
|
256
|
+
const hasRoots = pf.rootPicos().length > 0;
|
|
257
|
+
const base = {
|
|
258
|
+
version: engineVersion,
|
|
259
|
+
hasRoots,
|
|
260
|
+
allowSelfSignup: auth ? auth.allowSelfSignup() : false,
|
|
261
|
+
};
|
|
262
|
+
if (!auth) {
|
|
263
|
+
base.eci = uiECI;
|
|
264
|
+
res.json(base);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
268
|
+
auth
|
|
269
|
+
.needsAuthMigration()
|
|
270
|
+
.then((needsAuthMigration) => {
|
|
271
|
+
base.needsAuthMigration = needsAuthMigration;
|
|
272
|
+
return auth.whoami(token);
|
|
273
|
+
})
|
|
274
|
+
.then((who) => {
|
|
275
|
+
base.session = who;
|
|
276
|
+
if (who.authenticated && who.uiECI) {
|
|
277
|
+
base.eci = who.uiECI;
|
|
278
|
+
}
|
|
279
|
+
res.json(base);
|
|
280
|
+
})
|
|
281
|
+
.catch(next);
|
|
54
282
|
});
|
|
55
|
-
app.all("/api/flush", function (req, res, next) {
|
|
283
|
+
app.all("/api/flush", requireAuthSession, function (req, res, next) {
|
|
56
284
|
const attrs = mergeGetPost(req);
|
|
57
285
|
const url = attrs["url"];
|
|
58
286
|
if (typeof url !== "string") {
|
|
@@ -73,7 +301,7 @@ function server(core, uiECI) {
|
|
|
73
301
|
.catch(next);
|
|
74
302
|
}
|
|
75
303
|
});
|
|
76
|
-
app.all("/c/:eci/event/:domain/:name", function (req, res, next) {
|
|
304
|
+
app.all("/c/:eci/event/:domain/:name", requireChannelSession, function (req, res, next) {
|
|
77
305
|
core
|
|
78
306
|
.event({
|
|
79
307
|
eci: req.params.eci,
|
|
@@ -87,7 +315,7 @@ function server(core, uiECI) {
|
|
|
87
315
|
})
|
|
88
316
|
.catch(next);
|
|
89
317
|
});
|
|
90
|
-
app.all("/c/:eci/event-wait/:domain/:name", function (req, res, next) {
|
|
318
|
+
app.all("/c/:eci/event-wait/:domain/:name", requireChannelSession, function (req, res, next) {
|
|
91
319
|
core
|
|
92
320
|
.eventWait({
|
|
93
321
|
eci: req.params.eci,
|
|
@@ -101,7 +329,7 @@ function server(core, uiECI) {
|
|
|
101
329
|
})
|
|
102
330
|
.catch(next);
|
|
103
331
|
});
|
|
104
|
-
app.all("/sky/event/:eci/:eid/:domain/:type", function (req, res, next) {
|
|
332
|
+
app.all("/sky/event/:eci/:eid/:domain/:type", requireSkyBearerIfOAuth, function (req, res, next) {
|
|
105
333
|
const attrs = mergeGetPost(req);
|
|
106
334
|
if (req.params.eid !== "none" && !attrs.hasOwnProperty("__eid")) {
|
|
107
335
|
attrs.__eid = req.params.eid;
|
|
@@ -137,7 +365,7 @@ function server(core, uiECI) {
|
|
|
137
365
|
})
|
|
138
366
|
.catch(next);
|
|
139
367
|
});
|
|
140
|
-
app.all("/c/:eci/event/:domain/:name/query/:rid/:qname", function (req, res, next) {
|
|
368
|
+
app.all("/c/:eci/event/:domain/:name/query/:rid/:qname", requireChannelSession, function (req, res, next) {
|
|
141
369
|
const attrs = mergeGetPost(req);
|
|
142
370
|
core
|
|
143
371
|
.eventQuery({
|
|
@@ -157,8 +385,8 @@ function server(core, uiECI) {
|
|
|
157
385
|
})
|
|
158
386
|
.catch(next);
|
|
159
387
|
});
|
|
160
|
-
|
|
161
|
-
const funcPart = req.params.
|
|
388
|
+
function handleSkyQuery(req, res, next) {
|
|
389
|
+
const funcPart = req.params["function"].split(".");
|
|
162
390
|
core
|
|
163
391
|
.query({
|
|
164
392
|
eci: req.params.eci,
|
|
@@ -180,9 +408,9 @@ function server(core, uiECI) {
|
|
|
180
408
|
}
|
|
181
409
|
})
|
|
182
410
|
.catch(next);
|
|
183
|
-
}
|
|
184
|
-
app.all("/
|
|
185
|
-
const funcPart = req.params
|
|
411
|
+
}
|
|
412
|
+
app.all("/c/:eci/query/:rid/:name", requireChannelSession, function (req, res, next) {
|
|
413
|
+
const funcPart = req.params.name.split(".");
|
|
186
414
|
core
|
|
187
415
|
.query({
|
|
188
416
|
eci: req.params.eci,
|
|
@@ -205,8 +433,19 @@ function server(core, uiECI) {
|
|
|
205
433
|
})
|
|
206
434
|
.catch(next);
|
|
207
435
|
});
|
|
436
|
+
app.all("/sky/query/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
|
|
437
|
+
// Legacy alias; prefer /sky/query/ per Sky Query API docs.
|
|
438
|
+
app.all("/sky/cloud/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
|
|
208
439
|
app.use(function (err, req, res, next) {
|
|
209
440
|
var _a, _b, _c;
|
|
441
|
+
if (err instanceof auth_1.AuthError) {
|
|
442
|
+
res.status(err.statusCode).json({ error: err.message });
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (err instanceof oauth_1.OAuthError) {
|
|
446
|
+
res.status(err.statusCode).json({ error: err.oauthError, error_description: err.message });
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
210
449
|
let message = err + "";
|
|
211
450
|
console.error(err);
|
|
212
451
|
if ((_b = (_a = err === null || err === void 0 ? void 0 : err.krl_compiler) === null || _a === void 0 ? void 0 : _a.loc) === null || _b === void 0 ? void 0 : _b.start) {
|
|
@@ -232,3 +471,358 @@ function server(core, uiECI) {
|
|
|
232
471
|
});
|
|
233
472
|
return app;
|
|
234
473
|
}
|
|
474
|
+
function registerAuthRoutes(app, auth) {
|
|
475
|
+
function setSessionCookie(res, token, expiry) {
|
|
476
|
+
res.cookie(SESSION_COOKIE, token, {
|
|
477
|
+
httpOnly: true,
|
|
478
|
+
sameSite: "lax",
|
|
479
|
+
secure: auth.isSecure(),
|
|
480
|
+
path: "/",
|
|
481
|
+
expires: new Date(expiry),
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
function setCeremonyCookie(res, ceremonyId) {
|
|
485
|
+
res.cookie(CEREMONY_COOKIE, ceremonyId, {
|
|
486
|
+
httpOnly: true,
|
|
487
|
+
sameSite: "lax",
|
|
488
|
+
secure: auth.isSecure(),
|
|
489
|
+
path: "/",
|
|
490
|
+
maxAge: 5 * 60 * 1000,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
function clearCeremonyCookie(res) {
|
|
494
|
+
res.clearCookie(CEREMONY_COOKIE, { path: "/" });
|
|
495
|
+
}
|
|
496
|
+
// WebAuthn requires a specific, credentialed origin — never "*".
|
|
497
|
+
app.use("/auth", function (req, res, next) {
|
|
498
|
+
res.header("Access-Control-Allow-Origin", auth.rp().origin);
|
|
499
|
+
res.header("Access-Control-Allow-Credentials", "true");
|
|
500
|
+
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
501
|
+
res.header("Access-Control-Allow-Methods", "POST, GET, DELETE, OPTIONS");
|
|
502
|
+
res.header("Vary", "Origin");
|
|
503
|
+
if (req.method === "OPTIONS") {
|
|
504
|
+
res.status(204).end();
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
next();
|
|
508
|
+
});
|
|
509
|
+
function requireSession(req, res, next) {
|
|
510
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
511
|
+
auth
|
|
512
|
+
.getSession(token)
|
|
513
|
+
.then((session) => {
|
|
514
|
+
if (!session) {
|
|
515
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
req.picoSession = session;
|
|
519
|
+
next();
|
|
520
|
+
})
|
|
521
|
+
.catch(next);
|
|
522
|
+
}
|
|
523
|
+
app.post("/auth/register/options", function (req, res, next) {
|
|
524
|
+
auth
|
|
525
|
+
.registerNewAccountOptions({
|
|
526
|
+
displayName: req.body && req.body.displayName,
|
|
527
|
+
invite: req.body && req.body.invite,
|
|
528
|
+
})
|
|
529
|
+
.then(({ options, ceremonyId }) => {
|
|
530
|
+
setCeremonyCookie(res, ceremonyId);
|
|
531
|
+
res.json(options);
|
|
532
|
+
})
|
|
533
|
+
.catch(next);
|
|
534
|
+
});
|
|
535
|
+
app.post("/auth/register/verify", function (req, res, next) {
|
|
536
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
537
|
+
auth
|
|
538
|
+
.registerNewAccountVerify({ ceremonyId, response: req.body })
|
|
539
|
+
.then(({ account, session, uiECI }) => {
|
|
540
|
+
clearCeremonyCookie(res);
|
|
541
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
542
|
+
res.json({
|
|
543
|
+
verified: true,
|
|
544
|
+
accountId: account.accountId,
|
|
545
|
+
rootPicoId: account.rootPicoId,
|
|
546
|
+
uiECI,
|
|
547
|
+
});
|
|
548
|
+
})
|
|
549
|
+
.catch(next);
|
|
550
|
+
});
|
|
551
|
+
app.post("/auth/claim/options", function (req, res, next) {
|
|
552
|
+
auth
|
|
553
|
+
.claimPrimaryRootOptions({ displayName: req.body && req.body.displayName })
|
|
554
|
+
.then(({ options, ceremonyId }) => {
|
|
555
|
+
setCeremonyCookie(res, ceremonyId);
|
|
556
|
+
res.json(options);
|
|
557
|
+
})
|
|
558
|
+
.catch(next);
|
|
559
|
+
});
|
|
560
|
+
app.post("/auth/claim/verify", function (req, res, next) {
|
|
561
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
562
|
+
auth
|
|
563
|
+
.claimPrimaryRootVerify({ ceremonyId, response: req.body })
|
|
564
|
+
.then(({ account, session, uiECI }) => {
|
|
565
|
+
clearCeremonyCookie(res);
|
|
566
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
567
|
+
res.json({
|
|
568
|
+
verified: true,
|
|
569
|
+
accountId: account.accountId,
|
|
570
|
+
rootPicoId: account.rootPicoId,
|
|
571
|
+
uiECI,
|
|
572
|
+
});
|
|
573
|
+
})
|
|
574
|
+
.catch(next);
|
|
575
|
+
});
|
|
576
|
+
app.post("/auth/login/options", function (req, res, next) {
|
|
577
|
+
auth
|
|
578
|
+
.loginOptions()
|
|
579
|
+
.then(({ options, ceremonyId }) => {
|
|
580
|
+
setCeremonyCookie(res, ceremonyId);
|
|
581
|
+
res.json(options);
|
|
582
|
+
})
|
|
583
|
+
.catch(next);
|
|
584
|
+
});
|
|
585
|
+
app.post("/auth/login/verify", function (req, res, next) {
|
|
586
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
587
|
+
auth
|
|
588
|
+
.loginVerify({ ceremonyId, response: req.body })
|
|
589
|
+
.then(({ account, session }) => {
|
|
590
|
+
clearCeremonyCookie(res);
|
|
591
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
592
|
+
res.json({
|
|
593
|
+
verified: true,
|
|
594
|
+
accountId: account.accountId,
|
|
595
|
+
rootPicoId: account.rootPicoId,
|
|
596
|
+
});
|
|
597
|
+
})
|
|
598
|
+
.catch(next);
|
|
599
|
+
});
|
|
600
|
+
app.post("/auth/logout", function (req, res, next) {
|
|
601
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
602
|
+
auth
|
|
603
|
+
.deleteSession(token)
|
|
604
|
+
.then(() => {
|
|
605
|
+
res.clearCookie(SESSION_COOKIE, { path: "/" });
|
|
606
|
+
res.json({ ok: true });
|
|
607
|
+
})
|
|
608
|
+
.catch(next);
|
|
609
|
+
});
|
|
610
|
+
app.get("/auth/session", function (req, res, next) {
|
|
611
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
612
|
+
auth
|
|
613
|
+
.whoami(token)
|
|
614
|
+
.then((who) => res.json(who))
|
|
615
|
+
.catch(next);
|
|
616
|
+
});
|
|
617
|
+
app.post("/auth/credentials/options", requireSession, function (req, res, next) {
|
|
618
|
+
const session = req.picoSession;
|
|
619
|
+
auth
|
|
620
|
+
.addCredentialOptions({ accountId: session.accountId })
|
|
621
|
+
.then(({ options, ceremonyId }) => {
|
|
622
|
+
setCeremonyCookie(res, ceremonyId);
|
|
623
|
+
res.json(options);
|
|
624
|
+
})
|
|
625
|
+
.catch(next);
|
|
626
|
+
});
|
|
627
|
+
app.post("/auth/credentials/verify", requireSession, function (req, res, next) {
|
|
628
|
+
const session = req.picoSession;
|
|
629
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
630
|
+
auth
|
|
631
|
+
.addCredentialVerify({
|
|
632
|
+
ceremonyId,
|
|
633
|
+
accountId: session.accountId,
|
|
634
|
+
response: req.body,
|
|
635
|
+
label: req.body && req.body.label,
|
|
636
|
+
})
|
|
637
|
+
.then(({ credential }) => {
|
|
638
|
+
clearCeremonyCookie(res);
|
|
639
|
+
res.json({ verified: true, credential });
|
|
640
|
+
})
|
|
641
|
+
.catch(next);
|
|
642
|
+
});
|
|
643
|
+
app.delete("/auth/credentials/:id", requireSession, function (req, res, next) {
|
|
644
|
+
const session = req.picoSession;
|
|
645
|
+
auth
|
|
646
|
+
.deleteCredential(session.accountId, req.params.id)
|
|
647
|
+
.then(() => res.json({ ok: true }))
|
|
648
|
+
.catch(next);
|
|
649
|
+
});
|
|
650
|
+
app.get("/auth/invites/:token", function (req, res, next) {
|
|
651
|
+
auth
|
|
652
|
+
.peekInvite(req.params.token)
|
|
653
|
+
.then((peek) => res.json(peek))
|
|
654
|
+
.catch(next);
|
|
655
|
+
});
|
|
656
|
+
app.post("/auth/invites", requireSession, function (req, res, next) {
|
|
657
|
+
const session = req.picoSession;
|
|
658
|
+
auth
|
|
659
|
+
.createInvite({
|
|
660
|
+
createdByAccountId: session.accountId,
|
|
661
|
+
label: req.body && req.body.label,
|
|
662
|
+
bootstrapUrl: req.body && req.body.bootstrapUrl,
|
|
663
|
+
})
|
|
664
|
+
.then((invite) => res.json(invite))
|
|
665
|
+
.catch(next);
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
function registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookiesFn) {
|
|
669
|
+
function requireOAuthSession(req, res, next) {
|
|
670
|
+
if (!auth) {
|
|
671
|
+
res.status(503).json({ error: "Authentication is not configured" });
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
675
|
+
auth
|
|
676
|
+
.getSession(token)
|
|
677
|
+
.then((session) => {
|
|
678
|
+
if (!session) {
|
|
679
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
req.picoSession = session;
|
|
683
|
+
next();
|
|
684
|
+
})
|
|
685
|
+
.catch(next);
|
|
686
|
+
}
|
|
687
|
+
app.post("/oauth/token", function (req, res, next) {
|
|
688
|
+
// Prefer body, but accept query params too (manual/browser testing).
|
|
689
|
+
const body = _.assign({}, req.query, req.body);
|
|
690
|
+
oauth
|
|
691
|
+
.tokenGrant(body)
|
|
692
|
+
.then((token) => res.json(token))
|
|
693
|
+
.catch(next);
|
|
694
|
+
});
|
|
695
|
+
// Browser-friendly alias for manual token exchange during development.
|
|
696
|
+
app.get("/oauth/token", function (req, res, next) {
|
|
697
|
+
oauth
|
|
698
|
+
.tokenGrant(req.query)
|
|
699
|
+
.then((token) => res.json(token))
|
|
700
|
+
.catch(next);
|
|
701
|
+
});
|
|
702
|
+
app.get("/oauth/authorize", function (req, res, next) {
|
|
703
|
+
let params;
|
|
704
|
+
try {
|
|
705
|
+
params = (0, oauth_1.parseAuthorizeQuery)(req.query);
|
|
706
|
+
}
|
|
707
|
+
catch (err) {
|
|
708
|
+
next(err);
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
oauth
|
|
712
|
+
.getAppForAuthorize(params)
|
|
713
|
+
.then(async (appRecord) => {
|
|
714
|
+
if (!auth) {
|
|
715
|
+
res.status(503).send("Authentication is not configured");
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
719
|
+
const session = await auth.getSession(token);
|
|
720
|
+
if (!session) {
|
|
721
|
+
const returnPath = req.originalUrl || "/oauth/authorize";
|
|
722
|
+
res.redirect("/?oauth_return=" + encodeURIComponent(returnPath));
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
if (session.rootPicoId !== appRecord.rootPicoId) {
|
|
726
|
+
res.status(403).send("Signed in to a different mesh");
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
res.type("html").send((0, oauth_1.renderConsentHtml)(appRecord, params));
|
|
730
|
+
})
|
|
731
|
+
.catch(next);
|
|
732
|
+
});
|
|
733
|
+
app.post("/oauth/approve", function (req, res, next) {
|
|
734
|
+
if (!auth) {
|
|
735
|
+
res.status(503).send("Authentication is not configured");
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
739
|
+
auth
|
|
740
|
+
.getSession(token)
|
|
741
|
+
.then((session) => {
|
|
742
|
+
if (!session) {
|
|
743
|
+
res.status(401).send("Not authenticated");
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const params = (0, oauth_1.parseApproveBody)(req.body || {});
|
|
747
|
+
return oauth
|
|
748
|
+
.approveAuthorization(session, params)
|
|
749
|
+
.then((redirectUrl) => {
|
|
750
|
+
// 303 See Other: browser must follow with GET (OAuth code in query string).
|
|
751
|
+
res.redirect(303, redirectUrl);
|
|
752
|
+
});
|
|
753
|
+
})
|
|
754
|
+
.catch((err) => {
|
|
755
|
+
var _a, _b;
|
|
756
|
+
if (err instanceof oauth_1.OAuthError) {
|
|
757
|
+
const redirect_uri = String(((_a = req.body) === null || _a === void 0 ? void 0 : _a.redirect_uri) || "").trim();
|
|
758
|
+
if (redirect_uri) {
|
|
759
|
+
try {
|
|
760
|
+
const url = new URL(redirect_uri);
|
|
761
|
+
url.searchParams.set("error", err.oauthError);
|
|
762
|
+
url.searchParams.set("error_description", err.message);
|
|
763
|
+
const state = (_b = req.body) === null || _b === void 0 ? void 0 : _b.state;
|
|
764
|
+
if (state !== undefined && state !== "") {
|
|
765
|
+
url.searchParams.set("state", String(state));
|
|
766
|
+
}
|
|
767
|
+
res.redirect(303, url.toString());
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
catch (_e) {
|
|
771
|
+
// fall through to default error handler
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
next(err);
|
|
776
|
+
});
|
|
777
|
+
});
|
|
778
|
+
app.get("/oauth/apps", requireOAuthSession, function (req, res, next) {
|
|
779
|
+
const session = req.picoSession;
|
|
780
|
+
oauth
|
|
781
|
+
.listApps(session.rootPicoId)
|
|
782
|
+
.then((apps) => res.json({ apps }))
|
|
783
|
+
.catch(next);
|
|
784
|
+
});
|
|
785
|
+
app.post("/oauth/apps", requireOAuthSession, function (req, res, next) {
|
|
786
|
+
const session = req.picoSession;
|
|
787
|
+
const body = req.body || {};
|
|
788
|
+
oauth
|
|
789
|
+
.registerApp(session.rootPicoId, {
|
|
790
|
+
name: body.name,
|
|
791
|
+
redirect_uris: body.redirect_uris,
|
|
792
|
+
public_client: body.public_client,
|
|
793
|
+
})
|
|
794
|
+
.then((app) => res.json(app))
|
|
795
|
+
.catch(next);
|
|
796
|
+
});
|
|
797
|
+
app.delete("/oauth/apps/:clientId", requireOAuthSession, function (req, res, next) {
|
|
798
|
+
const session = req.picoSession;
|
|
799
|
+
oauth
|
|
800
|
+
.revokeApp(session.rootPicoId, req.params.clientId)
|
|
801
|
+
.then(() => res.json({ ok: true }))
|
|
802
|
+
.catch(next);
|
|
803
|
+
});
|
|
804
|
+
app.get("/oauth/channels/:eci/status", requireAuthSession, function (req, res, next) {
|
|
805
|
+
oauth
|
|
806
|
+
.channelStatusAsync(req.params.eci)
|
|
807
|
+
.then((status) => res.json(status))
|
|
808
|
+
.catch(next);
|
|
809
|
+
});
|
|
810
|
+
app.post("/oauth/channels/:eci/credentials", requireAuthSession, function (req, res, next) {
|
|
811
|
+
oauth
|
|
812
|
+
.createChannelSecret(req.params.eci)
|
|
813
|
+
.then((creds) => res.json(creds))
|
|
814
|
+
.catch(next);
|
|
815
|
+
});
|
|
816
|
+
app.delete("/oauth/channels/:eci/credentials", requireAuthSession, function (req, res, next) {
|
|
817
|
+
oauth
|
|
818
|
+
.revokeChannelSecret(req.params.eci)
|
|
819
|
+
.then(() => res.json({ ok: true }))
|
|
820
|
+
.catch(next);
|
|
821
|
+
});
|
|
822
|
+
app.delete("/oauth/channels/:eci/tokens", requireAuthSession, function (req, res, next) {
|
|
823
|
+
oauth
|
|
824
|
+
.revokeTokens(req.params.eci)
|
|
825
|
+
.then(() => res.json({ ok: true }))
|
|
826
|
+
.catch(next);
|
|
827
|
+
});
|
|
828
|
+
}
|