Skip to content

Commit ad0bb49

Browse files
Merge branch 'main' into main
2 parents 1caaf5a + 5713342 commit ad0bb49

File tree

10 files changed

+752
-15
lines changed

10 files changed

+752
-15
lines changed

.github/conda/meta.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ requirements:
2121
- typing-extensions
2222
- packaging
2323
- pyyaml
24-
- hf-xet >=1.1.0,<2.0.0
2524
run:
2625
- python
2726
- pip
@@ -31,7 +30,6 @@ requirements:
3130
- typing-extensions
3231
- packaging
3332
- pyyaml
34-
- hf-xet >=1.1.0,<2.0.0
3533

3634
test:
3735
imports:

.github/workflows/python-tests.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,13 @@ jobs:
9494
sudo apt install -y graphviz
9595
uv pip install "huggingface_hub[tensorflow-testing] @ ."
9696
;;
97+
98+
"Xet only")
99+
uv pip install "huggingface_hub[hf_xet] @ ."
100+
;;
97101
98102
esac
99103
100-
# If not "Xet only", we want to test upload/download with regular LFS workflow
101-
# => uninstall hf_xet to make sure we are not using it.
102-
if [[ "${{ matrix.test_name }}" != "Xet only" ]]; then
103-
uv pip uninstall hf_xet
104-
fi
105104
# Run tests
106105
- name: Run tests
107106
working-directory: ./src # For code coverage to work

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@
8686
title: Webhooks server
8787
- local: package_reference/serialization
8888
title: Serialization
89+
- local: package_reference/oauth
90+
title: OAuth
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!--⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
2+
rendered properly in your Markdown viewer.
3+
-->
4+
5+
6+
# OAuth and FastAPI
7+
8+
OAuth is an open standard for access delegation, commonly used to grant applications limited access to a user's information without exposing their credentials. When combined with FastAPI it allows you to build secure APIs that allow users to log in using external identity providers like Google or GitHub.
9+
In a usual scenario:
10+
- FastAPI will define the API endpoints and handles the HTTP requests.
11+
- OAuth is integrated using libraries like fastapi.security or external tools like Authlib.
12+
- When a user wants to log in, FastAPI redirects them to the OAuth provider’s login page.
13+
- After successful login, the provider redirects back with a token.
14+
- FastAPI verifies this token and uses it to authorize the user or fetch user profile data.
15+
16+
This approach helps avoid handling passwords directly and offloads identity management to trusted providers.
17+
18+
# Hugging Face OAuth Integration in FastAPI
19+
20+
This module provides tools to integrate Hugging Face OAuth into a FastAPI application. It enables user authentication using the Hugging Face platform including mocked behavior for local development and real OAuth flow for Spaces.
21+
22+
23+
24+
## OAuth Overview
25+
26+
The `attach_huggingface_oauth` function adds login, logout, and callback endpoints to your FastAPI app. When used in a Space, it connects to the Hugging Face OAuth system. When used locally it will inject a mocked user. Click here to learn more about [adding a Sign-In with HF option to your Space](https://huggingface.co/docs/hub/en/spaces-oauth)
27+
28+
29+
### How to use it?
30+
31+
```python
32+
from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
33+
from fastapi import FastAPI, Request
34+
35+
app = FastAPI()
36+
attach_huggingface_oauth(app)
37+
38+
@app.get("/")
39+
def greet_json(request: Request):
40+
oauth_info = parse_huggingface_oauth(request)
41+
if oauth_info is None:
42+
return {"msg": "Not logged in!"}
43+
return {"msg": f"Hello, {oauth_info.user_info.preferred_username}!"}
44+
```
45+
46+
<Tip>
47+
You might also be interested in [a practical example that demonstrates OAuth in action](https://huggingface.co/spaces/medoidai/GiveBackGPT/blob/main/src/main.py)
48+
</Tip>
49+
50+
### attach_huggingface_oauth
51+
52+
[[autodoc]] attach_huggingface_oauth
53+
54+
### parse_huggingface_oauth
55+
56+
[[autodoc]] parse_huggingface_oauth
57+
58+
### OAuthOrgInfo
59+
60+
[[autodoc]] OAuthOrgInfo
61+
62+
### OAuthUserInfo
63+
64+
[[autodoc]] OAuthUserInfo
65+
66+
### OAuthInfo
67+
68+
[[autodoc]] OAuthInfo

setup.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def get_version() -> str:
1414
install_requires = [
1515
"filelock",
1616
"fsspec>=2023.5.0",
17-
"hf-xet>=1.1.0,<2.0.0; platform_machine=='x86_64' or platform_machine=='amd64' or platform_machine=='arm64' or platform_machine=='aarch64'",
1817
"packaging>=20.9",
1918
"pyyaml>=5.1",
2019
"requests",
@@ -32,6 +31,13 @@ def get_version() -> str:
3231
"aiohttp", # for AsyncInferenceClient
3332
]
3433

34+
extras["oauth"] = [
35+
"authlib>=1.3.2", # minimum version to include https://github.com/lepture/authlib/pull/644
36+
"fastapi",
37+
"httpx", # required for authlib but not included in its dependencies
38+
"itsdangerous", # required for starlette SessionMiddleware
39+
]
40+
3541
extras["torch"] = [
3642
"torch",
3743
"safetensors[torch]",
@@ -56,11 +62,12 @@ def get_version() -> str:
5662
"keras<3.0",
5763
]
5864

59-
extras["hf_xet"] = ["hf_xet>=1.1.0,<2.0.0"]
65+
extras["hf_xet"] = ["hf-xet>=1.1.1,<2.0.0"]
6066

6167
extras["testing"] = (
6268
extras["cli"]
6369
+ extras["inference"]
70+
+ extras["oauth"]
6471
+ [
6572
"jedi",
6673
"Jinja2",

src/huggingface_hub/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@
7070
"logout",
7171
"notebook_login",
7272
],
73+
"_oauth": [
74+
"OAuthInfo",
75+
"OAuthOrgInfo",
76+
"OAuthUserInfo",
77+
"attach_huggingface_oauth",
78+
"parse_huggingface_oauth",
79+
],
7380
"_snapshot_download": [
7481
"snapshot_download",
7582
],
@@ -641,6 +648,9 @@
641648
"ModelCardData",
642649
"ModelHubMixin",
643650
"ModelInfo",
651+
"OAuthInfo",
652+
"OAuthOrgInfo",
653+
"OAuthUserInfo",
644654
"ObjectDetectionBoundingBox",
645655
"ObjectDetectionInput",
646656
"ObjectDetectionOutputElement",
@@ -762,6 +772,7 @@
762772
"add_collection_item",
763773
"add_space_secret",
764774
"add_space_variable",
775+
"attach_huggingface_oauth",
765776
"auth_check",
766777
"auth_list",
767778
"auth_switch",
@@ -862,6 +873,7 @@
862873
"move_repo",
863874
"notebook_login",
864875
"paper_info",
876+
"parse_huggingface_oauth",
865877
"parse_safetensors_file_metadata",
866878
"pause_inference_endpoint",
867879
"pause_space",
@@ -1026,6 +1038,13 @@ def __dir__():
10261038
logout, # noqa: F401
10271039
notebook_login, # noqa: F401
10281040
)
1041+
from ._oauth import (
1042+
OAuthInfo, # noqa: F401
1043+
OAuthOrgInfo, # noqa: F401
1044+
OAuthUserInfo, # noqa: F401
1045+
attach_huggingface_oauth, # noqa: F401
1046+
parse_huggingface_oauth, # noqa: F401
1047+
)
10291048
from ._snapshot_download import snapshot_download # noqa: F401
10301049
from ._space_api import (
10311050
SpaceHardware, # noqa: F401

0 commit comments

Comments
 (0)