Overview
The MABQ Agent API uses Azure Active Directory (Azure AD) JWT tokens for authentication. All requests must include a valid Bearer token from an authorized@transelec.cl user.
Authentication Flow
Required Headers
All authenticated requests must include an Authorization header:Azure AD access token in Bearer format
Example Request
Middleware Implementation
The authentication middleware is implemented inmain.py as a FastAPI middleware:
Token Validation
The middleware performs the following validation steps:1. Token Extraction
The token is extracted from theAuthorization header and sanitized:
2. JWT Structure Validation
The token must have exactly 3 parts (header.payload.signature):3. Signature Verification
The token signature is verified using Azure AD’s public keys (JWKS):Only RS256 (RSA) signatures are accepted
Must match the
AZURE_CLIENT_ID environment variableValid Azure AD token issuers for the tenant
4. Domain Validation
The user’s email must end with@transelec.cl:
User Profile
Upon successful authentication, the user profile is attached to the request state:User’s email address from the token (
preferred_username, upn, or email claim)User’s display name from the
name claimAzure AD tenant ID from the token
Error Responses
The middleware returns a 403 Forbidden response for authentication failures:Missing Token
Expired Token
Invalid Signature
Unauthorized Domain
Generic Validation Error
Detailed error message explaining the authentication failure
Exempt Paths
The following paths bypass authentication:Swagger UI documentation (development only)
OpenAPI schema (development only)
Root path GET requests (bypasses auth, connects to ADK agent)
All OPTIONS requests (CORS preflight)
The middleware checks
request.url.path in ["/docs", "/openapi.json", "/health"] but no /health endpoint is actually defined in the application. The root path / with GET method is exempt for the ADK agent endpoint.CORS preflight requests
Environment Variables
Azure AD tenant ID (found in Azure Portal > Azure Active Directory > Overview)
Application (client) ID from Azure AD app registration
Logging
The middleware logs authentication events for audit purposes:Debug Logging
For troubleshooting, the middleware logs token details:Security Best Practices
Related Documentation
- FastAPI Endpoints - API endpoint reference
- CopilotKit Endpoint - Frontend authentication
- HTTP Agent - Authorization header forwarding