Check Gemini OAuth token fields explicitly

Avoid treating unrelated credentials inside the OAuth file as a configured OAuth login.

Checks: 109 Gemini readiness tests; desktop packaging; git diff --check.
This commit is contained in:
Ryder Sun committed 2026-11-07 20:22:21 +08:00
1 parent 914f2d934b
commit 71bb4c6eda
2 files changed
+12 -2

No files matched your search

+11 -1
View File
@@ -110,6 +110,16 @@ function qwenOAuthCredentialsPresent(filename) {
} catch { return false }
}
function geminiOAuthCredentialsPresent(filename) {
try {
const contents = readCredentialFile(filename)
if (!contents) return false
const credentials = JSON.parse(contents)
return Boolean(credentials && typeof credentials === 'object' && !Array.isArray(credentials)
&& (credentialLiteral(credentials.access_token) || credentialLiteral(credentials.refresh_token)))
} catch { return false }
}
function openCodeReviewSelectedCredential(home, env, credentialSource, presetProviders) {
let config
try {
@@ -808,7 +818,7 @@ function nativeCredentialState(kind, options = {}) {
const selection = geminiNativeAuth({ home, env, workdir: options.workdir, platform: options.platform })
if (selection.uncertain) return { state: 'unknown', source: 'unverified' }
if (selection.authType === 'oauth-personal') {
return jsonContainsCredential(path.join(selection.root, 'oauth_creds.json'))
return geminiOAuthCredentialsPresent(path.join(selection.root, 'oauth_creds.json'))
? { state: 'ready', source: 'native-credential' }
: { state: 'unknown', source: 'unverified' }
}
@@ -38,7 +38,7 @@ test('Gemini readiness checks credentials for its selected auth mode', t => {
const noSelection = { GEMINI_CLI_SYSTEM_SETTINGS_PATH: path.join(home, 'system.json'), GOOGLE_API_KEY: 'unrelated' }
assert.deepEqual(nativeCredentialState('gemini', { home, env: noSelection }), { state: 'unknown', source: 'unverified' })
const env = { GEMINI_CLI_SYSTEM_SETTINGS_PATH: path.join(home, 'system.json'), GEMINI_CLI_AUTH_OVERRIDE: 'oauth-personal', GEMINI_API_KEY: 'unrelated' }
fs.unlinkSync(path.join(root, 'oauth_creds.json'))
fs.writeFileSync(path.join(root, 'oauth_creds.json'), JSON.stringify({ api_key: 'unrelated' }))
assert.deepEqual(nativeCredentialState('gemini', { home, env }), { state: 'unknown', source: 'unverified' })
fs.writeFileSync(path.join(root, 'oauth_creds.json'), JSON.stringify({ refresh_token: 'oauth-token' }))
assert.deepEqual(nativeCredentialState('gemini', { home, env }), { state: 'ready', source: 'native-credential' })