diff --git a/backend/src/lib/api.ts b/backend/src/lib/api.ts index df87ca6..f79424b 100644 --- a/backend/src/lib/api.ts +++ b/backend/src/lib/api.ts @@ -155,21 +155,40 @@ app.post("/login/step/username", async (req, res) => { }); req.session.login.session_id = session.id; - await deliveryProvider!.send([username, instance], "code: " + code); + try { + await deliveryProvider!.send([username, instance], "code: " + code); - req.session.save(() => { - res.send({ - success: true, - step: "CODE_SENT", - data: { - session_id: session.id, - account: - deliveryProvider!.service_account.username + - "@" + - deliveryProvider!.service_account.host, - }, + req.session.save(() => { + res.send({ + success: true, + step: "CODE_SENT", + data: { + session_id: session.id, + account: + deliveryProvider!.service_account.username + + "@" + + deliveryProvider!.service_account.host, + }, + }); }); - }); + } catch (e) { + console.error( + "Error while delivering to " + [username, instance].join("@"), + e + ); + + await prisma.authSession.delete({ where: { id: session.id } }); + req.session.login.session_id = undefined; + + req.session.save(() => { + res.send({ + success: false, + error: + "Error while sending: " + + ((e as any)?.message || "unknown error"), + }); + }); + } break; } diff --git a/backend/src/lib/express.ts b/backend/src/lib/express.ts index 62da7e9..4b4bd54 100644 --- a/backend/src/lib/express.ts +++ b/backend/src/lib/express.ts @@ -5,6 +5,7 @@ import bodyParser from "body-parser"; import path from "path"; import { oidc } from "./oidc.js"; import { makeClientPublic } from "./utils.js"; +import { errors as OIDC_Errors } from "oidc-provider"; import "../types/session-types.js"; import { APIRouter } from "./api.js"; @@ -44,13 +45,25 @@ app.use("/interaction/:uid", async (req, res, next) => { if (typeof req.session.user === "undefined") { res.redirect("/login?return=" + encodeURIComponent(req.originalUrl)); } else { - const returnTo = await oidc.interactionResult(req, res, { - login: { accountId: req.session.user.sub }, - }); + try { + const returnTo = await oidc.interactionResult(req, res, { + login: { accountId: req.session.user.sub }, + }); - req.session.destroy(() => { - res.redirect(returnTo); - }); + req.session.destroy(() => { + res.redirect(returnTo); + }); + } catch (e) { + console.error("Error while in interaction middleware", e); + + req.session.destroy(() => { + if (e instanceof OIDC_Errors.SessionNotFound) { + res.send("