| 1 | import { buildAccountDataExport } from "./dataExport.js"; |
| 2 | |
| 3 | export async function handleAuthRoute (req, res, url, { auth, stateDb }) |
| 4 | { |
| 5 | try |
| 6 | { |
| 7 | if (url.pathname === "/auth/status" && req.method === "GET") |
| 8 | { |
| 9 | const status = await auth.statusFromRequest(req, res); |
| 10 | sendJson(res, 200, status); |
| 11 | return true; |
| 12 | } |
| 13 | |
| 14 | if (url.pathname === "/auth/login" && req.method === "GET") |
| 15 | { |
| 16 | if (typeof auth.beginLogin !== "function") |
| 17 | { |
| 18 | sendText(res, 501, "OAuth login is not configured."); |
| 19 | return true; |
| 20 | } |
| 21 | auth.beginLogin(req, res, url.searchParams.get("return_to") ?? "/"); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | if (url.pathname === "/auth/callback" && req.method === "GET") |
| 26 | { |
| 27 | if (typeof auth.finishLogin !== "function") |
| 28 | { |
| 29 | sendText(res, 501, "OAuth login is not configured."); |
| 30 | return true; |
| 31 | } |
| 32 | await auth.finishLogin(req, res); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | if (url.pathname === "/auth/logout" && (req.method === "GET" || req.method === "POST")) |
| 37 | { |
| 38 | const returnTo = url.searchParams.get("return_to") ?? "/"; |
| 39 | if (typeof auth.logout === "function") |
| 40 | { await auth.logout(req, res, returnTo); } |
| 41 | else |
| 42 | { |
| 43 | res.writeHead(302, { Location: returnTo }); |
| 44 | res.end(); |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | if (url.pathname === "/auth/error" && req.method === "GET") |
| 50 | { |
| 51 | const message = url.searchParams.get("message") ?? "Sign-in failed."; |
| 52 | res.writeHead(200, { "content-type": "text/html; charset=utf-8" }); |
| 53 | res.end(`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Sign-in failed</title></head>` |
| 54 | + `<body><main><h1>Sign-in failed</h1><p>${escapeHtml(message)}</p>` |
| 55 | + `<p><a href="/auth/login">Try again</a></p></main></body></html>`); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | if (url.pathname === "/settings/export" && req.method === "GET") |
| 60 | { |
| 61 | const identity = await auth.authenticateRequest(req); |
| 62 | if (!identity) |
| 63 | { |
| 64 | sendJson(res, 401, { error: "Sign in required." }); |
| 65 | return true; |
| 66 | } |
| 67 | const account = stateDb.getAccountBySubject(identity.authSubject); |
| 68 | if (!account) |
| 69 | { |
| 70 | sendJson(res, 404, { error: "Account not found." }); |
| 71 | return true; |
| 72 | } |
| 73 | const payload = buildAccountDataExport({ stateDb }, account); |
| 74 | res.writeHead(200, { |
| 75 | "content-type": "application/json; charset=utf-8", |
| 76 | "content-disposition": 'attachment; filename="questward-data-export.json"' |
| 77 | }); |
| 78 | res.end(JSON.stringify(payload, null, 2)); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | catch (error) |
| 85 | { |
| 86 | sendJson(res, 500, { error: error.message ?? "Auth route failed." }); |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | export async function resolveAccount (stateDb, identity) |
| 92 | { |
| 93 | return stateDb.resolveAccountFromAuth({ |
| 94 | authSubject: identity.authSubject, |
| 95 | email: identity.email ?? null, |
| 96 | username: identity.username ?? null |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | function sendJson (res, status, body) |
| 101 | { |
| 102 | res.writeHead(status, { "content-type": "application/json; charset=utf-8" }); |
| 103 | res.end(JSON.stringify(body)); |
| 104 | } |
| 105 | |
| 106 | function sendText (res, status, text) |
| 107 | { |
| 108 | res.writeHead(status, { "content-type": "text/plain; charset=utf-8" }); |
| 109 | res.end(text); |
| 110 | } |
| 111 | |
| 112 | function escapeHtml (text) |
| 113 | { |
| 114 | return String(text) |
| 115 | .replaceAll("&", "&") |
| 116 | .replaceAll("<", "<") |
| 117 | .replaceAll(">", ">") |
| 118 | .replaceAll("\"", """); |
| 119 | } |