Fix logout type mismatch

This commit is contained in:
april 2024-01-02 17:39:37 -06:00
parent 5b6ed389c4
commit ca59125a85
4 changed files with 12 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import sys
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from database.utils import create_admin_user from database.utils import create_admin_user
from routes import users, flights, auth from routes import users, flights, auth
@ -23,6 +24,10 @@ async def lifespan(app: FastAPI):
# Initialize FastAPI # Initialize FastAPI
app = FastAPI(lifespan=lifespan) app = FastAPI(lifespan=lifespan)
# Allow CORS
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"],
allow_headers=["*"])
# Add subroutes # Add subroutes
app.include_router(users.router, tags=["Users"], prefix="/users") app.include_router(users.router, tags=["Users"], prefix="/users")
app.include_router(flights.router, tags=["Flights"], prefix="/flights") app.include_router(flights.router, tags=["Flights"], prefix="/flights")

View File

@ -9,7 +9,8 @@ from pydantic import ValidationError
from app.config import get_settings, Settings from app.config import get_settings, Settings
from database.tokens import is_blacklisted from database.tokens import is_blacklisted
from database.users import get_user_system_info, get_user_system_info_id from database.users import get_user_system_info, get_user_system_info_id
from schemas.user import TokenPayload, AuthLevel, UserDisplaySchema
from schemas.user import TokenPayload, AuthLevel, UserDisplaySchema, TokenSchema
reusable_oath = OAuth2PasswordBearer( reusable_oath = OAuth2PasswordBearer(
tokenUrl="/auth/login", tokenUrl="/auth/login",
@ -42,7 +43,7 @@ async def get_current_user(settings: Annotated[Settings, Depends(get_settings)],
async def get_current_user_token(settings: Annotated[Settings, Depends(get_settings)], async def get_current_user_token(settings: Annotated[Settings, Depends(get_settings)],
token: str = Depends(reusable_oath)) -> (UserDisplaySchema, str): token: str = Depends(reusable_oath)) -> (UserDisplaySchema, TokenSchema):
try: try:
payload = jwt.decode( payload = jwt.decode(
token, settings.jwt_secret_key, algorithms=[settings.jwt_algorithm] token, settings.jwt_secret_key, algorithms=[settings.jwt_algorithm]
@ -58,11 +59,11 @@ async def get_current_user_token(settings: Annotated[Settings, Depends(get_setti
if blacklisted: if blacklisted:
raise HTTPException(403, "Token expired", {"WWW-Authenticate": "Bearer"}) raise HTTPException(403, "Token expired", {"WWW-Authenticate": "Bearer"})
user = await get_user_system_info(id=token_data.sub) user = await get_user_system_info_id(id=token_data.sub)
if user is None: if user is None:
raise HTTPException(404, "Could not find user") raise HTTPException(404, "Could not find user")
return user return user, token
async def admin_required(user: Annotated[UserDisplaySchema, Depends(get_current_user)]): async def admin_required(user: Annotated[UserDisplaySchema, Depends(get_current_user)]):

View File

@ -50,7 +50,7 @@ async def get_flight(flight_id: str, user: UserDisplaySchema = Depends(get_curre
:return: Flight details :return: Flight details
""" """
flight = await db.retrieve_flight(flight_id) flight = await db.retrieve_flight(flight_id)
if flight.user != user.id and AuthLevel(user.level) != AuthLevel.ADMIN: if str(flight.user) != user.id and AuthLevel(user.level) != AuthLevel.ADMIN:
logger.info("Attempted access to unauthorized flight by %s", user.username) logger.info("Attempted access to unauthorized flight by %s", user.username)
raise HTTPException(403, "Unauthorized access") raise HTTPException(403, "Unauthorized access")

View File

@ -86,6 +86,7 @@ class FlightCreateSchema(BaseModel):
class FlightDisplaySchema(FlightCreateSchema): class FlightDisplaySchema(FlightCreateSchema):
user: PyObjectId
id: PyObjectId id: PyObjectId