8000 Update secret rotation when the policy is enabled using jwt by marcelomrwin · Pull Request #10853 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
< 8000 link crossorigin="anonymous" rel="modulepreload" href="https://github.githubassets.com/assets/81650-4f2747c2b64996c9.js" fetchpriority="low" />
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
8000
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.apache.http.client.utils.CloneUtils;
import org.jboss.logging.Logger;
import org.keycloak.OAuth2Constants;
import org.keycloak.authentication.AuthenticationFlowError;
Expand All @@ -37,7 +38,10 @@
import org.keycloak.jose.jws.JWSInput;
import org.keycloak.models.AuthenticationExecutionModel.Requirement;
import org.keycloak.models.SingleUseTokenStoreProvider;
import org.keycloak.models.delegate.ClientModelLazyDelegate;
import org.keycloak.models.utils.RepresentationToModel;
import org.keycloak.protocol.oidc.OIDCAdvancedConfigWrapper;
import org.keycloak.protocol.oidc.OIDCClientSecretConfigWrapper;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.protocol.oidc.OIDCLoginProtocolService;
import org.keycloak.models.ClientModel;
Expand All @@ -50,12 +54,11 @@
/**
* Client authentication based on JWT signed by client secret instead of private key .
* See <a href="http://openid.net/specs/openid-connect-core-1_0.html#ClientAuthentication">specs</a> for more details.
*
* <p>
* This is server side, which verifies JWT from client_assertion parameter, where the assertion was created on adapter side by
* org.keycloak.adapters.authentication.JWTClientSecretCredentialsProvider
*
* <p>
* TODO: Try to create abstract superclass to be shared with {@link JWTClientAuthenticator}. Most of the code can be reused
*
*/
public class JWTClientSecretAuthenticator extends AbstractClientAuthenticator {

Expand All @@ -67,7 +70,7 @@ public class JWTClientSecretAuthenticator extends AbstractClientAuthenticator {
public void authenticateClient(ClientAuthenticationFlowContext context) {

//KEYCLOAK-19461: Needed for quarkus resteasy implementation throws exception when called with mediaType authentication/json in OpenShiftTokenReviewEndpoint
if(!isFormDataRequest(context.getHttpRequest())) {
if (!isFormDataRequest(context.getHttpRequest())) {
Response challengeResponse = ClientAuthUtil.errorResponse(Response.Status.BAD_REQUEST.getStatusCode(), "invalid_client", "Parameter client_assertion_type is missing");
context.challenge(challengeResponse);
return;
Expand Down Expand Up @@ -145,10 +148,23 @@ public void authenticateClient(ClientAuthenticationFlowContext context) {
return;
}

//
OIDCClientSecretConfigWrapper wrapper = OIDCClientSecretConfigWrapper.fromClientModel(client);
if (wrapper.isClientSecretExpired()) {
context.failure(AuthenticationFlowError.INVALID_CLIENT_CREDENTIALS, null);
return;
}
//

boolean signatureValid;
try {
JsonWebToken jwt = context.getSession().tokens().decodeClientJWT(clientAssertion, client, JsonWebToken.class);
signatureValid = jwt != null;
//try authenticate with client rotated secret
if (!signatureValid && wrapper.hasRotatedSecret() && !wrapper.isClientRotatedSecretExpired()) {
jwt = context.getSession().tokens().decodeClientJWT(clientAssertion, wrapper.toRotatedClientModel(), JsonWebToken.class);
signatureValid = jwt != null;
}
} catch (RuntimeException e) {
Throwable cause = e.getCause() != null ? e.getCause() : e;
throw new RuntimeException("Signature on JWT token by client secret failed validation", cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ protected String getAttribute(String attrKey, String defaultValue) {
return value;
}

protected Object getAttributes() {
if (clientModel != null) return clientModel.getAttributes();
else
return clientRep.getAttributes();
}

protected void setAttribute(String attrKey, String attrValue) {
if (clientModel != null) {
if (attrValue != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.keycloak.protocol.oidc;

import java.io.InvalidObjectException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.keycloak.common.util.Time;
import org.keycloak.models.ClientModel;
import org.keycloak.models.ClientSecretConstants;
import org.keycloak.models.delegate.ClientModelLazyDelegate;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.utils.StringUtil;

Expand All @@ -33,8 +36,7 @@ public static OIDCClientSecretConfigWrapper fromClientModel(ClientModel client)
return new OIDCClientSecretConfigWrapper(client, null);
}

public static OIDCClientSecretConfigWrapper fromClientRepresentation(
ClientRepresentation clientRep) {
public static OIDCClientSecretConfigWrapper fromClientRepresentation(ClientRepresentation clientRep) {
return new OIDCClientSecretConfigWrapper(null, clientRep);
}

Expand Down Expand Up @@ -86,8 +88,7 @@ public void setClientSecretCreationTime(int creationTime) {
}

public boolean hasRotatedSecret() {
return StringUtil.isNotBlank(getAttribute(CLIENT_ROTATED_SECRET)) && StringUtil.isNotBlank(
getAttribute(CLIENT_ROTATED_SECRET_CREATION_TIME));
return StringUtil.isNotBlank(getAttribute(CLIENT_ROTATED_SECRET)) && StringUtil.isNotBlank(getAttribute(CLIENT_ROTATED_SECRET_CREATION_TIME));
}

public String getClientRotatedSecret() {
Expand Down Expand Up @@ -121,13 +122,10 @@ public void setClientRotatedSecretCreationTime() {

public void updateClientRepresentationAttributes(ClientRepresentation rep) {
rep.getAttributes().put(CLIENT_ROTATED_SECRET, getAttribute(CLIENT_ROTATED_SECRET));
rep.getAttributes()
.put(CLIENT_SECRET_CREATION_TIME, getAttribute(CLIENT_SECRET_CREATION_TIME));
rep.getAttributes().put(CLIENT_SECRET_CREATION_TIME, getAttribute(CLIENT_SECRET_CREATION_TIME));
rep.getAttributes().put(CLIENT_SECRET_EXPIRATION, getAttribute(CLIENT_SECRET_EXPIRATION));
rep.getAttributes().put(CLIENT_ROTATED_SECRET_CREATION_TIME,
getAttribute(CLIENT_ROTATED_SECRET_CREATION_TIME));
rep.getAttributes().put(CLIENT_ROTATED_SECRET_EXPIRATION_TIME,
getAttribute(CLIENT_ROTATED_SECRET_EXPIRATION_TIME));
rep.getAttributes().put(CLIENT_ROTATED_SECRET_CREATION_TIME, getAttribute(CLIENT_ROTATED_SECRET_CREATION_TIME));
rep.getAttributes().put(CLIENT_ROTATED_SECRET_EXPIRATION_TIME, getAttribute(CLIENT_ROTATED_SECRET_EXPIRATION_TIME));
}

public boolean hasClientSecretExpirationTime() {
Expand All @@ -145,29 +143,24 @@ public void setClientSecretExpirationTime(Integer expiration) {

public boolean isClientSecretExpired() {
if (hasClientSecretExpirationTime()) {
if (getClientSecretExpirationTime() < Time.currentTime()) {
return true;
}
return getClientSecretExpirationTime() < Time.currentTime();
}
return false;
}

public int getClientRotatedSecretExpirationTime() {
if (hasClientRotatedSecretExpirationTime()) {
return Integer.valueOf(
getAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_EXPIRATION_TIME));
return Integer.valueOf(getAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_EXPIRATION_TIME));
}
return 0;
}

public void setClientRotatedSecretExpirationTime(Integer expiration) {
setAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_EXPIRATION_TIME,
expiration != null ? String.valueOf(expiration) : null);
setAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_EXPIRATION_TIME, expiration != null ? String.valueOf(expiration) : null);
}

public boolean hasClientRotatedSecretExpirationTime() {
return StringUtil.isNotBlank(
getAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_EXPIRATION_TIME));
return StringUtil.isNotBlank(getAttribute(ClientSecretConstants.CLIENT_ROTATED_SECRET_EXPIRATION_TIME));
}

public boolean isClientRotatedSecretExpired() {
Expand Down Expand Up @@ -214,4 +207,26 @@ public String toJson() {
return "";
}
}

public ReadOnlyRotatedSecretClientModel toRotatedClientModel() throws InvalidObjectException {
if (Objects.isNull(this.clientModel))
throw new InvalidObjectException(getClass().getCanonicalName() + " does not have an attribute of type " + ClientModel.class.getCanonicalName());
return new ReadOnlyRotatedSecretClientModel();
}

/**
* Representation of a client model that passes information from a rotated secret. The goal is to act as a decorator/DTO just providing information and not updating objects persistently.
*/
public class ReadOnlyRotatedSecretClientModel extends ClientModelLazyDelegate {

private ReadOnlyRotatedSecretClientModel() {
super(() -> OIDCClientSecretConfigWrapper.this.clientModel);
}

@Override
public String getSecret() {
return OIDCClientSecretConfigWrapper.this.getClientRotatedSecret();
}

}
}
Loading
0