@coretext-ai/qa-google-contacts-a58500d5-8331-4ce9-a140-d204a9fae815
Version:
MCP server with google-contacts integration
62 lines • 2.23 kB
JavaScript
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
export class CredentialLoader {
/**
* Load OAuth credentials from file
*/
async loadCredentials(credentialFilePath) {
try {
const resolvedPath = this.resolvePath(credentialFilePath);
if (!await fs.pathExists(resolvedPath)) {
throw new Error(`Credential file not found: ${resolvedPath}`);
}
const content = await fs.readFile(resolvedPath, 'utf-8');
const credentialData = JSON.parse(content);
return this.extractCredentials(credentialData);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(`Failed to load credentials: ${errorMessage}`);
throw new Error(`Credential loading failed: ${errorMessage}`);
}
}
/**
* Extract credentials from various credential file formats
*/
extractCredentials(credentialData) {
if (credentialData.installed) {
return credentialData.installed;
}
else if (credentialData.web) {
return credentialData.web;
}
else {
throw new Error('Invalid credential file format. Must contain "installed" or "web" configuration.');
}
}
/**
* Resolve credential file path (handle environment variables and relative paths)
*/
resolvePath(filePath) {
// Handle environment variable references
if (filePath.startsWith('$')) {
const envVar = filePath.substring(1);
const envPath = process.env[envVar];
if (!envPath) {
throw new Error(`Environment variable ${envVar} not set`);
}
return path.resolve(envPath);
}
// Handle tilde expansion
if (filePath.startsWith('~')) {
return path.resolve(os.homedir(), filePath.substring(2));
}
// Handle relative paths
if (!path.isAbsolute(filePath)) {
return path.resolve(process.cwd(), filePath);
}
return filePath;
}
}
//# sourceMappingURL=credential-loader.js.map