def decorated_function(*args, **kwargs): token = request.headers.get("Authorization") if not token: raise Unauthorized(description="Authentication failed") try: user_name = Authentication.validate_jwt(jwt_token=token) kwargs.update({"username": user_name}) except: raise Unauthorized(description="Authentication failed") return f(*args, **kwargs)
def add_message(packet: DirectMessageDataModel): # get sender username from token sender_username = Authentication.validate_jwt(packet.token) # create full package to send and save in database data = MessagePacket(sender_username=sender_username, receiver_username=packet.receiver, content_type=packet.content_type, content=packet.content) message_in_db = ChatDb(sender_username=data.sender_username, receiver_username=data.receiver_username, content=data.content, content_type=data.content_type, is_seen=False, create_at=data.create_at) session.add(message_in_db) session.commit() data.id = message_in_db.id data.create_at = data.create_at.timestamp() return data
def decorated_function(*args, **kwargs): # data = Authentication.validate_jwt(args) # validate access if len(args) > 0: if "token" not in args[0]: raise ConnectionRefusedError("authentication failed") try: print(args[0]) user_name = Authentication.validate_jwt( json.loads(args[0])["token"]) kwargs.update({"username": user_name}) except Exception as ex: raise ConnectionRefusedError("authentication failed") else: raise ConnectionRefusedError("authentication failed") return f(*args, **kwargs)
def on_connect(self): data = Authentication.validate_jwt( request.args.get("token").encode('utf-8')) join_room(data, request.sid) emit("server_response", {"username": data, "sid": request.sid}) print(f'{data} connected')