pico-engine 1.4.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/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 +75 -43
- 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 +558 -15
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.wrangler.krl +4 -4
- package/package.json +14 -8
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +21 -20
- package/public/pico-labs-logo.png +0 -0
- 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 +115 -52
- 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 +643 -24
package/dist/server.js
CHANGED
|
@@ -6,19 +6,71 @@ 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");
|
|
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");
|
|
@@ -31,11 +83,19 @@ function server(core, uiECI) {
|
|
|
31
83
|
});
|
|
32
84
|
// Helmet 7+ CSP defaults include upgrade-insecure-requests, which breaks the
|
|
33
85
|
// UI on plain HTTP (browser upgrades /api/* to https and TLS fails).
|
|
34
|
-
|
|
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)({
|
|
35
89
|
contentSecurityPolicy: {
|
|
36
90
|
directives: Object.assign(Object.assign({}, helmet_1.default.contentSecurityPolicy.getDefaultDirectives()), { "upgrade-insecure-requests": null }),
|
|
37
91
|
},
|
|
38
|
-
})
|
|
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
|
+
});
|
|
39
99
|
app.use(express.static(path.resolve(__dirname, "..", "public")));
|
|
40
100
|
app.use(bodyParser.json({
|
|
41
101
|
type: [
|
|
@@ -49,10 +109,128 @@ function server(core, uiECI) {
|
|
|
49
109
|
type: "application/x-www-form-urlencoded",
|
|
50
110
|
extended: false,
|
|
51
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
|
+
}
|
|
52
205
|
app.get("/api/ui-context", function (req, res, next) {
|
|
53
|
-
|
|
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);
|
|
54
232
|
});
|
|
55
|
-
app.all("/api/flush", function (req, res, next) {
|
|
233
|
+
app.all("/api/flush", requireAuthSession, function (req, res, next) {
|
|
56
234
|
const attrs = mergeGetPost(req);
|
|
57
235
|
const url = attrs["url"];
|
|
58
236
|
if (typeof url !== "string") {
|
|
@@ -73,7 +251,7 @@ function server(core, uiECI) {
|
|
|
73
251
|
.catch(next);
|
|
74
252
|
}
|
|
75
253
|
});
|
|
76
|
-
app.all("/c/:eci/event/:domain/:name", function (req, res, next) {
|
|
254
|
+
app.all("/c/:eci/event/:domain/:name", requireChannelSession, function (req, res, next) {
|
|
77
255
|
core
|
|
78
256
|
.event({
|
|
79
257
|
eci: req.params.eci,
|
|
@@ -87,7 +265,7 @@ function server(core, uiECI) {
|
|
|
87
265
|
})
|
|
88
266
|
.catch(next);
|
|
89
267
|
});
|
|
90
|
-
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) {
|
|
91
269
|
core
|
|
92
270
|
.eventWait({
|
|
93
271
|
eci: req.params.eci,
|
|
@@ -101,7 +279,7 @@ function server(core, uiECI) {
|
|
|
101
279
|
})
|
|
102
280
|
.catch(next);
|
|
103
281
|
});
|
|
104
|
-
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) {
|
|
105
283
|
const attrs = mergeGetPost(req);
|
|
106
284
|
if (req.params.eid !== "none" && !attrs.hasOwnProperty("__eid")) {
|
|
107
285
|
attrs.__eid = req.params.eid;
|
|
@@ -137,7 +315,7 @@ function server(core, uiECI) {
|
|
|
137
315
|
})
|
|
138
316
|
.catch(next);
|
|
139
317
|
});
|
|
140
|
-
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) {
|
|
141
319
|
const attrs = mergeGetPost(req);
|
|
142
320
|
core
|
|
143
321
|
.eventQuery({
|
|
@@ -157,8 +335,8 @@ function server(core, uiECI) {
|
|
|
157
335
|
})
|
|
158
336
|
.catch(next);
|
|
159
337
|
});
|
|
160
|
-
|
|
161
|
-
const funcPart = req.params.
|
|
338
|
+
function handleSkyQuery(req, res, next) {
|
|
339
|
+
const funcPart = req.params["function"].split(".");
|
|
162
340
|
core
|
|
163
341
|
.query({
|
|
164
342
|
eci: req.params.eci,
|
|
@@ -180,9 +358,9 @@ function server(core, uiECI) {
|
|
|
180
358
|
}
|
|
181
359
|
})
|
|
182
360
|
.catch(next);
|
|
183
|
-
}
|
|
184
|
-
app.all("/
|
|
185
|
-
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(".");
|
|
186
364
|
core
|
|
187
365
|
.query({
|
|
188
366
|
eci: req.params.eci,
|
|
@@ -205,8 +383,19 @@ function server(core, uiECI) {
|
|
|
205
383
|
})
|
|
206
384
|
.catch(next);
|
|
207
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);
|
|
208
389
|
app.use(function (err, req, res, next) {
|
|
209
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
|
+
}
|
|
210
399
|
let message = err + "";
|
|
211
400
|
console.error(err);
|
|
212
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) {
|
|
@@ -232,3 +421,357 @@ function server(core, uiECI) {
|
|
|
232
421
|
});
|
|
233
422
|
return app;
|
|
234
423
|
}
|
|
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
|
+
}
|