From fc05f8095166fbb1a4d7bc7b9b3fb745b6d76f5b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 17 Sep 2022 12:10:58 +0200 Subject: [PATCH] remove unused websocket stuff Signed-off-by: Jens Langhammer --- .../{root/messages => core/ws}/__init__.py | 0 .../{root/messages => core/ws}/consumer.py | 18 +++++++++- authentik/lib/tests/utils.py | 4 --- authentik/root/messages/storage.py | 35 ------------------- authentik/root/settings.py | 3 -- authentik/root/websocket.py | 4 +-- 6 files changed, 19 insertions(+), 45 deletions(-) rename authentik/{root/messages => core/ws}/__init__.py (100%) rename authentik/{root/messages => core/ws}/consumer.py (54%) delete mode 100644 authentik/root/messages/storage.py diff --git a/authentik/root/messages/__init__.py b/authentik/core/ws/__init__.py similarity index 100% rename from authentik/root/messages/__init__.py rename to authentik/core/ws/__init__.py diff --git a/authentik/root/messages/consumer.py b/authentik/core/ws/consumer.py similarity index 54% rename from authentik/root/messages/consumer.py rename to authentik/core/ws/consumer.py index dcc3b5a0f..2a6d66981 100644 --- a/authentik/root/messages/consumer.py +++ b/authentik/core/ws/consumer.py @@ -3,7 +3,7 @@ from channels.generic.websocket import JsonWebsocketConsumer from django.core.cache import cache -class MessageConsumer(JsonWebsocketConsumer): +class FrontendConsumer(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""" @@ -21,3 +21,19 @@ class MessageConsumer(JsonWebsocketConsumer): def event_update(self, event: dict): """Event handler which is called by Messages Storage backend""" self.send_json(event) + + # prefix = f"user_{self.request.session.session_key}_messages_" + # keys = cache.keys(f"{prefix}*") + # for key in keys: + # uid = key.replace(prefix, "") + # for message in messages: + # async_to_sync(self.channel.send)( + # uid, + # { + # "type": "event.update", + # "message_type": "message", + # "level": message.level_tag, + # "tags": message.tags, + # "message": message.message, + # }, + # ) diff --git a/authentik/lib/tests/utils.py b/authentik/lib/tests/utils.py index 35ec8391d..c3959b3ab 100644 --- a/authentik/lib/tests/utils.py +++ b/authentik/lib/tests/utils.py @@ -1,5 +1,4 @@ """Test utils""" -from django.contrib.messages.middleware import MessageMiddleware from django.contrib.sessions.middleware import SessionMiddleware from django.http import HttpRequest from django.test.client import RequestFactory @@ -21,7 +20,4 @@ def get_request(*args, user=None, **kwargs): middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() - middleware = MessageMiddleware(dummy_get_response) - middleware.process_request(request) - request.session.save() return request diff --git a/authentik/root/messages/storage.py b/authentik/root/messages/storage.py deleted file mode 100644 index cdbc92627..000000000 --- a/authentik/root/messages/storage.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Channels Messages storage""" -from asgiref.sync import async_to_sync -from channels.layers import get_channel_layer -from django.contrib.messages.storage.base import BaseStorage, Message -from django.core.cache import cache -from django.http.request import HttpRequest - - -class ChannelsStorage(BaseStorage): - """Send contrib.messages over websocket""" - - def __init__(self, request: HttpRequest) -> None: - # pyright: reportGeneralTypeIssues=false - super().__init__(request) - self.channel = get_channel_layer() - - def _get(self): - return [], True - - def _store(self, messages: list[Message], response, *args, **kwargs): - prefix = f"user_{self.request.session.session_key}_messages_" - keys = cache.keys(f"{prefix}*") - for key in keys: - uid = key.replace(prefix, "") - for message in messages: - async_to_sync(self.channel.send)( - uid, - { - "type": "event.update", - "message_type": "message", - "level": message.level_tag, - "tags": message.tags, - "message": message.message, - }, - ) diff --git a/authentik/root/settings.py b/authentik/root/settings.py index 1041b1cda..a7938a180 100644 --- a/authentik/root/settings.py +++ b/authentik/root/settings.py @@ -58,7 +58,6 @@ INSTALLED_APPS = [ "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", - "django.contrib.messages", "django.contrib.staticfiles", "django.contrib.humanize", "authentik.admin", @@ -219,7 +218,6 @@ MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", - "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", "authentik.core.middleware.ImpersonateMiddleware", "django_prometheus.middleware.PrometheusAfterMiddleware", @@ -237,7 +235,6 @@ TEMPLATES = [ "django.template.context_processors.debug", "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", - "django.contrib.messages.context_processors.messages", "authentik.tenants.utils.context_processor", ], }, diff --git a/authentik/root/websocket.py b/authentik/root/websocket.py index 8a8bc1d86..1ebf18f8f 100644 --- a/authentik/root/websocket.py +++ b/authentik/root/websocket.py @@ -2,11 +2,11 @@ from channels.auth import AuthMiddlewareStack from django.urls import path +from authentik.core.ws.consumer import FrontendConsumer from authentik.lib.sentry import SentryWSMiddleware from authentik.outposts.channels import OutpostConsumer -from authentik.root.messages.consumer import MessageConsumer websocket_urlpatterns = [ path("ws/outpost//", SentryWSMiddleware(OutpostConsumer.as_asgi())), - path("ws/client/", AuthMiddlewareStack(SentryWSMiddleware(MessageConsumer.as_asgi()))), + path("ws/client/", AuthMiddlewareStack(SentryWSMiddleware(FrontendConsumer.as_asgi()))), ]