Spaces:
Runtime error
Runtime error
test with samesite none
Browse files
app.py
CHANGED
@@ -36,8 +36,8 @@ def show_profile(request: gr.Request) -> str:
|
|
36 |
|
37 |
def js_open(url: str) -> str:
|
38 |
# Taken from https://cmgdo.com/external-link-in-gradio-button/
|
39 |
-
return f"function() {{window.location.assign('{url}');}}"
|
40 |
-
|
41 |
|
42 |
|
43 |
with gr.Blocks() as demo:
|
|
|
36 |
|
37 |
def js_open(url: str) -> str:
|
38 |
# Taken from https://cmgdo.com/external-link-in-gradio-button/
|
39 |
+
# return f"function() {{window.location.assign('{url}');}}"
|
40 |
+
return f"function() {{window.open('{url}', '_blank');}}"
|
41 |
|
42 |
|
43 |
with gr.Blocks() as demo:
|
auth.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import os
|
2 |
import hashlib
|
|
|
3 |
from authlib.integrations.starlette_client import OAuth
|
4 |
from fastapi import FastAPI
|
5 |
from fastapi.requests import Request
|
6 |
-
from fastapi.responses import RedirectResponse
|
7 |
from starlette.middleware.sessions import SessionMiddleware
|
8 |
|
9 |
|
@@ -19,6 +20,7 @@ for value in (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_SCOPES, OPENID_PROVIDE
|
|
19 |
USER_INFO_URL = OPENID_PROVIDER_URL + "/oauth/userinfo"
|
20 |
METADATA_URL = OPENID_PROVIDER_URL + "/.well-known/openid-configuration"
|
21 |
|
|
|
22 |
oauth = OAuth()
|
23 |
oauth.register(
|
24 |
name="huggingface",
|
@@ -30,32 +32,36 @@ oauth.register(
|
|
30 |
|
31 |
# Hack to close the login/logout page once the user is logged in/out.
|
32 |
# TODO: can it be less hacky?
|
33 |
-
|
34 |
|
35 |
|
36 |
async def oauth_login(request: Request):
|
37 |
redirect_uri = str(request.url_for("oauth_redirect_callback"))
|
38 |
if ".hf.space" in redirect_uri: # In Space, FastAPI redirect as http but we want https
|
39 |
redirect_uri = redirect_uri.replace("http://", "https://")
|
40 |
-
print(redirect_uri)
|
41 |
return await oauth.huggingface.authorize_redirect(request, redirect_uri)
|
42 |
|
43 |
|
44 |
async def oauth_logout(request: Request) -> RedirectResponse:
|
45 |
request.session.pop("user", None)
|
46 |
-
return RedirectResponse("/")
|
|
|
47 |
|
48 |
|
49 |
async def oauth_redirect_callback(request: Request) -> RedirectResponse:
|
50 |
-
print("this one")
|
51 |
-
print(request.session)
|
52 |
token = await oauth.huggingface.authorize_access_token(request)
|
53 |
request.session["user"] = token["userinfo"] # TODO: we should store entire token
|
54 |
-
return RedirectResponse("/")
|
|
|
55 |
|
56 |
|
57 |
def attach_oauth(app: FastAPI) -> None:
|
58 |
-
app.add_middleware(
|
|
|
|
|
|
|
|
|
|
|
59 |
app.get("/login/huggingface")(oauth_login)
|
60 |
app.get("/login/callback")(oauth_redirect_callback)
|
61 |
app.get("/logout")(oauth_logout)
|
|
|
1 |
import os
|
2 |
import hashlib
|
3 |
+
from typing import Any
|
4 |
from authlib.integrations.starlette_client import OAuth
|
5 |
from fastapi import FastAPI
|
6 |
from fastapi.requests import Request
|
7 |
+
from fastapi.responses import RedirectResponse, HTMLResponse
|
8 |
from starlette.middleware.sessions import SessionMiddleware
|
9 |
|
10 |
|
|
|
20 |
USER_INFO_URL = OPENID_PROVIDER_URL + "/oauth/userinfo"
|
21 |
METADATA_URL = OPENID_PROVIDER_URL + "/.well-known/openid-configuration"
|
22 |
|
23 |
+
|
24 |
oauth = OAuth()
|
25 |
oauth.register(
|
26 |
name="huggingface",
|
|
|
32 |
|
33 |
# Hack to close the login/logout page once the user is logged in/out.
|
34 |
# TODO: can it be less hacky?
|
35 |
+
CLOSE_WINDOW_HTML = HTMLResponse("<script>window.close();</script>")
|
36 |
|
37 |
|
38 |
async def oauth_login(request: Request):
|
39 |
redirect_uri = str(request.url_for("oauth_redirect_callback"))
|
40 |
if ".hf.space" in redirect_uri: # In Space, FastAPI redirect as http but we want https
|
41 |
redirect_uri = redirect_uri.replace("http://", "https://")
|
|
|
42 |
return await oauth.huggingface.authorize_redirect(request, redirect_uri)
|
43 |
|
44 |
|
45 |
async def oauth_logout(request: Request) -> RedirectResponse:
|
46 |
request.session.pop("user", None)
|
47 |
+
# return RedirectResponse("/")
|
48 |
+
return CLOSE_WINDOW_HTML
|
49 |
|
50 |
|
51 |
async def oauth_redirect_callback(request: Request) -> RedirectResponse:
|
|
|
|
|
52 |
token = await oauth.huggingface.authorize_access_token(request)
|
53 |
request.session["user"] = token["userinfo"] # TODO: we should store entire token
|
54 |
+
# return RedirectResponse("/")
|
55 |
+
return CLOSE_WINDOW_HTML
|
56 |
|
57 |
|
58 |
def attach_oauth(app: FastAPI) -> None:
|
59 |
+
app.add_middleware(
|
60 |
+
SessionMiddleware,
|
61 |
+
secret_key="000" + hashlib.sha256(OAUTH_CLIENT_SECRET.encode()).hexdigest(),
|
62 |
+
same_site="none",
|
63 |
+
https_only=True,
|
64 |
+
)
|
65 |
app.get("/login/huggingface")(oauth_login)
|
66 |
app.get("/login/callback")(oauth_redirect_callback)
|
67 |
app.get("/logout")(oauth_logout)
|