Authentication Core: - Security utilities: JWT token creation, validation, hashing - AuthService: Azure AD token validation via Microsoft Graph API - User session management with access/refresh tokens - Token expiration handling (1 hour access, 7 days refresh) API Endpoints: - POST /api/v1/auth/login - Login with Azure AD MSAL token - POST /api/v1/auth/refresh - Refresh access token - POST /api/v1/auth/logout - Logout and invalidate session - GET /api/v1/auth/me - Get current user info - GET /api/v1/auth/health - Auth service health check Middleware: - get_current_user: Extract and validate user from Bearer token - get_current_active_user: Ensure user is active - get_current_admin_user: Require admin role - get_optional_user: Optional authentication Security Features: - JWT with HS256 signing - Token hashing with bcrypt for storage - Session validation with expiration checks - Microsoft Graph API integration for Azure AD validation - IP address and user agent tracking - Active session management Schemas: - LoginRequest/Response with tokens and user info - RefreshTokenRequest/Response - UserInfo for current user details - LogoutResponse Main App Updates: - Connected auth router to /api/v1/auth - All authentication endpoints now accessible Dependencies Added: - pyjwt for JWT handling - httpx for async HTTP requests to Microsoft Graph Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
83 lines
1.8 KiB
Python
83 lines
1.8 KiB
Python
"""
|
|
APAC Ops Bot - FastAPI Application Entry Point
|
|
|
|
This is the main application file that initializes FastAPI,
|
|
configures middleware, and sets up routing.
|
|
"""
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
|
|
from app.config import settings
|
|
from app.database import init_db, close_db
|
|
|
|
# Import routers
|
|
from app.api.v1.router import api_router
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""
|
|
Lifespan context manager for startup and shutdown events
|
|
"""
|
|
# Startup
|
|
print(f"🚀 Starting {settings.APP_NAME}...")
|
|
print(f"📦 Environment: {settings.APP_ENV}")
|
|
await init_db()
|
|
print("✅ Database initialized")
|
|
|
|
yield
|
|
|
|
# Shutdown
|
|
print("🔄 Shutting down...")
|
|
await close_db()
|
|
print("✅ Cleanup complete")
|
|
|
|
|
|
# Initialize FastAPI application
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
description="AI-powered operations assistant for Oliver Agency's APAC region",
|
|
version="1.0.0",
|
|
docs_url="/docs" if settings.DEBUG else None,
|
|
redoc_url="/redoc" if settings.DEBUG else None,
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# Configure CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins_list,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
# Health check endpoint
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""
|
|
Health check endpoint for monitoring
|
|
"""
|
|
return {
|
|
"status": "healthy",
|
|
"app": settings.APP_NAME,
|
|
"environment": settings.APP_ENV,
|
|
}
|
|
|
|
|
|
# Include API routers
|
|
app.include_router(api_router, prefix="/api/v1")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
uvicorn.run(
|
|
"app.main:app",
|
|
host="0.0.0.0",
|
|
port=8000,
|
|
reload=settings.DEBUG,
|
|
)
|