Update verify.js
Browse files
verify.js
CHANGED
|
@@ -1,37 +1,81 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
const verify = async (req, res, next) => {
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
if (!apiKey) {
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
}
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try {
|
| 10 |
-
|
|
|
|
| 11 |
if (!req.account) {
|
| 12 |
-
|
|
|
|
| 13 |
error: {
|
| 14 |
-
message: '服务暂时不可用,无法获取有效账户',
|
| 15 |
type: 'service_unavailable',
|
| 16 |
code: 'account_unavailable'
|
| 17 |
}
|
| 18 |
-
})
|
| 19 |
}
|
| 20 |
-
|
| 21 |
-
next()
|
| 22 |
} catch (error) {
|
| 23 |
-
console.error('
|
| 24 |
-
return res.status(503).json({
|
| 25 |
error: {
|
| 26 |
-
message: '服务暂时不可用',
|
| 27 |
type: 'service_unavailable',
|
| 28 |
-
code: '
|
| 29 |
}
|
| 30 |
-
})
|
| 31 |
}
|
| 32 |
} else {
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
-
}
|
| 36 |
|
| 37 |
-
module.exports = verify
|
|
|
|
| 1 |
+
// verify.js
|
| 2 |
+
|
| 3 |
+
const manager = require('../lib/manager'); // 假设 ../lib/manager 路径相对于此文件是正确的
|
| 4 |
+
|
| 5 |
const verify = async (req, res, next) => {
|
| 6 |
+
console.log('[AUTH_MIDDLEWARE] Received request. Verifying API key...');
|
| 7 |
+
console.log(`[AUTH_MIDDLEWARE] Request Headers: ${JSON.stringify(req.headers)}`); // 打印所有请求头,方便调试
|
| 8 |
+
|
| 9 |
+
// 从 x-api-key 请求头获取 token
|
| 10 |
+
// HTTP headers are case-insensitive, but Node.js's req.headers object keys are lowercased.
|
| 11 |
+
const apiKey = req.headers['x-api-key'];
|
| 12 |
+
|
| 13 |
if (!apiKey) {
|
| 14 |
+
console.log('[AUTH_MIDDLEWARE] Unauthorized: x-api-key header is missing.');
|
| 15 |
+
return res.status(401).json({
|
| 16 |
+
error: {
|
| 17 |
+
message: 'Unauthorized: x-api-key header is missing.',
|
| 18 |
+
type: 'authentication_error',
|
| 19 |
+
code: 'api_key_missing'
|
| 20 |
+
}
|
| 21 |
+
});
|
| 22 |
}
|
| 23 |
|
| 24 |
+
console.log(`[AUTH_MIDDLEWARE] Received x-api-key: "${apiKey}"`);
|
| 25 |
+
|
| 26 |
+
// 从环境变量获取预期的 AUTH_TOKEN
|
| 27 |
+
const expectedToken = process.env.AUTH_TOKEN;
|
| 28 |
+
|
| 29 |
+
if (!expectedToken) {
|
| 30 |
+
console.error('[AUTH_MIDDLEWARE] CRITICAL: AUTH_TOKEN environment variable is not set on the server!');
|
| 31 |
+
return res.status(500).json({
|
| 32 |
+
error: {
|
| 33 |
+
message: 'Internal Server Error: Authentication token not configured.',
|
| 34 |
+
type: 'server_error',
|
| 35 |
+
code: 'auth_token_not_set'
|
| 36 |
+
}
|
| 37 |
+
});
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
if (apiKey === expectedToken) {
|
| 41 |
+
console.log('[AUTH_MIDDLEWARE] API key verification successful.');
|
| 42 |
try {
|
| 43 |
+
console.log('[AUTH_MIDDLEWARE] Attempting to get account...');
|
| 44 |
+
req.account = await manager.getAccount();
|
| 45 |
if (!req.account) {
|
| 46 |
+
console.warn('[AUTH_MIDDLEWARE] Account not found after successful API key verification.');
|
| 47 |
+
return res.status(503).json({
|
| 48 |
error: {
|
| 49 |
+
message: '服务暂时不可用,无法获取有效账户 (Service temporarily unavailable, cannot retrieve a valid account)',
|
| 50 |
type: 'service_unavailable',
|
| 51 |
code: 'account_unavailable'
|
| 52 |
}
|
| 53 |
+
});
|
| 54 |
}
|
| 55 |
+
console.log(`[AUTH_MIDDLEWARE] Account retrieved successfully. User: ${JSON.stringify(req.account)}`);
|
| 56 |
+
next();
|
| 57 |
} catch (error) {
|
| 58 |
+
console.error('[AUTH_MIDDLEWARE] Error while getting account:', error);
|
| 59 |
+
return res.status(503).json({
|
| 60 |
error: {
|
| 61 |
+
message: '服务暂时不可用 (Service temporarily unavailable)',
|
| 62 |
type: 'service_unavailable',
|
| 63 |
+
code: 'internal_error_account_retrieval'
|
| 64 |
}
|
| 65 |
+
});
|
| 66 |
}
|
| 67 |
} else {
|
| 68 |
+
// 为了安全,不要在日志中直接打印预期的 token,除非在非常受控的调试环境中
|
| 69 |
+
// 可以打印接收到的 token 和预期 token 的部分信息(例如长度或哈希值)进行比较
|
| 70 |
+
console.warn(`[AUTH_MIDDLEWARE] Unauthorized: Invalid API key. Received: "${apiKey}", Expected token (length): ${expectedToken.length}`);
|
| 71 |
+
return res.status(401).json({
|
| 72 |
+
error: {
|
| 73 |
+
message: 'Unauthorized: Invalid API Key.',
|
| 74 |
+
type: 'authentication_error',
|
| 75 |
+
code: 'invalid_api_key'
|
| 76 |
+
}
|
| 77 |
+
});
|
| 78 |
}
|
| 79 |
+
};
|
| 80 |
|
| 81 |
+
module.exports = verify;
|