Hub Python 库文档

OAuth 和 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 添加 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}!"}
您可能还对[一个演示 OAuth 实际应用的实用示例](https://huggingface.co/spaces/Wauplin/fastapi-oauth/blob/main/app.py)感兴趣。有关更全面的实现,请查看[medoidai/GiveBackGPT](https://huggingface.co/spaces/medoidai/GiveBackGPT) Space,它在一个全面的应用程序中实现了 Hugging Face 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 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 登录的信息。

< > 在 GitHub 上更新