uv_auth/
lib.rs

1use std::sync::{Arc, LazyLock};
2
3use tracing::trace;
4
5use uv_redacted::DisplaySafeUrl;
6
7use crate::credentials::Authentication;
8pub use access_token::AccessToken;
9use cache::CredentialsCache;
10pub use credentials::{Credentials, Username};
11pub use index::{AuthPolicy, Index, Indexes};
12pub use keyring::KeyringProvider;
13pub use middleware::AuthMiddleware;
14pub use pyx::{
15    DEFAULT_TOLERANCE_SECS, PyxJwt, PyxOAuthTokens, PyxTokenStore, PyxTokens, TokenStoreError,
16};
17pub use realm::{Realm, RealmRef};
18pub use service::{Service, ServiceParseError};
19pub use store::{AuthBackend, AuthScheme, TextCredentialStore, TomlCredentialError};
20
21mod access_token;
22mod cache;
23mod credentials;
24mod index;
25mod keyring;
26mod middleware;
27mod providers;
28mod pyx;
29mod realm;
30mod service;
31mod store;
32
33// TODO(zanieb): Consider passing a cache explicitly throughout
34
35/// Global authentication cache for a uv invocation
36///
37/// This is used to share credentials across uv clients.
38pub(crate) static CREDENTIALS_CACHE: LazyLock<CredentialsCache> =
39    LazyLock::new(CredentialsCache::default);
40
41/// Populate the global authentication store with credentials on a URL, if there are any.
42///
43/// Returns `true` if the store was updated.
44pub fn store_credentials_from_url(url: &DisplaySafeUrl) -> bool {
45    if let Some(credentials) = Credentials::from_url(url) {
46        trace!("Caching credentials for {url}");
47        CREDENTIALS_CACHE.insert(url, Arc::new(Authentication::from(credentials)));
48        true
49    } else {
50        false
51    }
52}
53
54/// Populate the global authentication store with credentials on a URL, if there are any.
55///
56/// Returns `true` if the store was updated.
57pub fn store_credentials(url: &DisplaySafeUrl, credentials: Credentials) {
58    trace!("Caching credentials for {url}");
59    CREDENTIALS_CACHE.insert(url, Arc::new(Authentication::from(credentials)));
60}