from datetime import timedelta from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import OAuth2PasswordRequestForm from fastapi.middleware.cors import CORSMiddleware from users import Token, User, fake_users_db from auth_utils import ( authenticate_user, create_access_token, get_current_active_user, ACCESS_TOKEN_EXPIRE_MINUTES ) app = FastAPI(title="Trading Agents API", version="1.0.0") # Configure CORS origins = [ "http://localhost", "http://localhost:3000", "http://localhost:8000", "http://localhost:8080", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.post("/token", response_model=Token) async def login(form_data: OAuth2PasswordRequestForm = Depends()): """Login endpoint to get access token.""" user = authenticate_user(fake_users_db, form_data.username, form_data.password) if not user: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", headers={"WWW-Authenticate": "Bearer"}, ) access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) access_token = create_access_token( data={"sub": user.username}, expires_delta=access_token_expires ) return {"access_token": access_token, "token_type": "bearer"} @app.get("/users/me", response_model=User) async def read_users_me(current_user: User = Depends(get_current_active_user)): """Get current user information.""" return current_user @app.get("/protected") async def protected_route(current_user: User = Depends(get_current_active_user)): """Example protected route.""" return {"message": f"Hello {current_user.username}, this is a protected route!"} @app.get("/") async def root(): """Root endpoint.""" return {"message": "Trading Agents API with OAuth2"} @app.get("/health") async def health_check(): """Health check endpoint.""" return {"status": "healthy"}