How madhvesh77/sso-spike-backend works
Project type and purpose
• This is a small, self-contained FastAPI microservice whose sole purpose is to expose three HTTP endpoints and protect two of them with Microsoft Entra ID (Azure AD) JWT authentication.
• It validates incoming Bearer tokens by fetching the JSON Web Key Set (JWKS) from Azure AD, caches the keys, verifies signature, expiration, issuer, audience and enforces role-based access.
File structure
• Top level:
• auth.py → encapsulates all JWT validation logic and JWKS fetching/caching
• main.py → FastAPI app, defines routes, imports auth logic as dependency/middleware
• requirements.txt, .env.example → environment and dependency setup
• README.md → documentation
• No database, no message queues or other services—pure HTTP API and external Azure AD JWKS.
Readme insights
• The README describes endpoints, required environment variables (TENANT_ID, API_AUDIENCE), and the security features (RSA256 signature check, TTL-cached JWKS, RBAC).
• It clearly calls out three endpoints: /health (public), /api/profile (any valid JWT), /api/admin-only (requires “approver” role).
Main components
a. Client (could be web app, mobile app, curl, Postman)
b. FastAPI microservice
– API Gateway layer (uvicorn)
– Routing layer (/health, /api/profile, /api/admin-only)
– Authentication middleware (auth.py)
– JWKS cache component (in-memory TTL cache via cachetools)
c. Microsoft Entra ID (Azure AD) JWKS endpoint (external service)
Relationships and data flow
1. Client sends HTTP request to FastAPI.
2. Routing layer invokes authentication middleware for protected routes.
3. Auth module checks in-memory cache for signing keys.
– Cache hit → verify JWT signature, claims, roles
– Cache miss → HTTP GET to Azure AD JWKS endpoint → populate cache → verify
4. On success, control returns to route handler (profile or admin logic), returns JSON.
5. /health bypasses auth and returns {"ok": true}.
Architectural patterns and principles
• Separation of concerns: routing (main.py) vs. security logic (auth.py).
• Caching layer to reduce external calls (cachetools TTL).
• Dependency injection via FastAPI’s Depends for middleware.
• Stateless service: no local session store, JWT holds all identity data.