mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-21 02:40:14 +01:00
🔑 feat: Base64 Google Service Keys and Reliable Private Key Formats (#8385)
This commit is contained in:
parent
8523074e87
commit
19320f2296
2 changed files with 126 additions and 4 deletions
|
|
@ -29,8 +29,20 @@ export async function loadServiceKey(keyPath: string): Promise<GoogleServiceKey
|
|||
|
||||
let serviceKey: unknown;
|
||||
|
||||
// Check if it's base64 encoded (common pattern for storing in env vars)
|
||||
if (keyPath.trim().match(/^[A-Za-z0-9+/]+=*$/)) {
|
||||
try {
|
||||
const decoded = Buffer.from(keyPath.trim(), 'base64').toString('utf-8');
|
||||
// Try to parse the decoded string as JSON
|
||||
serviceKey = JSON.parse(decoded);
|
||||
} catch {
|
||||
// Not base64 or not valid JSON after decoding, continue with other methods
|
||||
// Silent failure - not critical
|
||||
}
|
||||
}
|
||||
|
||||
// Check if it's a stringified JSON (starts with '{')
|
||||
if (keyPath.trim().startsWith('{')) {
|
||||
if (!serviceKey && keyPath.trim().startsWith('{')) {
|
||||
try {
|
||||
serviceKey = JSON.parse(keyPath);
|
||||
} catch (error) {
|
||||
|
|
@ -39,7 +51,7 @@ export async function loadServiceKey(keyPath: string): Promise<GoogleServiceKey
|
|||
}
|
||||
}
|
||||
// Check if it's a URL
|
||||
else if (/^https?:\/\//.test(keyPath)) {
|
||||
else if (!serviceKey && /^https?:\/\//.test(keyPath)) {
|
||||
try {
|
||||
const response = await axios.get(keyPath);
|
||||
serviceKey = response.data;
|
||||
|
|
@ -47,7 +59,7 @@ export async function loadServiceKey(keyPath: string): Promise<GoogleServiceKey
|
|||
logger.error(`Failed to fetch the service key from URL: ${keyPath}`, error);
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
} else if (!serviceKey) {
|
||||
// It's a file path
|
||||
try {
|
||||
const absolutePath = path.isAbsolute(keyPath) ? keyPath : path.resolve(keyPath);
|
||||
|
|
@ -75,5 +87,30 @@ export async function loadServiceKey(keyPath: string): Promise<GoogleServiceKey
|
|||
return null;
|
||||
}
|
||||
|
||||
return serviceKey as GoogleServiceKey;
|
||||
// Fix private key formatting if needed
|
||||
const key = serviceKey as GoogleServiceKey;
|
||||
if (key.private_key && typeof key.private_key === 'string') {
|
||||
// Replace escaped newlines with actual newlines
|
||||
// When JSON.parse processes "\\n", it becomes "\n" (single backslash + n)
|
||||
// When JSON.parse processes "\n", it becomes an actual newline character
|
||||
key.private_key = key.private_key.replace(/\\n/g, '\n');
|
||||
|
||||
// Also handle the String.raw`\n` case mentioned in Stack Overflow
|
||||
key.private_key = key.private_key.split(String.raw`\n`).join('\n');
|
||||
|
||||
// Ensure proper PEM format
|
||||
if (!key.private_key.includes('\n')) {
|
||||
// If no newlines are present, try to format it properly
|
||||
const privateKeyMatch = key.private_key.match(
|
||||
/^(-----BEGIN [A-Z ]+-----)(.*)(-----END [A-Z ]+-----)$/,
|
||||
);
|
||||
if (privateKeyMatch) {
|
||||
const [, header, body, footer] = privateKeyMatch;
|
||||
// Add newlines after header and before footer
|
||||
key.private_key = `${header}\n${body}\n${footer}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue