-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
392 lines (362 loc) · 13.8 KB
/
Copy pathserver.js
File metadata and controls
392 lines (362 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env node
const http = require("http");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const rootDir = __dirname;
const envPath = path.join(rootDir, ".env.local");
function loadEnvFile(filePath) {
if (!fs.existsSync(filePath)) {
return {};
}
const values = {};
for (const line of fs.readFileSync(filePath, "utf8").split(/\r?\n/)) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) {
continue;
}
const index = trimmed.indexOf("=");
if (index === -1) {
continue;
}
values[trimmed.slice(0, index)] = trimmed.slice(index + 1);
}
return values;
}
const fileEnv = loadEnvFile(envPath);
function env(name, fallback = "") {
return process.env[name] || fileEnv[name] || fallback;
}
const config = {
daptinBaseUrl: env("DAPTIN_BASE_URL", "http://localhost:6336").replace(/\/$/, ""),
daptinApiUrl: env("DAPTIN_API_URL", env("DAPTIN_BASE_URL", "http://localhost:6336")).replace(/\/$/, ""),
daptinBrowserUrl: env("DAPTIN_BROWSER_URL", env("DAPTIN_BASE_URL", "http://localhost:6336")).replace(/\/$/, ""),
demoBaseUrl: env("DEMO_BASE_URL", "http://localhost:7777").replace(/\/$/, ""),
adminToken: env("DAPTIN_ADMIN_TOKEN"),
authenticatorName: env("AUTHENTICATOR_NAME"),
oauthConnectId: env("OAUTH_CONNECT_REFERENCE_ID"),
clientId: env("OAUTH_CLIENT_ID"),
clientSecret: env("OAUTH_CLIENT_SECRET"),
};
const listenUrl = new URL(config.demoBaseUrl);
const port = Number(listenUrl.port || (listenUrl.protocol === "https:" ? 443 : 80));
const host = env("LISTEN_HOST", listenUrl.hostname || "localhost");
function base64Url(input) {
return Buffer.from(input)
.toString("base64")
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
function randomToken(size = 32) {
return base64Url(crypto.randomBytes(size));
}
function pkceChallenge(verifier) {
return base64Url(crypto.createHash("sha256").update(verifier).digest());
}
function htmlEscape(value) {
return String(value)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function jsonBlock(value) {
return `<pre>${htmlEscape(JSON.stringify(value, null, 2))}</pre>`;
}
function parseCookies(request) {
const cookies = {};
const header = request.headers.cookie || "";
for (const part of header.split(";")) {
const index = part.indexOf("=");
if (index === -1) {
continue;
}
cookies[part.slice(0, index).trim()] = decodeURIComponent(part.slice(index + 1).trim());
}
return cookies;
}
function setCookie(response, name, value) {
const cookie = `${name}=${encodeURIComponent(value)}; Path=/; HttpOnly; SameSite=Lax`;
const existing = response.getHeader("set-cookie");
if (!existing) {
response.setHeader("set-cookie", cookie);
} else if (Array.isArray(existing)) {
response.setHeader("set-cookie", [...existing, cookie]);
} else {
response.setHeader("set-cookie", [existing, cookie]);
}
}
function redirect(response, location) {
response.writeHead(302, { location });
response.end();
}
function sendHtml(response, title, body, status = 200) {
response.writeHead(status, { "content-type": "text/html; charset=utf-8" });
response.end(`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${htmlEscape(title)}</title>
<style>
:root { color-scheme: light dark; font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
body { margin: 0; background: #f7f7f4; color: #1f2723; }
main { max-width: 920px; margin: 0 auto; padding: 48px 24px; }
h1 { font-size: 32px; line-height: 1.15; margin: 0 0 12px; letter-spacing: 0; }
h2 { font-size: 20px; margin: 32px 0 8px; }
p { line-height: 1.6; color: #48514d; }
.actions { display: flex; flex-wrap: wrap; gap: 12px; margin: 24px 0; }
a.button { display: inline-flex; align-items: center; min-height: 40px; padding: 0 14px; border: 1px solid #1f6f55; border-radius: 6px; text-decoration: none; color: white; background: #1f6f55; font-weight: 650; }
a.secondary { background: white; color: #1f6f55; }
code { background: rgba(31, 111, 85, 0.1); padding: 2px 5px; border-radius: 4px; }
pre { overflow: auto; padding: 16px; border-radius: 6px; background: #1f2723; color: #eef6f2; }
.panel { border: 1px solid #d8ded9; border-radius: 8px; padding: 20px; background: white; margin: 18px 0; }
.warning { color: #8a4f00; }
@media (prefers-color-scheme: dark) {
body { background: #151916; color: #ecf2ee; }
p { color: #bac6bf; }
.panel { background: #1d241f; border-color: #354239; }
a.secondary { background: #1d241f; }
}
</style>
</head>
<body><main>${body}</main></body>
</html>`);
}
async function fetchJson(url, options = {}) {
const response = await fetch(url, options);
const text = await response.text();
let data = null;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = text;
}
if (!response.ok) {
const detail = typeof data === "string" ? data : JSON.stringify(data);
throw new Error(`${response.status} ${response.statusText} from ${url}: ${detail}`);
}
return data;
}
function collectActionItems(response) {
const items = [];
const data = response && response.data;
if (Array.isArray(data)) {
for (const row of data) {
if (Array.isArray(row.attributes)) {
items.push(...row.attributes);
}
}
} else if (data && Array.isArray(data.attributes)) {
items.push(...data.attributes);
} else if (Array.isArray(response && response.attributes)) {
items.push(...response.attributes);
}
return items;
}
function actionItem(response, type, key) {
return collectActionItems(response).find((item) => item && item.type === type && (!key || item.key === key));
}
function requireConfig(keys) {
const missing = keys.filter((key) => !config[key]);
if (missing.length > 0) {
throw new Error(`Missing configuration: ${missing.join(", ")}. Run npm run setup first.`);
}
}
function renderHome(response) {
const missing = ["adminToken", "authenticatorName", "oauthConnectId", "clientId", "clientSecret"].filter((key) => !config[key]);
const configWarning = missing.length
? `<p class="warning">Missing generated setup values: <code>${htmlEscape(missing.join(", "))}</code>. Run <code>npm run setup</code>.</p>`
: "";
sendHtml(response, "Daptin OAuth Provider Demo", `
<h1>Daptin OAuth Provider Demo</h1>
<p>This local app exercises Daptin as an OAuth provider and as an OAuth client of itself.</p>
${configWarning}
<div class="actions">
<a class="button" href="/plain-client/begin">Plain OAuth client login</a>
<a class="button secondary" href="/daptin-consumer/begin">Daptin oauth_connect self-login</a>
</div>
<div class="panel">
<h2>Current configuration</h2>
<p>Daptin browser URL: <code>${htmlEscape(config.daptinBrowserUrl)}</code></p>
<p>Daptin API URL: <code>${htmlEscape(config.daptinApiUrl)}</code></p>
<p>Demo: <code>${htmlEscape(config.demoBaseUrl)}</code></p>
<p>Authenticator: <code>${htmlEscape(config.authenticatorName || "(not configured)")}</code></p>
<p>Client ID: <code>${htmlEscape(config.clientId || "(not configured)")}</code></p>
</div>
<div class="panel">
<h2>Before running</h2>
<p>Sign in to Daptin in the same browser at <code>${htmlEscape(config.daptinBrowserUrl)}</code>. The provider authorization page uses the browser's Daptin session.</p>
</div>
`);
}
async function beginPlainClient(request, response) {
requireConfig(["clientId", "clientSecret"]);
const state = randomToken();
const verifier = randomToken(48);
const authorizeUrl = new URL(`${config.daptinBrowserUrl}/oauth/authorize`);
authorizeUrl.searchParams.set("response_type", "code");
authorizeUrl.searchParams.set("client_id", config.clientId);
authorizeUrl.searchParams.set("redirect_uri", `${config.demoBaseUrl}/plain-client/callback`);
authorizeUrl.searchParams.set("scope", "openid profile email");
authorizeUrl.searchParams.set("state", state);
authorizeUrl.searchParams.set("code_challenge", pkceChallenge(verifier));
authorizeUrl.searchParams.set("code_challenge_method", "S256");
setCookie(response, "plain_state", state);
setCookie(response, "plain_verifier", verifier);
redirect(response, authorizeUrl.toString());
}
async function finishPlainClient(request, response, url) {
requireConfig(["clientId", "clientSecret"]);
const cookies = parseCookies(request);
const expectedState = cookies.plain_state;
const state = url.searchParams.get("state");
const code = url.searchParams.get("code");
const error = url.searchParams.get("error");
if (error) {
throw new Error(`Provider returned OAuth error: ${error}`);
}
if (!code) {
throw new Error("Callback did not include code.");
}
if (!expectedState || expectedState !== state) {
throw new Error("OAuth state mismatch.");
}
if (!cookies.plain_verifier) {
throw new Error("Missing PKCE verifier cookie.");
}
const body = new URLSearchParams({
grant_type: "authorization_code",
code,
redirect_uri: `${config.demoBaseUrl}/plain-client/callback`,
code_verifier: cookies.plain_verifier,
});
const credentials = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64");
const token = await fetchJson(`${config.daptinApiUrl}/oauth/token`, {
method: "POST",
headers: {
authorization: `Basic ${credentials}`,
"content-type": "application/x-www-form-urlencoded",
accept: "application/json",
},
body,
});
const userinfo = await fetchJson(`${config.daptinApiUrl}/oauth/userinfo`, {
headers: {
authorization: `Bearer ${token.access_token}`,
accept: "application/json",
},
});
sendHtml(response, "Plain OAuth Client Result", `
<h1>Plain OAuth client result</h1>
<p>The demo exchanged the authorization code at Daptin's token endpoint and fetched userinfo.</p>
<div class="actions"><a class="button secondary" href="/">Back</a></div>
<h2>UserInfo</h2>
${jsonBlock(userinfo)}
<h2>Token response</h2>
${jsonBlock(token)}
`);
}
async function beginDaptinConsumer(request, response) {
requireConfig(["adminToken", "oauthConnectId"]);
const result = await fetchJson(`${config.daptinApiUrl}/action/oauth_connect/oauth_login_begin`, {
method: "POST",
headers: {
authorization: `Bearer ${config.adminToken}`,
"content-type": "application/json",
accept: "application/json",
},
body: JSON.stringify({
attributes: {
oauth_connect_id: config.oauthConnectId,
},
}),
});
const stateItem = actionItem(result, "client.store.set", "secret");
const redirectItem = actionItem(result, "client.redirect");
const location = redirectItem && (redirectItem.location || redirectItem.value || (redirectItem.attributes && redirectItem.attributes.location));
const state = stateItem && (stateItem.value || (stateItem.attributes && stateItem.attributes.value));
if (!location) {
throw new Error(`Daptin did not return a client.redirect item: ${JSON.stringify(result)}`);
}
if (state) {
setCookie(response, "daptin_consumer_state", state);
}
redirect(response, location);
}
async function finishDaptinConsumer(request, response, url) {
requireConfig(["adminToken", "authenticatorName"]);
const cookies = parseCookies(request);
const state = url.searchParams.get("state");
const code = url.searchParams.get("code");
const authenticator = url.searchParams.get("authenticator") || config.authenticatorName;
const error = url.searchParams.get("error");
if (error) {
throw new Error(`Provider returned OAuth error: ${error}`);
}
if (!code || !state) {
throw new Error("Callback did not include code and state.");
}
if (cookies.daptin_consumer_state && cookies.daptin_consumer_state !== state) {
throw new Error("Daptin consumer state mismatch.");
}
const result = await fetchJson(`${config.daptinApiUrl}/action/oauth_token/oauth.login.response`, {
method: "POST",
headers: {
authorization: `Bearer ${config.adminToken}`,
"content-type": "application/json",
accept: "application/json",
},
body: JSON.stringify({
attributes: {
code,
state,
authenticator,
},
}),
});
sendHtml(response, "Daptin Consumer Result", `
<h1>Daptin oauth_connect result</h1>
<p>Daptin consumed its own provider response through <code>oauth.login.response</code>.</p>
<div class="actions"><a class="button secondary" href="/">Back</a></div>
${jsonBlock(result)}
`);
}
async function route(request, response) {
const url = new URL(request.url, config.demoBaseUrl);
if (url.pathname === "/") {
renderHome(response);
return;
}
if (url.pathname === "/plain-client/begin") {
await beginPlainClient(request, response);
return;
}
if (url.pathname === "/plain-client/callback") {
await finishPlainClient(request, response, url);
return;
}
if (url.pathname === "/daptin-consumer/begin") {
await beginDaptinConsumer(request, response);
return;
}
if (url.pathname === "/callback") {
await finishDaptinConsumer(request, response, url);
return;
}
sendHtml(response, "Not Found", `<h1>Not Found</h1><p>No route for <code>${htmlEscape(url.pathname)}</code>.</p>`, 404);
}
const server = http.createServer((request, response) => {
route(request, response).catch((error) => {
sendHtml(response, "Demo Error", `
<h1>Demo Error</h1>
<p>${htmlEscape(error.message)}</p>
${jsonBlock({ stack: error.stack })}
<div class="actions"><a class="button secondary" href="/">Back</a></div>
`, 500);
});
});
server.listen(port, host, () => {
console.log(`Daptin OAuth provider demo listening at ${config.demoBaseUrl}`);
});