Update verify.js
Browse files
verify.js
CHANGED
|
@@ -1,49 +1,9 @@
|
|
| 1 |
-
// verify.js
|
| 2 |
|
| 3 |
const manager = require('../lib/manager');
|
| 4 |
|
| 5 |
const verify = async (req, res, next) => {
|
| 6 |
-
|
| 7 |
-
console.log(`[AUTH_MIDDLEWARE] Request Headers: ${JSON.stringify(req.headers)}`);
|
| 8 |
-
|
| 9 |
-
const apiKeyHeaderValue = req.headers['x-api-key']; // Get the raw header value
|
| 10 |
-
|
| 11 |
-
if (!apiKeyHeaderValue) {
|
| 12 |
-
console.log('[AUTH_MIDDLEWARE] Unauthorized: x-api-key header is missing.');
|
| 13 |
-
return res.status(401).json({
|
| 14 |
-
error: {
|
| 15 |
-
message: 'Unauthorized: x-api-key header is missing.',
|
| 16 |
-
type: 'authentication_error',
|
| 17 |
-
code: 'api_key_missing'
|
| 18 |
-
}
|
| 19 |
-
});
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
let apiKey = apiKeyHeaderValue; // Initialize apiKey with the full header value
|
| 23 |
-
|
| 24 |
-
// Check if the header value starts with "Bearer " (case-insensitive) and remove it
|
| 25 |
-
if (apiKeyHeaderValue.toLowerCase().startsWith('bearer ')) {
|
| 26 |
-
apiKey = apiKeyHeaderValue.substring(7); // "Bearer " is 7 characters long
|
| 27 |
-
console.log(`[AUTH_MIDDLEWARE] "Bearer " prefix found and stripped from x-api-key. Effective API key: "${apiKey}"`);
|
| 28 |
-
} else {
|
| 29 |
-
console.log(`[AUTH_MIDDLEWARE] No "Bearer " prefix found in x-api-key. Using as is: "${apiKeyHeaderValue}"`);
|
| 30 |
-
// apiKey remains apiKeyHeaderValue
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
console.log(`[AUTH_MIDDLEWARE] Processed x-api-key for comparison: "${apiKey}"`);
|
| 34 |
-
|
| 35 |
-
const expectedToken = process.env.AUTH_TOKEN;
|
| 36 |
-
|
| 37 |
-
if (!expectedToken) {
|
| 38 |
-
console.error('[AUTH_MIDDLEWARE] CRITICAL: AUTH_TOKEN environment variable is not set on the server!');
|
| 39 |
-
return res.status(500).json({
|
| 40 |
-
error: {
|
| 41 |
-
message: 'Internal Server Error: Authentication token not configured.',
|
| 42 |
-
type: 'server_error',
|
| 43 |
-
code: 'auth_token_not_set'
|
| 44 |
-
}
|
| 45 |
-
});
|
| 46 |
-
}
|
| 47 |
|
| 48 |
if (apiKey === expectedToken) {
|
| 49 |
console.log('[AUTH_MIDDLEWARE] API key verification successful.');
|
|
@@ -60,27 +20,25 @@ const verify = async (req, res, next) => {
|
|
| 60 |
}
|
| 61 |
});
|
| 62 |
}
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
next();
|
| 65 |
} catch (error) {
|
| 66 |
-
console.error('[AUTH_MIDDLEWARE] Error while getting account:', error);
|
| 67 |
return res.status(503).json({
|
| 68 |
error: {
|
| 69 |
message: '服务暂时不可用 (Service temporarily unavailable)',
|
| 70 |
type: 'service_unavailable',
|
| 71 |
-
code: '
|
| 72 |
}
|
| 73 |
});
|
| 74 |
}
|
| 75 |
} else {
|
| 76 |
-
|
| 77 |
-
return res.status(401).json({
|
| 78 |
-
error: {
|
| 79 |
-
message: 'Unauthorized: Invalid API Key.',
|
| 80 |
-
type: 'authentication_error',
|
| 81 |
-
code: 'invalid_api_key'
|
| 82 |
-
}
|
| 83 |
-
});
|
| 84 |
}
|
| 85 |
};
|
| 86 |
|
|
|
|
| 1 |
+
// verify.js (带有详细日志的版本)
|
| 2 |
|
| 3 |
const manager = require('../lib/manager');
|
| 4 |
|
| 5 |
const verify = async (req, res, next) => {
|
| 6 |
+
// ... (之前的 API key 验证逻辑) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
if (apiKey === expectedToken) {
|
| 9 |
console.log('[AUTH_MIDDLEWARE] API key verification successful.');
|
|
|
|
| 20 |
}
|
| 21 |
});
|
| 22 |
}
|
| 23 |
+
// 修改这一行,避免 stringify 整个可能包含循环引用的对象
|
| 24 |
+
if (req.account.email) { // 假设 email 是一个你想记录的安全属性
|
| 25 |
+
console.log(`[AUTH_MIDDLEWARE] Account retrieved successfully. User Email: ${req.account.email}`);
|
| 26 |
+
} else {
|
| 27 |
+
console.log(`[AUTH_MIDDLEWARE] Account object retrieved successfully (details not logged to avoid circular structure error).`);
|
| 28 |
+
}
|
| 29 |
next();
|
| 30 |
} catch (error) {
|
| 31 |
+
console.error('[AUTH_MIDDLEWARE] Error while getting account (could be during retrieval or logging):', error); // 错误可能发生在这里或之前的 stringify
|
| 32 |
return res.status(503).json({
|
| 33 |
error: {
|
| 34 |
message: '服务暂时不可用 (Service temporarily unavailable)',
|
| 35 |
type: 'service_unavailable',
|
| 36 |
+
code: 'internal_error_account_retrieval_or_logging' // 更精确的错误码
|
| 37 |
}
|
| 38 |
});
|
| 39 |
}
|
| 40 |
} else {
|
| 41 |
+
// ... (无效 API key 的逻辑) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
};
|
| 44 |
|