auth.ts105 lines, 2.5 KB
| 1 | import { Request, Response, NextFunction } from "express"; |
| 2 | import jwt from "jsonwebtoken"; |
| 3 | import { createErrorResponse } from "../../../shared/utils"; |
| 4 | |
| 5 | // Extend Express Request interface to include user |
| 6 | declare global { |
| 7 | namespace Express { |
| 8 | interface Request { |
| 9 | user?: any; |
| 10 | } |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Public routes that don't require authentication |
| 16 | */ |
| 17 | const publicRoutes = [ |
| 18 | "/health", |
| 19 | "/status", |
| 20 | "/", |
| 21 | "/api/auth/register", |
| 22 | "/api/auth/login", |
| 23 | "/api/auth/refresh", |
| 24 | ]; |
| 25 | |
| 26 | /** |
| 27 | * Check if a route is public (doesn't require authentication) |
| 28 | */ |
| 29 | export function isPublicRoute(path: string): boolean { |
| 30 | return publicRoutes.some((route) => { |
| 31 | if (route.endsWith("*")) { |
| 32 | return path.startsWith(route.slice(0, -1)); |
| 33 | } |
| 34 | return path === route || path.startsWith(route + "/"); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * JWT Authentication Middleware for API Gateway |
| 40 | */ |
| 41 | export function gatewayAuth(req: Request, res: Response, next: NextFunction) { |
| 42 | // Skip authentication for public routes |
| 43 | if (isPublicRoute(req.path)) { |
| 44 | return next(); |
| 45 | } |
| 46 | |
| 47 | const authHeader = req.headers["authorization"]; |
| 48 | const token = authHeader && authHeader.split(" ")[1]; // Bearer TOKEN |
| 49 | |
| 50 | if (!token) { |
| 51 | return res.status(401).json(createErrorResponse("Access token required")); |
| 52 | } |
| 53 | |
| 54 | const jwtSecret = process.env.JWT_SECRET; |
| 55 | if (!jwtSecret) { |
| 56 | console.error("JWT_SECRET not configured in API Gateway"); |
| 57 | return res |
| 58 | .status(500) |
| 59 | .json(createErrorResponse("Server configuration error")); |
| 60 | } |
| 61 | |
| 62 | jwt.verify(token, jwtSecret, (err: any, decoded: any) => { |
| 63 | if (err) { |
| 64 | return res |
| 65 | .status(403) |
| 66 | .json(createErrorResponse("Invalid or expired token")); |
| 67 | } |
| 68 | |
| 69 | // Add user info to request for forwarding to services |
| 70 | req.user = decoded; |
| 71 | |
| 72 | // Add user info to headers for service communication |
| 73 | req.headers["x-user-id"] = decoded.userId; |
| 74 | req.headers["x-user-email"] = decoded.email; |
| 75 | |
| 76 | next(); |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Optional Authentication Middleware |
| 82 | * Adds user to request if token is valid, but doesn't require it |
| 83 | */ |
| 84 | export function optionalAuth(req: Request, res: Response, next: NextFunction) { |
| 85 | const authHeader = req.headers["authorization"]; |
| 86 | const token = authHeader && authHeader.split(" ")[1]; |
| 87 | |
| 88 | if (!token) { |
| 89 | return next(); |
| 90 | } |
| 91 | |
| 92 | const jwtSecret = process.env.JWT_SECRET; |
| 93 | if (!jwtSecret) { |
| 94 | return next(); |
| 95 | } |
| 96 | |
| 97 | jwt.verify(token, jwtSecret, (err: any, decoded: any) => { |
| 98 | if (!err) { |
| 99 | req.user = decoded; |
| 100 | req.headers["x-user-id"] = decoded.userId; |
| 101 | req.headers["x-user-email"] = decoded.email; |
| 102 | } |
| 103 | next(); |
| 104 | }); |
| 105 | } |