Files
crm-ai/bridge/src/auth.js
T
2026-10-23 10:35:28 +08:00

24 lines
652 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.
'use strict';
const crypto = require('crypto');
/**
* 工具服务入口鉴权:LibreDesk 在工具的 Headers 配置里带 X-Tools-Key。
* 未配置 key 时 fail-closed(返回 enabled=false,server 会直接 503)。
*/
function createAuther(expectedKey) {
const key = String(expectedKey || '');
const expected = Buffer.from(key);
function verify(value) {
if (!key) return false;
const given = Buffer.from(String(value || ''));
if (given.length !== expected.length) return false;
return crypto.timingSafeEqual(given, expected);
}
return { enabled: key.length > 0, verify };
}
module.exports = { createAuther };