From 21bc4bcadcc70bee1f85df89570139b281c3616b Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Thu, 14 Mar 2024 16:26:43 -0700 Subject: [PATCH] Fix `neopass-server` to respond correctly to `/token` requests Hey nice, now we can actually get a round-trip auth success! This gets us to the `Devise::OmniauthCallbacksController#neopass` success method, so now we need to add our logic to actually login/signup! --- bin/neopass-server | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/bin/neopass-server b/bin/neopass-server index dee0e1c9..0291e862 100755 --- a/bin/neopass-server +++ b/bin/neopass-server @@ -41,10 +41,10 @@ async function startBackingServer(port) { } async function startMainServer(port) { - const fetch = await import("node-fetch"); + const fetch = (await import("node-fetch")).default; const app = express(); - app.use(express.raw()); + app.use(express.text({ type: "*/*" })); app.get("/", (req, res) => res.end("NeoPass development server for DTI!")); @@ -53,12 +53,27 @@ async function startMainServer(port) { res.redirect(`http://localhost:8686/authorize?${query}`); }); - app.post("/oauth2/token", (req, res) => { - // For POST requests, the HTTP spec doesn't allow a redirect to a POST, - // so we proxy the request instead. - // TODO: Actually do that! For now we just log it. - console.log(req.body); - res.end("owo"); + app.post("/oauth2/token", async (req, res) => { + try { + // For POST requests, the HTTP spec doesn't allow a redirect to a + // POST, so we proxy the request instead. + const backingRes = await fetch("http://localhost:8686/token", { + method: "POST", + headers: { + "Content-Type": req.get("Content-Type"), + }, + body: req.body, + }); + if (!backingRes.ok) { + throw new Error(`backing server returned status ${res.status}`); + } + + res.set("Content-Type", backingRes.headers.get("Content-Type")); + return res.end(await backingRes.text()); + } catch (error) { + console.error(error); + return res.end(error.message); + } }); await new Promise((resolve) => app.listen(port, resolve));