Example #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.")
Example #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.")
Example #3
0
    def get():
        auth_header = request.headers.get("Authorization")

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

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

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

            sentiments = get_all_sentiments(page, per_page)

            return sentiments.items, 200
        except ExpiredSignatureError:
            logger.error(f"Auth-token {token} has expired")
            sentiment_namespace.abort(
                401, "Token expired. Please log in again."
            )
        except InvalidTokenError:
            logger.error(f"Auth-token {token} is invalid")
            sentiment_namespace.abort(
                401, "Invalid token. Please log in again."
            )
Example #4
0
    def get(request_id):
        auth_header = request.headers.get("Authorization")

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

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

            sentiment = get_sentiment_by_id(request_id)

            if not sentiment:
                logger.info(f"Invalid request_id for token {token}")
                sentiment_namespace.abort(
                    404, f"Sentiment {request_id} does not exist"
                )

            return sentiment, 200
        except ExpiredSignatureError:
            logger.error(f"Auth-token {token} has expired")
            sentiment_namespace.abort(
                401, "Token expired. Please log in again."
            )
        except InvalidTokenError:
            logger.error(f"Auth-token {token} is invalid")
            sentiment_namespace.abort(
                401, "Invalid token. Please log in again."
            )
Example #5
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.")
Example #6
0
    def post():
        request_data = request.get_json()
        refresh_token = request_data.get("refresh_token")

        try:
            user_id = get_user_id_by_token(refresh_token)
            token = update_token(refresh_token, user_id)
            logger.info(f"Refreshed token for user with id {user_id}")
            return token, 200
        except ExpiredSignatureError:
            logger.error(f"Auth-token {refresh_token} has expired")
            auth_namespace.abort(401, "Token expired. Please log in again.")
        except InvalidTokenError:
            logger.error(f"Auth-token {refresh_token} is invalid")
            auth_namespace.abort(401, "Invalid token. Please log in again.")