Documentation Index
Fetch the complete documentation index at: https://docs.emergence.ai/llms.txt
Use this file to discover all available pages before exploring further.
Authentication
CRAFT uses Keycloak for all authentication. Every API request requires a valid JWT token, and each organization maps to a dedicated Keycloak realm. This page covers OIDC flows, token management, machine-to-machine authentication, and session configuration.Authentication Architecture
- User Authentication
- Machine-to-Machine
Browser-based applications authenticate using the OIDC Authorization Code flow with PKCE:
- The frontend redirects the user to the Keycloak realm login page
- The user authenticates (password, SSO, or MFA)
- Keycloak returns an authorization code to the frontend callback URL
- The frontend exchanges the code for JWT tokens (access + refresh + ID)
- The access token is included in all API requests as
Authorization: Bearer <token>
JWT Token Structure
Access tokens issued by Keycloak contain the following claims used by the platform:| Claim | Source | Description |
|---|---|---|
sub | Keycloak | Unique user identifier |
iss | Keycloak realm URL | Issuer URL including the realm ID |
org_id | Derived from realm | Organization ID (realm ID = org ID) |
groups | Keycloak groups | Group memberships for role mapping |
aud | Keycloak client | Audience — identifies the intended recipient of the token |
exp | Keycloak | Token expiration timestamp |
iat | Keycloak | Token issuance timestamp |
Token Validation
Platform services validate JWT tokens using the following process:JWKS retrieval
The service fetches the Keycloak JWKS (JSON Web Key Set) from the realm’s well-known endpoint:
https://<keycloak-host>/realms/<org-id>/protocol/openid-connect/certsSignature verification
The token’s RS256 signature is verified against the public key from the JWKS endpoint.
Claims validation
The service validates the
iss (issuer) and exp (expiration) claims. Expired or malformed tokens receive a 401 Unauthorized response. Audience (aud) validation is not currently enforced; this is a known limitation tracked for remediation.Multi-Realm Architecture
Each organization in the platform is a separate Keycloak realm:| Aspect | Implementation |
|---|---|
| Realm ID | Equals the organization ID |
| User directory | Each realm has its own user store |
| IdP configuration | SSO providers are configured per realm |
| Client registrations | Applications and service accounts are per realm |
| Session management | Sessions are isolated per realm |
Single Sign-On (SSO)
Keycloak supports SSO via both OIDC and SAML 2.0 for enterprise identity providers:| IdP | Protocol | Configuration |
|---|---|---|
| Microsoft Entra ID | OIDC | App registration + group claims |
| Okta | OIDC | Application integration + group filter |
| Google Workspace | OIDC | OAuth client credentials |
| PingFederate | SAML 2.0 | SP metadata exchange |
SSO Features
- Per-tenant configuration: Each organization connects to its own IdP
- Just-in-time provisioning: Users are created on first SSO login
- Forced SSO: Organizations can disable password login, requiring all users to authenticate via the configured IdP
- Session management: Configurable idle timeout, max session duration, and forced re-authentication
Anonymous Access
The platform supports optional anonymous read-only access for public resources:| Setting | Value |
|---|---|
| Scope | Read-only access to PUBLIC entities via search and list endpoints |
| Rate limit | 30 requests per minute (target; must be enforced at ingress/WAF layer by deploying organization) |
| Redaction | Security schemes and credentials are omitted from responses |
| Default | Disabled per tenant; must be explicitly enabled |
| Implementation | OptionalAuth dependency returns None for unauthenticated requests |
Anonymous endpoints must be explicitly allowlisted in the service configuration. The standard
get_auth() dependency always requires a valid JWT.Rate Limiting
Rate limiting must be enforced at the ingress/WAF layer by the deploying organization. The following targets are recommended:| Operation Type | Recommended Limit | Enforcement |
|---|---|---|
| Writes (POST, PUT, PATCH, DELETE) | 60/min | Ingress/WAF policy |
| Reads (GET, POST :search) | 300/min | Ingress/WAF policy |
| Bulk operations | 10/min | Ingress/WAF policy |
| Anonymous reads | 30/min | Ingress/WAF policy |
429 Too Many Requests with a Retry-After header and include standard rate limit response headers (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset).
Token Management Best Practices
Token refresh strategy
Token refresh strategy
Use the refresh token to obtain new access tokens before the access token expires. Keycloak refresh tokens have a longer lifetime than access tokens. Implement proactive refresh (e.g., refresh when 75% of the token lifetime has elapsed) rather than waiting for a 401 response.
Client credential rotation
Client credential rotation
Rotate client secrets periodically. Keycloak supports multiple active client secrets during rotation, allowing a grace period for credential updates across services.
Token storage
Token storage
Never store tokens in localStorage for browser applications. Use httpOnly cookies or in-memory storage with the token refresh flow. For server-side applications, store tokens in encrypted session state.
Troubleshooting
401 Unauthorized on valid token
401 Unauthorized on valid token
SSO redirect loop
SSO redirect loop
Verify the redirect URI configured in the IdP matches the Keycloak broker endpoint. Check that the Keycloak realm has the identity provider enabled and the browser flow is configured correctly.
Groups claim missing from token
Groups claim missing from token
Ensure a group mapper is configured in the Keycloak client settings. The mapper must be set to add group memberships to the access token (not just the ID token).
Next Steps
SSO Integration
Configure SSO with Entra ID, Okta, or Google Workspace.
RBAC Configuration
Set up OpenFGA roles and permissions for authorization.
API Authentication
Learn how to authenticate API requests with JWT tokens.
Network Security
Review TLS and network policies for securing authentication flows.

