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.