修复用户提供的 密钥太短导致无法保存授权的问题

This commit is contained in:
mouyong
2025-07-23 22:34:26 +08:00
committed by GitHub
parent ef8e0eb1e1
commit 33561bcc80

View File

@@ -21,9 +21,14 @@ const OAUTH_SCOPES = ['https://www.googleapis.com/auth/cloud-platform'];
// 加密相关常量 // 加密相关常量
const ALGORITHM = 'aes-256-cbc'; const ALGORITHM = 'aes-256-cbc';
const ENCRYPTION_KEY = Buffer.from(config.security.encryptionKey, 'hex'); const ENCRYPTION_SALT = 'gemini-account-salt';
const IV_LENGTH = 16; const IV_LENGTH = 16;
// 生成加密密钥(使用与 claudeAccountService 相同的方法)
function generateEncryptionKey() {
return crypto.scryptSync(config.security.encryptionKey, ENCRYPTION_SALT, 32);
}
// Gemini 账户键前缀 // Gemini 账户键前缀
const GEMINI_ACCOUNT_KEY_PREFIX = 'gemini_account:'; const GEMINI_ACCOUNT_KEY_PREFIX = 'gemini_account:';
const SHARED_GEMINI_ACCOUNTS_KEY = 'shared_gemini_accounts'; const SHARED_GEMINI_ACCOUNTS_KEY = 'shared_gemini_accounts';
@@ -32,8 +37,9 @@ const ACCOUNT_SESSION_MAPPING_PREFIX = 'gemini_session_account_mapping:';
// 加密函数 // 加密函数
function encrypt(text) { function encrypt(text) {
if (!text) return ''; if (!text) return '';
const key = generateEncryptionKey();
const iv = crypto.randomBytes(IV_LENGTH); const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv); const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
let encrypted = cipher.update(text); let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]); encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex'); return iv.toString('hex') + ':' + encrypted.toString('hex');
@@ -43,10 +49,11 @@ function encrypt(text) {
function decrypt(text) { function decrypt(text) {
if (!text) return ''; if (!text) return '';
try { try {
const key = generateEncryptionKey();
const textParts = text.split(':'); const textParts = text.split(':');
const iv = Buffer.from(textParts.shift(), 'hex'); const iv = Buffer.from(textParts.shift(), 'hex');
const encryptedText = Buffer.from(textParts.join(':'), 'hex'); const encryptedText = Buffer.from(textParts.join(':'), 'hex');
const decipher = crypto.createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv); const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
let decrypted = decipher.update(encryptedText); let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]); decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString(); return decrypted.toString();
@@ -670,4 +677,4 @@ module.exports = {
isTokenExpired, isTokenExpired,
OAUTH_CLIENT_ID, OAUTH_CLIENT_ID,
OAUTH_SCOPES OAUTH_SCOPES
}; };