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. In a usual scenario:
This approach helps avoid handling passwords directly and offloads identity management to trusted providers.
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.
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
from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
from fastapi import FastAPI, Request
app = FastAPI()
attach_huggingface_oauth(app)
@app.get("/")
def greet_json(request: Request):
oauth_info = parse_huggingface_oauth(request)
if oauth_info is None:
return {"msg": "Not logged in!"}
return {"msg": f"Hello, {oauth_info.user_info.preferred_username}!"}( app: fastapi.FastAPI route_prefix: str = '/' )
Add OAuth endpoints to a FastAPI app to enable OAuth login with Hugging Face.
How to use:
parse_huggingface_oauth(request) to retrieve the OAuth info.None is returned./oauth/huggingface/login and /oauth/huggingface/logout for the user to log in and out.Example:
from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
# Create a FastAPI app
app = FastAPI()
# Add OAuth endpoints to the FastAPI app
attach_huggingface_oauth(app)
# Add a route that greets the user if they are logged in
@app.get("/")
def greet_json(request: Request):
# Retrieve the OAuth info from the request
oauth_info = parse_huggingface_oauth(request) # e.g. OAuthInfo dataclass
if oauth_info is None:
return {"msg": "Not logged in!"}
return {"msg": f"Hello, {oauth_info.user_info.preferred_username}!"}Returns the information from a logged in user as a OAuthInfo object.
For flexibility and future-proofing, this method is very lax in its parsing and does not raise errors.
Missing fields are set to None without a warning.
Return None, if the user is not logged in (no info in session cookie).
See attach_huggingface_oauth() for an example on how to use this method.
( sub: str name: str preferred_username: str picture: str is_enterprise: bool can_pay: typing.Optional[bool] = None role_in_org: typing.Optional[str] = None security_restrictions: typing.Optional[typing.List[typing.Literal['ip', 'token-policy', 'mfa', 'sso']]] = None )
Parameters
str) —
Unique identifier for the org. OpenID Connect field. str) —
The org’s full name. OpenID Connect field. str) —
The org’s username. OpenID Connect field. str) —
The org’s profile picture URL. OpenID Connect field. bool) —
Whether the org is an enterprise org. Hugging Face field. Optional[bool], optional) —
Whether the org has a payment method set up. Hugging Face field. Optional[str], optional) —
The user’s role in the org. Hugging Face field. Optional[List[Literal["ip", "token-policy", "mfa", "sso"]]], optional) —
Array of security restrictions that the user hasn’t completed for this org. Possible values: “ip”, “token-policy”, “mfa”, “sso”. Hugging Face field. Information about an organization linked to a user logged in with OAuth.
( sub: str name: str preferred_username: str email_verified: typing.Optional[bool] email: typing.Optional[str] picture: str profile: str website: typing.Optional[str] is_pro: bool can_pay: typing.Optional[bool] orgs: typing.Optional[typing.List[huggingface_hub._oauth.OAuthOrgInfo]] )
Parameters
str) —
Unique identifier for the user, even in case of rename. OpenID Connect field. str) —
The user’s full name. OpenID Connect field. str) —
The user’s username. OpenID Connect field. Optional[bool], optional) —
Indicates if the user’s email is verified. OpenID Connect field. Optional[str], optional) —
The user’s email address. OpenID Connect field. str) —
The user’s profile picture URL. OpenID Connect field. str) —
The user’s profile URL. OpenID Connect field. Optional[str], optional) —
The user’s website URL. OpenID Connect field. bool) —
Whether the user is a pro user. Hugging Face field. Optional[bool], optional) —
Whether the user has a payment method set up. Hugging Face field. Optional[List[OrgInfo]], optional) —
List of organizations the user is part of. Hugging Face field. Information about a user logged in with OAuth.
( access_token: str access_token_expires_at: datetime user_info: OAuthUserInfo state: typing.Optional[str] scope: str )
Parameters
str) —
The access token. datetime.datetime) —
The expiration date of the access token. str, optional) —
State passed to the OAuth provider in the original request to the OAuth provider. str) —
Granted scope. Information about the OAuth login.