Hi I am working on the Backend for an app that uses Django Rest Framework as well as Django Channels. In order to make both of these work together I have had to implement my own decorators to get the Token from the message on Django Channels which works great.
I get the token using the following decorator which I use when I am for the initial connection and the second for processing messages from the Websocket client. And can associate the channel with a user, however I need to be able to do this when the user def token_request_parameter(func): """ Checks the presence of a "token" request parameter and tries to authenticate the user based on its content. """ @wraps(func) def inner(message, *args, **kwargs): # Taken from channels.session.http_session try: if "method" not in message.content: message.content['method'] = "FAKE" request = AsgiRequest(message) except Exception as e: raise ValueError("Cannot parse HTTP message - are you sure this is a HTTP consumer? %s" % e) token = request.GET.get("token", None) if token is None: _close_reply_channel(message) raise ValueError("Missing token request parameter. Closing channel.") auth = TokenAuthentication() user, _ = auth.authenticate_credentials(token) message.token = token message.user = user return func(message, *args, **kwargs) return inner def token_message_text_field(func): """ Checks the presence of a "token" field on the message's text field and tries to authenticate the user based on its content. """ @wraps(func) def inner(message, *args, **kwargs): if not hasattr(message, 'token'): raise ValueError('Did not see a Token session to get auth from') message_text = message.get('text', None) if message_text is None: _close_reply_channel(message) raise ValueError("Missing text field. Closing channel.") try: message_text_json = json.loads(message_text) except ValueError: _close_reply_channel(message) raise token = message_text_json.pop('token', None) if token is None: _close_reply_channel(message) raise ValueError("Missing token field. Closing channel.") auth = TokenAuthentication() user, _ = auth.authenticate_credentials(token) message.token = token message.user = user message.text = json.dumps(message_text_json.get('message')) return func(message, *args, **kwargs) return inner However I need to be able to know the user for disconnection. My question is how do I associate a disconnection to a user? The token_message_text_field doesnt seem to be able to get it. Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1491fe41-275d-419c-bb3e-38fdecdfc35d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.