gcloud-sonar-ai
Version:
A production-ready NPM package that provides a Perplexity Sonar alternative using Google Cloud Vertex AI and Gemini models.
45 lines • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthManager = void 0;
const google_auth_library_1 = require("google-auth-library");
class AuthManager {
constructor(apiKey, projectId) {
this.apiKey = apiKey;
if (projectId || process.env.GOOGLE_CLOUD_PROJECT) {
this.auth = new google_auth_library_1.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
projectId: projectId || process.env.GOOGLE_CLOUD_PROJECT,
});
}
}
async getAuthHeaders() {
if (this.apiKey) {
return { 'Authorization': `Bearer ${this.apiKey}` };
}
if (this.auth) {
try {
const client = await this.auth.getClient();
const token = await client.getAccessToken();
if (!token || !token.token) {
throw new Error('Failed to obtain access token');
}
return { 'Authorization': `Bearer ${token.token}` };
}
catch (error) {
throw new Error(`Authentication failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
throw new Error('No authentication method available. Provide either apiKey or configure Google Cloud credentials.');
}
async validateAuth() {
try {
await this.getAuthHeaders();
return true;
}
catch {
return false;
}
}
}
exports.AuthManager = AuthManager;
//# sourceMappingURL=auth.js.map