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)