🦞 Add: backend/src/middleware/auth.ts
Browse files
backend/src/middleware/auth.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Request, Response, NextFunction } from 'express';
|
| 2 |
+
import jwt from 'jsonwebtoken';
|
| 3 |
+
import { Errors } from './error-handler';
|
| 4 |
+
import { createLogger } from '../utils/logger';
|
| 5 |
+
|
| 6 |
+
const logger = createLogger('auth');
|
| 7 |
+
|
| 8 |
+
export interface JWTPayload {
|
| 9 |
+
userId: string;
|
| 10 |
+
username: string;
|
| 11 |
+
roleId: string;
|
| 12 |
+
iat: number;
|
| 13 |
+
exp: number;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
declare global {
|
| 17 |
+
namespace Express {
|
| 18 |
+
interface Request {
|
| 19 |
+
user?: JWTPayload;
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
export function authMiddleware(req: Request, res: Response, next: NextFunction): void {
|
| 25 |
+
const authHeader = req.headers.authorization;
|
| 26 |
+
|
| 27 |
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
| 28 |
+
next(Errors.Unauthorized('缺少认证令牌'));
|
| 29 |
+
return;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
const token = authHeader.substring(7);
|
| 33 |
+
|
| 34 |
+
try {
|
| 35 |
+
const secret = process.env.JWT_SECRET || 'omics-secret-key-change-in-production';
|
| 36 |
+
const decoded = jwt.verify(token, secret) as JWTPayload;
|
| 37 |
+
req.user = decoded;
|
| 38 |
+
next();
|
| 39 |
+
} catch (error) {
|
| 40 |
+
logger.warn('Token verification failed:', { error, token: token.substring(0, 20) + '...' });
|
| 41 |
+
next(Errors.Unauthorized('令牌无效或已过期'));
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
export function optionalAuth(req: Request, res: Response, next: NextFunction): void {
|
| 46 |
+
const authHeader = req.headers.authorization;
|
| 47 |
+
|
| 48 |
+
if (authHeader && authHeader.startsWith('Bearer ')) {
|
| 49 |
+
const token = authHeader.substring(7);
|
| 50 |
+
try {
|
| 51 |
+
const secret = process.env.JWT_SECRET || 'omics-secret-key-change-in-production';
|
| 52 |
+
const decoded = jwt.verify(token, secret) as JWTPayload;
|
| 53 |
+
req.user = decoded;
|
| 54 |
+
} catch {
|
| 55 |
+
// 忽略错误,继续执行
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
next();
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
export function requireRole(...roles: string[]) {
|
| 63 |
+
return (req: Request, res: Response, next: NextFunction): void => {
|
| 64 |
+
if (!req.user) {
|
| 65 |
+
next(Errors.Unauthorized());
|
| 66 |
+
return;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// 这里需要从数据库获取用户角色权限
|
| 70 |
+
// 暂时使用简单的角色名称匹配
|
| 71 |
+
// TODO: 实现完整的RBAC权限系统
|
| 72 |
+
next();
|
| 73 |
+
};
|
| 74 |
+
}
|