Skip to content

Commit 0c7d276

Browse files
committed
Add HF provider
1 parent 2c8e394 commit 0c7d276

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

crates/uv-auth/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod credentials;
1515
mod index;
1616
mod keyring;
1717
mod middleware;
18+
mod providers;
1819
mod realm;
1920

2021
// TODO(zanieb): Consider passing a cache explicitly throughout

crates/uv-auth/src/middleware.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use reqwest::{Request, Response};
77
use reqwest_middleware::{Error, Middleware, Next};
88
use tracing::{debug, trace, warn};
99

10+
use crate::providers::HuggingFaceProvider;
1011
use crate::{
1112
CREDENTIALS_CACHE, CredentialsCache, KeyringProvider,
1213
cache::FetchUrl,
@@ -457,9 +458,8 @@ impl AuthMiddleware {
457458
Some(credentials)
458459
};
459460

460-
return self
461-
.complete_request(credentials, request, extensions, next, auth_policy)
462-
.await;
461+
self.complete_request(credentials, request, extensions, next, auth_policy)
462+
.await
463463
}
464464

465465
/// Fetch credentials for a URL.
@@ -503,6 +503,13 @@ impl AuthMiddleware {
503503
return credentials;
504504
}
505505

506+
// Support for known providers, like Hugging Face.
507+
if let Some(credentials) = HuggingFaceProvider::credentials_for(url).map(Arc::new) {
508+
debug!("Found Hugging Face credentials for {url}");
509+
self.cache().fetches.done(key, Some(credentials.clone()));
510+
return Some(credentials);
511+
}
512+
506513
// Netrc support based on: <https://github.com/gribouille/netrc>.
507514
let credentials = if let Some(credentials) = self.netrc.get().and_then(|netrc| {
508515
debug!("Checking netrc for credentials for {url}");

crates/uv-auth/src/providers.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::sync::LazyLock;
2+
3+
use url::Url;
4+
5+
use uv_static::EnvVars;
6+
7+
use crate::Credentials;
8+
use crate::realm::Realm;
9+
10+
/// The [`Realm`] for the Hugging Face platform.
11+
static HUGGING_FACE_REALM: LazyLock<Realm> = LazyLock::new(|| {
12+
let url = Url::parse("https://huggingface.co").expect("Failed to parse Hugging Face URL");
13+
Realm::from(&url)
14+
});
15+
16+
/// The authentication token for the Hugging Face platform, if set.
17+
static HUGGING_FACE_TOKEN: LazyLock<Option<Vec<u8>>> = LazyLock::new(|| {
18+
if std::env::var_os(EnvVars::UV_NO_HF_TOKEN).is_some() {
19+
return None;
20+
}
21+
std::env::var(EnvVars::HF_TOKEN)
22+
.map(String::into_bytes)
23+
.ok()
24+
});
25+
26+
/// A provider for authentication credentials for the Hugging Face platform.
27+
#[derive(Debug, Clone, PartialEq, Eq)]
28+
pub(crate) struct HuggingFaceProvider;
29+
30+
impl HuggingFaceProvider {
31+
/// Returns the credentials for the Hugging Face platform, if available.
32+
pub(crate) fn credentials_for(url: &Url) -> Option<Credentials> {
33+
if Realm::from(url) == *HUGGING_FACE_REALM {
34+
if let Some(token) = HUGGING_FACE_TOKEN.as_ref() {
35+
return Some(Credentials::Bearer {
36+
token: token.clone(),
37+
});
38+
}
39+
}
40+
None
41+
}
42+
}

crates/uv-static/src/env_vars.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,11 @@ impl EnvVars {
765765

766766
/// Disable GitHub-specific requests that allow uv to skip `git fetch` in some circumstances.
767767
pub const UV_NO_GITHUB_FAST_PATH: &'static str = "UV_NO_GITHUB_FAST_PATH";
768+
769+
/// Authentication token for Hugging Face requests. When set, uv will use this token
770+
/// when making requests to `https://huggingface.co/` and any subdomains.
771+
pub const HF_TOKEN: &'static str = "HF_TOKEN";
772+
773+
/// Disable Hugging Face authentication, even if `HF_TOKEN` is set.
774+
pub const UV_NO_HF_TOKEN: &'static str = "UV_NO_HF_TOKEN";
768775
}

docs/reference/environment.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ Ignore `.env` files when executing `uv run` commands.
252252

253253
Disable GitHub-specific requests that allow uv to skip `git fetch` in some circumstances.
254254

255+
### `UV_NO_HF_TOKEN`
256+
257+
Disable Hugging Face authentication, even if `HF_TOKEN` is set.
258+
255259
### `UV_NO_INSTALLER_METADATA`
256260

257261
Skip writing `uv` installer metadata files (e.g., `INSTALLER`, `REQUESTED`, and `direct_url.json`) to site-packages `.dist-info` directories.
@@ -528,6 +532,11 @@ See [force-color.org](https://force-color.org).
528532

529533
Used for trusted publishing via `uv publish`.
530534

535+
### `HF_TOKEN`
536+
537+
Authentication token for Hugging Face requests. When set, uv will use this token
538+
when making requests to `https://huggingface.co/` and any subdomains.
539+
531540
### `HOME`
532541

533542
The standard `HOME` env var.

0 commit comments

Comments
 (0)