diff options
Diffstat (limited to 'packages/core/src/utils')
| -rw-r--r-- | packages/core/src/utils/user_id.test.ts | 26 | ||||
| -rw-r--r-- | packages/core/src/utils/user_id.ts | 30 |
2 files changed, 26 insertions, 30 deletions
diff --git a/packages/core/src/utils/user_id.test.ts b/packages/core/src/utils/user_id.test.ts index 185d6c95..42d8958c 100644 --- a/packages/core/src/utils/user_id.test.ts +++ b/packages/core/src/utils/user_id.test.ts @@ -5,7 +5,7 @@ */ import { describe, it, expect } from 'vitest'; -import { getInstallationId, getGoogleAccountId } from './user_id.js'; +import { getInstallationId, getGoogleAccountEmail } from './user_id.js'; describe('user_id', () => { describe('getInstallationId', () => { @@ -22,30 +22,24 @@ describe('user_id', () => { }); }); - describe('getGoogleAccountId', () => { - it('should return a non-empty string', async () => { - const result = await getGoogleAccountId(); + describe('getGoogleAccountEmail', () => { + it('should return a non-empty string', () => { + const result = getGoogleAccountEmail(); expect(result).toBeDefined(); expect(typeof result).toBe('string'); // Should be consistent on subsequent calls - const secondCall = await getGoogleAccountId(); + const secondCall = getGoogleAccountEmail(); expect(secondCall).toBe(result); }); - it('should return empty string when no Google Account ID is cached, or a valid ID when cached', async () => { - // The function can return either an empty string (if no cached ID) or a valid Google Account ID (if cached) - const googleAccountIdResult = await getGoogleAccountId(); + it('should return empty string when no Google Account email is cached', () => { + // In a clean test environment, there should be no cached Google Account email + const googleAccountEmailResult = getGoogleAccountEmail(); - expect(googleAccountIdResult).toBeDefined(); - expect(typeof googleAccountIdResult).toBe('string'); - - // Should be either empty string or a numeric string (Google Account ID) - if (googleAccountIdResult !== '') { - // If we have a cached ID, it should be a numeric string - expect(googleAccountIdResult).toMatch(/^\d+$/); - } + // They should be the same when no Google Account email is cached + expect(googleAccountEmailResult).toBe(''); }); }); }); diff --git a/packages/core/src/utils/user_id.ts b/packages/core/src/utils/user_id.ts index 42bbee35..045cbd77 100644 --- a/packages/core/src/utils/user_id.ts +++ b/packages/core/src/utils/user_id.ts @@ -8,8 +8,11 @@ import * as os from 'os'; import * as fs from 'fs'; import * as path from 'path'; import { randomUUID } from 'crypto'; +import { createRequire } from 'module'; import { GEMINI_DIR } from './paths.js'; +const require = createRequire(import.meta.url); + const homeDir = os.homedir() ?? ''; const geminiDir = path.join(homeDir, GEMINI_DIR); const installationIdFile = path.join(geminiDir, 'installation_id'); @@ -58,24 +61,23 @@ export function getInstallationId(): string { } /** - * Retrieves the obfuscated Google Account ID for the currently authenticated user. - * When OAuth is available, returns the user's cached Google Account ID. Otherwise, returns the installation ID. - * @returns A string ID for the user (Google Account ID if available, otherwise installation ID). + * Retrieves the email for the currently authenticated user. + * When OAuth is available, returns the user's cached email. Otherwise, returns an empty string. + * @returns A string email for the user (Google Account email if available, otherwise empty string). */ -export async function getGoogleAccountId(): Promise<string> { - // Try to get cached Google Account ID first +export function getGoogleAccountEmail(): string { + // Try to get cached Google Account email first try { - // Dynamic import to avoid circular dependencies - const { getCachedGoogleAccountId } = await import( - '../code_assist/oauth2.js' - ); - const googleAccountId = getCachedGoogleAccountId(); - if (googleAccountId) { - return googleAccountId; + // Dynamically import to avoid circular dependencies + // eslint-disable-next-line no-restricted-syntax + const { getCachedGoogleAccountEmail } = require('../code_assist/oauth2.js'); + const googleAccountEmail = getCachedGoogleAccountEmail(); + if (googleAccountEmail) { + return googleAccountEmail; } } catch (error) { - // If there's any error accessing Google Account ID, just return empty string - console.debug('Could not get cached Google Account ID:', error); + // If there's any error accessing Google Account email, just return empty string + console.debug('Could not get cached Google Account email:', error); } return ''; |
