authentik.events.consumer

websocket Message consumer

 1"""websocket Message consumer"""
 2
 3from hashlib import sha256
 4
 5from asgiref.sync import async_to_sync
 6from channels.exceptions import DenyConnection
 7from channels.generic.websocket import JsonWebsocketConsumer
 8from django.db import connection
 9
10from authentik.core.models import User
11
12
13def build_user_group(user: User):
14    return sha256(f"{connection.schema_name}/group_client_user_{user.uuid}".encode()).hexdigest()
15
16
17class ClientConsumer(JsonWebsocketConsumer):
18    """Consumer which sends django.contrib.messages Messages over WS.
19    channel_name is saved into cache with user_id, and when a add_message is called"""
20
21    user: User | None = None
22
23    def connect(self):
24        user = self.scope.get("user")
25        if user is None or not user.is_authenticated:
26            raise DenyConnection()
27        self.user = user
28        self.accept()
29        async_to_sync(self.channel_layer.group_add)(build_user_group(self.user), self.channel_name)
30
31    def disconnect(self, code):
32        if self.user:
33            async_to_sync(self.channel_layer.group_discard)(
34                build_user_group(self.user), self.channel_name
35            )
36
37    def event_notification(self, event: dict):
38        """Event handler for new notifications"""
39        self.send_json({"message_type": "notification.new", **event})
def build_user_group(user: authentik.core.models.User):
14def build_user_group(user: User):
15    return sha256(f"{connection.schema_name}/group_client_user_{user.uuid}".encode()).hexdigest()
class ClientConsumer(channels.generic.websocket.JsonWebsocketConsumer):
18class ClientConsumer(JsonWebsocketConsumer):
19    """Consumer which sends django.contrib.messages Messages over WS.
20    channel_name is saved into cache with user_id, and when a add_message is called"""
21
22    user: User | None = None
23
24    def connect(self):
25        user = self.scope.get("user")
26        if user is None or not user.is_authenticated:
27            raise DenyConnection()
28        self.user = user
29        self.accept()
30        async_to_sync(self.channel_layer.group_add)(build_user_group(self.user), self.channel_name)
31
32    def disconnect(self, code):
33        if self.user:
34            async_to_sync(self.channel_layer.group_discard)(
35                build_user_group(self.user), self.channel_name
36            )
37
38    def event_notification(self, event: dict):
39        """Event handler for new notifications"""
40        self.send_json({"message_type": "notification.new", **event})

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

user: authentik.core.models.User | None = None
def connect(self):
24    def connect(self):
25        user = self.scope.get("user")
26        if user is None or not user.is_authenticated:
27            raise DenyConnection()
28        self.user = user
29        self.accept()
30        async_to_sync(self.channel_layer.group_add)(build_user_group(self.user), self.channel_name)
def disconnect(self, code):
32    def disconnect(self, code):
33        if self.user:
34            async_to_sync(self.channel_layer.group_discard)(
35                build_user_group(self.user), self.channel_name
36            )

Called when a WebSocket connection is closed.

def event_notification(self, event: dict):
38    def event_notification(self, event: dict):
39        """Event handler for new notifications"""
40        self.send_json({"message_type": "notification.new", **event})

Event handler for new notifications