auth: use the globally constructed client builder (#16979)

## Summary

Instead of each subcommand instantiating its own `BaseClientBuilder`,
let's use the globally constructed one.


## Test Plan

Existing tests.
This commit is contained in:
Zsolt Dollenstein 2025-12-04 20:04:33 +00:00 committed by GitHub
parent 0c5391a7c7
commit fb5de2228c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 104 deletions

View File

@ -12,7 +12,7 @@ use uv_preview::{Preview, PreviewFeatures};
use uv_redacted::DisplaySafeUrl; use uv_redacted::DisplaySafeUrl;
use uv_warnings::warn_user; use uv_warnings::warn_user;
use crate::{commands::ExitStatus, printer::Printer, settings::NetworkSettings}; use crate::{commands::ExitStatus, printer::Printer};
/// Request format for the Bazel credential helper protocol. /// Request format for the Bazel credential helper protocol.
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -60,8 +60,8 @@ impl TryFrom<Credentials> for BazelCredentialResponse {
async fn credentials_for_url( async fn credentials_for_url(
url: &DisplaySafeUrl, url: &DisplaySafeUrl,
client_builder: BaseClientBuilder<'_>,
preview: Preview, preview: Preview,
network_settings: &NetworkSettings,
) -> Result<Option<Credentials>> { ) -> Result<Option<Credentials>> {
let pyx_store = PyxTokenStore::from_settings()?; let pyx_store = PyxTokenStore::from_settings()?;
@ -85,16 +85,9 @@ async fn credentials_for_url(
.unwrap_or(url.to_string()) .unwrap_or(url.to_string())
); );
} }
let client = BaseClientBuilder::new( let client = client_builder
network_settings.connectivity, .auth_integration(uv_client::AuthIntegration::NoAuthMiddleware)
network_settings.native_tls, .build();
network_settings.allow_insecure_host.clone(),
preview,
network_settings.timeout,
network_settings.retries,
)
.auth_integration(uv_client::AuthIntegration::NoAuthMiddleware)
.build();
let token = pyx_store let token = pyx_store
.access_token(client.for_host(pyx_store.api()).raw_client(), 0) .access_token(client.for_host(pyx_store.api()).raw_client(), 0)
.await .await
@ -123,8 +116,8 @@ async fn credentials_for_url(
/// ///
/// Full spec is [available here](https://github.com/bazelbuild/proposals/blob/main/designs/2022-06-07-bazel-credential-helpers.md) /// Full spec is [available here](https://github.com/bazelbuild/proposals/blob/main/designs/2022-06-07-bazel-credential-helpers.md)
pub(crate) async fn helper( pub(crate) async fn helper(
client_builder: BaseClientBuilder<'_>,
preview: Preview, preview: Preview,
network_settings: &NetworkSettings,
printer: Printer, printer: Printer,
) -> Result<ExitStatus> { ) -> Result<ExitStatus> {
if !preview.is_enabled(PreviewFeatures::AUTH_HELPER) { if !preview.is_enabled(PreviewFeatures::AUTH_HELPER) {
@ -138,7 +131,7 @@ pub(crate) async fn helper(
// TODO: make this logic generic over the protocol by providing `request.uri` from a // TODO: make this logic generic over the protocol by providing `request.uri` from a
// trait - that should help with adding new protocols // trait - that should help with adding new protocols
let credentials = credentials_for_url(&request.uri, preview, network_settings).await?; let credentials = credentials_for_url(&request.uri, client_builder, preview).await?;
let response = serde_json::to_string( let response = serde_json::to_string(
&credentials &credentials

View File

@ -17,7 +17,6 @@ use uv_preview::Preview;
use crate::commands::ExitStatus; use crate::commands::ExitStatus;
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::NetworkSettings;
// We retry no more than this many times when polling for login status. // We retry no more than this many times when polling for login status.
const STATUS_RETRY_LIMIT: u32 = 60; const STATUS_RETRY_LIMIT: u32 = 60;
@ -28,7 +27,7 @@ pub(crate) async fn login(
username: Option<String>, username: Option<String>,
password: Option<String>, password: Option<String>,
token: Option<String>, token: Option<String>,
network_settings: &NetworkSettings, client_builder: BaseClientBuilder<'_>,
printer: Printer, printer: Printer,
preview: Preview, preview: Preview,
) -> Result<ExitStatus> { ) -> Result<ExitStatus> {
@ -41,16 +40,9 @@ pub(crate) async fn login(
bail!("Cannot specify a password when logging in to pyx"); bail!("Cannot specify a password when logging in to pyx");
} }
let client = BaseClientBuilder::new( let client = client_builder
network_settings.connectivity, .auth_integration(AuthIntegration::NoAuthMiddleware)
network_settings.native_tls, .build();
network_settings.allow_insecure_host.clone(),
preview,
network_settings.timeout,
network_settings.retries,
)
.auth_integration(AuthIntegration::NoAuthMiddleware)
.build();
let access_token = pyx_login_with_browser(&pyx_store, &client, &printer).await?; let access_token = pyx_login_with_browser(&pyx_store, &client, &printer).await?;
let jwt = PyxJwt::decode(&access_token)?; let jwt = PyxJwt::decode(&access_token)?;

View File

@ -9,7 +9,6 @@ use uv_distribution_types::IndexUrl;
use uv_pep508::VerbatimUrl; use uv_pep508::VerbatimUrl;
use uv_preview::Preview; use uv_preview::Preview;
use crate::settings::NetworkSettings;
use crate::{commands::ExitStatus, printer::Printer}; use crate::{commands::ExitStatus, printer::Printer};
/// Logout from a service. /// Logout from a service.
@ -18,13 +17,13 @@ use crate::{commands::ExitStatus, printer::Printer};
pub(crate) async fn logout( pub(crate) async fn logout(
service: Service, service: Service,
username: Option<String>, username: Option<String>,
network_settings: &NetworkSettings, client_builder: BaseClientBuilder<'_>,
printer: Printer, printer: Printer,
preview: Preview, preview: Preview,
) -> Result<ExitStatus> { ) -> Result<ExitStatus> {
let pyx_store = PyxTokenStore::from_settings()?; let pyx_store = PyxTokenStore::from_settings()?;
if pyx_store.is_known_domain(service.url()) { if pyx_store.is_known_domain(service.url()) {
return pyx_logout(&pyx_store, network_settings, printer, preview).await; return pyx_logout(&pyx_store, client_builder, printer).await;
} }
let backend = AuthBackend::from_settings(preview).await?; let backend = AuthBackend::from_settings(preview).await?;
@ -93,20 +92,11 @@ pub(crate) async fn logout(
/// Log out via the [`PyxTokenStore`], invalidating the existing tokens. /// Log out via the [`PyxTokenStore`], invalidating the existing tokens.
async fn pyx_logout( async fn pyx_logout(
store: &PyxTokenStore, store: &PyxTokenStore,
network_settings: &NetworkSettings, client_builder: BaseClientBuilder<'_>,
printer: Printer, printer: Printer,
preview: Preview,
) -> Result<ExitStatus> { ) -> Result<ExitStatus> {
// Initialize the client. // Initialize the client.
let client = BaseClientBuilder::new( let client = client_builder.build();
network_settings.connectivity,
network_settings.native_tls,
network_settings.allow_insecure_host.clone(),
preview,
network_settings.timeout,
network_settings.retries,
)
.build();
// Retrieve the token store. // Retrieve the token store.
let Some(tokens) = store.read().await? else { let Some(tokens) = store.read().await? else {

View File

@ -11,13 +11,12 @@ use uv_preview::Preview;
use crate::commands::ExitStatus; use crate::commands::ExitStatus;
use crate::commands::auth::login; use crate::commands::auth::login;
use crate::printer::Printer; use crate::printer::Printer;
use crate::settings::NetworkSettings;
/// Show the token that will be used for a service. /// Show the token that will be used for a service.
pub(crate) async fn token( pub(crate) async fn token(
service: Service, service: Service,
username: Option<String>, username: Option<String>,
network_settings: &NetworkSettings, client_builder: BaseClientBuilder<'_>,
printer: Printer, printer: Printer,
preview: Preview, preview: Preview,
) -> Result<ExitStatus> { ) -> Result<ExitStatus> {
@ -26,16 +25,9 @@ pub(crate) async fn token(
if username.is_some() { if username.is_some() {
bail!("Cannot specify a username when logging in to pyx"); bail!("Cannot specify a username when logging in to pyx");
} }
let client = BaseClientBuilder::new( let client = client_builder
network_settings.connectivity, .auth_integration(AuthIntegration::NoAuthMiddleware)
network_settings.native_tls, .build();
network_settings.allow_insecure_host.clone(),
preview,
network_settings.timeout,
network_settings.retries,
)
.auth_integration(AuthIntegration::NoAuthMiddleware)
.build();
pyx_refresh(&pyx_store, &client, printer).await?; pyx_refresh(&pyx_store, &client, printer).await?;
return Ok(ExitStatus::Success); return Ok(ExitStatus::Success);

View File

@ -479,12 +479,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
command: AuthCommand::Login(args), command: AuthCommand::Login(args),
}) => { }) => {
// Resolve the settings from the command-line arguments and workspace configuration. // Resolve the settings from the command-line arguments and workspace configuration.
let args = settings::AuthLoginSettings::resolve( let args = settings::AuthLoginSettings::resolve(args);
args,
&cli.top_level.global_args,
filesystem.as_ref(),
&environment,
);
show_settings!(args); show_settings!(args);
commands::auth_login( commands::auth_login(
@ -492,7 +487,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
args.username, args.username,
args.password, args.password,
args.token, args.token,
&args.network_settings, client_builder,
printer, printer,
globals.preview, globals.preview,
) )
@ -502,18 +497,13 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
command: AuthCommand::Logout(args), command: AuthCommand::Logout(args),
}) => { }) => {
// Resolve the settings from the command-line arguments and workspace configuration. // Resolve the settings from the command-line arguments and workspace configuration.
let args = settings::AuthLogoutSettings::resolve( let args = settings::AuthLogoutSettings::resolve(args);
args,
&cli.top_level.global_args,
filesystem.as_ref(),
&environment,
);
show_settings!(args); show_settings!(args);
commands::auth_logout( commands::auth_logout(
args.service, args.service,
args.username, args.username,
&args.network_settings, client_builder,
printer, printer,
globals.preview, globals.preview,
) )
@ -523,18 +513,13 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
command: AuthCommand::Token(args), command: AuthCommand::Token(args),
}) => { }) => {
// Resolve the settings from the command-line arguments and workspace configuration. // Resolve the settings from the command-line arguments and workspace configuration.
let args = settings::AuthTokenSettings::resolve( let args = settings::AuthTokenSettings::resolve(args);
args,
&cli.top_level.global_args,
filesystem.as_ref(),
&environment,
);
show_settings!(args); show_settings!(args);
commands::auth_token( commands::auth_token(
args.service, args.service,
args.username, args.username,
&args.network_settings, client_builder,
printer, printer,
globals.preview, globals.preview,
) )
@ -558,7 +543,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
match args.command { match args.command {
AuthHelperCommand::Get => { AuthHelperCommand::Get => {
commands::auth_helper(globals.preview, &globals.network_settings, printer).await commands::auth_helper(client_builder, globals.preview, printer).await
} }
} }
} }

View File

@ -3877,23 +3877,14 @@ impl PublishSettings {
pub(crate) struct AuthLogoutSettings { pub(crate) struct AuthLogoutSettings {
pub(crate) service: Service, pub(crate) service: Service,
pub(crate) username: Option<String>, pub(crate) username: Option<String>,
// Both CLI and configuration.
pub(crate) network_settings: NetworkSettings,
} }
impl AuthLogoutSettings { impl AuthLogoutSettings {
/// Resolve the [`AuthLogoutSettings`] from the CLI and filesystem configuration. /// Resolve the [`AuthLogoutSettings`] from the CLI and filesystem configuration.
pub(crate) fn resolve( pub(crate) fn resolve(args: AuthLogoutArgs) -> Self {
args: AuthLogoutArgs,
global_args: &GlobalArgs,
filesystem: Option<&FilesystemOptions>,
environment: &EnvironmentOptions,
) -> Self {
Self { Self {
service: args.service, service: args.service,
username: args.username, username: args.username,
network_settings: NetworkSettings::resolve(global_args, filesystem, environment),
} }
} }
} }
@ -3903,23 +3894,14 @@ impl AuthLogoutSettings {
pub(crate) struct AuthTokenSettings { pub(crate) struct AuthTokenSettings {
pub(crate) service: Service, pub(crate) service: Service,
pub(crate) username: Option<String>, pub(crate) username: Option<String>,
// Both CLI and configuration.
pub(crate) network_settings: NetworkSettings,
} }
impl AuthTokenSettings { impl AuthTokenSettings {
/// Resolve the [`AuthTokenSettings`] from the CLI and filesystem configuration. /// Resolve the [`AuthTokenSettings`] from the CLI and filesystem configuration.
pub(crate) fn resolve( pub(crate) fn resolve(args: AuthTokenArgs) -> Self {
args: AuthTokenArgs,
global_args: &GlobalArgs,
filesystem: Option<&FilesystemOptions>,
environment: &EnvironmentOptions,
) -> Self {
Self { Self {
service: args.service, service: args.service,
username: args.username, username: args.username,
network_settings: NetworkSettings::resolve(global_args, filesystem, environment),
} }
} }
} }
@ -3931,25 +3913,16 @@ pub(crate) struct AuthLoginSettings {
pub(crate) username: Option<String>, pub(crate) username: Option<String>,
pub(crate) password: Option<String>, pub(crate) password: Option<String>,
pub(crate) token: Option<String>, pub(crate) token: Option<String>,
// Both CLI and configuration.
pub(crate) network_settings: NetworkSettings,
} }
impl AuthLoginSettings { impl AuthLoginSettings {
/// Resolve the [`AuthLoginSettings`] from the CLI and filesystem configuration. /// Resolve the [`AuthLoginSettings`] from the CLI and filesystem configuration.
pub(crate) fn resolve( pub(crate) fn resolve(args: AuthLoginArgs) -> Self {
args: AuthLoginArgs,
global_args: &GlobalArgs,
filesystem: Option<&FilesystemOptions>,
environment: &EnvironmentOptions,
) -> Self {
Self { Self {
service: args.service, service: args.service,
username: args.username, username: args.username,
password: args.password, password: args.password,
token: args.token, token: args.token,
network_settings: NetworkSettings::resolve(global_args, filesystem, environment),
} }
} }
} }