Skip to content

Commit c0d8f9f

Browse files
vennempclaudetusharmathamitksingh1490
authored
fix(auth): include GoogleAdc in token refresh match arm (#2486)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Tushar Mathur <tusharmath@gmail.com> Co-authored-by: Amit Singh <amitksingh1490@gmail.com>
1 parent e25c1c0 commit c0d8f9f

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

crates/forge_infra/src/auth/strategy.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ impl AuthStrategy for GoogleAdcStrategy {
383383
// authentication This ensures the user has run 'gcloud auth
384384
// application-default login'
385385
use google_cloud_auth::credentials::Builder;
386+
const VERTEX_AI_SCOPES: &[&str] =
387+
&["https://www.googleapis.com/auth/cloud-platform"];
386388
let credentials = Builder::default()
389+
.with_scopes(VERTEX_AI_SCOPES.iter().map(|s| s.to_string()))
387390
.build_access_token_credentials()
388391
.map_err(|e| {
389392
AuthError::CompletionFailed(format!(
@@ -397,7 +400,7 @@ impl AuthStrategy for GoogleAdcStrategy {
397400
.await
398401
.map_err(|e| {
399402
AuthError::CompletionFailed(format!(
400-
"{e}. Please run 'gcloud auth application-default login' to set up credentials."
403+
"Failed to obtain access token: {e}. Your ADC credentials may be expired — run 'gcloud auth application-default login' to re-authenticate."
401404
))
402405
})?;
403406

@@ -415,10 +418,13 @@ impl AuthStrategy for GoogleAdcStrategy {
415418
}
416419
}
417420

418-
async fn refresh(&self, _credential: &AuthCredential) -> anyhow::Result<AuthCredential> {
421+
async fn refresh(&self, credential: &AuthCredential) -> anyhow::Result<AuthCredential> {
419422
// Google ADC handles token refresh automatically
420423
// We just need to get a fresh token using the Builder API
424+
// Vertex AI requires the cloud-platform scope
425+
const VERTEX_AI_SCOPES: &[&str] = &["https://www.googleapis.com/auth/cloud-platform"];
421426
let credentials = Builder::default()
427+
.with_scopes(VERTEX_AI_SCOPES.iter().map(|s| s.to_string()))
422428
.build_access_token_credentials()
423429
.map_err(|e| {
424430
AuthError::RefreshFailed(format!(
@@ -427,13 +433,16 @@ impl AuthStrategy for GoogleAdcStrategy {
427433
})?;
428434

429435
let access_token = credentials.access_token().await.map_err(|e| {
430-
AuthError::RefreshFailed(format!("Failed to refresh Google access token: {e}"))
436+
AuthError::RefreshFailed(format!(
437+
"Failed to refresh Google access token: {e}. Your ADC credentials may be expired — run 'gcloud auth application-default login' to re-authenticate."
438+
))
431439
})?;
432440

433441
Ok(AuthCredential::new_google_adc(
434442
self.provider_id.clone(),
435443
ApiKey::from(access_token.token),
436-
))
444+
)
445+
.url_params(credential.url_params.clone()))
437446
}
438447
}
439448

crates/forge_services/src/provider_auth.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ where
8686
let auth_method = match &auth_context_response {
8787
AuthContextResponse::ApiKey(response) => {
8888
// Check if provider supports Google ADC and if it's the Google ADC marker
89-
if provider_id == forge_domain::ProviderId::VERTEX_AI
90-
&& response.response.api_key.as_ref() == "google_adc_marker"
91-
{
89+
let is_vertex_provider = provider_id == forge_domain::ProviderId::VERTEX_AI
90+
|| provider_id == forge_domain::ProviderId::VERTEX_AI_ANTHROPIC;
91+
if is_vertex_provider && response.response.api_key.as_ref() == "google_adc_marker" {
9292
// Vertex AI uses Google ADC
9393
forge_domain::AuthMethod::google_adc()
9494
} else {
@@ -151,7 +151,8 @@ where
151151
match auth_method {
152152
AuthMethod::OAuthDevice(_)
153153
| AuthMethod::OAuthCode(_)
154-
| AuthMethod::CodexDevice(_) => {
154+
| AuthMethod::CodexDevice(_)
155+
| AuthMethod::GoogleAdc => {
155156
// Get existing credential
156157
let existing_credential =
157158
self.infra.get_credential(&provider.id).await?.ok_or_else(

0 commit comments

Comments
 (0)