29 lines
1.0 KiB
JavaScript
29 lines
1.0 KiB
JavaScript
const { hmacSha256 } = require('../utils/hmac');
|
|
|
|
/**
|
|
* 生成转发到 Python 后端时所需的内部服务鉴权 Header
|
|
* @param {string} userId - 当前登录用户 ID
|
|
* @returns {Object} 需要附加到请求上的 Headers
|
|
*/
|
|
function buildInternalHeaders(userId, userInfo = {}) {
|
|
const service = 'java-gateway';
|
|
const timestamp = String(Date.now());
|
|
const message = `${service}:${userId}:${timestamp}`;
|
|
const signature = hmacSha256(process.env.INTERNAL_SERVICE_SECRET, message);
|
|
|
|
return {
|
|
'X-Internal-Service': service,
|
|
'X-Internal-User-Id': userId,
|
|
'X-Internal-Timestamp': timestamp,
|
|
'X-Internal-Signature': signature,
|
|
'X-User-Name': encodeURIComponent(userInfo.name || ''),
|
|
'X-User-Sex': userInfo.sex || '',
|
|
'X-User-Is-Driver': String(userInfo.isDriver || false),
|
|
'X-User-Dept-Id': String(userInfo.deptId || 0),
|
|
'X-User-Dept-Name': encodeURIComponent(userInfo.deptName || ''),
|
|
'X-User-Role-List': encodeURIComponent(JSON.stringify(userInfo.roleList || [])),
|
|
};
|
|
}
|
|
|
|
module.exports = { buildInternalHeaders };
|