2022-03-23 10:04:02 +00:00
|
|
|
import re
|
|
|
|
|
2022-03-12 12:46:31 +00:00
|
|
|
import peewee
|
|
|
|
from quart import session, redirect, url_for
|
2023-08-29 18:53:40 +00:00
|
|
|
from quart_keycloak import Keycloak, KeycloakAuthToken, KeycloakLogoutRequest
|
2023-08-29 18:45:04 +00:00
|
|
|
from yellow.factory import keycloak
|
2022-03-12 12:46:31 +00:00
|
|
|
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")
|
|
|
|
|
2022-03-12 12:46:31 +00:00
|
|
|
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'))
|