Abhishek Thakur
commited on
Commit
·
6ce1c19
1
Parent(s):
f4e9090
oauth try
Browse files- competitions/oauth.py +10 -4
competitions/oauth.py
CHANGED
@@ -9,6 +9,7 @@ import os
|
|
9 |
import urllib.parse
|
10 |
|
11 |
import fastapi
|
|
|
12 |
from authlib.integrations.starlette_client import OAuth
|
13 |
from fastapi.responses import RedirectResponse
|
14 |
from starlette.middleware.sessions import SessionMiddleware
|
@@ -73,14 +74,19 @@ def _add_oauth_routes(app: fastapi.FastAPI) -> None:
|
|
73 |
async def oauth_login(request: fastapi.Request):
|
74 |
"""Endpoint that redirects to HF OAuth page."""
|
75 |
# Define target (where to redirect after login)
|
76 |
-
|
77 |
-
redirect_uri = request.url_for("auth")
|
78 |
return await oauth.huggingface.authorize_redirect(request, redirect_uri) # type: ignore
|
79 |
|
80 |
-
@app.get("/
|
81 |
async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
|
82 |
"""Endpoint that handles the OAuth callback."""
|
83 |
-
oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
|
|
|
|
|
|
|
|
|
|
|
84 |
request.session["oauth_info"] = oauth_info
|
85 |
return _redirect_to_target(request)
|
86 |
|
|
|
9 |
import urllib.parse
|
10 |
|
11 |
import fastapi
|
12 |
+
from authlib.integrations.base_client.errors import MismatchingStateError
|
13 |
from authlib.integrations.starlette_client import OAuth
|
14 |
from fastapi.responses import RedirectResponse
|
15 |
from starlette.middleware.sessions import SessionMiddleware
|
|
|
74 |
async def oauth_login(request: fastapi.Request):
|
75 |
"""Endpoint that redirects to HF OAuth page."""
|
76 |
# Define target (where to redirect after login)
|
77 |
+
redirect_uri = _generate_redirect_uri(request)
|
78 |
+
# redirect_uri = request.url_for("auth")
|
79 |
return await oauth.huggingface.authorize_redirect(request, redirect_uri) # type: ignore
|
80 |
|
81 |
+
@app.get("/login/callback")
|
82 |
async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
|
83 |
"""Endpoint that handles the OAuth callback."""
|
84 |
+
# oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
|
85 |
+
try:
|
86 |
+
oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
|
87 |
+
except MismatchingStateError:
|
88 |
+
print("Session dict:", dict(request.session))
|
89 |
+
raise
|
90 |
request.session["oauth_info"] = oauth_info
|
91 |
return _redirect_to_target(request)
|
92 |
|