OIDC bearer tokens are not routed to a domain-specific directory when the username claim has no domain (usernameDomain is ignored)

Issue Description

We use Stalwart Enterprise with the internal directory as the default authentication directory. One domain is assigned a domain-specific OIDC directory backed by Keycloak.

OIDC directory configuration:

  • Introspection: client ID stalwart, client secret, HTTP Basic (client_secret_basic)
  • Username Claim: preferred_username
  • Username Domain: our-domain.com
  • Name Claim: name
  • Groups Claim: not set

Login with a valid access token fails. Our token’s preferred_username is max.mustermann, without a domain. The directory is configured to append @our-domain.com, but that setting never takes effect. Stalwart has to pick a directory before it can apply that directory’s usernameDomain. Since the raw claim has no domain, no domain-specific directory is found, and Stalwart falls back to the default (internal) directory. The internal directory then fails to decode the external JWT.

This makes usernameDomain unusable for bearer-token authentication whenever the OIDC directory is assigned per domain rather than set as the default. It is a circular dependency: the setting is needed to find the directory, but it lives inside the directory.

This also affects the user creation of new users: When I would use an existing “mail” claim from our KeyCloak, where our LDAP Sync stores the internal user “mail” like [email protected] and I apply it to a domain our-domain.com, it also fails, because it does not find a domain “business.internal”, although we mapped it to that domain. Weird.

Expected Behavior

A bearer token issued by the configured OIDC provider should be routed to the domain-specific OIDC directory, even when the username claim contains no domain and the directory supplies one through usernameDomain.

Two ways to solve this, each without decoding the token against the wrong directory:

  • Issuer-based routing: match the token’s iss claim against the issuer or endpoint configured in each OIDC directory and select that directory.
  • Static claim / domain mapping: let an OIDC directory declare a claim (or a static value) that maps tokens to a domain, so routing does not depend on the username claim containing an @.

At the very least, the documentation for usernameDomain should state that it only works when the OIDC directory is the default authentication directory.

Actual Behavior

Authentication is rejected, and the error message points to the default directory even though a domain directory is configured:

Authentication failed
A login attempt was rejected due to incorrect credentials; repeated failures from the same IP may trigger an authentication ban.
Caused by: Authentication error
An internal error occurred while processing an authentication request; the login cannot proceed.
Reason: Failed to decode token. If you are using an external OIDC provider, make sure it is configured as the default directory under the Authentication object.
Caused by: crates/common/src/auth/oauth/token.rs:172

Cross-check: after switching the Username Claim to email (value [email protected]), clearing Username Domain, and assigning the OIDC directory to business.internal instead, login works. Routing therefore only works when the domain is already present in the raw claim value.

Reproduction Steps

  1. Keep the internal directory as the default Authentication Directory.
  2. Create an OIDC directory pointing to Keycloak, with Username Claim preferred_username and Username Domain example.com.
  3. Assign this OIDC directory to the domain example.com.
  4. Get an access token from Keycloak for a user whose preferred_username has no @ (e.g. john).
  5. Authenticate against Stalwart with the token (e.g. via webmail or JMAP).
  6. Observe “Failed to decode token” from token.rs:172.
  7. Change Username Claim to email, where the value contains the domain, clear Username Domain, and assign the directory to the email’s domain. Login now succeeds.

Relevant Log Output

Authentication failed
A login attempt was rejected due to incorrect credentials; repeated failures from the same IP may trigger an authentication ban.
Caused by: Authentication error
An internal error occurred while processing an authentication request; the login cannot proceed.
Reason: Failed to decode token. If you are using an external OIDC provider, make sure it is configured as the default directory under the Authentication object.
Caused by: crates/common/src/auth/oauth/token.rs:172

Stalwart Version

v1.x

Installation Method

Docker

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

Internal

Additional Context

example token contents:

{
  "iss": "https://keycloak.corp.de/realms/ucs",
  "aud": "account",
  "sub": "f:<redacted>:max.mustermann",
  "typ": "Bearer",
  "azp": "bulwark",
  "scope": "openid groups profile email",
  "email_verified": false,
  "name": "Max Mustermann",
  "preferred_username": "max.mustermann",
  "email": "[email protected]"
}

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Let me check the source for routing.

Issuer-based routing already exists in the server source, but it only runs as a fallback. In your token, the email claim is what stops it from running.

Before decoding a bearer token, Stalwart reads its payload without verifying it and looks for a domain. In get_directory_for_token (crates/common/src/auth/authentication.rs), the domain is the part after @ in the first of email, preferred_username or upn that contains an @. Routing then works like this:

  • A domain was found: the server looks up the directory for that domain, and falls back to the default directory if none is assigned.
  • No domain was found: the server matches iss against the issuerUrl of the OIDC directories. This is Enterprise-only, and it needs exactly one matching directory.

Your token has preferred_username: max.mustermann and email: [email protected]. The email claim yields the domain business.internal, so the issuer fallback is never tried. Nothing is assigned to business.internal, so the server uses the internal default directory and fails with “Failed to decode token”. usernameDomain is applied later, once the OIDC directory is already selected. That matches your cross-check, where assigning the directory to business.internal worked.

You have two ways to get the setup you want:

  1. Also assign the OIDC directory to the domain business.internal, which is the one in the email claim. Tokens then reach the OIDC directory. The account itself is still [email protected] through usernameDomain, and the domain check afterwards passes because that domain is served by the same directory.
  2. Remove the email claim from the access token, for example by dropping the email scope or its mapper in Keycloak. Routing then falls back to the issuer, as long as issuerUrl matches iss (https://keycloak.corp.de/realms/ucs) and only one OIDC directory claims that issuer.

I didn’t confirm this against a running server, so treat the explanation as a reading of the source. The docs’ per-domain section only says that bearer tokens are “routed by the claims of the token”. They don’t spell out the email precedence you hit.

Docs: Overview | Stalwart and OpenID Connect | Stalwart

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

Crazy. That worked. Thanks a lot. (Did not expect such helpful response of the AI agent - props @stalwart!)

But one thing would be great then: Having an option to control the scopes requested by the WebUI when requesting an OAuth Token from the OIDC Provider. Then, we could remove the “email” scope and have that whole problem fixed.

Hi,

The routing is fixed. A bearer token without a user name went to the directory of the domain in its email, preferred_username or upn claim, and the issuer was only compared when none of those held an address. So your email claim on business.internal, a domain the server doesn’t host, sent Keycloak’s tokens to the internal default directory before usernameDomain had any chance to apply. Now the server matches iss against the OpenID Connect directories first, and only falls back to the claim domain when no single directory claims that issuer. Your Keycloak directory gets its tokens even while they carry email, builds [email protected] from preferred_username and usernameDomain, and the check that the account’s domain is served by that same directory still applies.

The fix ships in the next weekly Stalwart release, and the directories page will describe the routing order: Overview | Stalwart. Once you’re on it, you can leave the email scope in Keycloak as it is.