Spaces:
Paused
Paused
Download shared/auth_example.py from VAH6002/ai_data_scientist: direct link, hf CLI and curl.
- Browser
- Download file 3.78 kB
-
https://huggingface.co/spaces/VAH6002/ai_data_scientist/resolve/main/shared/auth_example.py
- Command line
-
hf download hf://spaces/VAH6002/ai_data_scientist/shared/auth_example.py
-
curl -L -o auth_example.py https://huggingface.co/spaces/VAH6002/ai_data_scientist/resolve/main/shared/auth_example.py
3.78 kB
| """ | |
| Example usage of Clerk authentication integration. | |
| Demonstrates how to set up FastAPI with Clerk authentication. | |
| """ | |
| from fastapi import FastAPI, Depends, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import uvicorn | |
| from shared.middleware import AuthenticationMiddleware, require_auth, require_admin_permission | |
| from shared.auth_endpoints import auth_router, oauth_router | |
| from shared.auth import UserContext | |
| from shared.config import settings | |
| def create_app() -> FastAPI: | |
| """Create FastAPI application with Clerk authentication.""" | |
| app = FastAPI( | |
| title="MCP AI Data Scientist API", | |
| description="AI-powered data science platform with Clerk authentication", | |
| version="1.0.0" | |
| ) | |
| # Add CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Configure appropriately for production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Add authentication middleware | |
| app.add_middleware( | |
| AuthenticationMiddleware, | |
| exclude_paths=[ | |
| "/health", | |
| "/docs", | |
| "/redoc", | |
| "/openapi.json", | |
| "/oauth/.well-known/oauth-authorization-server" | |
| ] | |
| ) | |
| # Include authentication and OAuth routers | |
| app.include_router(auth_router) | |
| app.include_router(oauth_router) | |
| # Example protected endpoints | |
| async def root(): | |
| """Public root endpoint.""" | |
| return {"message": "MCP AI Data Scientist API", "status": "running"} | |
| async def health_check(): | |
| """Health check endpoint (no auth required).""" | |
| return {"status": "healthy", "service": "mcp-ai-data-scientist"} | |
| async def protected_endpoint(user: UserContext = Depends(require_auth)): | |
| """Example protected endpoint requiring authentication.""" | |
| return { | |
| "message": "This is a protected endpoint", | |
| "user_id": user.clerk_user_id, | |
| "email": user.email, | |
| "permissions": user.permissions | |
| } | |
| async def admin_only_endpoint(user: UserContext = Depends(require_admin_permission)): | |
| """Example admin-only endpoint.""" | |
| return { | |
| "message": "This endpoint requires admin permissions", | |
| "user_id": user.clerk_user_id, | |
| "admin": True | |
| } | |
| async def start_analysis( | |
| business_context: str, | |
| user: UserContext = Depends(require_auth) | |
| ): | |
| """Example endpoint to start data analysis (requires authentication).""" | |
| # This would integrate with the MCP servers | |
| return { | |
| "message": "Analysis started", | |
| "user_id": user.clerk_user_id, | |
| "business_context": business_context, | |
| "session_id": "mock-session-123" | |
| } | |
| return app | |
| # Example of how to run the application | |
| if __name__ == "__main__": | |
| app = create_app() | |
| uvicorn.run( | |
| app, | |
| host=settings.fastapi_host, | |
| port=settings.fastapi_port, | |
| reload=settings.fastapi_debug | |
| ) | |
| # Example usage in other modules: | |
| """ | |
| from shared.auth_example import create_app | |
| from shared.middleware import require_auth, require_write_permission | |
| from shared.auth import UserContext | |
| app = create_app() | |
| @app.post("/my-endpoint") | |
| async def my_endpoint(user: UserContext = Depends(require_write_permission)): | |
| # This endpoint requires write permission | |
| return {"user": user.clerk_user_id} | |
| """ |