Skip to content

Honor explicit username mapper over email-as-username during first-broker-login - #50429

Open
gaurav0107 wants to merge 1 commit into
keycloak:mainfrom
gaurav0107:fix/49300-usernametemplatemapper-is-overridden-by
Open

gaurav0107 wants to merge 1 commit into
keycloak:mainfrom
gaurav0107:fix/49300-usernametemplatemapper-is-overridden-by

Conversation

@gaurav0107

Copy link
Copy Markdown
Contributor

Closes #49300

Summary

When a realm has Email as username (registrationEmailAsUsername) enabled,
an explicitly configured identity-provider UsernameTemplateMapper was silently
ignored during first-broker-login: the brokered user's username was set to the
email instead of the mapped value.

The root cause is an inconsistency between two code paths:

  • IdentityBrokerService already resolves the username with the correct
    precedence — it keeps the mapper-provided modelUsername when present and only
    falls back to the email (when email-as-username is enabled) if no mapper set a
    username.
  • IdpCreateUserIfUniqueAuthenticator#getUsername then re-derived the username
    with realm.isRegistrationEmailAsUsername() ? brokerContext.getEmail() : brokerContext.getModelUsername(), unconditionally preferring the email and
    clobbering the already-resolved value.

This change makes the authenticator honor the resolved modelUsername and fall
back to the existing email-as-username behavior only when it is null. The fix
is also inherited by IdpDetectExistingBrokerUserAuthenticator. Realms without a
username mapper are unaffected: modelUsername already equals the email in that
case, so the behavior is unchanged.

Testing

Added usernameTemplateMapperShouldTakePrecedenceOverEmailAsUsername to
UsernameTemplateMapperTest, which enables registrationEmailAsUsername on the
consumer realm, performs first-broker-login, and asserts the created user's
username equals the mapper template result (${ALIAS}_${CLAIM.sub}) rather than
the email.

Note on tooling

This contribution was prepared with the assistance of an AI coding agent. I have
reviewed and understand every change, and I am able to explain and revise it in
response to review feedback.

@gaurav0107
gaurav0107 marked this pull request as ready for review July 1, 2026 20:01
@gaurav0107
gaurav0107 requested review from a team as code owners July 1, 2026 20:01
Copilot AI review requested due to automatic review settings July 1, 2026 20:01
@gaurav0107
gaurav0107 requested review from a team as code owners July 1, 2026 20:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a first-broker-login inconsistency where a configured UsernameTemplateMapper was overridden by the realm-level Email as username setting, causing brokered users to be created with their email instead of the mapped username.

Changes:

  • Update IdpCreateUserIfUniqueAuthenticator#getUsername to honor the already-resolved BrokeredIdentityContext.modelUsername and only fall back to email-as-username when modelUsername is null.
  • Add a regression test ensuring UsernameTemplateMapper takes precedence even when registrationEmailAsUsername is enabled.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
services/src/main/java/org/keycloak/authentication/authenticators/broker/IdpCreateUserIfUniqueAuthenticator.java Stops re-deriving/clobbering the brokered username so mapper-provided modelUsername wins over email-as-username.
testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/UsernameTemplateMapperTest.java Adds a regression test covering mapper precedence when registrationEmailAsUsername is enabled.

…oker-login

When a realm has "Email as username" enabled, IdpCreateUserIfUniqueAuthenticator
ignored a username supplied by an identity-provider UsernameTemplateMapper and
used the email instead. IdentityBrokerService already resolves the username with
the correct precedence (mapper value first, email as fallback), so honor that
resolved modelUsername in the authenticator rather than re-applying the
email-as-username rule unconditionally.

Closes keycloak#49300

Signed-off-by: gaurav0107 <gauravdubey0107@gmail.com>
@gaurav0107
gaurav0107 force-pushed the fix/49300-usernametemplatemapper-is-overridden-by branch from 8d45bef to 741c825 Compare July 3, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UsernameTemplateMapper is overridden by realm setting "Email as username"

2 participants