Issue Description
Environment
- Stalwart version: v0.16.26
- OS: Debian 13.6 (amd64)
- Directory type: Internal
- Authentication directory: External OIDC (Microsoft Entra ID)
Description
When configuring an external OIDC directory pointing to Microsoft Entra ID (Azure AD),
WebUI authentication fails with JWT validation failed: InvalidSignature.
The root cause is that the Stalwart WebUI SPA requests only standard OpenID Connect
scopes (openid email profile offline_access) in the authorization request to Entra.
Microsoft Entra responds with an access token whose audience is Microsoft Graph
(aud: 00000003-0000-0000-c000-000000000000), not the registered application.
Graph API access tokens are signed with Microsoft-internal keys that are not published
in the tenant’s JWKS endpoint, making them impossible to verify by any third-party service.
Expected Behavior
The authorization request sent to the external OIDC provider should include a
configurable list of scopes. When the operator adds an application-specific scope
such as api://client-id/Stalwart.Access, Entra issues a JWT with aud = client-id,
signed with the tenant public JWKS keys, which Stalwart can verify successfully.
Alternatively, the Required Scopes field already present in the OIDC directory
configuration could be reused: those scopes should be appended to the outgoing
authorization request, not only used for incoming token validation.
Requested fix: please add a configuration field to the OIDC directory
(for example additionalScopes) that allows operators to specify extra scopes
to be appended to the authorization request sent to the external OIDC provider.
This is a minimal change that would make Stalwart work correctly with Microsoft
Entra ID, one of the most widely deployed enterprise identity providers.
Actual Behavior
The WebUI redirects correctly to Entra, the user authenticates successfully,
and the browser returns to /account/oauth/callback with the authorization code.
The SPA exchanges the code directly with Entra via PKCE, obtains an access token,
and sends it as a Bearer token to /jmap/session.
Stalwart responds with:
Authentication error: JWT validation failed: InvalidSignature
causedBy: directory/src/core/dispatch.rs line 19
Decoding the access token sent by the SPA to /jmap/session shows:
aud: 00000003-0000-0000-c000-000000000000 (Microsoft Graph)
iss: sts.windows.net / tenant issuer
appid: our registered client-id
The audience is Microsoft Graph, not the registered application.
The token is signed with Microsoft-internal keys not present in the public JWKS endpoint.
Root cause: the Stalwart WebUI SPA hardcodes the OAuth scope request to:
openid email profile offline_access
Microsoft Entra only issues a verifiable JWT with aud = client-id when at least one
application-specific scope is included in the authorization request, for example:
api://client-id/Stalwart.Access
Since no application-specific scope is requested, Entra always returns a Graph token
with an unverifiable signature, regardless of requestedAccessTokenVersion,
Required Audience configuration in Stalwart, or any other App Registration setting.
Reproduction Steps
-
Install Stalwart v0.16.26 with internal directory.
-
Create an App Registration in Microsoft Entra ID:
- Supported account types: single tenant
- Redirect URI type Web: hostname/
- Redirect URI type SPA: hostname/account/oauth/callback
- Expose an API: set Application ID URI to api://client-id
- Add scope named Stalwart.Access
- API permissions: openid, email, profile, User.Read (Graph delegated)
- Grant admin consent
- Manifest: set requestedAccessTokenVersion to 2
-
In Stalwart WebUI, create an OIDC directory:
- Issuer URL: Entra ID v2.0 OIDC endpoint for the tenant
- Required Audience: client-id
- Required Scopes: openid, email
- Username Claim: preferred_username
- Name Claim: name
-
In Settings > Authentication > General, set Authentication Directory
to the newly created OIDC directory. -
Open the WebUI account page in an incognito browser window.
-
Enter a valid Entra user account and click Continue.
-
Complete Microsoft authentication (password + MFA if enabled).
-
Observe: browser returns to /account/oauth/callback, then immediately
receives 401 Unauthorized from /jmap/session.
Relevant Log Output
Logging level set to Trace. Entries during a failed authentication attempt:
TRACE HTTP response body: Stalwart correctly fetches the Entra OIDC discovery
document including jwks_uri and userinfo_endpoint. Status 200.
ERROR Authentication error:
reason = “Token validation error: JWT validation failed: InvalidSignature”
causedBy = “crates/directory/src/core/dispatch.rs:19”
Token signature cannot be verified because the signing key is not in the
public JWKS endpoint (Graph token signed with internal Microsoft key).
Authorization request captured from browser Network tab shows:
response_type=code
scope=openid email profile offline_access
Only standard OIDC scopes are present. No application-specific scope is included.
This causes Entra to issue a Graph token instead of a verifiable application token.
Stalwart Version
v0.16.x
Installation Method
Binary (Linux)
Database Backend
RocksDB
Blob Storage
RocksDB
Search Engine
Internal
Directory Backend
OIDC
Additional Context
- Stalwart deployed on internal DMZ network, not yet publicly exposed.
Testing via local hosts file. Server has full outbound internet access
and correctly reaches all Microsoft endpoints. Issue confirmed to be
independent of public reachability. - No reverse proxy. Stalwart handles TLS directly with a wildcard
Let’s Encrypt certificate. - Firewall: UFW active, ports 22 and 443 open inbound only.
- Entra ID P1 license. Users synchronized from on-premise Active Directory
via Entra Connect. - Test user exists in Entra without Exchange Online or M365 license.
This is intentional: the use case is providing local mailboxes on Stalwart
for occasional users without full M365 licenses, reusing corporate
credentials and MFA policies via SSO. - This issue blocks a real-world enterprise deployment scenario where
Microsoft Entra ID is the central identity provider and Stalwart
is the self-hosted mail server.
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