OIDC: Inability to request additional scopes

Issue Description

With the OIDC directory’s claimUsername set to a custom claim (e.g. mail_account), the server never includes the scope that provides the claim in the scopes it requests for sign-in. Providers that offer custom claims per scope, such as Authelia 4.39, therefore never include the claim. In required_scopes:

  1. Only the scopes of standard claims are known (OIDC Core §5.4). A custom claim’s scope is provider-defined (RFC 6749 §3.3), so it can’t be inferred.
  2. Any scope missing from scopes_supported is dropped, but providers MAY leave supported scopes unadvertised (OIDC Discovery §3; RFC 8414 §2). Authelia never advertises custom scopes.

Expected Behavior

The scopes requested at sign-in include “the scope that carries the claimUsername claim” (docs), so the claim is released and the server resolves the account it names.

This could be supported by an optional extraScopes list on the OIDC directory, appended to the computed scopes without the scopes_supported check. A minimal patch is attached.

Actual Behavior

No scope for the custom claim is requested, so the claim is never released. The server’s resolve_username then silently falls back to the email claim. The user is signed in to an account named after their email, or sign-in fails if there’s no email, instead of the configured claim being used.

Reproduction Steps

  1. In Authelia, define a custom scope mail_account releasing a custom claim mail_account to Stalwart’s client.
  2. In Stalwart’s OIDC directory, set claimUsername = "mail_account".
  3. Sign in through the WebUI and inspect the authorisation request: no mail_account scope is requested.
  4. Observe that the server resolves the account from email rather than mail_account, or that sign-in fails if no email is provided.

Stalwart Version

v1.x

Installation Method

NixOS

Database Backend

PostgreSQL

Blob Storage

S3-compatible

Search Engine

Meilisearch

Directory Backend

OIDC

Additional Context

Related: WebUI OIDC with Microsoft Entra ID always fails: InvalidSignature because SPA only requests Graph scopes - #5 by stalwart

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

Your reading of the source is correct. In the current source, required_scopes (crates/directory/src/backend/oidc/config.rs) behaves like this:

  • It builds the scope list from a fixed map of claim names to scopes: email and email_verified map to email. preferred_username, name, given_name, family_name and nickname map to profile. groups maps to groups.
  • Any claim not in that map, such as mail_account, is skipped with continue. No scope is requested for it.
  • Even mapped scopes are only added if they appear in the provider’s scopes_supported. If the provider doesn’t advertise scopes_supported, only the standard ones are added.

So with claimUsername = "mail_account" no scope carries that claim. Authelia also doesn’t advertise custom scopes, so an inferred scope would be dropped anyway. resolve_username then falls back to email, as you describe.

I found no extraScopes setting or any other way to add scopes in the source or docs. The docs say the requested scopes include the scope carrying the claimUsername claim, but that only holds for the standard claims in the map above. I haven’t checked your attached patch.

I can’t suggest a working workaround from the docs. Options I can see are to have Authelia release the claim under a standard scope that Stalwart already requests, if your provider setup allows that, or to wait for an extraScopes-style option. Whether that option gets added is up to the maintainers.

Docs: OpenID Connect | Stalwart

This isn’t covered in the current docs beyond the above. A community member or staff member will follow up on the feature request.

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

Update: it seems the attachment failed, as do subsequent attempts to attach a file. I also don’t see support for code blocks here.

Three backticks ```, Ctrl E or press the </> button in the formatting bar will give a code block.

Yea, I tried that but I’m only getting inline code blocks.

Hi @tecosaur,

The next weekly Stalwart release adds an oauthScopes list to the Application object. The WebUI appends those scopes to the ones it already computes, and it skips the scopes_supported check for them, so an unadvertised Authelia scope goes through too. For your setup, open the WebUI application, add mail_account to its OAuth Scopes, and the authorization request will carry it, so the claim comes back and claimUsername = "mail_account" resolves the right account.

I put it on the Application, not on the directory, because the WebUI is the only client that can use it. Mail, calendar and contacts apps that follow the OAuth Profile for Open Public Clients, Apple Mail for example, only ever request openid and the interoperable urn:ietf:params:oauth:scope:* scopes, and they have no setting for anything else. If you plan to sign in from apps like that, the claim has to be in every access token Authelia issues for Stalwart, or come with those scopes. The OIDC directory page now spells this out, and it no longer says a custom claim gets its scope requested automatically.