How can one enable a mix of authenticated and unauthenticated websockets using Django Channels?

17 Views Asked by At

I'm running a Django application with DRF and Channels and I've configured a number of websockets, one of which I need to be open/insecure. However, all I can figure out is how to enable/disable security at the application level, not at the consumer specific level.

Here is the asgi.py configuration:

application = ProtocolTypeRouter(
    {
        "http": django_application,
        "websocket": AllowedHostsOriginValidator(
            AuthMiddlewareStack(URLRouter(websocket_routing.websocket_urlpatterns))
        )
    }
)

Alternatively, to disable the authentication, I can switch the websocket entry to URLRouter(websocket_routing.websocket_urlpatterns)

With the DRF views, its easy enough to disable authentication:

class WebhookCallEvents(APIView):

    authentication_classes = []
    permission_classes = []

But these properties don't seem to work in a consumer class:

class MyConsumer(AsyncWebsocketConsumer):

    authentication_classes = []
    permission_classes = []

How would one enable authentication for some websockets but have it disabled for others within the same application?

Looks like this is a duplicate of this question over here: Django Channels 2 websockets multiple AuthMiddlewareStacks

However, none of the answers are accepted, so perhaps restating the question will yield some results.

0

There are 0 best solutions below