-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Rotated client secret remains valid when the feature is disabled #50910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/base/src/test/java/org/keycloak/tests/client/ClientSecretRotationDisabledTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package org.keycloak.tests.client; | ||
|
|
||
| import org.keycloak.common.util.Time; | ||
| import org.keycloak.models.ClientSecretConstants; | ||
| import org.keycloak.testframework.annotations.InjectUser; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.oauth.OAuthClient; | ||
| import org.keycloak.testframework.oauth.annotations.InjectOAuthClient; | ||
| import org.keycloak.testframework.realm.ManagedUser; | ||
| import org.keycloak.testframework.remote.runonserver.InjectRunOnServer; | ||
| import org.keycloak.testframework.remote.runonserver.RunOnServerClient; | ||
| import org.keycloak.tests.common.TestRealmUserConfig; | ||
| import org.keycloak.testsuite.util.oauth.AccessTokenResponse; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| @KeycloakIntegrationTest | ||
| public class ClientSecretRotationDisabledTest { | ||
|
|
||
| private static final String CLIENT_ID = "test-app"; | ||
|
|
||
| @InjectOAuthClient | ||
| OAuthClient oauth; | ||
|
|
||
| @InjectUser(config = TestRealmUserConfig.class) | ||
| ManagedUser user; | ||
|
|
||
| @InjectRunOnServer | ||
| RunOnServerClient runOnServer; | ||
|
|
||
| /** | ||
| * Verifies that rotated client secrets are not accepted when the CLIENT_SECRET_ROTATION | ||
| * feature is disabled, even if the rotated secret attributes remain in the database. | ||
| * | ||
| * @see <a href="https://github.com/keycloak/keycloak/issues/50855">Issue #50855</a> | ||
| */ | ||
| @Test | ||
| public void rotatedSecretNotAcceptedWhenFeatureDisabled() { | ||
| String originalSecret = oauth.clientResource().getSecret().getValue(); | ||
|
|
||
| // Verify the original secret works before rotating | ||
| oauth.client(CLIENT_ID, originalSecret); | ||
| oauth.doLogin(user.getUsername(), "password"); | ||
| String code = oauth.parseLoginResponse().getCode(); | ||
| AccessTokenResponse response = oauth.doAccessTokenRequest(code); | ||
| assertEquals(200, response.getStatusCode()); | ||
| assertNotNull(response.getAccessToken()); | ||
|
|
||
| String newSecret = oauth.clientResource().generateNewSecret().getValue(); | ||
|
|
||
| // Set rotated secret attributes directly on the server-side model to bypass the admin | ||
| // API cleanup in ClientResource.update() which always removes rotation info when no | ||
| // rotation executor is active. | ||
| String creationTime = String.valueOf(Time.currentTimeSeconds()); | ||
| String expirationTime = String.valueOf(Time.currentTimeSeconds() + 3600); | ||
| runOnServer.run(session -> { | ||
| var realmModel = session.getContext().getRealm(); | ||
| var client = realmModel.getClientByClientId(CLIENT_ID); | ||
| client.setAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET, originalSecret); | ||
| client.setAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_CREATION_TIME, creationTime); | ||
| client.setAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_EXPIRATION_TIME, expirationTime); | ||
| }); | ||
|
|
||
| // Authentication with the rotated (original) secret must fail when feature is disabled | ||
| oauth.client(CLIENT_ID, originalSecret); | ||
| oauth.openLoginForm(); | ||
| code = oauth.parseLoginResponse().getCode(); | ||
| response = oauth.doAccessTokenRequest(code); | ||
| assertEquals(401, response.getStatusCode()); | ||
|
|
||
| // Authentication with the current (new) secret must always work | ||
| oauth.client(CLIENT_ID, newSecret); | ||
| oauth.openLoginForm(); | ||
| code = oauth.parseLoginResponse().getCode(); | ||
| response = oauth.doAccessTokenRequest(code); | ||
| assertEquals(200, response.getStatusCode()); | ||
| assertNotNull(response.getAccessToken()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.