Authentication Flow
SSO, Cognito, session creation, token validation, and service identity.
This document describes the end-to-end authentication flow from user login through API access.
Flow overview
- User initiates login — client redirects to Cognito hosted UI or custom auth screen
- Cognito authenticates — validates credentials, MFA if required, returns tokens
- Client stores tokens — access token, ID token, and refresh token in secure storage
- API requests — client sends Bearer access token with each request
- Backend validates — middleware verifies token signature, expiry, and scopes
- Token refresh — client uses refresh token before access token expires
Token lifecycle
// Client-side token refresh
async function getValidToken() {
const session = await fetchAuthSession();
if (session.tokens?.accessToken) {
return session.tokens.accessToken.toString();
}
await signInWithRedirect();
}Service-to-service auth
Internal services use IAM roles or service account tokens — never user credentials:
curl "$INTERNAL_API/v1/health" \
-H "Authorization: Bearer $SERVICE_TOKEN"Security boundaries
- User tokens are scoped to their permissions — never escalate
- Service tokens are scoped to specific internal endpoints
- Admin actions require additional MFA verification
- All auth events are logged for audit