Exemplo n.º 1
0
    def delete(user_id):
        auth_header = request.headers.get("Authorization")

        if not auth_header:
            logger.info(f"Authorization header not found in {request}")
            users_namespace.abort(403, "Token required to fetch the user")

        try:
            token = auth_header.split()[1]

            get_user_id_by_token(token)

            user = get_user_by_id(user_id)

            if not user:
                logger.info(f"Invalid user_id for token {token}")
                users_namespace.abort(404, f"User {user_id} does not exist")

            remove_user(user)

            return {}, 204
        except ExpiredSignatureError:
            logger.error(f"Auth-token {token} has expired")
            users_namespace.abort(401, "Token expired. Please log in again.")
        except InvalidTokenError:
            logger.error(f"Auth-token {token} is invalid")
            users_namespace.abort(401, "Invalid token. Please log in again.")
Exemplo n.º 2
0
    def put(user_id):
        auth_header = request.headers.get("Authorization")

        if not auth_header:
            logger.info(f"Authorization header not found in {request}")
            users_namespace.abort(403, "Token required to fetch the user")

        try:
            token = auth_header.split()[1]

            get_user_id_by_token(token)

            request_data = request.get_json()

            user = get_user_by_id(user_id)

            if not user:
                logger.info(f"Invalid user_id for token {token}")
                users_namespace.abort(404, f"User {user_id} does not exist")

            updated_user = update_user(
                user, request_data["username"], request_data["email"]
            )

            return updated_user, 200
        except ExpiredSignatureError:
            logger.error(f"Auth-token {token} has expired")
            users_namespace.abort(401, "Token expired. Please log in again.")
        except InvalidTokenError:
            logger.error(f"Auth-token {token} is invalid")
            users_namespace.abort(401, "Invalid token. Please log in again.")
Exemplo n.º 3
0
    def get():
        auth_header = request.headers.get("Authorization")

        if not auth_header:
            logger.info(f"Authorization header not found in {request}")
            users_namespace.abort(403, "Token required to fetch the user list")

        try:
            token = auth_header.split()[1]

            get_user_id_by_token(token)

            return get_all_users(), 200
        except ExpiredSignatureError:
            logger.error(f"Auth-token {token} has expired")
            users_namespace.abort(401, "Token expired. Please log in again.")
        except InvalidTokenError:
            logger.error(f"Auth-token {token} is invalid")
            users_namespace.abort(401, "Invalid token. Please log in again.")
Exemplo n.º 4
0
    def get():
        auth_header = request.headers.get("Authorization")

        if not auth_header:
            logger.info(f"Authorization header not found in {request}")
            users_namespace.abort(403, "Token required to fetch the user list")

        try:
            token = auth_header.split()[1]

            get_user_id_by_token(token)

            args = user_parser.parse_args()
            page = int(args.get("page", 1))
            per_page = current_app.config.get("POSTS_PER_PAGE")

            users = get_all_users(page, per_page)
            return users.items, 200
        except ExpiredSignatureError:
            logger.error(f"Auth-token {token} has expired")
            users_namespace.abort(401, "Token expired. Please log in again.")
        except InvalidTokenError:
            logger.error(f"Auth-token {token} is invalid")
            users_namespace.abort(401, "Invalid token. Please log in again.")