8000 Allow replacing existing federated identity link for same user (opt-in) by Oluwatobi-Mustapha · Pull Request #48176 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Allow replacing existing federated identity link for same user (opt-in) - #48176

Open
Oluwatobi-Mustapha wants to merge 1 commit into
keycloak:mainfrom
Oluwatobi-Mustapha:fix-35059-client-initiated-link-replace
Open

Oluwatobi-Mustapha wants to merge 1 commit into
keycloak:mainfrom
Oluwatobi-Mustapha:fix-35059-client-initiated-link-replace

Conversation

@Oluwatobi-Mustapha
@Oluwatobi-Mustapha Oluwatobi-Mustapha commented Apr 16, 2026 •
Copy link
Copy Markdown
Contributor

Closes #35059

Let users switch their linked external account without unlinking first when the identity provider opts in.

  • Users can still relink the same account to refresh its token. Keycloak rejects replacement accounts already linked to someone else.
  • Replacing a link clears the old account's stored credentials from the user's online and offline sessions.

@Oluwatobi-Mustapha
Copy link
Copy Markdown
Contributor Author

Hi @sguilhen, I’ve pushed a fix for #35059: #48176

It follows the direction we discussed:

  • keeps current behavior by default
  • adds an opt-in replacement path when the same user already has a link for the provider
  • preserves the single-link-per-(user,provider) invariant
  • adds coverage for disabled/enabled replacement and legacy client-initiated linking

Could you please review and let me know if you want any adjustments?

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

Adds an opt-in identity provider configuration that allows client-initiated account linking to replace an existing federated identity link for the same user (instead of failing), and emits a dedicated audit event when that override occurs.

Changes:

  • Introduces allowLinkingWithExistingFederatedIdentity (config key + accessor methods) on IdentityProviderModel.
  • Updates account-linking flow to detect an existing link for the current user and either fail (default) or replace the link (opt-in), emitting FEDERATED_IDENTITY_OVERRIDE_LINK.
  • Extends broker tests to cover both “replacement disabled” and “replacement enabled” scenarios.

Reviewed changes

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

File Description
services/src/main/java/org/keycloak/services/resources/IdentityBrokerService.java Adds opt-in replacement logic for existing federated identity links and emits an override-link event.
server-spi/src/main/java/org/keycloak/models/IdentityProviderModel.java Adds config key + getter/setter for the new opt-in behavior.
testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/KcOidcBrokerIdpLinkActionTest.java Adds integration tests validating default failure vs opt-in replacement behavior and event emission.

Copilot AI review requested due to automatic review settings June 19, 2026 15:17
@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from 4129a4f to f03f25b Compare June 19, 2026 15:17
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from f03f25b to 401da9d Compare June 19, 2026 15:25
Copilot AI review requested due to automatic review settings June 19, 2026 15:34
@Oluwatobi-Mustapha
Oluwatobi-Mustapha requested a review from a team as a code owner June 19, 2026 15:35
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@sguilhen
Copy link
Copy Markdown
Contributor

@Oluwatobi-Mustapha Both new tests (testAccountLinkingWithExistingLinkFailsWhenReplacementDisabled and testAccountLinkingWithExistingLinkReplacedWhenEnabled) fail in CI with:

java.io.NotSerializableException: org.keycloak.testsuite.broker.KcOidcBrokerIdpLinkActionTest

The addFederatedIdentity and assertFederatedIdentity helper methods use testingClient.server().run(session -> { ... }), and the lambdas reference bc.getIDPAlias() which implicitly captures this (the test class instance). Since the test class isn't Serializable, the lambda can't be serialized for remote execution. The fix is to capture bc.getIDPAlias() into a local variable before the lambda:

String idpAlias = bc.getIDPAlias();
testingClient.server(bc.consumerRealmName()).run(session -> {
    // use idpAlias instead of bc.getIDPAlias()
});

Copilot AI review requested due to automatic review settings June 19, 2026 19:22
@Oluwatobi-Mustapha
Copy link
Copy Markdown
Contributor Author

@sguilhen Like you hinted, I added a docs section for the new option, covering the default behavior, the opt-in replacement behavior, and the fact that replacement only applies to the same authenticated user. I also kept the implementation note explaining why the replacement path uses remove+add instead of updateFederatedIdentity(…)

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.broker.KcOidcBrokerTest#loginWithExistingUserWithBruteForceEnabled

Keycloak CI - Java Distribution IT (windows-latest - temurin - 21)

org.openqa.selenium.TimeoutException: 
Expected condition failed: waiting for value to contain (ignoring case) "sign in to". Current value: "AUTH_RESPONSE" (tried for 5 second(s) with 500 milliseconds interval)
Build info: version: '4.28.1', revision: '73f5ad48a2'
System info: os.name: 'Windows Server 2025', os.arch: 'amd64', os.version: '10.0', java.version: '21.0.11'
Driver info: org.jboss.arquillian.drone.webdriver.htmlunit.DroneHtmlUnitDriver_ByGraphene
...

Report flaky test

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from df4f4d6 to bad6606 Compare October 10, 2026 08:25
Copilot AI balanced review requested due to automatic review settings October 10, 2026 08:25
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.

🟡 Changes recommended

Replacement currently retains credentials belonging to the previous external account.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 10, 2026 09:10
@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from bad6606 to c263334 Compare October 10, 2026 09:10
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.

🔵 Needs a closer look

Identity-link replacement is security-sensitive and warrants final human review despite no concrete defects found.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from c263334 to 960c9c6 Compare November 26, 2026 13:53
Copilot AI balanced review requested due to automatic review settings November 26, 2026 13:53
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.

🟡 Changes recommended

Account-replacement tests lack coverage in the supported-database suite, and the documentation needs to distinguish relinking the same external account from replacing it.

2 open issues

Review effort: Balanced

@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from 960c9c6 to 761f955 Compare November 26, 2026 14:17
Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:17
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.

🟡 Changes recommended

Replacing an account link can retain the previous account’s refresh token in active sessions, allowing token exchange as the wrong account and potentially persisting its token against the new link.

1 open issue
2 resolved since last review

Review effort: Balanced

@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from 761f955 to 37a12ed Compare November 26, 2026 14:44
Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:44
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.

🟡 Changes recommended

Link replacement can persist and report a deprecated provider ID when both ownership lookups miss, and current test results for all supported database jobs are missing.

3 open issues
1 resolved since last review

Review effort: Balanced

Add opt-in replacement for client-initiated account linking and document the setting.

Closes keycloak#35059

Signed-off-by: Oluwatobi Mustapha <oluwatobimustapha539@gmail.com>
@Oluwatobi-Mustapha
Oluwatobi-Mustapha force-pushed the fix-35059-client-initiated-link-replace branch from 37a12ed to 2b45edb Compare November 27, 2026 09:52
Copilot AI balanced review requested due to automatic review settings November 27, 2026 09:52
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.

🟢 Approval recommended

No active review issues are reported for the opt-in identity-provider link replacement change.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

@Oluwatobi-Mustapha
Copy link
Copy Markdown
Contributor Author

@sguilhen, please review when you can.

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.

Client-initiated account linking fails when user already has a federated identity

4 participants

0