pico-engine 1.3.0 → 1.5.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 +147 -0
- package/dist/RulesetRegistryLoaderFs.js +4 -7
- package/dist/auth/AuthService.js +537 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/cli.js +11 -0
- package/dist/index.js +83 -46
- package/dist/logging.js +36 -27
- package/dist/oauth/OAuthService.js +356 -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 +80 -0
- package/dist/server.js +565 -17
- package/dist/utils/toFileUrl.js +1 -2
- package/krl/io.picolabs.did-o.krl +25 -5
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.pds.krl +344 -0
- package/krl/io.picolabs.pico-engine-ui.krl +43 -10
- package/krl/io.picolabs.wrangler.krl +21 -20
- package/package.json +29 -29
- package/public/pico-engine-ui.css +4 -4
- package/public/pico-engine-ui.js +50 -16
- package/public/pico-labs-logo.png +0 -0
- package/src/RulesetRegistryLoaderFs.ts +6 -10
- package/src/auth/AuthService.ts +755 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/cli.ts +11 -0
- package/src/index.ts +123 -56
- package/src/logging.ts +36 -47
- package/src/oauth/OAuthService.ts +474 -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 +97 -0
- package/src/server.ts +647 -17
package/dist/server.js
CHANGED
|
@@ -1,24 +1,76 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.server =
|
|
3
|
+
exports.server = server;
|
|
4
4
|
const bodyParser = require("body-parser");
|
|
5
5
|
const express = require("express");
|
|
6
|
-
const
|
|
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");
|
|
9
11
|
const engineVersion = require("../package.json").version;
|
|
12
|
+
const SESSION_COOKIE = "pico-session";
|
|
13
|
+
const CEREMONY_COOKIE = "pico-ceremony";
|
|
14
|
+
function localhostChannelBypassEnabled() {
|
|
15
|
+
return process.env.PICO_ENGINE_ALLOW_LOCALHOST_C !== "0";
|
|
16
|
+
}
|
|
10
17
|
function mergeGetPost(req) {
|
|
11
18
|
// give preference to post body params
|
|
12
19
|
return _.assign({}, req.query, req.body, { _headers: req.headers });
|
|
13
20
|
}
|
|
14
|
-
function
|
|
21
|
+
function parseCookies(req) {
|
|
22
|
+
const header = req.headers.cookie;
|
|
23
|
+
const out = {};
|
|
24
|
+
if (typeof header !== "string") {
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
for (const part of header.split(";")) {
|
|
28
|
+
const idx = part.indexOf("=");
|
|
29
|
+
if (idx < 0) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const name = part.slice(0, idx).trim();
|
|
33
|
+
const value = part.slice(idx + 1).trim();
|
|
34
|
+
if (name) {
|
|
35
|
+
out[name] = decodeURIComponent(value);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
function isLocalhostAddress(addr) {
|
|
41
|
+
if (!addr) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const normalized = addr.replace(/^::ffff:/, "");
|
|
45
|
+
return normalized === "127.0.0.1" || normalized === "::1";
|
|
46
|
+
}
|
|
47
|
+
function isLocalhostRequest(req) {
|
|
48
|
+
var _a;
|
|
49
|
+
if (isLocalhostAddress(req.ip)) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
if (isLocalhostAddress((_a = req.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
const host = req.headers.host || "";
|
|
56
|
+
return host.startsWith("localhost:") || host.startsWith("127.0.0.1:");
|
|
57
|
+
}
|
|
58
|
+
function server(core, uiECI, auth, oauth) {
|
|
59
|
+
const pf = core.picoFramework;
|
|
15
60
|
const app = express();
|
|
16
61
|
app.use(function (req, res, next) {
|
|
62
|
+
// /auth routes need a specific credentialed CORS origin (below), not "*".
|
|
63
|
+
if (req.path.indexOf("/auth") === 0) {
|
|
64
|
+
return next();
|
|
65
|
+
}
|
|
17
66
|
res.header("Access-Control-Allow-Origin", "*");
|
|
18
|
-
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
67
|
+
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
|
|
19
68
|
next();
|
|
20
69
|
});
|
|
21
70
|
app.use('*', (req, res, next) => {
|
|
71
|
+
if (req.path.indexOf("/auth") === 0) {
|
|
72
|
+
return next();
|
|
73
|
+
}
|
|
22
74
|
if (req.method == "OPTIONS") {
|
|
23
75
|
res.header("Allow", "POST, GET, OPTIONS");
|
|
24
76
|
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
|
|
@@ -29,7 +81,21 @@ function server(core, uiECI) {
|
|
|
29
81
|
next();
|
|
30
82
|
}
|
|
31
83
|
});
|
|
32
|
-
|
|
84
|
+
// Helmet 7+ CSP defaults include upgrade-insecure-requests, which breaks the
|
|
85
|
+
// UI on plain HTTP (browser upgrades /api/* to https and TLS fails).
|
|
86
|
+
// OAuth consent pages must not use form-action 'self' — after Allow, the
|
|
87
|
+
// browser follows a redirect to the app's redirect_uri (external origin).
|
|
88
|
+
const defaultHelmet = (0, helmet_1.default)({
|
|
89
|
+
contentSecurityPolicy: {
|
|
90
|
+
directives: Object.assign(Object.assign({}, helmet_1.default.contentSecurityPolicy.getDefaultDirectives()), { "upgrade-insecure-requests": null }),
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
app.use(function (req, res, next) {
|
|
94
|
+
if (req.path === "/oauth/authorize" || req.path === "/oauth/approve") {
|
|
95
|
+
return (0, helmet_1.default)({ contentSecurityPolicy: false })(req, res, next);
|
|
96
|
+
}
|
|
97
|
+
return defaultHelmet(req, res, next);
|
|
98
|
+
});
|
|
33
99
|
app.use(express.static(path.resolve(__dirname, "..", "public")));
|
|
34
100
|
app.use(bodyParser.json({
|
|
35
101
|
type: [
|
|
@@ -43,10 +109,128 @@ function server(core, uiECI) {
|
|
|
43
109
|
type: "application/x-www-form-urlencoded",
|
|
44
110
|
extended: false,
|
|
45
111
|
}));
|
|
112
|
+
if (auth) {
|
|
113
|
+
registerAuthRoutes(app, auth);
|
|
114
|
+
}
|
|
115
|
+
function isEciInRootTree(rootPicoId, eci) {
|
|
116
|
+
try {
|
|
117
|
+
let pico = pf.getPico(eci);
|
|
118
|
+
while (pico) {
|
|
119
|
+
if (pico.id === rootPicoId) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
if (!pico.parent) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
pico = pf.getPico(pico.parent);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch (_e) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
function requireAuthSession(req, res, next) {
|
|
134
|
+
if (!auth) {
|
|
135
|
+
next();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
139
|
+
auth
|
|
140
|
+
.getSession(token)
|
|
141
|
+
.then((session) => {
|
|
142
|
+
if (!session) {
|
|
143
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const eci = req.params.eci;
|
|
147
|
+
if (eci && !isEciInRootTree(session.rootPicoId, eci)) {
|
|
148
|
+
res.status(403).json({ error: "Not authorized for this pico" });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
req.picoSession = session;
|
|
152
|
+
next();
|
|
153
|
+
})
|
|
154
|
+
.catch(next);
|
|
155
|
+
}
|
|
156
|
+
/** `/c/*` is internal: passkey session, or localhost for in-engine HTTP loops. */
|
|
157
|
+
function requireChannelSession(req, res, next) {
|
|
158
|
+
if (!auth) {
|
|
159
|
+
next();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (localhostChannelBypassEnabled() && isLocalhostRequest(req)) {
|
|
163
|
+
next();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
requireAuthSession(req, res, next);
|
|
167
|
+
}
|
|
168
|
+
/** External Sky API: Bearer when channel is oauth-webhook or mesh has OAuth ruleset. */
|
|
169
|
+
function requireSkyBearerIfOAuth(req, res, next) {
|
|
170
|
+
if (!oauth) {
|
|
171
|
+
next();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const eci = req.params.eci;
|
|
175
|
+
if (!eci) {
|
|
176
|
+
next();
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
oauth
|
|
180
|
+
.skyRequiresBearer(eci)
|
|
181
|
+
.then((required) => {
|
|
182
|
+
if (!required) {
|
|
183
|
+
next();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const header = req.headers.authorization;
|
|
187
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) {
|
|
188
|
+
res.status(401).json({ error: "Bearer token required" });
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const token = header.slice("Bearer ".length).trim();
|
|
192
|
+
return oauth.validateSkyBearerToken(token, eci).then((valid) => {
|
|
193
|
+
if (!valid) {
|
|
194
|
+
res.status(401).json({ error: "Invalid or expired token" });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
next();
|
|
198
|
+
});
|
|
199
|
+
})
|
|
200
|
+
.catch(next);
|
|
201
|
+
}
|
|
202
|
+
if (oauth) {
|
|
203
|
+
registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
|
|
204
|
+
}
|
|
46
205
|
app.get("/api/ui-context", function (req, res, next) {
|
|
47
|
-
|
|
206
|
+
const hasRoots = pf.rootPicos().length > 0;
|
|
207
|
+
const base = {
|
|
208
|
+
version: engineVersion,
|
|
209
|
+
hasRoots,
|
|
210
|
+
allowSelfSignup: auth ? auth.allowSelfSignup() : false,
|
|
211
|
+
};
|
|
212
|
+
if (!auth) {
|
|
213
|
+
base.eci = uiECI;
|
|
214
|
+
res.json(base);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
218
|
+
auth
|
|
219
|
+
.needsAuthMigration()
|
|
220
|
+
.then((needsAuthMigration) => {
|
|
221
|
+
base.needsAuthMigration = needsAuthMigration;
|
|
222
|
+
return auth.whoami(token);
|
|
223
|
+
})
|
|
224
|
+
.then((who) => {
|
|
225
|
+
base.session = who;
|
|
226
|
+
if (who.authenticated && who.uiECI) {
|
|
227
|
+
base.eci = who.uiECI;
|
|
228
|
+
}
|
|
229
|
+
res.json(base);
|
|
230
|
+
})
|
|
231
|
+
.catch(next);
|
|
48
232
|
});
|
|
49
|
-
app.all("/api/flush", function (req, res, next) {
|
|
233
|
+
app.all("/api/flush", requireAuthSession, function (req, res, next) {
|
|
50
234
|
const attrs = mergeGetPost(req);
|
|
51
235
|
const url = attrs["url"];
|
|
52
236
|
if (typeof url !== "string") {
|
|
@@ -67,7 +251,7 @@ function server(core, uiECI) {
|
|
|
67
251
|
.catch(next);
|
|
68
252
|
}
|
|
69
253
|
});
|
|
70
|
-
app.all("/c/:eci/event/:domain/:name", function (req, res, next) {
|
|
254
|
+
app.all("/c/:eci/event/:domain/:name", requireChannelSession, function (req, res, next) {
|
|
71
255
|
core
|
|
72
256
|
.event({
|
|
73
257
|
eci: req.params.eci,
|
|
@@ -81,7 +265,7 @@ function server(core, uiECI) {
|
|
|
81
265
|
})
|
|
82
266
|
.catch(next);
|
|
83
267
|
});
|
|
84
|
-
app.all("/c/:eci/event-wait/:domain/:name", function (req, res, next) {
|
|
268
|
+
app.all("/c/:eci/event-wait/:domain/:name", requireChannelSession, function (req, res, next) {
|
|
85
269
|
core
|
|
86
270
|
.eventWait({
|
|
87
271
|
eci: req.params.eci,
|
|
@@ -95,7 +279,7 @@ function server(core, uiECI) {
|
|
|
95
279
|
})
|
|
96
280
|
.catch(next);
|
|
97
281
|
});
|
|
98
|
-
app.all("/sky/event/:eci/:eid/:domain/:type", function (req, res, next) {
|
|
282
|
+
app.all("/sky/event/:eci/:eid/:domain/:type", requireSkyBearerIfOAuth, function (req, res, next) {
|
|
99
283
|
const attrs = mergeGetPost(req);
|
|
100
284
|
if (req.params.eid !== "none" && !attrs.hasOwnProperty("__eid")) {
|
|
101
285
|
attrs.__eid = req.params.eid;
|
|
@@ -131,7 +315,7 @@ function server(core, uiECI) {
|
|
|
131
315
|
})
|
|
132
316
|
.catch(next);
|
|
133
317
|
});
|
|
134
|
-
app.all("/c/:eci/event/:domain/:name/query/:rid/:qname", function (req, res, next) {
|
|
318
|
+
app.all("/c/:eci/event/:domain/:name/query/:rid/:qname", requireChannelSession, function (req, res, next) {
|
|
135
319
|
const attrs = mergeGetPost(req);
|
|
136
320
|
core
|
|
137
321
|
.eventQuery({
|
|
@@ -151,8 +335,8 @@ function server(core, uiECI) {
|
|
|
151
335
|
})
|
|
152
336
|
.catch(next);
|
|
153
337
|
});
|
|
154
|
-
|
|
155
|
-
const funcPart = req.params.
|
|
338
|
+
function handleSkyQuery(req, res, next) {
|
|
339
|
+
const funcPart = req.params["function"].split(".");
|
|
156
340
|
core
|
|
157
341
|
.query({
|
|
158
342
|
eci: req.params.eci,
|
|
@@ -174,9 +358,9 @@ function server(core, uiECI) {
|
|
|
174
358
|
}
|
|
175
359
|
})
|
|
176
360
|
.catch(next);
|
|
177
|
-
}
|
|
178
|
-
app.all("/
|
|
179
|
-
const funcPart = req.params
|
|
361
|
+
}
|
|
362
|
+
app.all("/c/:eci/query/:rid/:name", requireChannelSession, function (req, res, next) {
|
|
363
|
+
const funcPart = req.params.name.split(".");
|
|
180
364
|
core
|
|
181
365
|
.query({
|
|
182
366
|
eci: req.params.eci,
|
|
@@ -199,8 +383,19 @@ function server(core, uiECI) {
|
|
|
199
383
|
})
|
|
200
384
|
.catch(next);
|
|
201
385
|
});
|
|
386
|
+
app.all("/sky/query/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
|
|
387
|
+
// Legacy alias; prefer /sky/query/ per Sky Query API docs.
|
|
388
|
+
app.all("/sky/cloud/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
|
|
202
389
|
app.use(function (err, req, res, next) {
|
|
203
390
|
var _a, _b, _c;
|
|
391
|
+
if (err instanceof auth_1.AuthError) {
|
|
392
|
+
res.status(err.statusCode).json({ error: err.message });
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (err instanceof oauth_1.OAuthError) {
|
|
396
|
+
res.status(err.statusCode).json({ error: err.oauthError, error_description: err.message });
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
204
399
|
let message = err + "";
|
|
205
400
|
console.error(err);
|
|
206
401
|
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) {
|
|
@@ -226,4 +421,357 @@ function server(core, uiECI) {
|
|
|
226
421
|
});
|
|
227
422
|
return app;
|
|
228
423
|
}
|
|
229
|
-
|
|
424
|
+
function registerAuthRoutes(app, auth) {
|
|
425
|
+
function setSessionCookie(res, token, expiry) {
|
|
426
|
+
res.cookie(SESSION_COOKIE, token, {
|
|
427
|
+
httpOnly: true,
|
|
428
|
+
sameSite: "lax",
|
|
429
|
+
secure: auth.isSecure(),
|
|
430
|
+
path: "/",
|
|
431
|
+
expires: new Date(expiry),
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
function setCeremonyCookie(res, ceremonyId) {
|
|
435
|
+
res.cookie(CEREMONY_COOKIE, ceremonyId, {
|
|
436
|
+
httpOnly: true,
|
|
437
|
+
sameSite: "lax",
|
|
438
|
+
secure: auth.isSecure(),
|
|
439
|
+
path: "/",
|
|
440
|
+
maxAge: 5 * 60 * 1000,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
function clearCeremonyCookie(res) {
|
|
444
|
+
res.clearCookie(CEREMONY_COOKIE, { path: "/" });
|
|
445
|
+
}
|
|
446
|
+
// WebAuthn requires a specific, credentialed origin — never "*".
|
|
447
|
+
app.use("/auth", function (req, res, next) {
|
|
448
|
+
res.header("Access-Control-Allow-Origin", auth.rp().origin);
|
|
449
|
+
res.header("Access-Control-Allow-Credentials", "true");
|
|
450
|
+
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
451
|
+
res.header("Access-Control-Allow-Methods", "POST, GET, DELETE, OPTIONS");
|
|
452
|
+
res.header("Vary", "Origin");
|
|
453
|
+
if (req.method === "OPTIONS") {
|
|
454
|
+
res.status(204).end();
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
next();
|
|
458
|
+
});
|
|
459
|
+
function requireSession(req, res, next) {
|
|
460
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
461
|
+
auth
|
|
462
|
+
.getSession(token)
|
|
463
|
+
.then((session) => {
|
|
464
|
+
if (!session) {
|
|
465
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
req.picoSession = session;
|
|
469
|
+
next();
|
|
470
|
+
})
|
|
471
|
+
.catch(next);
|
|
472
|
+
}
|
|
473
|
+
app.post("/auth/register/options", function (req, res, next) {
|
|
474
|
+
auth
|
|
475
|
+
.registerNewAccountOptions({
|
|
476
|
+
displayName: req.body && req.body.displayName,
|
|
477
|
+
invite: req.body && req.body.invite,
|
|
478
|
+
})
|
|
479
|
+
.then(({ options, ceremonyId }) => {
|
|
480
|
+
setCeremonyCookie(res, ceremonyId);
|
|
481
|
+
res.json(options);
|
|
482
|
+
})
|
|
483
|
+
.catch(next);
|
|
484
|
+
});
|
|
485
|
+
app.post("/auth/register/verify", function (req, res, next) {
|
|
486
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
487
|
+
auth
|
|
488
|
+
.registerNewAccountVerify({ ceremonyId, response: req.body })
|
|
489
|
+
.then(({ account, session, uiECI }) => {
|
|
490
|
+
clearCeremonyCookie(res);
|
|
491
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
492
|
+
res.json({
|
|
493
|
+
verified: true,
|
|
494
|
+
accountId: account.accountId,
|
|
495
|
+
rootPicoId: account.rootPicoId,
|
|
496
|
+
uiECI,
|
|
497
|
+
});
|
|
498
|
+
})
|
|
499
|
+
.catch(next);
|
|
500
|
+
});
|
|
501
|
+
app.post("/auth/claim/options", function (req, res, next) {
|
|
502
|
+
auth
|
|
503
|
+
.claimPrimaryRootOptions({ displayName: req.body && req.body.displayName })
|
|
504
|
+
.then(({ options, ceremonyId }) => {
|
|
505
|
+
setCeremonyCookie(res, ceremonyId);
|
|
506
|
+
res.json(options);
|
|
507
|
+
})
|
|
508
|
+
.catch(next);
|
|
509
|
+
});
|
|
510
|
+
app.post("/auth/claim/verify", function (req, res, next) {
|
|
511
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
512
|
+
auth
|
|
513
|
+
.claimPrimaryRootVerify({ ceremonyId, response: req.body })
|
|
514
|
+
.then(({ account, session, uiECI }) => {
|
|
515
|
+
clearCeremonyCookie(res);
|
|
516
|
+
setSessionCookie(res, session.token, session.expiry);
|
|
517
|
+
res.json({
|
|
518
|
+
verified: true,
|
|
519
|
+
accountId: account.accountId,
|
|
520
|
+
rootPicoId: account.rootPicoId,
|
|
521
|
+
uiECI,
|
|
522
|
+
});
|
|
523
|
+
})
|
|
524
|
+
.catch(next);
|
|
525
|
+
});
|
|
526
|
+
app.post("/auth/login/options", function (req, res, next) {
|
|
527
|
+
auth
|
|
528
|
+
.loginOptions()
|
|
529
|
+
.then(({ options, ceremonyId }) => {
|
|
530
|
+
setCeremonyCookie(res, ceremonyId);
|
|
531
|
+
res.json(options);
|
|
532
|
+
})
|
|
533
|
+
.catch(next);
|
|
534
|
+
});
|
|
535
|
+
app.post("/auth/login/verify", function (req, res, next) {
|
|
536
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
537
|
+
auth
|
|
538
|
+
.loginVerify({ ceremonyId, response: req.body })
|
|
539
|
+
.then(({ account, session }) => {
|
|
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
|
+
});
|
|
547
|
+
})
|
|
548
|
+
.catch(next);
|
|
549
|
+
});
|
|
550
|
+
app.post("/auth/logout", function (req, res, next) {
|
|
551
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
552
|
+
auth
|
|
553
|
+
.deleteSession(token)
|
|
554
|
+
.then(() => {
|
|
555
|
+
res.clearCookie(SESSION_COOKIE, { path: "/" });
|
|
556
|
+
res.json({ ok: true });
|
|
557
|
+
})
|
|
558
|
+
.catch(next);
|
|
559
|
+
});
|
|
560
|
+
app.get("/auth/session", function (req, res, next) {
|
|
561
|
+
const token = parseCookies(req)[SESSION_COOKIE] || "";
|
|
562
|
+
auth
|
|
563
|
+
.whoami(token)
|
|
564
|
+
.then((who) => res.json(who))
|
|
565
|
+
.catch(next);
|
|
566
|
+
});
|
|
567
|
+
app.post("/auth/credentials/options", requireSession, function (req, res, next) {
|
|
568
|
+
const session = req.picoSession;
|
|
569
|
+
auth
|
|
570
|
+
.addCredentialOptions({ accountId: session.accountId })
|
|
571
|
+
.then(({ options, ceremonyId }) => {
|
|
572
|
+
setCeremonyCookie(res, ceremonyId);
|
|
573
|
+
res.json(options);
|
|
574
|
+
})
|
|
575
|
+
.catch(next);
|
|
576
|
+
});
|
|
577
|
+
app.post("/auth/credentials/verify", requireSession, function (req, res, next) {
|
|
578
|
+
const session = req.picoSession;
|
|
579
|
+
const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
|
|
580
|
+
auth
|
|
581
|
+
.addCredentialVerify({
|
|
582
|
+
ceremonyId,
|
|
583
|
+
accountId: session.accountId,
|
|
584
|
+
response: req.body,
|
|
585
|
+
label: req.body && req.body.label,
|
|
586
|
+
})
|
|
587
|
+
.then(({ credential }) => {
|
|
588
|
+
clearCeremonyCookie(res);
|
|
589
|
+
res.json({ verified: true, credential });
|
|
590
|
+
})
|
|
591
|
+
.catch(next);
|
|
592
|
+
});
|
|
593
|
+
app.delete("/auth/credentials/:id", requireSession, function (req, res, next) {
|
|
594
|
+
const session = req.picoSession;
|
|
595
|
+
auth
|
|
596
|
+
.deleteCredential(session.accountId, req.params.id)
|
|
597
|
+
.then(() => res.json({ ok: true }))
|
|
598
|
+
.catch(next);
|
|
599
|
+
});
|
|
600
|
+
app.get("/auth/invites/:token", function (req, res, next) {
|
|
601
|
+
auth
|
|
602
|
+
.peekInvite(req.params.token)
|
|
603
|
+
.then((peek) => res.json(peek))
|
|
604
|
+
.catch(next);
|
|
605
|
+
});
|
|
606
|
+
app.post("/auth/invites", requireSession, function (req, res, next) {
|
|
607
|
+
const session = req.picoSession;
|
|
608
|
+
auth
|
|
609
|
+
.createInvite({
|
|
610
|
+
createdByAccountId: session.accountId,
|
|
611
|
+
label: req.body && req.body.label,
|
|
612
|
+
})
|
|
613
|
+
.then((invite) => res.json(invite))
|
|
614
|
+
.catch(next);
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
function registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookiesFn) {
|
|
618
|
+
function requireOAuthSession(req, res, next) {
|
|
619
|
+
if (!auth) {
|
|
620
|
+
res.status(503).json({ error: "Authentication is not configured" });
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
624
|
+
auth
|
|
625
|
+
.getSession(token)
|
|
626
|
+
.then((session) => {
|
|
627
|
+
if (!session) {
|
|
628
|
+
res.status(401).json({ error: "Not authenticated" });
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
req.picoSession = session;
|
|
632
|
+
next();
|
|
633
|
+
})
|
|
634
|
+
.catch(next);
|
|
635
|
+
}
|
|
636
|
+
app.post("/oauth/token", function (req, res, next) {
|
|
637
|
+
// Prefer body, but accept query params too (manual/browser testing).
|
|
638
|
+
const body = _.assign({}, req.query, req.body);
|
|
639
|
+
oauth
|
|
640
|
+
.tokenGrant(body)
|
|
641
|
+
.then((token) => res.json(token))
|
|
642
|
+
.catch(next);
|
|
643
|
+
});
|
|
644
|
+
// Browser-friendly alias for manual token exchange during development.
|
|
645
|
+
app.get("/oauth/token", function (req, res, next) {
|
|
646
|
+
oauth
|
|
647
|
+
.tokenGrant(req.query)
|
|
648
|
+
.then((token) => res.json(token))
|
|
649
|
+
.catch(next);
|
|
650
|
+
});
|
|
651
|
+
app.get("/oauth/authorize", function (req, res, next) {
|
|
652
|
+
let params;
|
|
653
|
+
try {
|
|
654
|
+
params = (0, oauth_1.parseAuthorizeQuery)(req.query);
|
|
655
|
+
}
|
|
656
|
+
catch (err) {
|
|
657
|
+
next(err);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
oauth
|
|
661
|
+
.getAppForAuthorize(params)
|
|
662
|
+
.then(async (appRecord) => {
|
|
663
|
+
if (!auth) {
|
|
664
|
+
res.status(503).send("Authentication is not configured");
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
668
|
+
const session = await auth.getSession(token);
|
|
669
|
+
if (!session) {
|
|
670
|
+
const returnPath = req.originalUrl || "/oauth/authorize";
|
|
671
|
+
res.redirect("/?oauth_return=" + encodeURIComponent(returnPath));
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
if (session.rootPicoId !== appRecord.rootPicoId) {
|
|
675
|
+
res.status(403).send("Signed in to a different mesh");
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
res.type("html").send((0, oauth_1.renderConsentHtml)(appRecord, params));
|
|
679
|
+
})
|
|
680
|
+
.catch(next);
|
|
681
|
+
});
|
|
682
|
+
app.post("/oauth/approve", function (req, res, next) {
|
|
683
|
+
if (!auth) {
|
|
684
|
+
res.status(503).send("Authentication is not configured");
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
|
|
688
|
+
auth
|
|
689
|
+
.getSession(token)
|
|
690
|
+
.then((session) => {
|
|
691
|
+
if (!session) {
|
|
692
|
+
res.status(401).send("Not authenticated");
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
const params = (0, oauth_1.parseApproveBody)(req.body || {});
|
|
696
|
+
return oauth
|
|
697
|
+
.approveAuthorization(session, params)
|
|
698
|
+
.then((redirectUrl) => {
|
|
699
|
+
// 303 See Other: browser must follow with GET (OAuth code in query string).
|
|
700
|
+
res.redirect(303, redirectUrl);
|
|
701
|
+
});
|
|
702
|
+
})
|
|
703
|
+
.catch((err) => {
|
|
704
|
+
var _a, _b;
|
|
705
|
+
if (err instanceof oauth_1.OAuthError) {
|
|
706
|
+
const redirect_uri = String(((_a = req.body) === null || _a === void 0 ? void 0 : _a.redirect_uri) || "").trim();
|
|
707
|
+
if (redirect_uri) {
|
|
708
|
+
try {
|
|
709
|
+
const url = new URL(redirect_uri);
|
|
710
|
+
url.searchParams.set("error", err.oauthError);
|
|
711
|
+
url.searchParams.set("error_description", err.message);
|
|
712
|
+
const state = (_b = req.body) === null || _b === void 0 ? void 0 : _b.state;
|
|
713
|
+
if (state !== undefined && state !== "") {
|
|
714
|
+
url.searchParams.set("state", String(state));
|
|
715
|
+
}
|
|
716
|
+
res.redirect(303, url.toString());
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
catch (_e) {
|
|
720
|
+
// fall through to default error handler
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
next(err);
|
|
725
|
+
});
|
|
726
|
+
});
|
|
727
|
+
app.get("/oauth/apps", requireOAuthSession, function (req, res, next) {
|
|
728
|
+
const session = req.picoSession;
|
|
729
|
+
oauth
|
|
730
|
+
.listApps(session.rootPicoId)
|
|
731
|
+
.then((apps) => res.json({ apps }))
|
|
732
|
+
.catch(next);
|
|
733
|
+
});
|
|
734
|
+
app.post("/oauth/apps", requireOAuthSession, function (req, res, next) {
|
|
735
|
+
const session = req.picoSession;
|
|
736
|
+
const body = req.body || {};
|
|
737
|
+
oauth
|
|
738
|
+
.registerApp(session.rootPicoId, {
|
|
739
|
+
name: body.name,
|
|
740
|
+
redirect_uris: body.redirect_uris,
|
|
741
|
+
public_client: body.public_client,
|
|
742
|
+
})
|
|
743
|
+
.then((app) => res.json(app))
|
|
744
|
+
.catch(next);
|
|
745
|
+
});
|
|
746
|
+
app.delete("/oauth/apps/:clientId", requireOAuthSession, function (req, res, next) {
|
|
747
|
+
const session = req.picoSession;
|
|
748
|
+
oauth
|
|
749
|
+
.revokeApp(session.rootPicoId, req.params.clientId)
|
|
750
|
+
.then(() => res.json({ ok: true }))
|
|
751
|
+
.catch(next);
|
|
752
|
+
});
|
|
753
|
+
app.get("/oauth/channels/:eci/status", requireAuthSession, function (req, res, next) {
|
|
754
|
+
oauth
|
|
755
|
+
.channelStatusAsync(req.params.eci)
|
|
756
|
+
.then((status) => res.json(status))
|
|
757
|
+
.catch(next);
|
|
758
|
+
});
|
|
759
|
+
app.post("/oauth/channels/:eci/credentials", requireAuthSession, function (req, res, next) {
|
|
760
|
+
oauth
|
|
761
|
+
.createChannelSecret(req.params.eci)
|
|
762
|
+
.then((creds) => res.json(creds))
|
|
763
|
+
.catch(next);
|
|
764
|
+
});
|
|
765
|
+
app.delete("/oauth/channels/:eci/credentials", requireAuthSession, function (req, res, next) {
|
|
766
|
+
oauth
|
|
767
|
+
.revokeChannelSecret(req.params.eci)
|
|
768
|
+
.then(() => res.json({ ok: true }))
|
|
769
|
+
.catch(next);
|
|
770
|
+
});
|
|
771
|
+
app.delete("/oauth/channels/:eci/tokens", requireAuthSession, function (req, res, next) {
|
|
772
|
+
oauth
|
|
773
|
+
.revokeTokens(req.params.eci)
|
|
774
|
+
.then(() => res.json({ ok: true }))
|
|
775
|
+
.catch(next);
|
|
776
|
+
});
|
|
777
|
+
}
|
package/dist/utils/toFileUrl.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toFileUrl =
|
|
3
|
+
exports.toFileUrl = toFileUrl;
|
|
4
4
|
const path = require("path");
|
|
5
5
|
function toFileUrl(str) {
|
|
6
6
|
var pathName = path.resolve(str).replace(/\\/g, "/");
|
|
@@ -9,4 +9,3 @@ function toFileUrl(str) {
|
|
|
9
9
|
}
|
|
10
10
|
return encodeURI("file://" + pathName);
|
|
11
11
|
}
|
|
12
|
-
exports.toFileUrl = toFileUrl;
|