2026-05-10 13:12:44 +08:00

39 lines
868 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* API鉴权中间件
* Phase A简单Token鉴权
* Token通过环境变量 BRAIN_API_TOKEN 配置
*/
module.exports = function authMiddleware(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({
error: true,
code: 'UNAUTHORIZED',
message: 'Missing or invalid Authorization header'
});
}
const token = authHeader.slice(7);
const expectedToken = process.env.BRAIN_API_TOKEN;
if (!expectedToken) {
return res.status(500).json({
error: true,
code: 'CONFIG_ERROR',
message: 'BRAIN_API_TOKEN not configured on server'
});
}
if (token !== expectedToken) {
return res.status(403).json({
error: true,
code: 'FORBIDDEN',
message: 'Invalid API token'
});
}
next();
};