summaryrefslogtreecommitdiff
path: root/packages/core/src/code_assist/oauth2.ts
diff options
context:
space:
mode:
authorTommaso Sciortino <[email protected]>2025-06-18 09:49:13 -0700
committerGitHub <[email protected]>2025-06-18 09:49:13 -0700
commit3453b977b8d200ebb5195cc2db38d89dbc2a7323 (patch)
tree6607d774db9d7aed03f30e57e35d2d57fc597596 /packages/core/src/code_assist/oauth2.ts
parent5b2cea8eda60e9cc07a82a0bb69d5f00a13bd96b (diff)
Support logging in with Application Default Credentials (#1157)
Co-authored-by: N. Taylor Mullen <[email protected]>
Diffstat (limited to 'packages/core/src/code_assist/oauth2.ts')
-rw-r--r--packages/core/src/code_assist/oauth2.ts24
1 files changed, 10 insertions, 14 deletions
diff --git a/packages/core/src/code_assist/oauth2.ts b/packages/core/src/code_assist/oauth2.ts
index 84c72fca..9e15f65b 100644
--- a/packages/core/src/code_assist/oauth2.ts
+++ b/packages/core/src/code_assist/oauth2.ts
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import { OAuth2Client } from 'google-auth-library';
+import { OAuth2Client, Credentials } from 'google-auth-library';
import * as http from 'http';
import url from 'url';
import crypto from 'crypto';
@@ -42,23 +42,12 @@ const SIGN_IN_FAILURE_URL =
const GEMINI_DIR = '.gemini';
const CREDENTIAL_FILENAME = 'oauth_creds.json';
-export async function clearCachedCredentials(): Promise<void> {
- await fs.rm(getCachedCredentialPath());
-}
-
export async function getOauthClient(): Promise<OAuth2Client> {
try {
return await getCachedCredentialClient();
} catch (_) {
const loggedInClient = await webLoginClient();
-
- await fs.mkdir(path.dirname(getCachedCredentialPath()), {
- recursive: true,
- });
- await fs.writeFile(
- getCachedCredentialPath(),
- JSON.stringify(loggedInClient.credentials, null, 2),
- );
+ await setCachedCredentials(loggedInClient.credentials);
return loggedInClient;
}
}
@@ -149,7 +138,6 @@ function getAvailablePort(): Promise<number> {
async function getCachedCredentialClient(): Promise<OAuth2Client> {
try {
const creds = await fs.readFile(getCachedCredentialPath(), 'utf-8');
-
const oAuth2Client = new OAuth2Client({
clientId: OAUTH_CLIENT_ID,
clientSecret: OAUTH_CLIENT_SECRET,
@@ -165,6 +153,14 @@ async function getCachedCredentialClient(): Promise<OAuth2Client> {
}
}
+async function setCachedCredentials(credentials: Credentials) {
+ const filePath = getCachedCredentialPath();
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
+
+ const credString = JSON.stringify(credentials, null, 2);
+ await fs.writeFile(filePath, credString);
+}
+
function getCachedCredentialPath(): string {
return path.join(os.homedir(), GEMINI_DIR, CREDENTIAL_FILENAME);
}