Wauplin HF staff commited on
Commit
5b7fcb5
·
1 Parent(s): 1248eba
Files changed (2) hide show
  1. README.md +1 -2
  2. auth.py +0 -77
README.md CHANGED
@@ -4,11 +4,10 @@ emoji: 🏆
4
  colorFrom: pink
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 3.36.1
8
  python_version: 3.10.6
9
  app_file: app.py
10
  hf_oauth: true
11
- hf_oauth_redirect_path: /login/callback
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
4
  colorFrom: pink
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 3.36.1 https://gradio-builds.s3.amazonaws.com/f4d696f71f3fc2f5fd8f9d5f5287cb1d27ef93d4/gradio-3.39.0-py3-none-any.whl
8
  python_version: 3.10.6
9
  app_file: app.py
10
  hf_oauth: true
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
auth.py DELETED
@@ -1,77 +0,0 @@
1
- import hashlib
2
- import os
3
-
4
- from authlib.integrations.base_client import MismatchingStateError
5
- from authlib.integrations.starlette_client import OAuth
6
- from fastapi import FastAPI
7
- from fastapi.requests import Request
8
- from fastapi.responses import HTMLResponse, RedirectResponse
9
- from starlette.middleware.sessions import SessionMiddleware
10
-
11
-
12
- OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
13
- OAUTH_CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET")
14
- OAUTH_SCOPES = os.environ.get("OAUTH_SCOPES")
15
- OPENID_PROVIDER_URL = os.environ.get("OPENID_PROVIDER_URL")
16
-
17
- for value in (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_SCOPES, OPENID_PROVIDER_URL):
18
- if value is None:
19
- raise ValueError("Missing environment variable")
20
-
21
- USER_INFO_URL = OPENID_PROVIDER_URL + "/oauth/userinfo"
22
- METADATA_URL = OPENID_PROVIDER_URL + "/.well-known/openid-configuration"
23
-
24
-
25
- oauth = OAuth()
26
- oauth.register(
27
- name="huggingface",
28
- client_id=OAUTH_CLIENT_ID,
29
- client_secret=OAUTH_CLIENT_SECRET,
30
- client_kwargs={"scope": OAUTH_SCOPES},
31
- server_metadata_url=METADATA_URL,
32
- )
33
-
34
- # Close the login/logout page once the user is logged in/out.
35
- CLOSE_WINDOW_HTML = HTMLResponse("<script>window.close();</script>")
36
-
37
- # If no third-party cookie, open a new tab to login/logout + redirect to the gradio app on this tab.
38
- OPEN_WINDOW_HTML = HTMLResponse("<script>window.open('{url}', '_blank'); window.location.replace('/');</script>")
39
-
40
-
41
- async def oauth_login(request: Request):
42
- redirect_uri = str(request.url_for("oauth_redirect_callback"))
43
- if ".hf.space" in redirect_uri: # In Space, FastAPI redirect as http but we want https
44
- redirect_uri = redirect_uri.replace("http://", "https://")
45
- return await oauth.huggingface.authorize_redirect(request, redirect_uri)
46
-
47
-
48
- async def oauth_logout(request: Request) -> RedirectResponse:
49
- request.session.pop("user", None)
50
- return CLOSE_WINDOW_HTML
51
-
52
-
53
- async def oauth_redirect_callback(request: Request) -> RedirectResponse:
54
- try:
55
- token = await oauth.huggingface.authorize_access_token(request)
56
- request.session["user"] = token["userinfo"] # TODO: we should store the entire token
57
- print("New user: ", token["userinfo"]["name"])
58
- close_tab = True
59
- except MismatchingStateError:
60
- # Third-party cookies are most likely forbidden meaning the session will not be set inside the Space iframe.
61
- # To counterpart this, we redirect the user to use the Space url outside of the iframe.
62
- print("Mismatch error: open in new window")
63
- close_tab = False
64
-
65
- return CLOSE_WINDOW_HTML if close_tab else OPEN_WINDOW_HTML.format(url=request.url_for("oauth_login"))
66
-
67
-
68
- def attach_oauth(app: FastAPI) -> None:
69
- app.add_middleware(
70
- SessionMiddleware,
71
- secret_key="000" + hashlib.sha256(OAUTH_CLIENT_SECRET.encode()).hexdigest(),
72
- same_site="none",
73
- https_only=True,
74
- )
75
- app.get("/login/huggingface")(oauth_login)
76
- app.get("/login/callback")(oauth_redirect_callback)
77
- app.get("/logout")(oauth_logout)