Hub Python 庫文件
OAuth 和 FastAPI
並獲得增強的文件體驗
開始使用
OAuth 和 FastAPI
OAuth 是一種開放的訪問授權標準,通常用於在不暴露使用者憑據的情況下,授予應用程式對使用者資訊的有限訪問許可權。當與 FastAPI 結合使用時,它允許您構建安全的 API,讓使用者可以使用 Google 或 GitHub 等外部身份提供者進行登入。在一般情況下,
- FastAPI 將定義 API 端點並處理 HTTP 請求。
- OAuth 使用諸如 fastapi.security 或 Authlib 等外部工具的庫進行整合。
- 當用戶想要登入時,FastAPI 將他們重定向到 OAuth 提供者的登入頁面。
- 成功登入後,提供者將帶著令牌重定向回來。
- FastAPI 驗證此令牌並使用它來授權使用者或獲取使用者配置檔案資料。
這種方法有助於避免直接處理密碼,並將身份管理交給受信任的提供者。
在 FastAPI 中整合 Hugging Face OAuth
此模組提供了將 Hugging Face OAuth 整合到 FastAPI 應用程式的工具。它支援使用 Hugging Face 平臺進行使用者身份驗證,包括本地開發用的模擬行為和用於 Spaces 的真實 OAuth 流程。
OAuth 概覽
attach_huggingface_oauth
函式將登入、登出和回撥端點新增到您的 FastAPI 應用程式。在 Space 中使用時,它會連線到 Hugging Face OAuth 系統。在本地使用時,它將注入一個模擬使用者。點選此處瞭解更多關於向您的 Space 新增 Hugging Face 登入選項。
如何使用?
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}!"}
attach_huggingface_oauth
向 FastAPI 應用程式新增 OAuth 端點以啟用 Hugging Face OAuth 登入。
如何使用
- 在您的 FastAPI 應用程式上呼叫此方法以新增 OAuth 端點。
- 在您的路由處理程式內部,呼叫
parse_huggingface_oauth(request)
以檢索 OAuth 資訊。 - 如果使用者已登入,將返回一個包含使用者資訊OAuthInfo物件。如果未登入,則返回
None
。 - 在您的應用程式中,請確保新增指向
/oauth/huggingface/login
和/oauth/huggingface/logout
的連結,以便使用者登入和登出。
示例
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}!"}
parse_huggingface_oauth
以 OAuthInfo 物件的形式返回已登入使用者的資訊。
為了靈活性和麵向未來,此方法在解析時非常寬鬆,不會引發錯誤。缺失的欄位將設定為 None
,而不發出警告。
如果使用者未登入(會話 cookie 中沒有資訊),則返回 None
。
有關如何使用此方法的示例,請參見 attach_huggingface_oauth()。
OAuthOrgInfo
class huggingface_hub.OAuthOrgInfo
< 來源 >( 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 )
引數
- sub (
str
) — 組織的唯一識別符號。OpenID Connect 欄位。 - name (
str
) — 組織的完整名稱。OpenID Connect 欄位。 - preferred_username (
str
) — 組織的使用者名稱。OpenID Connect 欄位。 - picture (
str
) — 組織的個人資料圖片 URL。OpenID Connect 欄位。 - is_enterprise (
bool
) — 組織是否為企業組織。Hugging Face 欄位。 - can_pay (
Optional[bool]
, 可選) — 組織是否設定了支付方式。Hugging Face 欄位。 - role_in_org (
Optional[str]
, 可選) — 使用者在組織中的角色。Hugging Face 欄位。 - security_restrictions (
Optional[List[Literal["ip", "token-policy", "mfa", "sso"]]]
, 可選) — 使用者未完成的此組織的安全限制陣列。可能的值:"ip", "token-policy", "mfa", "sso"。Hugging Face 欄位。
與使用 OAuth 登入的使用者相關聯的組織資訊。
OAuthUserInfo
class huggingface_hub.OAuthUserInfo
< 來源 >( 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]] )
引數
- sub (
str
) — 使用者的唯一識別符號,即使改名也如此。OpenID Connect 欄位。 - name (
str
) — 使用者的全名。OpenID Connect 欄位。 - preferred_username (
str
) — 使用者的使用者名稱。OpenID Connect 欄位。 - email_verified (
Optional[bool]
, 可選) — 指示使用者的電子郵件是否已驗證。OpenID Connect 欄位。 - email (
Optional[str]
, 可選) — 使用者的電子郵件地址。OpenID Connect 欄位。 - picture (
str
) — 使用者的個人資料圖片 URL。OpenID Connect 欄位。 - profile (
str
) — 使用者的個人資料 URL。OpenID Connect 欄位。 - website (
Optional[str]
, 可選) — 使用者的網站 URL。OpenID Connect 欄位。 - is_pro (
bool
) — 使用者是否是專業使用者。Hugging Face 欄位。 - can_pay (
Optional[bool]
, 可選) — 使用者是否設定了支付方式。Hugging Face 欄位。 - orgs (
Optional[List[OrgInfo]]
, 可選) — 使用者所屬的組織列表。Hugging Face 欄位。
關於透過 OAuth 登入的使用者的資訊。
OAuthInfo
class huggingface_hub.OAuthInfo
< 來源 >( access_token: str access_token_expires_at: datetime user_info: OAuthUserInfo state: typing.Optional[str] scope: str )
引數
- access_token (
str
) — 訪問令牌。 - access_token_expires_at (
datetime.datetime
) — 訪問令牌的過期日期。 - user_info (OAuthUserInfo) — 使用者資訊。
- state (
str
, 可選) — 原始請求中傳遞給 OAuth 提供商的狀態。 - scope (
str
) — 授予的範圍。
關於 OAuth 登入的資訊。