-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
4 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
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,69 @@ | ||
import json | ||
from fastapi import APIRouter | ||
from starlette.requests import Request | ||
from starlette.middleware.sessions import SessionMiddleware | ||
from starlette.responses import HTMLResponse, RedirectResponse | ||
from authlib.integrations.starlette_client import OAuth, OAuthError | ||
from starlette.config import Config | ||
from yamlns import namespace as ns | ||
from . import persons | ||
|
||
|
||
config = Config('config.fastapi') | ||
print(config.file_values) | ||
|
||
oauth = OAuth(config) | ||
|
||
|
||
CONF_URL = 'https://accounts.google.com/.well-known/openid-configuration' | ||
oauth.register( | ||
name='google', | ||
server_metadata_url=CONF_URL, | ||
client_kwargs={ | ||
'scope': 'openid email profile' | ||
} | ||
) | ||
|
||
router = APIRouter() | ||
""" | ||
@router.get('/') | ||
async def homepage(request: Request): | ||
user = request.session.get('user') | ||
if user: | ||
data = json.dumps(user) | ||
html = ( | ||
f'<pre>{data}</pre>' | ||
'<a href="/logout">logout</a>' | ||
) | ||
return HTMLResponse(html) | ||
return HTMLResponse('<a href="/login">login</a>') | ||
""" | ||
|
||
@router.get('/login') | ||
async def login(request: Request): | ||
redirect_uri = request.url_for('auth') | ||
print(redirect_uri) | ||
return await oauth.google.authorize_redirect(request, redirect_uri) | ||
|
||
|
||
@router.get('/auth') | ||
async def auth(request: Request): | ||
try: | ||
token = await oauth.google.authorize_access_token(request) | ||
except OAuthError as error: | ||
return HTMLResponse(f'<h1>{error.error}</h1>') | ||
user = await oauth.google.parse_id_token(request, token) | ||
|
||
if user: | ||
username = persons.byEmail(user['email']) | ||
if username: | ||
request.session['user'] = dict(user) | ||
return RedirectResponse(url='/') | ||
|
||
|
||
@router.get('/logout') | ||
async def logout(request: Request): | ||
request.session.pop('user', None) | ||
return RedirectResponse(url='/') | ||
|
||
|
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