|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import { v4 as uuidv4 } from "uuid"; |
| 3 | +import { UserService } from "../services/UserService"; |
| 4 | +import { EventEmitter } from "events"; |
| 5 | +import { signToken } from "../utils/jwt"; |
| 6 | +import { isVersionValid } from "../utils/version"; |
| 7 | +import { verifySignature } from "signature-validator"; |
| 8 | + |
| 9 | +const MIN_REQUIRED_VERSION = "0.4.0"; |
| 10 | + |
| 11 | +export class AuthController { |
| 12 | + private userService: UserService; |
| 13 | + private eventEmitter: EventEmitter; |
| 14 | + |
| 15 | + constructor() { |
| 16 | + this.userService = new UserService(); |
| 17 | + this.eventEmitter = new EventEmitter(); |
| 18 | + } |
| 19 | + |
| 20 | + sseStream = async (req: Request, res: Response) => { |
| 21 | + const { id } = req.params; |
| 22 | + |
| 23 | + res.writeHead(200, { |
| 24 | + "Content-Type": "text/event-stream", |
| 25 | + "Cache-Control": "no-cache", |
| 26 | + Connection: "keep-alive", |
| 27 | + "Access-Control-Allow-Origin": "*", |
| 28 | + }); |
| 29 | + |
| 30 | + const handler = (data: any) => { |
| 31 | + res.write(`data: ${JSON.stringify(data)}\n\n`); |
| 32 | + }; |
| 33 | + |
| 34 | + this.eventEmitter.on(id, handler); |
| 35 | + |
| 36 | + req.on("close", () => { |
| 37 | + this.eventEmitter.off(id, handler); |
| 38 | + res.end(); |
| 39 | + }); |
| 40 | + |
| 41 | + req.on("error", (error) => { |
| 42 | + console.error("SSE Error:", error); |
| 43 | + this.eventEmitter.off(id, handler); |
| 44 | + res.end(); |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + getOffer = async (req: Request, res: Response) => { |
| 49 | + const url = new URL( |
| 50 | + "/api/auth", |
| 51 | + process.env.PUBLIC_ESIGNER_BASE_URL, |
| 52 | + ).toString(); |
| 53 | + const session = uuidv4(); |
| 54 | + const offer = `w3ds://auth?redirect=${url}&session=${session}&platform=esigner`; |
| 55 | + res.json({ uri: offer }); |
| 56 | + }; |
| 57 | + |
| 58 | + login = async (req: Request, res: Response) => { |
| 59 | + try { |
| 60 | + const { ename, session, appVersion, signature } = req.body; |
| 61 | + |
| 62 | + if (!ename) { |
| 63 | + return res.status(400).json({ error: "ename is required" }); |
| 64 | + } |
| 65 | + |
| 66 | + if (!session) { |
| 67 | + return res.status(400).json({ error: "session is required" }); |
| 68 | + } |
| 69 | + |
| 70 | + if (!signature) { |
| 71 | + return res.status(400).json({ error: "signature is required" }); |
| 72 | + } |
| 73 | + |
| 74 | + if (!appVersion || !isVersionValid(appVersion, MIN_REQUIRED_VERSION)) { |
| 75 | + const errorMessage = { |
| 76 | + error: true, |
| 77 | + message: `Your eID Wallet app version is outdated. Please update to version ${MIN_REQUIRED_VERSION} or later.`, |
| 78 | + type: "version_mismatch" |
| 79 | + }; |
| 80 | + this.eventEmitter.emit(session, errorMessage); |
| 81 | + return res.status(400).json({ |
| 82 | + error: "App version too old", |
| 83 | + message: errorMessage.message |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + const registryBaseUrl = process.env.PUBLIC_REGISTRY_URL; |
| 88 | + if (!registryBaseUrl) { |
| 89 | + console.error("PUBLIC_REGISTRY_URL not configured"); |
| 90 | + return res.status(500).json({ error: "Server configuration error" }); |
| 91 | + } |
| 92 | + |
| 93 | + const verificationResult = await verifySignature({ |
| 94 | + eName: ename, |
| 95 | + signature: signature, |
| 96 | + payload: session, |
| 97 | + registryBaseUrl: registryBaseUrl, |
| 98 | + }); |
| 99 | + |
| 100 | + if (!verificationResult.valid) { |
| 101 | + console.error("Signature validation failed:", verificationResult.error); |
| 102 | + return res.status(401).json({ |
| 103 | + error: "Invalid signature", |
| 104 | + message: verificationResult.error |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + let user = await this.userService.findByEname(ename); |
| 109 | + |
| 110 | + if (!user) { |
| 111 | + throw new Error("User not found"); |
| 112 | + } |
| 113 | + |
| 114 | + const token = signToken({ userId: user.id }); |
| 115 | + |
| 116 | + const data = { |
| 117 | + user: { |
| 118 | + id: user.id, |
| 119 | + ename: user.ename, |
| 120 | + isVerified: user.isVerified, |
| 121 | + isPrivate: user.isPrivate, |
| 122 | + }, |
| 123 | + token, |
| 124 | + }; |
| 125 | + this.eventEmitter.emit(session, data); |
| 126 | + res.status(200).json(data); |
| 127 | + } catch (error) { |
| 128 | + console.error("Error during login:", error); |
| 129 | + res.status(500).json({ error: "Internal server error" }); |
| 130 | + } |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | + |
0 commit comments