django_aws_api_gateway_websockets.views module

class django_aws_api_gateway_websockets.views.WebSocketTokenView(**kwargs)[source]

Bases: View

CSRF-protected endpoint to generate one-time WebSocket tokens.

Usage:
  1. Client calls this endpoint with session cookie + CSRF token

  2. Returns a one-time token valid for 60 seconds

  3. Client uses token in WebSocket connection URL: ws://…?ws_token=<token>

post(request, *args, **kwargs)[source]
class django_aws_api_gateway_websockets.views.WebSocketView(**kwargs)[source]

Bases: View

The base WebSocket View for handling messages sent from the client via AWS API Gateway. Expects a URL slug parameter called route

ADDITIONAL_ALLOWED_HANDLERS = []
ALLOWED_HANDLERS = {}
CHANNEL_NAME_PATTERN = re.compile('^[a-zA-Z0-9_-]+$')
MAX_BODY_SIZE = 131072
MAX_CHANNEL_NAME_LENGTH = 191
RATE_LIMIT_ENABLED = True
RATE_LIMIT_MAX_ATTEMPTS = 20
RATE_LIMIT_WINDOW_MINUTES = 1
USE_WS_TOKEN = True
additional_required_headers = ['X-Amzn-Trace-Id', 'X-Forwarded-Port', 'X-Real-Ip']
all_permissions_required = []
api_gateway = None
aws_api_gateway_id = None
body = {}
change_channel(request, *args, **kwargs)[source]

Update the channel the user is connected to

connect(request, *args, **kwargs) JsonResponse | HttpResponseBadRequest[source]

Handle the connection route in a standard way that ensures the User to Connectionid mapping persists

debug = False
debug_log = None
default(request, *args, **kwargs) JsonResponse[source]

Overload this method if you want to have a default message handler

disconnect(request, *args, **kwargs)[source]

Using connectionId update websocket table to show as disconnected

dispatch(request, *args, **kwargs)[source]

Determine the correct method to call.

If the body contains a key identified by self.handler_selection_key and that value is a method on the view then that method will be called. ELSE IF the body contains a key identified by self.route_selection_key and that value is a method on the view then that method will be called. ELSE the default method will be called

Checks for the expected headers. Tries to dispatch to the right method; if a method doesn’t exist, defer to the default handler. If the Handler Selection Key is missing defer to the route selection error handler. If the request method isn’t on the approved list then defer to the normal error handler .

expected_useragent_prefix = 'AmazonAPIGateway_'
handler_selection_key = 'handler'
handler_selection_key_missing(request, *args, **kwargs) HttpResponseBadRequest[source]

Method for handling missing handler_selection_key

has_all_permission(request) bool[source]

Test if the user has ALL: of the required permissions

has_any_permission(request) bool[source]

Test if the user has ANY of the required permissions

invalid_useragent(request, *args, **kwargs) HttpResponseBadRequest[source]

Method for handling unexpected useragents

missing_headers(request, *args, **kwargs) HttpResponseBadRequest[source]

Method for handling missing headers

model = None
permission_denied(request, *args, **kwargs) HttpResponseBadRequest[source]

Method for handling denied access

permissions_required = []
required_connection_headers = ['Cookie', 'Origin', 'Sec-Websocket-Extensions', 'Sec-Websocket-Key', 'Sec-Websocket-Version']
required_headers = ['Host', 'X-Forwarded-For', 'X-Forwarded-Proto', 'Content-Length', 'Connectionid', 'User-Agent', 'X-Amzn-Apigateway-Api-Id']
route_selection_key = 'action'
route_selection_key_missing(request, *args, **kwargs) HttpResponseBadRequest[source]

Method for handling missing route_selection_key

setup(request, *args, **kwargs)[source]

Converts the request.body string back into a dictionary and assign to the objects body property for ease

websocket_session = None