feat: add OAuth provider abstraction for CLI configuration (#3157)

Signed-off-by: Adam Tarantino <tarantino.adam@gmail.com>
This commit is contained in:
Adam Tarantino
2025-08-06 08:04:49 -07:00
committed by GitHub
parent 90ef14979d
commit c2f08557af
6 changed files with 191 additions and 39 deletions
+35 -1
View File
@@ -387,7 +387,12 @@ impl Provider for GithubCopilotProvider {
GITHUB_COPILOT_DEFAULT_MODEL,
GITHUB_COPILOT_KNOWN_MODELS.to_vec(),
GITHUB_COPILOT_DOC_URL,
vec![ConfigKey::new("GITHUB_COPILOT_TOKEN", true, true, None)],
vec![ConfigKey::new_oauth(
"GITHUB_COPILOT_TOKEN",
true,
true,
None,
)],
)
}
@@ -466,4 +471,33 @@ impl Provider for GithubCopilotProvider {
models.sort();
Ok(Some(models))
}
async fn configure_oauth(&self) -> Result<(), ProviderError> {
let config = Config::global();
// Check if token already exists and is valid
if config.get_secret::<String>("GITHUB_COPILOT_TOKEN").is_ok() {
// Try to refresh API info to validate the token
match self.refresh_api_info().await {
Ok(_) => return Ok(()), // Token is valid
Err(_) => {
// Token is invalid, continue with OAuth flow
tracing::debug!("Existing token is invalid, starting OAuth flow");
}
}
}
// Start OAuth device code flow
let token = self
.get_access_token()
.await
.map_err(|e| ProviderError::Authentication(format!("OAuth flow failed: {}", e)))?;
// Save the token
config
.set_secret("GITHUB_COPILOT_TOKEN", Value::String(token))
.map_err(|e| ProviderError::ExecutionError(format!("Failed to save token: {}", e)))?;
Ok(())
}
}