Skip to content

Authentication Setup

Mozaiks uses a pluggable auth adapter system that works with any auth provider.

Quick Start

Running Without Authentication

A host with no authentication configuration at all refuses to start and names the choices below. To run without authentication, say so explicitly:

AUTH_ENABLED=false

Setting AUTH_ANON_ACCESS on its own is also an explicit choice, unless identity-provider settings are present: those turn authentication on, while AUTH_ENABLED=false keeps it off whatever else is set (and the host warns about the provider settings it ignores). Generated public apps ship AUTH_ANON_ACCESS=public this way.

No tokens are required, and AUTH_ANON_ACCESS decides whom the host serves:

AUTH_ANON_ACCESS Who is served What they get
local (default) requests from this machine only. Every other request gets no development access: routes that need an identity refuse it (HTTP 403, WebSocket closed with 1008), and routes that need none serve it like a request without credentials development access: the anonymous user with AUTH_ANON_ROLES and the development scopes, dev personas (below), trusted module dispatch
public every client an anonymous visitor: no roles, AUTH_ANON_SCOPES or access_as_user only, its own user id only, module permissions and entitlements enforced. Visitors share one identity: they see and change each other's chats and per-user records, and entitlements are checked against it. For public apps and their previews; Studio refuses it, and it cannot be combined with AUTH_ANON_ROLES
open every client that can reach the host development access, as for local. Use it only where the network itself limits who can connect, for example a container whose port is published only on 127.0.0.1 (the host browser reaches a container through Docker's gateway, not as this machine)

A request is from this machine only when all of these hold, and the refusal names the one that failed:

  • its peer is a loopback address (IPv4, IPv6, or IPv4-mapped IPv6);
  • it carries no X-Forwarded-For, X-Real-IP or Forwarded header at all. local is for a host your browser reaches directly; behind a reverse proxy use authentication, public or open. The web shell's dev server adds X-Forwarded-For only for clients on other machines;
  • its Host header, if any, names this machine: localhost, a .localhost name, or a loopback IP literal. Open the app at http://localhost:<port> or http://127.0.0.1:<port>, not at a name that merely resolves to 127.0.0.1;
  • its Origin header, if any, is an http/https origin on such a host, so pages from other sites cannot use your browser to reach the host;
  • without an Origin header, it does not carry Sec-Fetch-Site: cross-site. Browsers send no Origin on cross-site image and script loads or navigations, which are GET requests, and some GET routes change state (module dispatch, /api/me). Browsers without fetch metadata do not send the header, so this rule cannot stop them.

mozaiks serve and mozaiks studio refuse local on a --listen address other machines can reach.

For local user-to-user testing, such as DM notifications, keep auth disabled but assign each browser profile a different dev persona. The no-auth dependency accepts these request-scoped overrides only for requests with development access (AUTH_ENABLED=false, and this machine or AUTH_ANON_ACCESS=open):

  • Header: X-Mozaiks-Dev-User-Id: dev_alice
  • Cookie: mozaiks_dev_user_id=dev_alice
  • Query param for one-off API calls: ?dev_user_id=dev_alice

Optional comma-separated overrides are also supported:

  • X-Mozaiks-Dev-Roles, mozaiks_dev_roles, or dev_roles
  • X-Mozaiks-Dev-Scopes, mozaiks_dev_scopes, or dev_scopes

Browser workflow:

document.cookie = "mozaiks_dev_user_id=dev_alice; path=/";
document.cookie = "mozaiks_dev_roles=admin,user; path=/";

Use another browser profile or incognito window with mozaiks_dev_user_id=dev_bob. Sending a DM from dev_alice to dev_bob can create a notification for Bob; sending to yourself will not, because the messaging module excludes the sender from recipient_ids.


Provider Setup

Inspect a generated app's login

Authenticated draft previews initially show the running app's customer login. The artifact's App screen and Login screen controls switch the preview surface. The ChatUI Results control opens the workflow artifact beside its conversation. Expand artifact uses the existing view mode, and Return to chat restores the prior conversation layout and mounted artifact. The shared shell resolves /?inspect=login through the running app/config/auth.yaml login route. The login keeps its normal branding from app/brand/theme_config.json, copy and sign-in button, including when an app session already exists. Sign-in starts only after that button is clicked. Custom login components must preserve the exact inspect=login query while displaying their normal customer design and suppressing their signed-in redirect.

The query changes presentation only. It grants no roles, owner membership or Admin access. The explicit Open as owner action in artifact chrome uses the existing owner flow, which verifies /api/admin/config before offering Open Admin. The configured provider may reuse its browser session through OIDC and PKCE. Provider passwords stay at the provider. Self-hosters configure their own provider through the existing auth contract and environment settings.

Browser tests with mocked identity responses establish UI and request behavior. A real provider session and backend authorization require separate local or hosted acceptance evidence.

Embedded web shells use the existing authorization transport seam to open the provider in a popup after an explicit sign-in click. Ordinary top-level login keeps its redirect behavior. The popup returns through the configured same-origin callback route. Its transport-owned, expiring transaction marker binds the callback to its state. The receiving window checks the exact popup source, origin and callback before the shared adapter validates PKCE, state and nonce and exchanges the code. Only the callback URL crosses between these windows. Provider passwords stay in the provider window.

Custom callback pages can call relayBrowserAuthorization(callbackPath) before their ordinary callback handler. It returns true only for the matching owned popup transaction, including repeated effect execution. An unrelated opener does not select this path. Custom auth adapters receive the optional authorizationTransport through shell bootstrap and must explicitly support it when used inside an embedded shell. Browser transports declare kind: browser. Existing native transports retain their native callback validation.

A blocked or closed popup leaves sign-in retryable. Providers that sever the popup relationship require separate qualification. Artifact sandbox permissions remain unchanged. This transport grants no application roles or Admin authority.

Supabase

SUPABASE_URL=https://xyzcompany.supabase.co

That's it. The adapter auto-detects from SUPABASE_URL and constructs the JWKS endpoint automatically.

Optional: For local development with Supabase, add the JWT secret:

SUPABASE_JWT_SECRET=your-jwt-secret


Keycloak

KEYCLOAK_URL=https://keycloak.example.com
KEYCLOAK_REALM=myrealm
KEYCLOAK_CLIENT_ID=my-api  # required API audience, distinct from the browser client

Keycloak does not put the API client ID in a browser client's access-token aud claim by default. Create a separate API client and add an Audience mapper to the browser client (Included Client Audience = the API client, Add to access token on, Add to ID token off). The built-in adapter also requires Keycloak's signed typ: Bearer access-token claim.


Auth0

AUTH_PROVIDER=jwt
MOZAIKS_OIDC_AUTHORITY=https://your-tenant.auth0.com
AUTH_AUDIENCE=your-api-identifier  # required
AUTH_SCOPES_FORMAT=array

Set AUTH_JWKS_URL and AUTH_ISSUER only when you want to override OIDC discovery explicitly.


Generic OIDC (Okta, Azure AD, etc.)

AUTH_PROVIDER=jwt
MOZAIKS_OIDC_AUTHORITY=https://your-provider
AUTH_AUDIENCE=your-api  # required

AUTH_AUDIENCE is required for the jwt provider, whichever way it is selected. Every token's aud claim is verified against it; there is no setting that skips the check. The default access-token rule requires the RFC 9068 at+jwt JOSE header. For an issuer that uses another signed, access-token-only claim, set both AUTH_ACCESS_TOKEN_TYPE_CLAIM and AUTH_ACCESS_TOKEN_TYPE_VALUE to its claim name and access-token value. For Keycloak with the generic JWT adapter, use typ and Bearer. An ID token must not carry that value. The API audience must be distinct from the browser client ID.

For providers that require a tenant segment in the discovery URL, set:

MOZAIKS_OIDC_TENANT_ID=your-tenant-or-directory-id

You can also bypass authority composition with:

MOZAIKS_OIDC_DISCOVERY_URL=https://your-provider/.well-known/openid-configuration

Explicit overrides remain supported:

AUTH_JWKS_URL=https://your-provider/.well-known/jwks.json
AUTH_ISSUER=https://your-provider/

Independently Configured JWT Verifiers

Use the public adapter directly when a process validates tokens for multiple resources with distinct issuer, audience or claim settings:

from mozaiksai.core.auth import GenericJWTAdapter, JWTAdapterConfig

verifier = GenericJWTAdapter(config=JWTAdapterConfig(
    issuer="https://identity.example.com",
    jwks_url="https://identity.example.com/.well-known/jwks.json",
    audience="resource-api",
    scopes_claim="scope",
    required_scope="resource.read",
    clock_skew_seconds=0,
))
claims = await verifier.validate_token(access_token)

Both classes are also public through mozaiksai.core.auth.adapters. Reuse an instance for its configured resource; each instance owns its discovery and JWKS caches. Construction does not register the instance, change environment settings or replace the adapter returned by get_auth_adapter(). The latter continues to use the process configuration. JWTAdapterConfig.from_env(settings) accepts an explicit settings mapping when environment-style names are convenient. Config objects are mutable: treat them as fixed after constructing a verifier.

Signature, issuer, audience and access-token type checks are the same as for the process adapter. An audience is required. The default audience_mode="contains" accepts an audience list containing the configured value. Distinct configured audiences alone do not isolate a token listing both.

For a resource that requires its own token, set audience_mode="single" on its JWTAdapterConfig, or AUTH_AUDIENCE_MODE=single for the process JWT adapter. The signed aud must equal the configured audience as a string or a one-entry array. Additional entries, including duplicates, are refused. Apply this policy at every resource that might accept the token; tightening one verifier does not constrain another. Unknown modes fail configuration. The registry includes this setting in its configuration snapshot and cache identity; existing verifier instances retain their original configuration. This option belongs to the generic JWT adapter, not to every registered provider.

The default clock skew is 120 seconds. Set clock_skew_seconds=0 to reject an expired token without that allowance at validation time. This does not revoke already-returned claims or continuously authorize a connection. Token validation does not issue OAuth grants, establish product consent, grant membership or replace current resource authorization and revocation checks.

Claim Mappings

Different providers put user data in different JWT claims. Configure as needed:

Variable Default Description
AUTH_USER_ID_CLAIM sub Claim containing user ID
AUTH_EMAIL_CLAIM email Claim containing email
AUTH_EMAIL_VERIFIED_CLAIM email_verified, only while AUTH_EMAIL_CLAIM is email Boolean claim saying the AUTH_EMAIL_CLAIM value is verified
AUTH_NAME_CLAIM name Claim containing display name
AUTH_ROLES_CLAIM roles Claim containing user roles
AUTH_SCOPES_CLAIM scp Claim containing scopes
AUTH_SCOPES_FORMAT space space (Azure) or array (Auth0)

Admin allowlist needs a verified email

An email in app/app.json admins grants the admin role only when the identity provider says that email is verified: the token's verified-email claim must be the JSON boolean true. A missing claim, false, or a string such as "true" grants nothing. Without this rule, anyone could register an account with an admin's address on a provider with open sign-up, before the admin does, and become an admin. Matching ignores case and is ASCII-only: a non-ASCII address, in the token or in admins, never matches, because case folding can turn one address into another.

An admin role issued in the token needs no verified email, but the provider must issue it where the adapter reads it. The generic jwt adapter reads only the top-level claim named by AUTH_ROLES_CLAIM (default roles); it does not read Keycloak's nested realm_access.roles. The keycloak adapter reads realm_access.roles and the resource_access roles of KEYCLOAK_CLIENT_ID. The packaged Keycloak realm defines no admin role and no roles mapper, so on the packaged Compose stack (generic jwt) the working path is a verified email; see Admin access and email verification for both options.

Provider Verified-email claim
Generic JWT (jwt) AUTH_EMAIL_VERIFIED_CLAIM. Unset, it is email_verified (the standard OIDC claim) while AUTH_EMAIL_CLAIM is email, and nothing otherwise. Keycloak access tokens carry email_verified through the email client scope. A provider that adds email to access tokens with a custom claim or action must add the verified flag the same way.
Keycloak (keycloak) email_verified, from the realm's built-in email client scope
Supabase (supabase) top-level email_verified only. Supabase does not issue one by default; add it with a custom access token hook. The copy inside user_metadata is writable by the user and never counts.
No auth (none) never verified. With authentication off, a request with development access still uses the allowlist for AUTH_ANON_EMAIL or a dev persona's X-Mozaiks-Dev-Email, since it can already take any role; or use AUTH_ANON_ROLES=admin,user

email_verified vouches for the email claim and nothing else. If you point AUTH_EMAIL_CLAIM at another claim, such as preferred_username or upn, and leave AUTH_EMAIL_VERIFIED_CLAIM unset, no email counts as verified, so admins never grants admin, and the backend logs an AUTH_EMAIL_VERIFIED_CLAIM_UNSET warning when it builds the adapter. A username is not a verified address: on a realm with open sign-up, anyone can choose an admin's address as their username. When you set both settings, Mozaiks trusts the pairing, so set AUTH_EMAIL_VERIFIED_CLAIM only to a boolean claim that vouches for the value of the AUTH_EMAIL_CLAIM claim.

Microsoft Entra ID issues no email_verified claim. Grant admin there through an app role in the roles claim, or set AUTH_EMAIL_VERIFIED_CLAIM to an optional claim that vouches for the claim AUTH_EMAIL_CLAIM names in your tenant.

A refused allowlisted email gets the same 403 Admin access required as any other non-admin, so the response does not reveal which addresses are admins. The server logs an ADMIN_EMAIL_UNVERIFIED warning naming the user id and provider.


Auto-Detection

If AUTH_PROVIDER is not set, the system auto-detects based on environment variables:

If this is set... Provider used
AUTH_ENABLED=false none
SUPABASE_URL supabase
KEYCLOAK_URL + KEYCLOAK_REALM keycloak
AUTH_JWKS_URL + AUTH_ISSUER jwt
MOZAIKS_OIDC_AUTHORITY or MOZAIKS_OIDC_DISCOVERY_URL jwt
AUTH_ANON_ACCESS only (no provider above) none (explicit, with that posture)
Nothing none (implicit demo mode: hosts refuse to start)

Resolution is canonical and fails closed. One parser (mozaiksai.core.auth.adapters.registry.resolve_auth_config) interprets the auth environment into an immutable resolved state; every predicate, the adapter cache, and startup validation consume that same interpretation:

  • AUTH_ENABLED=true with no detectable provider is a fatal configuration error in every environment (startup refuses to boot; provider resolution raises). The runtime never silently falls back to the trusted-bypass none adapter when auth was explicitly requested.
  • Contradictory explicit declarations are fatal instead of silently picking one: AUTH_ENABLED=true + AUTH_PROVIDER=none, and AUTH_ENABLED=false
  • a real explicit AUTH_PROVIDER. (Passive provider signals such as a SUPABASE_URL left in a developer .env do not contradict an explicit disable — the explicit switch wins over passive presence.)
  • An unknown AUTH_PROVIDER value, or a selected provider whose configuration cannot validate tokens (for example AUTH_PROVIDER=jwt without JWKS/issuer/discovery settings), is fatal.
  • A token-validating provider without its audience binding is fatal: jwt requires AUTH_AUDIENCE and keycloak requires KEYCLOAK_CLIENT_ID. Both adapters verify the aud claim on every token and reject tokens without one, so on an issuer shared by several applications a token minted for another application never validates.
  • An unrecognized AUTH_ENABLED value (for example a typo like tru) is fatal instead of silently disabling auth.
  • Unauthenticated operation is allowlisted, not denylisted. No-auth operation — explicit AUTH_ENABLED=false, explicit AUTH_PROVIDER=none, or implicit demo mode — is permitted only when the resolved environment is one of the recognized local names development, local, test, or when no environment is configured at all. Every other explicit value rejects it, fatally, at startup and at provider resolution, independent of MOZAIKS_STARTUP_CHECKS mode. That includes known deployments (production, staging) and unknown, short, or regional names such as dev, prod, prod-us, staging-eu, production-east, preview, qa, or any custom value — an unrecognized environment never inherits development privilege. Unknown environments may still boot normally with authentication configured.
  • Implicit demo mode (none without explicit disablement) applies only when no auth configuration is present at all. It serves nobody: hosts refuse to start in it, whatever MOZAIKS_STARTUP_CHECKS says, and a request that reaches one anyway (a host composed without its lifespan) is refused with HTTP 401. Resolution itself still succeeds, so request-time predicates keep working in processes that configure no auth.
  • Development privileges are bound to the principal when it is created, never read from the process-wide auth switch. Anonymous roles, dev personas, trusted module dispatch, naming other users, and the local branch of the billing fulfillment ingress need a principal with development access (has_local_development_access): an explicit disablement, an environment that permits it, AUTH_ANON_ACCESS local or open, and, for local, a request from this machine. Admin needs the admin role or the email allowlist; authentication being off no longer lets a principal without roles into the admin API. With development access and no admin role the refusal says to set AUTH_ANON_ROLES=admin,user.

ENV / ENVIRONMENT resolution

ENV is the primary signal and ENVIRONMENT the fallback, but the two are resolved canonically rather than by precedence alone:

  • both values are trimmed first; empty or whitespace-only means absent, so a blank ENV never masks a non-blank ENVIRONMENT
  • values compare case-insensitively; there are no short-name aliases
  • if both are non-blank and resolve to different environments (for example ENV=development with ENVIRONMENT=production), that is a fatal configuration error — the runtime refuses to silently choose one
  • if both resolve to the same environment, that is accepted
  • both absent keeps the documented implicit local default

Adapter cache coherence

The cached adapter is keyed by a fingerprint derived from the same immutable configuration snapshot the adapter is constructed from, covering the complete set of inputs for the resolved provider — not just provider selection. Changing any meaning-bearing value (issuer, JWKS/discovery URL, audience, any claim mapping, scope format, clock skew, algorithms, JWKS or discovery cache TTL, Keycloak claim mappings, Supabase secret, anonymous persona settings) rebuilds the adapter, so request-time validation always uses the configuration startup validated.

An adapter is bound to its snapshot for its whole lifetime, including the discovery and JWKS clients it creates lazily on first use. Those clients receive the adapter's own URLs and cache TTLs, and OIDCDiscoveryClient and JWKSClient never read the environment, so an adapter created under one configuration can never begin validating tokens against another after the environment changes. A configuration change produces a new adapter; the previous one stays internally consistent.

AUTH_JWKS_CACHE_TTL (default 3600) and AUTH_DISCOVERY_CACHE_TTL (default 86400) must be integer seconds, zero or greater, where 0 means always refetch and is never treated as "unset". Absent, empty, and whitespace-only values all normalize to the same canonical default, resolved once by mozaiksai.core.auth.cache_ttl and reused by configuration resolution and the adapter's own config — the two can never disagree. Malformed values fail startup rather than surfacing later during lazy client construction on a request path. Cache expiry compares elapsed time against the TTL rather than adding it to a timestamp, so any accepted value — including a very large one — stays valid during real cache use.

Custom adapter contracts

Custom adapters registered via register_adapter declare their own cache identity:

register_adapter("my-custom", MyCustomAdapter, config_identity=lambda: cfg.revision)

config_identity must be a non-blank string, or a callable returning one, that changes whenever the adapter's configuration changes. Malformed identities fail closed rather than being coerced: a non-string value, an empty or whitespace-only string, or a callable that raises is rejected with an AuthError (literal values are validated at registration; callable results at each resolution). Without a config_identity the adapter is deliberately never cached — it is rebuilt on every resolution — so a changed custom configuration can never silently reuse an adapter validated under an older one. Re-registering a provider also invalidates any cached adapter.

Adapter constructors receive the configuration snapshot as settings=. The constructor contract is established positively at registration, before any construction attempt, and registration fails rather than assuming an adapter takes no configuration:

A mode is accepted only when the runtime's exact invocation — Adapter(settings=...) or Adapter() — binds successfully against the complete signature. Naming a usable settings parameter is necessary but not sufficient: a constructor that also demands arguments the runtime cannot supply is rejected when it registers, not at first use.

Constructor Outcome
(settings) / (*, settings) snapshot supplied
(settings=None, optional=None) snapshot supplied
(**kwargs) snapshot supplied
() / all parameters optional / (*args) constructed with no arguments
(settings, required) / (settings, *, required) rejected — the call cannot bind
(required, **kwargs) rejected — the call cannot bind
(settings) positional-only rejected — make it keyword-accessible
a required parameter the runtime cannot supply rejected
signature cannot be inspected rejected unless constructor_mode is declared

For constructors that genuinely cannot be inspected (C extensions, exotic callables), declare the contract explicitly:

register_adapter("my-custom", MyAdapter, config_identity="v1",
                 constructor_mode="settings_keyword")  # or "no_settings"

When the signature is inspectable the declaration is verified against it, so explicit metadata cannot claim an invocation that provably would not work.

An exception raised inside a constructor body — including TypeError — is a real construction failure and fails closed. The runtime never retries construction without the snapshot.

Privileged HTTP surfaces can additionally require authenticated provenance: UserPrincipal.is_authenticated is true only for principals produced by validating a real bearer token against the configured adapter. Anonymous principals with development access and request-scoped dev personas may carry admin-looking roles/scopes, but they are never authenticated provenance. has_local_development_access is true only for those two; an anonymous visitor (AUTH_ANON_ACCESS=public) has neither.


Custom Adapter

Register your own auth provider:

from mozaiksai.core.auth.adapters import register_adapter, UserClaims, BaseAuthAdapter

class MyAuthAdapter(BaseAuthAdapter):
    name = "my-provider"

    async def validate_token(self, token: str) -> UserClaims:
        # Your validation logic
        decoded = my_validate(token)
        return UserClaims(
            user_id=decoded["sub"],
            email=decoded.get("email"),
            roles=decoded.get("roles", []),
            scopes=[],
            raw_claims=decoded,
            provider=self.name,
        )

    def is_enabled(self) -> bool:
        return bool(os.getenv("MY_AUTH_SECRET"))

# Register before app startup
register_adapter("my-provider", MyAuthAdapter)

Then set:

AUTH_PROVIDER=my-provider
MY_AUTH_SECRET=...


WebSocket Auth

Browsers cannot set request headers on a WebSocket handshake, so the access token is carried in the Sec-WebSocket-Protocol header — the one handshake header the browser WebSocket API exposes. This is the normal production path and requires no opt-in.

Clients offer two subprotocol values, the marker followed by the base64url-encoded (unpadded) token:

new WebSocket(url, ['mozaiks.bearer.v1', base64url(accessToken)]);

The shared browser adapter does this for you:

import { openAuthenticatedWebSocket } from '@mozaiks/chat-ui/adapters/websocketAuth.js';

const socket = openAuthenticatedWebSocket(wsUrl, accessToken);

The runtime decodes the token, validates it through the configured auth adapter — the same adapter and the same validation as HTTP routes — binds the resulting WebSocketUser, and selects only mozaiks.bearer.v1 on accept. The credential is never echoed back and never appears in the URL.

Missing or invalid credentials close the connection with code 1008 before accept.

Query-param tokens (local development only)

A query-string token (?access_token=...) is rejected by default. Tokens in URLs land in server access logs, browser history, Referer headers, and shared links. It remains available as an explicit opt-in for local development and for non-browser clients that cannot use the subprotocol path:

MOZAIKS_WS_ALLOW_QUERY_TOKEN=true   # never set this in production or staging

Leave it false (the default) everywhere else. Production browser clients do not need it.

The flag covers the WebSocket handshake only. HTTP routes read the token from the Authorization: Bearer header and never from the URL, whatever the flag says.