This commit is contained in:
Jakub Miazek 2023-10-24 20:28:30 +02:00
parent 587690cb3e
commit 4e2d3026c8
3 changed files with 13 additions and 18 deletions

View File

@ -5,7 +5,7 @@ class BadRequestHTTPException(HTTPException):
def __init__(self, msg: str): def __init__(self, msg: str):
super().__init__( super().__init__(
status_code=status.HTTP_400_BAD_REQUEST, 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): def __init__(self, msg: str):
super().__init__( super().__init__(
status_code=status.HTTP_403_FORBIDDEN, 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): def __init__(self, msg: str):
super().__init__( super().__init__(
status_code=status.HTTP_404_NOT_FOUND, 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): def __init__(self, msg: str):
super().__init__( super().__init__(
status_code=status.HTTP_409_CONFLICT, 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): def __init__(self, msg: str):
super().__init__( super().__init__(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=msg if msg else "Service not available", detail=msg or "Service not available",
) )

View File

@ -133,5 +133,4 @@ class Paragraph(Base):
async def find(cls, db_session: AsyncSession, character: str): async def find(cls, db_session: AsyncSession, character: str):
stmt = select(cls).join(Character).join(Chapter).join(Work).where(Character.name == character) stmt = select(cls).join(Character).join(Chapter).join(Work).where(Character.name == character)
result = await db_session.execute(stmt) result = await db_session.execute(stmt)
instance = result.scalars().all() return result.scalars().all()
return instance

View File

@ -10,10 +10,7 @@ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
async def verify_jwt(request: Request, token: str) -> bool: async def verify_jwt(request: Request, token: str) -> bool:
_payload = await request.app.state.redis.get(token) _payload = await request.app.state.redis.get(token)
if _payload: return bool(_payload)
return True
else:
return False
class AuthBearer(HTTPBearer): class AuthBearer(HTTPBearer):
@ -22,14 +19,13 @@ class AuthBearer(HTTPBearer):
async def __call__(self, request: Request): async def __call__(self, request: Request):
credentials: HTTPAuthorizationCredentials = await super().__call__(request) credentials: HTTPAuthorizationCredentials = await super().__call__(request)
if credentials: if not credentials:
if not credentials.scheme == "Bearer": raise HTTPException(status_code=403, detail="Invalid authorization code.")
if credentials.scheme != "Bearer":
raise HTTPException(status_code=403, detail="Invalid authentication scheme.") raise HTTPException(status_code=403, detail="Invalid authentication scheme.")
if not await verify_jwt(request, credentials.credentials): if not await verify_jwt(request, credentials.credentials):
raise HTTPException(status_code=403, detail="Invalid token or expired token.") raise HTTPException(status_code=403, detail="Invalid token or expired token.")
return credentials.credentials return credentials.credentials
else:
raise HTTPException(status_code=403, detail="Invalid authorization code.")
async def create_access_token(user: User, request: Request): async def create_access_token(user: User, request: Request):