2025-06-19 16:52:22 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2026-05-13 13:35:21 -04:00
|
|
|
import { AuthType, loadApiKey } from '@google/gemini-cli-core';
|
2025-08-28 13:52:25 -04:00
|
|
|
import { loadEnvironment, loadSettings } from './settings.js';
|
2026-05-29 01:33:01 +00:00
|
|
|
import fs from 'node:fs';
|
2025-06-19 16:52:22 -07:00
|
|
|
|
2026-05-13 13:35:21 -04:00
|
|
|
export async function validateAuthMethod(
|
|
|
|
|
authMethod: string,
|
2026-05-29 01:33:01 +00:00
|
|
|
experimentalByoid?: boolean,
|
2026-05-13 13:35:21 -04:00
|
|
|
): Promise<string | null> {
|
2026-05-29 01:33:01 +00:00
|
|
|
const settings = loadSettings();
|
|
|
|
|
loadEnvironment(settings.merged, process.cwd());
|
2025-07-07 15:52:04 -07:00
|
|
|
if (
|
|
|
|
|
authMethod === AuthType.LOGIN_WITH_GOOGLE ||
|
2025-11-14 11:39:11 -05:00
|
|
|
authMethod === AuthType.COMPUTE_ADC
|
2025-07-07 15:52:04 -07:00
|
|
|
) {
|
2025-06-19 16:52:22 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authMethod === AuthType.USE_GEMINI) {
|
2026-05-13 13:35:21 -04:00
|
|
|
const key = process.env['GEMINI_API_KEY'] || (await loadApiKey());
|
|
|
|
|
if (!key) {
|
2025-11-20 20:57:59 -08:00
|
|
|
return (
|
|
|
|
|
'When using Gemini API, you must specify the GEMINI_API_KEY environment variable.\n' +
|
|
|
|
|
'Update your environment and try again (no reload needed if using .env)!'
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-06-19 16:52:22 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authMethod === AuthType.USE_VERTEX_AI) {
|
2025-06-22 17:30:58 -07:00
|
|
|
const hasVertexProjectLocationConfig =
|
2025-08-17 12:43:21 -04:00
|
|
|
!!process.env['GOOGLE_CLOUD_PROJECT'] &&
|
|
|
|
|
!!process.env['GOOGLE_CLOUD_LOCATION'];
|
|
|
|
|
const hasGoogleApiKey = !!process.env['GOOGLE_API_KEY'];
|
2025-06-22 17:30:58 -07:00
|
|
|
if (!hasVertexProjectLocationConfig && !hasGoogleApiKey) {
|
|
|
|
|
return (
|
2025-07-08 09:37:10 -07:00
|
|
|
'When using Vertex AI, you must specify either:\n' +
|
2025-06-22 17:30:58 -07:00
|
|
|
'• GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION environment variables.\n' +
|
|
|
|
|
'• GOOGLE_API_KEY environment variable (if using express mode).\n' +
|
2025-07-08 09:37:10 -07:00
|
|
|
'Update your environment and try again (no reload needed if using .env)!'
|
2025-06-22 17:30:58 -07:00
|
|
|
);
|
2025-06-19 16:52:22 -07:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 01:33:01 +00:00
|
|
|
if (authMethod === AuthType.BYOID) {
|
|
|
|
|
const isByoidEnabled =
|
|
|
|
|
experimentalByoid || settings.merged.experimental?.byoid;
|
|
|
|
|
if (!isByoidEnabled) {
|
|
|
|
|
return 'BYOID authentication is experimental and must be enabled via experimental.byoid in settings.';
|
|
|
|
|
}
|
|
|
|
|
const configPath = settings.merged.security.auth.byoidConfigPath;
|
|
|
|
|
if (!configPath) {
|
|
|
|
|
return (
|
|
|
|
|
'When using BYOID, you must specify the security.auth.byoidConfigPath setting.\n' +
|
|
|
|
|
'Update your settings and try again!'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(configPath)) {
|
|
|
|
|
return `BYOID configuration file not found at: ${configPath}`;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 16:52:22 -07:00
|
|
|
return 'Invalid auth method selected.';
|
2025-08-28 13:52:25 -04:00
|
|
|
}
|