mirror of
https://github.com/openai/codex.git
synced 2026-11-30 00:10:49 +00:00
validate api key before login success
This commit is contained in:
@@ -9,6 +9,7 @@ pub use auth::unauthenticated_auth_provider;
|
||||
pub use bearer_auth_provider::BearerAuthProvider;
|
||||
pub use bearer_auth_provider::BearerAuthProvider as CoreAuthProvider;
|
||||
pub use codex_protocol::account::ProviderAccount;
|
||||
pub use models_endpoint::validate_api_key_with_models_endpoint;
|
||||
pub use provider::ModelProvider;
|
||||
pub use provider::ProviderAccountError;
|
||||
pub use provider::ProviderAccountResult;
|
||||
|
||||
@@ -16,6 +16,7 @@ use codex_login::CodexAuth;
|
||||
use codex_login::collect_auth_env_telemetry;
|
||||
use codex_login::default_client::build_reqwest_client;
|
||||
use codex_model_provider_info::ModelProviderInfo;
|
||||
use codex_models_manager::client_version_to_whole;
|
||||
use codex_models_manager::manager::ModelsEndpointClient;
|
||||
use codex_otel::TelemetryAuthMode;
|
||||
use codex_protocol::error::CodexErr;
|
||||
@@ -36,6 +37,7 @@ const MODELS_ENDPOINT: &str = "/models";
|
||||
pub(crate) struct OpenAiModelsEndpoint {
|
||||
provider_info: ModelProviderInfo,
|
||||
auth_manager: Option<Arc<AuthManager>>,
|
||||
auth_override: Option<CodexAuth>,
|
||||
}
|
||||
|
||||
impl OpenAiModelsEndpoint {
|
||||
@@ -46,10 +48,23 @@ impl OpenAiModelsEndpoint {
|
||||
Self {
|
||||
provider_info,
|
||||
auth_manager,
|
||||
auth_override: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_auth(provider_info: ModelProviderInfo, auth: CodexAuth) -> Self {
|
||||
Self {
|
||||
provider_info,
|
||||
auth_manager: None,
|
||||
auth_override: Some(auth),
|
||||
}
|
||||
}
|
||||
|
||||
async fn auth(&self) -> Option<CodexAuth> {
|
||||
if let Some(auth) = self.auth_override.as_ref() {
|
||||
return Some(auth.clone());
|
||||
}
|
||||
|
||||
match self.auth_manager.as_ref() {
|
||||
Some(auth_manager) => auth_manager.auth().await,
|
||||
None => None,
|
||||
@@ -65,6 +80,16 @@ impl OpenAiModelsEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn validate_api_key_with_models_endpoint(
|
||||
provider_info: ModelProviderInfo,
|
||||
api_key: &str,
|
||||
) -> CoreResult<()> {
|
||||
OpenAiModelsEndpoint::with_auth(provider_info, CodexAuth::from_api_key(api_key))
|
||||
.list_models(&client_version_to_whole())
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ModelsEndpointClient for OpenAiModelsEndpoint {
|
||||
fn has_command_auth(&self) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user