YellWOWPages/yellow/auth.py

27 lines
789 B
Python
Raw Normal View History

2022-03-23 10:04:02 +00:00
import re
import peewee
from quart import session, redirect, url_for
from quart_keycloak import Keycloak, KeycloakAuthToken, KeycloakLogoutRequest
2023-08-29 18:45:04 +00:00
from yellow.factory import keycloak
from yellow.models import User
2023-08-29 18:45:04 +00:00
@keycloak.after_login()
async def handle_user_login(auth_token: KeycloakAuthToken):
username = auth_token.username
2022-03-23 10:04:02 +00:00
if not re.match(r"^[a-zA-Z0-9_\.-]+$", username):
raise Exception("bad username")
try:
user = User.select().where(User.id == uid).get()
except peewee.DoesNotExist:
user = None
if not user:
# create new user if it does not exist yet
user = User.create(id=uid, username=username)
# user is now logged in
session['user'] = user.to_json()
2023-08-29 18:45:04 +00:00
return redirect(url_for('bp_routes.dashboard'))