_oauth.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. import datetime
  2. import hashlib
  3. import logging
  4. import os
  5. import time
  6. import urllib.parse
  7. import warnings
  8. from dataclasses import dataclass
  9. from typing import TYPE_CHECKING, Literal
  10. from . import constants
  11. from .hf_api import whoami
  12. from .utils import experimental, get_token
  13. logger = logging.getLogger(__name__)
  14. if TYPE_CHECKING:
  15. import fastapi
  16. @dataclass
  17. class OAuthOrgInfo:
  18. """
  19. Information about an organization linked to a user logged in with OAuth.
  20. Attributes:
  21. sub (`str`):
  22. Unique identifier for the org. OpenID Connect field.
  23. name (`str`):
  24. The org's full name. OpenID Connect field.
  25. preferred_username (`str`):
  26. The org's username. OpenID Connect field.
  27. picture (`str`):
  28. The org's profile picture URL. OpenID Connect field.
  29. plan (`str`, *optional*):
  30. The org's plan (e.g., "enterprise", "team"). Hugging Face field.
  31. can_pay (`Optional[bool]`, *optional*):
  32. Whether the org has a payment method set up. Hugging Face field.
  33. role_in_org (`Optional[str]`, *optional*):
  34. The user's role in the org. Hugging Face field.
  35. security_restrictions (`Optional[list[Literal["ip", "token-policy", "mfa", "sso"]]]`, *optional*):
  36. Array of security restrictions that the user hasn't completed for this org. Possible values: "ip", "token-policy", "mfa", "sso". Hugging Face field.
  37. """
  38. sub: str
  39. name: str
  40. preferred_username: str
  41. picture: str
  42. plan: str | None = None
  43. can_pay: bool | None = None
  44. role_in_org: str | None = None
  45. security_restrictions: list[Literal["ip", "token-policy", "mfa", "sso"]] | None = None
  46. @dataclass
  47. class OAuthUserInfo:
  48. """
  49. Information about a user logged in with OAuth.
  50. Attributes:
  51. sub (`str`):
  52. Unique identifier for the user, even in case of rename. OpenID Connect field.
  53. name (`str`):
  54. The user's full name. OpenID Connect field.
  55. preferred_username (`str`):
  56. The user's username. OpenID Connect field.
  57. email_verified (`Optional[bool]`, *optional*):
  58. Indicates if the user's email is verified. OpenID Connect field.
  59. email (`Optional[str]`, *optional*):
  60. The user's email address. OpenID Connect field.
  61. picture (`str`):
  62. The user's profile picture URL. OpenID Connect field.
  63. profile (`str`):
  64. The user's profile URL. OpenID Connect field.
  65. website (`Optional[str]`, *optional*):
  66. The user's website URL. OpenID Connect field.
  67. is_pro (`bool`):
  68. Whether the user is a pro user. Hugging Face field.
  69. can_pay (`Optional[bool]`, *optional*):
  70. Whether the user has a payment method set up. Hugging Face field.
  71. orgs (`Optional[list[OrgInfo]]`, *optional*):
  72. List of organizations the user is part of. Hugging Face field.
  73. """
  74. sub: str
  75. name: str
  76. preferred_username: str
  77. email_verified: bool | None
  78. email: str | None
  79. picture: str
  80. profile: str
  81. website: str | None
  82. is_pro: bool
  83. can_pay: bool | None
  84. orgs: list[OAuthOrgInfo] | None
  85. @dataclass
  86. class OAuthInfo:
  87. """
  88. Information about the OAuth login.
  89. Attributes:
  90. access_token (`str`):
  91. The access token.
  92. access_token_expires_at (`datetime.datetime`):
  93. The expiration date of the access token.
  94. user_info ([`OAuthUserInfo`]):
  95. The user information.
  96. state (`str`, *optional*):
  97. State passed to the OAuth provider in the original request to the OAuth provider.
  98. scope (`str`):
  99. Granted scope.
  100. """
  101. access_token: str
  102. access_token_expires_at: datetime.datetime
  103. user_info: OAuthUserInfo
  104. state: str | None
  105. scope: str
  106. @experimental
  107. def attach_huggingface_oauth(app: "fastapi.FastAPI", route_prefix: str = "/"):
  108. """
  109. Add OAuth endpoints to a FastAPI app to enable OAuth login with Hugging Face.
  110. How to use:
  111. - Call this method on your FastAPI app to add the OAuth endpoints.
  112. - Inside your route handlers, call `parse_huggingface_oauth(request)` to retrieve the OAuth info.
  113. - If user is logged in, an [`OAuthInfo`] object is returned with the user's info. If not, `None` is returned.
  114. - In your app, make sure to add links to `/oauth/huggingface/login` and `/oauth/huggingface/logout` for the user to log in and out.
  115. Example:
  116. ```py
  117. from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
  118. # Create a FastAPI app
  119. app = FastAPI()
  120. # Add OAuth endpoints to the FastAPI app
  121. attach_huggingface_oauth(app)
  122. # Add a route that greets the user if they are logged in
  123. @app.get("/")
  124. def greet_json(request: Request):
  125. # Retrieve the OAuth info from the request
  126. oauth_info = parse_huggingface_oauth(request) # e.g. OAuthInfo dataclass
  127. if oauth_info is None:
  128. return {"msg": "Not logged in!"}
  129. return {"msg": f"Hello, {oauth_info.user_info.preferred_username}!"}
  130. ```
  131. """
  132. # TODO: handle generic case (handling OAuth in a non-Space environment with custom dev values) (low priority)
  133. # Add SessionMiddleware to the FastAPI app to store the OAuth info in the session.
  134. # Session Middleware requires a secret key to sign the cookies. Let's use a hash
  135. # of the OAuth secret key to make it unique to the Space + updated in case OAuth
  136. # config gets updated. When ran locally, we use an empty string as a secret key.
  137. try:
  138. from starlette.middleware.sessions import SessionMiddleware
  139. except ImportError as e:
  140. raise ImportError(
  141. "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
  142. "`huggingface_hub[oauth]` to your requirements.txt file in order to install the required dependencies."
  143. ) from e
  144. session_secret = (constants.OAUTH_CLIENT_SECRET or "") + "-v1"
  145. app.add_middleware(
  146. SessionMiddleware, # type: ignore
  147. secret_key=hashlib.sha256(session_secret.encode()).hexdigest(),
  148. same_site="none",
  149. https_only=True,
  150. ) # type: ignore
  151. # Add OAuth endpoints to the FastAPI app:
  152. # - {route_prefix}/oauth/huggingface/login
  153. # - {route_prefix}/oauth/huggingface/callback
  154. # - {route_prefix}/oauth/huggingface/logout
  155. # If the app is running in a Space, OAuth is enabled normally.
  156. # Otherwise, we mock the endpoints to make the user log in with a fake user profile - without any calls to hf.co.
  157. route_prefix = route_prefix.strip("/")
  158. if os.getenv("SPACE_ID") is not None:
  159. logger.info("OAuth is enabled in the Space. Adding OAuth routes.")
  160. _add_oauth_routes(app, route_prefix=route_prefix)
  161. else:
  162. logger.info("App is not running in a Space. Adding mocked OAuth routes.")
  163. _add_mocked_oauth_routes(app, route_prefix=route_prefix)
  164. def parse_huggingface_oauth(request: "fastapi.Request") -> OAuthInfo | None:
  165. """
  166. Returns the information from a logged-in user as a [`OAuthInfo`] object.
  167. For flexibility and future-proofing, this method is very lax in its parsing and does not raise errors.
  168. Missing fields are set to `None` without a warning.
  169. Return `None`, if the user is not logged in (no info in session cookie).
  170. See [`attach_huggingface_oauth`] for an example on how to use this method.
  171. """
  172. if "oauth_info" not in request.session:
  173. logger.debug("No OAuth info in session.")
  174. return None
  175. logger.debug("Parsing OAuth info from session.")
  176. oauth_data = request.session["oauth_info"]
  177. user_data = oauth_data.get("userinfo", {})
  178. orgs_data = user_data.get("orgs", [])
  179. orgs = (
  180. [
  181. OAuthOrgInfo(
  182. sub=org.get("sub"),
  183. name=org.get("name"),
  184. preferred_username=org.get("preferred_username"),
  185. picture=org.get("picture"),
  186. plan=org.get("plan"),
  187. can_pay=org.get("canPay"),
  188. role_in_org=org.get("roleInOrg"),
  189. security_restrictions=org.get("securityRestrictions"),
  190. )
  191. for org in orgs_data
  192. ]
  193. if orgs_data
  194. else None
  195. )
  196. user_info = OAuthUserInfo(
  197. sub=user_data.get("sub"),
  198. name=user_data.get("name"),
  199. preferred_username=user_data.get("preferred_username"),
  200. email_verified=user_data.get("email_verified"),
  201. email=user_data.get("email"),
  202. picture=user_data.get("picture"),
  203. profile=user_data.get("profile"),
  204. website=user_data.get("website"),
  205. is_pro=user_data.get("isPro"),
  206. can_pay=user_data.get("canPay"),
  207. orgs=orgs,
  208. )
  209. return OAuthInfo(
  210. access_token=oauth_data.get("access_token"),
  211. access_token_expires_at=datetime.datetime.fromtimestamp(oauth_data.get("expires_at")),
  212. user_info=user_info,
  213. state=oauth_data.get("state"),
  214. scope=oauth_data.get("scope"),
  215. )
  216. def _add_oauth_routes(app: "fastapi.FastAPI", route_prefix: str) -> None:
  217. """Add OAuth routes to the FastAPI app (login, callback handler and logout)."""
  218. try:
  219. import fastapi
  220. from authlib.integrations.base_client.errors import MismatchingStateError
  221. from authlib.integrations.starlette_client import OAuth
  222. from fastapi.responses import RedirectResponse
  223. except ImportError as e:
  224. raise ImportError(
  225. "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
  226. "`huggingface_hub[oauth]` to your requirements.txt file."
  227. ) from e
  228. # Check environment variables
  229. msg = (
  230. "OAuth is required but '{}' environment variable is not set. Make sure you've enabled OAuth in your Space by"
  231. " setting `hf_oauth: true` in the Space metadata."
  232. )
  233. if constants.OAUTH_CLIENT_ID is None:
  234. raise ValueError(msg.format("OAUTH_CLIENT_ID"))
  235. if constants.OAUTH_CLIENT_SECRET is None:
  236. raise ValueError(msg.format("OAUTH_CLIENT_SECRET"))
  237. if constants.OAUTH_SCOPES is None:
  238. raise ValueError(msg.format("OAUTH_SCOPES"))
  239. if constants.OPENID_PROVIDER_URL is None:
  240. raise ValueError(msg.format("OPENID_PROVIDER_URL"))
  241. # Register OAuth server
  242. oauth = OAuth()
  243. oauth.register(
  244. name="huggingface",
  245. client_id=constants.OAUTH_CLIENT_ID,
  246. client_secret=constants.OAUTH_CLIENT_SECRET,
  247. client_kwargs={"scope": constants.OAUTH_SCOPES},
  248. server_metadata_url=constants.OPENID_PROVIDER_URL + "/.well-known/openid-configuration",
  249. )
  250. login_uri, callback_uri, logout_uri = _get_oauth_uris(route_prefix)
  251. # Register OAuth endpoints
  252. @app.get(login_uri)
  253. async def oauth_login(request: fastapi.Request) -> RedirectResponse:
  254. """Endpoint that redirects to HF OAuth page."""
  255. redirect_uri = _generate_redirect_uri(request)
  256. return await oauth.huggingface.authorize_redirect(request, redirect_uri) # type: ignore
  257. @app.get(callback_uri)
  258. async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
  259. """Endpoint that handles the OAuth callback."""
  260. try:
  261. oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
  262. except MismatchingStateError:
  263. # Parse query params
  264. nb_redirects = int(request.query_params.get("_nb_redirects", 0))
  265. target_url = request.query_params.get("_target_url")
  266. # Build redirect URI with the same query params as before and bump nb_redirects count
  267. query_params: dict[str, int | str] = {"_nb_redirects": nb_redirects + 1}
  268. if target_url:
  269. query_params["_target_url"] = target_url
  270. redirect_uri = f"{login_uri}?{urllib.parse.urlencode(query_params)}"
  271. # If the user is redirected more than 3 times, it is very likely that the cookie is not working properly.
  272. # (e.g. browser is blocking third-party cookies in iframe). In this case, redirect the user in the
  273. # non-iframe view.
  274. if nb_redirects > constants.OAUTH_MAX_REDIRECTS:
  275. host = os.environ.get("SPACE_HOST")
  276. if host is None: # cannot happen in a Space
  277. raise RuntimeError(
  278. "App is not running in a Space (SPACE_HOST environment variable is not set). Cannot redirect to non-iframe view."
  279. ) from None
  280. host_url = "https://" + host.rstrip("/")
  281. return RedirectResponse(host_url + redirect_uri)
  282. # Redirect the user to the login page again
  283. return RedirectResponse(redirect_uri)
  284. # OAuth login worked => store the user info in the session and redirect
  285. logger.debug("Successfully logged in with OAuth. Storing user info in session.")
  286. request.session["oauth_info"] = oauth_info
  287. return RedirectResponse(_get_redirect_target(request))
  288. @app.get(logout_uri)
  289. async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
  290. """Endpoint that logs out the user (e.g. delete info from cookie session)."""
  291. logger.debug("Logged out with OAuth. Removing user info from session.")
  292. request.session.pop("oauth_info", None)
  293. return RedirectResponse(_get_redirect_target(request))
  294. def _add_mocked_oauth_routes(app: "fastapi.FastAPI", route_prefix: str = "/") -> None:
  295. """Add fake oauth routes if app is run locally and OAuth is enabled.
  296. Using OAuth will have the same behavior as in a Space but instead of authenticating with HF, a mocked user profile
  297. is added to the session.
  298. """
  299. try:
  300. import fastapi
  301. from fastapi.responses import RedirectResponse
  302. from starlette.datastructures import URL
  303. except ImportError as e:
  304. raise ImportError(
  305. "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
  306. "`huggingface_hub[oauth]` to your requirements.txt file."
  307. ) from e
  308. warnings.warn(
  309. "OAuth is not supported outside of a Space environment. To help you debug your app locally, the oauth endpoints"
  310. " are mocked to return your profile and token. To make it work, your machine must be logged in to Huggingface."
  311. )
  312. mocked_oauth_info = _get_mocked_oauth_info()
  313. login_uri, callback_uri, logout_uri = _get_oauth_uris(route_prefix)
  314. # Define OAuth routes
  315. @app.get(login_uri)
  316. async def oauth_login(request: fastapi.Request) -> RedirectResponse:
  317. """Fake endpoint that redirects to HF OAuth page."""
  318. # Define target (where to redirect after login)
  319. redirect_uri = _generate_redirect_uri(request)
  320. return RedirectResponse(callback_uri + "?" + urllib.parse.urlencode({"_target_url": redirect_uri}))
  321. @app.get(callback_uri)
  322. async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
  323. """Endpoint that handles the OAuth callback."""
  324. request.session["oauth_info"] = mocked_oauth_info
  325. return RedirectResponse(_get_redirect_target(request))
  326. @app.get(logout_uri)
  327. async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
  328. """Endpoint that logs out the user (e.g. delete cookie session)."""
  329. request.session.pop("oauth_info", None)
  330. logout_url = URL("/").include_query_params(**request.query_params)
  331. return RedirectResponse(url=logout_url, status_code=302) # see https://github.com/gradio-app/gradio/pull/9659
  332. def _generate_redirect_uri(request: "fastapi.Request") -> str:
  333. if "_target_url" in request.query_params:
  334. # if `_target_url` already in query params => respect it
  335. target = request.query_params["_target_url"]
  336. else:
  337. # otherwise => keep query params
  338. target = "/?" + urllib.parse.urlencode(request.query_params)
  339. redirect_uri = request.url_for("oauth_redirect_callback").include_query_params(_target_url=target)
  340. redirect_uri_as_str = str(redirect_uri)
  341. if redirect_uri.netloc.endswith(".hf.space"):
  342. # In Space, FastAPI redirect as http but we want https
  343. redirect_uri_as_str = redirect_uri_as_str.replace("http://", "https://")
  344. return redirect_uri_as_str
  345. def _get_redirect_target(request: "fastapi.Request", default_target: str = "/") -> str:
  346. return request.query_params.get("_target_url", default_target)
  347. def _get_mocked_oauth_info() -> dict:
  348. token = get_token()
  349. if token is None:
  350. raise ValueError(
  351. "Your machine must be logged in to HF to debug an OAuth app locally. Please"
  352. " run `hf auth login` or set `HF_TOKEN` as environment variable "
  353. "with one of your access token. You can generate a new token in your "
  354. "settings page (https://huggingface.co/settings/tokens)."
  355. )
  356. user = whoami()
  357. if user["type"] != "user":
  358. raise ValueError(
  359. "Your machine is not logged in with a personal account. Please use a "
  360. "personal access token. You can generate a new token in your settings page"
  361. " (https://huggingface.co/settings/tokens)."
  362. )
  363. return {
  364. "access_token": token,
  365. "token_type": "bearer",
  366. "expires_in": 8 * 60 * 60, # 8 hours
  367. "id_token": "FOOBAR",
  368. "scope": "openid profile",
  369. "refresh_token": "hf_oauth__refresh_token",
  370. "expires_at": int(time.time()) + 8 * 60 * 60, # 8 hours
  371. "userinfo": {
  372. "sub": "0123456789",
  373. "name": user["fullname"],
  374. "preferred_username": user["name"],
  375. "profile": f"https://huggingface.co/{user['name']}",
  376. "picture": user["avatarUrl"],
  377. "website": "",
  378. "aud": "00000000-0000-0000-0000-000000000000",
  379. "auth_time": 1691672844,
  380. "nonce": "aaaaaaaaaaaaaaaaaaa",
  381. "iat": 1691672844,
  382. "exp": 1691676444,
  383. "iss": "https://huggingface.co",
  384. },
  385. }
  386. def _get_oauth_uris(route_prefix: str = "/") -> tuple[str, str, str]:
  387. route_prefix = route_prefix.strip("/")
  388. if route_prefix:
  389. route_prefix = f"/{route_prefix}"
  390. return (
  391. f"{route_prefix}/oauth/huggingface/login",
  392. f"{route_prefix}/oauth/huggingface/callback",
  393. f"{route_prefix}/oauth/huggingface/logout",
  394. )