Hub Python 庫文件

OAuth and FastAPI

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

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 中新增“使用 HF 登入”選項

如何使用它?

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}!"}

您可能還對一個展示 OAuth 實際應用的示例感興趣。有關更全面的實現,請檢視medoidai/GiveBackGPT Space,它在完整的應用程式中實現了 HF OAuth。

attach_huggingface_oauth

huggingface_hub.attach_huggingface_oauth

< >

( app: fastapi.FastAPI route_prefix: str = '/' )

為 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

huggingface_hub.parse_huggingface_oauth

< >

( request: fastapi.Request )

OAuthInfo 物件的形式返回已登入使用者的資訊。

為了靈活性和未來相容性,此方法解析非常寬鬆,不會引發錯誤。缺失的欄位將設定為 None,且不發出警告。

如果使用者未登入(會話 Cookie 中沒有資訊),則返回 None

請參閱 attach_huggingface_oauth() 以獲取如何使用此方法的示例。

OAuthOrgInfo

class huggingface_hub.OAuthOrgInfo

< >

( sub: str name: str preferred_username: str picture: str plan: typing.Optional[str] = None can_pay: typing.Optional[bool] = None role_in_org: typing.Optional[str] = None security_restrictions: typing.Optional[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 欄位。
  • plan (str, 可選) — 組織的計劃(例如,“enterprise”,“team”)。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[list[huggingface_hub._oauth.OAuthOrgInfo]] )

引數

  • sub (str) — 使用者的唯一識別符號,即使重新命名後也保持不變。OpenID Connect 欄位。
  • name (str) — 使用者的全名。OpenID Connect 欄位。
  • preferred_username (str) — 使用者的使用者名稱。OpenID Connect 欄位。
  • email_verified (Optional[bool], optional) — 指示使用者的電子郵件是否已驗證。OpenID Connect 欄位。
  • email (Optional[str], optional) — 使用者的電子郵件地址。OpenID Connect 欄位。
  • picture (str) — 使用者的頭像 URL。OpenID Connect 欄位。
  • profile (str) — 使用者的個人資料 URL。OpenID Connect 欄位。
  • website (Optional[str], optional) — 使用者的網站 URL。OpenID Connect 欄位。
  • is_pro (bool) — 使用者是否為 pro 使用者。Hugging Face 欄位。
  • can_pay (Optional[bool], optional) — 使用者是否設定了支付方式。Hugging Face 欄位。
  • orgs (Optional[list[OrgInfo]], optional) — 使用者所屬的組織列表。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, optional) — 傳遞給 OAuth 提供商的原始請求中的狀態。
  • scope (str) — 授予的範圍。

有關 OAuth 登入的詳細資訊。

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.