-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demo: https://huggingface.co/spaces/lilacai/nikhil_staging We use Google login to authenticate the user. This uses the OAuth2 flow where a user clicks /login, redirects to /auth which sets a cookie, and redirects back to the app. - Add a login button in the top-left. - When in an iframe, pop the user to new tab. This is necessary as we can't set the oauth2 cookie from inside the iframe.
- Loading branch information
Showing
23 changed files
with
494 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Router for Google OAuth2 login.""" | ||
|
||
from urllib.parse import urlparse, urlunparse | ||
|
||
from authlib.integrations.starlette_client import OAuth, OAuthError | ||
from fastapi import APIRouter, Request, Response | ||
from fastapi.responses import HTMLResponse | ||
from starlette.config import Config | ||
from starlette.responses import RedirectResponse | ||
|
||
from .config import CONFIG | ||
from .router_utils import RouteErrorHandler | ||
|
||
router = APIRouter(route_class=RouteErrorHandler) | ||
|
||
GOOGLE_CLIENT_ID = CONFIG.get('GOOGLE_CLIENT_ID', None) | ||
GOOGLE_CLIENT_SECRET = CONFIG.get('GOOGLE_CLIENT_SECRET', None) | ||
LILAC_AUTH_ENABLED = CONFIG.get('LILAC_AUTH_ENABLED', False) | ||
if LILAC_AUTH_ENABLED: | ||
if GOOGLE_CLIENT_ID is None or GOOGLE_CLIENT_SECRET is None: | ||
raise ValueError( | ||
'Missing `GOOGLE_CLIENT_ID` or `GOOGLE_CLIENT_SECRET` when `LILAC_AUTH_ENABLED=true`') | ||
SECRET_KEY = CONFIG.get('LILAC_OAUTH_SECRET_KEY', None) | ||
if not SECRET_KEY: | ||
raise ValueError('Missing `LILAC_OAUTH_SECRET_KEY` when `LILAC_AUTH_ENABLED=true`') | ||
|
||
# Set up oauth | ||
oauth = OAuth( | ||
Config(environ={ | ||
'GOOGLE_CLIENT_ID': GOOGLE_CLIENT_ID, | ||
'GOOGLE_CLIENT_SECRET': GOOGLE_CLIENT_SECRET | ||
})) | ||
oauth.register( | ||
name='google', | ||
server_metadata_url='https://accounts.google.com/.well-known/openid-configuration', | ||
client_kwargs={'scope': 'openid email profile'}, | ||
) | ||
|
||
|
||
@router.get('/login') | ||
async def login(request: Request, origin_url: str) -> RedirectResponse: | ||
"""Redirects to Google OAuth login page.""" | ||
auth_path = urlunparse(urlparse(origin_url)._replace(path='/google/auth')) | ||
return await oauth.google.authorize_redirect(request, auth_path) | ||
|
||
|
||
@router.get('/auth') | ||
async def auth(request: Request) -> Response: | ||
"""Handles the Google OAuth callback.""" | ||
try: | ||
token = await oauth.google.authorize_access_token(request) | ||
except OAuthError as error: | ||
return HTMLResponse(f'<h1>{error}</h1>') | ||
request.session['user'] = token['userinfo'] | ||
return RedirectResponse(url='/') | ||
|
||
|
||
@router.get('/logout') | ||
def logout(request: Request) -> RedirectResponse: | ||
"""Logs the user out.""" | ||
request.session.pop('user', None) | ||
return RedirectResponse(url='/') |
Oops, something went wrong.