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
- Keep the internal directory as the default Authentication Directory.
- Create an OIDC directory pointing to Keycloak, with Username Claim preferred_username and Username Domain example.com.
- Assign this OIDC directory to the domain example.com.
- Get an access token from Keycloak for a user whose preferred_username has no @ (e.g. john).
- Authenticate against Stalwart with the token (e.g. via webmail or JMAP).
- Observe “Failed to decode token” from token.rs:172.
- 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