8000 Enable configurable `client_id` parameter validation for federated client assertions by slaskawi · Pull Request #48026 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ private boolean validateClient() {
return failure("Token sub claim is required");
}

String clientIdParam = context.getHttpRequest().getDecodedFormParameters().getFirst(OAuth2Constants.CLIENT_ID);
if (clientIdParam != null && !clientIdParam.equals(clientId)) {
logger.debug("client_id parameter does not match JWT subject");
return failure("client_id parameter does not match sub claim");
}

String expectedTokenIssuer = getExpectedTokenIssuer();
if (expectedTokenIssuer != null && !expectedTokenIssuer.equals(token.getIssuer())) {
return false;
Expand All @@ -116,11 +110,16 @@ private boolean validateClient() {

if (client == null) {
return failure(AuthenticationFlowError.CLIENT_NOT_FOUND);
} else {
context.getEvent().client(client.getClientId());
context.setClient(client);
}

String clientIdParam = context.getHttpRequest().getDecodedFormParameters().getFirst(OAuth2Constants.CLIENT_ID);
if (clientIdParam != null && !clientIdParam.equals(client.getClientId())) {
return failure("client_id parameter does not match authenticated client");
}

context.getEvent().client(client.getClientId());
context.setClient(client);

if (!client.isEnabled()) {
return failure(AuthenticationFlowError.CLIENT_DISABLED);
}
< 10BC8 a href="#diff-fcc144cc0c22323ed6abbf4776ce8cab2902caf3ede57c98190463145bd4e70d" id="expand-down-link--diff-fcc144cc0c22323ed6abbf4776ce8cab2902caf3ede57c98190463145bd4e70d" class="js-expand directional-expander single-expander" aria-label="Expand Down" data-url="/keycloak/keycloak/blob_excerpt/d75a02621d9523e957c53d6e445cb2da661b639e?context=pull_request&diff=unified&direction=down&in_wiki_context&last_left=126&last_right=125&left=187&left_hunk_size&mode=100644&path=services%2Fsrc%2Fmain%2Fjava%2Forg%2Fkeycloak%2Fauthentication%2Fauthenticators%2Fclient%2FAbstractJWTClientValidator.java&pull_request_id=3527043983&right=187&right_hunk_size" data-left-range="127-186" data-right-range="126-186"> Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.RealmBuilder;
import org.keycloak.testframework.realm.RealmConfig;
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
Expand Down Expand Up @@ -72,6 +73,30 @@ public void testOldIAt() {
assertSuccess(internalClientId, jwt.getId(), expectedTokenIssuer, externalClientId, events.poll());
}

@Test
public void testValidTokenWithClientId() {
JsonWebToken jwt = createDefaultToken();
String jws = getIdentityProvider().encodeToken(jwt);
AccessTokenResponse response = oAuthClient.clientCredentialsGrantRequest()
.client(INTERNAL_CLIENT_ID)
.clientJwt(jws, getClientAssertionType())
.send();
assertSuccess(internalClientId, response);
assertSuccess(internalClientId, jwt.getId(), expectedTokenIssuer, externalClientId, events.poll());
}

@Test
public void testWrongClientIdFailsValidation() {
JsonWebToken jwt = createDefaultToken();
String jws = getIdentityProvider().encodeToken(jwt);
AccessTokenResponse response = oAuthClient.clientCredentialsGrantRequest()
.client("completely-wrong-id")
.clientJwt(jws, getClientAssertionType())
.send();
assertFailure(response);
assertFailure("completely-wrong-id", expectedTokenIssuer, externalClientId, jwt.getId(), "client_not_found", events.poll());
}

@Test
public void testReuse() {
JsonWebToken jwt = createDefaultToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ protected void authorization() {
if (clientAssertion != null && clientAssertionType != null) {
parameter("client_assertion_type", clientAssertionType);
parameter("client_assertion", clientAssertion);
if (this.clientId != null) {
parameter("client_id", this.clientId);
}
} else if (tokenType != null && tokenValue != null) {
header("Authorization", tokenType + " " + tokenValue);
} else if (clientSecret != null) {
Expand Down
Loading
0