diff --git a/authentik/root/messages/consumer.py b/authentik/root/messages/consumer.py index 4c0250c04..f71073d1c 100644 --- a/authentik/root/messages/consumer.py +++ b/authentik/root/messages/consumer.py @@ -7,13 +7,16 @@ class MessageConsumer(JsonWebsocketConsumer): """Consumer which sends django.contrib.messages Messages over WS. channel_name is saved into cache with user_id, and when a add_message is called""" + session_key: str + def connect(self): self.accept() - cache.set(f"user_{self.scope['user'].pk}_messages_{self.channel_name}", True) + self.session_key = self.scope["session"].session_key + cache.set(f"user_{self.session_key}_messages_{self.channel_name}", True, None) # pylint: disable=unused-argument def disconnect(self, close_code): - cache.delete(f"user_{self.scope['user'].pk}_messages_{self.channel_name}") + cache.delete(f"user_{self.session_key}_messages_{self.channel_name}") def event_update(self, event: dict): """Event handler which is called by Messages Storage backend""" diff --git a/authentik/root/messages/storage.py b/authentik/root/messages/storage.py index 41988576f..33710b823 100644 --- a/authentik/root/messages/storage.py +++ b/authentik/root/messages/storage.py @@ -16,7 +16,7 @@ class ChannelsStorage(FallbackStorage): self.channel = get_channel_layer() def _store(self, messages: list[Message], response, *args, **kwargs): - prefix = f"user_{self.request.user.pk}_messages_" + prefix = f"user_{self.request.session.session_key}_messages_" keys = cache.keys(f"{prefix}*") if len(keys) < 1: return super()._store(messages, response, *args, **kwargs)