From 4e2d3026c86738ddae44595f64c43e2f8bda55f5 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Tue, 24 Oct 2023 20:28:30 +0200 Subject: [PATCH] refactor --- app/exceptions.py | 10 +++++----- app/models/shakespeare.py | 3 +-- app/services/auth.py | 18 +++++++----------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/exceptions.py b/app/exceptions.py index 8f1806a..b6d7a2e 100644 --- a/app/exceptions.py +++ b/app/exceptions.py @@ -5,7 +5,7 @@ class BadRequestHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_400_BAD_REQUEST, - detail=msg if msg else "Bad request", + detail=msg or "Bad request", ) @@ -31,7 +31,7 @@ class ForbiddenHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_403_FORBIDDEN, - detail=msg if msg else "Requested resource is forbidden", + detail=msg or "Requested resource is forbidden", ) @@ -39,7 +39,7 @@ class NotFoundHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_404_NOT_FOUND, - detail=msg if msg else "Requested resource is not found", + detail=msg or "Requested resource is not found", ) @@ -47,7 +47,7 @@ class ConflictHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_409_CONFLICT, - detail=msg if msg else "Conflicting resource request", + detail=msg or "Conflicting resource request", ) @@ -55,5 +55,5 @@ class ServiceNotAvailableHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=msg if msg else "Service not available", + detail=msg or "Service not available", ) diff --git a/app/models/shakespeare.py b/app/models/shakespeare.py index 88d9558..262bd35 100644 --- a/app/models/shakespeare.py +++ b/app/models/shakespeare.py @@ -133,5 +133,4 @@ class Paragraph(Base): async def find(cls, db_session: AsyncSession, character: str): stmt = select(cls).join(Character).join(Chapter).join(Work).where(Character.name == character) result = await db_session.execute(stmt) - instance = result.scalars().all() - return instance + return result.scalars().all() diff --git a/app/services/auth.py b/app/services/auth.py index e8cf692..10d22f4 100644 --- a/app/services/auth.py +++ b/app/services/auth.py @@ -10,10 +10,7 @@ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials async def verify_jwt(request: Request, token: str) -> bool: _payload = await request.app.state.redis.get(token) - if _payload: - return True - else: - return False + return bool(_payload) class AuthBearer(HTTPBearer): @@ -22,14 +19,13 @@ class AuthBearer(HTTPBearer): async def __call__(self, request: Request): credentials: HTTPAuthorizationCredentials = await super().__call__(request) - if credentials: - if not credentials.scheme == "Bearer": - raise HTTPException(status_code=403, detail="Invalid authentication scheme.") - if not await verify_jwt(request, credentials.credentials): - raise HTTPException(status_code=403, detail="Invalid token or expired token.") - return credentials.credentials - else: + if not credentials: raise HTTPException(status_code=403, detail="Invalid authorization code.") + if credentials.scheme != "Bearer": + raise HTTPException(status_code=403, detail="Invalid authentication scheme.") + if not await verify_jwt(request, credentials.credentials): + raise HTTPException(status_code=403, detail="Invalid token or expired token.") + return credentials.credentials async def create_access_token(user: User, request: Request):