From d4419d66c196260363772a12b591d4e9f170d418 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 16 Jun 2021 22:48:26 +0200 Subject: [PATCH] core: fix error when creating AuthenticatedSession without key Signed-off-by: Jens Langhammer --- authentik/core/models.py | 6 +++++- authentik/core/signals.py | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/authentik/core/models.py b/authentik/core/models.py index 677627d64..fc88d10e8 100644 --- a/authentik/core/models.py +++ b/authentik/core/models.py @@ -494,8 +494,12 @@ class AuthenticatedSession(ExpiringModel): last_used = models.DateTimeField(auto_now=True) @staticmethod - def from_request(request: HttpRequest, user: User) -> "AuthenticatedSession": + def from_request( + request: HttpRequest, user: User + ) -> Optional["AuthenticatedSession"]: """Create a new session from a http request""" + if not hasattr(request, "session") or not request.session.session_key: + return None return AuthenticatedSession( session_key=request.session.session_key, user=user, diff --git a/authentik/core/signals.py b/authentik/core/signals.py index daca16ce1..497fbdc8b 100644 --- a/authentik/core/signals.py +++ b/authentik/core/signals.py @@ -49,7 +49,9 @@ def user_logged_in_session(sender, request: HttpRequest, user: "User", **_): """Create an AuthenticatedSession from request""" from authentik.core.models import AuthenticatedSession - AuthenticatedSession.from_request(request, user).save() + session = AuthenticatedSession.from_request(request, user) + if session: + session.save() @receiver(user_logged_out)