|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import { EventEmitter } from "events"; |
| 3 | +import { v4 as uuidv4 } from "uuid"; |
| 4 | +import jwt from "jsonwebtoken"; |
| 5 | +import { verifySignature } from "signature-validator"; |
| 6 | +import { isVersionValid } from "../utils/version"; |
| 7 | +import { addSession, isSessionValid } from "../constants"; |
| 8 | + |
| 9 | +const MIN_REQUIRED_VERSION = "0.4.0"; |
| 10 | +const JWT_EXPIRES_IN = "7d"; |
| 11 | + |
| 12 | +export class AuthController { |
| 13 | + private eventEmitter = new EventEmitter(); |
| 14 | + |
| 15 | + getOffer = async (_req: Request, res: Response) => { |
| 16 | + console.log("[auth] GET /api/auth/offer hit"); |
| 17 | + const baseUrl = process.env.NEXT_PUBLIC_CALENDAR_APP_URL; |
| 18 | + if (!baseUrl) { |
| 19 | + console.error("[auth] NEXT_PUBLIC_CALENDAR_APP_URL is not set"); |
| 20 | + return res.status(500).json({ error: "Server configuration error: NEXT_PUBLIC_CALENDAR_APP_URL not set" }); |
| 21 | + } |
| 22 | + |
| 23 | + let redirectUri: string; |
| 24 | + try { |
| 25 | + redirectUri = new URL("/api/auth", baseUrl).toString(); |
| 26 | + } catch (err) { |
| 27 | + console.error("[auth] Invalid NEXT_PUBLIC_CALENDAR_APP_URL:", baseUrl, err); |
| 28 | + return res.status(500).json({ error: "Server configuration error: invalid base URL" }); |
| 29 | + } |
| 30 | + |
| 31 | + const session = uuidv4(); |
| 32 | + addSession(session); |
| 33 | + const offer = `w3ds://auth?redirect=${encodeURIComponent(redirectUri)}&session=${session}&platform=${encodeURIComponent(baseUrl)}`; |
| 34 | + console.log("[auth] offer created, redirectUri=", redirectUri, "platform=", baseUrl); |
| 35 | + res.json({ uri: offer, sessionId: session }); |
| 36 | + }; |
| 37 | + |
| 38 | + sseStream = async (req: Request, res: Response) => { |
| 39 | + const { id } = req.params; |
| 40 | + console.log("[auth] GET /api/auth/sessions/:id hit, sessionId=", id); |
| 41 | + res.writeHead(200, { |
| 42 | + "Content-Type": "text/event-stream", |
| 43 | + "Cache-Control": "no-cache", |
| 44 | + Connection: "keep-alive", |
| 45 | + "Access-Control-Allow-Origin": "*", |
| 46 | + }); |
| 47 | + const handler = (data: unknown) => { |
| 48 | + res.write(`data: ${JSON.stringify(data)}\n\n`); |
| 49 | + }; |
| 50 | + this.eventEmitter.on(id, handler); |
| 51 | + const heartbeat = setInterval(() => { |
| 52 | + try { |
| 53 | + res.write(": heartbeat\n\n"); |
| 54 | + } catch { |
| 55 | + clearInterval(heartbeat); |
| 56 | + } |
| 57 | + }, 30000); |
| 58 | + req.on("close", () => { |
| 59 | + clearInterval(heartbeat); |
| 60 | + this.eventEmitter.off(id, handler); |
| 61 | + res.end(); |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + login = async (req: Request, res: Response) => { |
| 66 | + console.log("[auth] POST /api/auth hit"); |
| 67 | + try { |
| 68 | + const { ename, session, signature, appVersion } = req.body; |
| 69 | + console.log("[auth] body: ename=", ename, "session=", session?.slice(0, 8) + "...", "appVersion=", appVersion, "signature present=", !!signature); |
| 70 | + |
| 71 | + if (!ename) { |
| 72 | + console.log("[auth] reject: ename missing"); |
| 73 | + return res.status(400).json({ error: "ename is required" }); |
| 74 | + } |
| 75 | + if (!session) { |
| 76 | + console.log("[auth] reject: session missing"); |
| 77 | + return res.status(400).json({ error: "session is required" }); |
| 78 | + } |
| 79 | + if (!signature) { |
| 80 | + console.log("[auth] reject: signature missing"); |
| 81 | + return res.status(400).json({ error: "signature is required" }); |
| 82 | + } |
| 83 | + |
| 84 | + if (!isSessionValid(session)) { |
| 85 | + console.log("[auth] reject: invalid or expired session"); |
| 86 | + return res |
| 87 | + .status(400) |
| 88 | + .json({ error: "Invalid or expired session", message: "Please request a new login offer." }); |
| 89 | + } |
| 90 | + |
| 91 | + if (!appVersion || !isVersionValid(appVersion, MIN_REQUIRED_VERSION)) { |
| 92 | + console.log("[auth] reject: app version too old", appVersion); |
| 93 | + return res.status(400).json({ |
| 94 | + error: "App version too old", |
| 95 | + message: `Please update eID Wallet to version ${MIN_REQUIRED_VERSION} or later.`, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + const registryBaseUrl = process.env.PUBLIC_REGISTRY_URL; |
| 100 | + if (!registryBaseUrl) { |
| 101 | + console.log("[auth] reject: PUBLIC_REGISTRY_URL not set"); |
| 102 | + return res.status(500).json({ error: "Server configuration error" }); |
| 103 | + } |
| 104 | + |
| 105 | + console.log("[auth] verifying signature with registry", registryBaseUrl); |
| 106 | + const verificationResult = await verifySignature({ |
| 107 | + eName: ename, |
| 108 | + signature, |
| 109 | + payload: session, |
| 110 | + registryBaseUrl, |
| 111 | + }); |
| 112 | + |
| 113 | + if (!verificationResult.valid) { |
| 114 | + console.log("[auth] reject: signature invalid", verificationResult.error); |
| 115 | + return res.status(401).json({ |
| 116 | + error: "Invalid signature", |
| 117 | + message: verificationResult.error, |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + const secret = process.env.JWT_SECRET || "calendar-api-dev-secret"; |
| 122 | + const token = jwt.sign( |
| 123 | + { ename }, |
| 124 | + secret, |
| 125 | + { expiresIn: JWT_EXPIRES_IN } |
| 126 | + ); |
| 127 | + |
| 128 | + console.log("[auth] login success, ename=", ename); |
| 129 | + this.eventEmitter.emit(session, { token }); |
| 130 | + res.status(200).json({ token }); |
| 131 | + } catch (error) { |
| 132 | + console.error("[auth] login error:", error); |
| 133 | + res.status(500).json({ error: "Internal server error" }); |
| 134 | + } |
| 135 | + }; |
| 136 | +} |
0 commit comments