def populate_scope(self, scope):
        # Get the token
        data = parse_qs(scope['query_string'].decode('utf8'))

        # Try to authenticate the user
        try:
            # This will automatically validate the token and raise an error if token is invalid
            token = data['token'][0]
            UntypedToken(token)
        except (InvalidToken, TokenError, KeyError) as e:
            # Token is invalid
            # TODO: raise some websocket's extension
            return None

        #  Then token is valid, decode it
        decoded_data = jwt.decode(token,
                                  settings.SECRET_KEY,
                                  algorithms=['HS256'])
        # Will return a dictionary like -
        # {
        #     "token_type": "access",
        #     "exp": 1568770772,
        #     "jti": "5c15e80d65b04c20ad34d77b6703251b",
        #     "user_id": 6
        # }
        scope['user_id'] = decoded_data['user_id']

        # Add it to the scope if it's not there already
        if 'user' not in scope:
            scope['user'] = UserLazyObject()
Beispiel #2
0
 def populate_scope(self, scope):
     # Make sure we have a session
     if "session" not in scope:
         raise ValueError(
             "AuthMiddleware cannot find session in scope. SessionMiddleware must be above it."
         )
     # Add it to the scope if it's not there already
     if "user" not in scope:
         scope["user"] = UserLazyObject()
 def populate_scope(self, scope):
     # Add it to the scope if it's not there already
     if "user" not in scope:
         scope["user"] = UserLazyObject()
 def populate_scope(self, scope):
     if "user" not in scope:
         scope["user"] = UserLazyObject()
Beispiel #5
0
 def populate_scope(self, scope):
     if 'user' not in scope:
         scope['user'] = UserLazyObject()