KEYCLOAK-1795 Add just one clientAuthenticatorType per client
This commit is contained in:
1 parent
25e53db96f
commit
bc189554aa
38 files changed
+275
-206
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package org.keycloak.authentication;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -11,6 +13,7 @@ import org.keycloak.events.Errors;
|
||||
import org.keycloak.models.AuthenticationExecutionModel;
|
||||
import org.keycloak.models.AuthenticationFlowModel;
|
||||
import org.keycloak.models.ClientModel;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
@@ -18,19 +21,12 @@ import org.keycloak.models.ClientModel;
|
||||
public class ClientAuthenticationFlow implements AuthenticationFlow {
|
||||
|
||||
Response alternativeChallenge = null;
|
||||
boolean alternativeSuccessful = false;
|
||||
List<AuthenticationExecutionModel> executions;
|
||||
Iterator<AuthenticationExecutionModel> executionIterator;
|
||||
AuthenticationProcessor processor;
|
||||
AuthenticationFlowModel flow;
|
||||
|
||||
private List<String> successAuthenticators = new LinkedList<>();
|
||||
|
||||
public ClientAuthenticationFlow(AuthenticationProcessor processor, AuthenticationFlowModel flow) {
|
||||
this.processor = processor;
|
||||
this.flow = flow;
|
||||
this.executions = processor.getRealm().getAuthenticationExecutions(flow.getId());
|
||||
this.executionIterator = executions.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,131 +36,109 @@ public class ClientAuthenticationFlow implements AuthenticationFlow {
|
||||
|
||||
@Override
|
||||
public Response processFlow() {
|
||||
while (executionIterator.hasNext()) {
|
||||
AuthenticationExecutionModel model = executionIterator.next();
|
||||
|
||||
if (model.isDisabled()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (model.isAlternative() && alternativeSuccessful) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (model.isAuthenticatorFlow()) {
|
||||
AuthenticationFlow authenticationFlow;
|
||||
authenticationFlow = processor.createFlowExecution(model.getFlowId(), model);
|
||||
|
||||
Response flowChallenge = authenticationFlow.processFlow();
|
||||
if (flowChallenge == null) {
|
||||
if (model.isAlternative()) alternativeSuccessful = true;
|
||||
continue;
|
||||
} else {
|
||||
if (model.isAlternative()) {
|
||||
alternativeChallenge = flowChallenge;
|
||||
} else if (model.isRequired()) {
|
||||
return flowChallenge;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
return flowChallenge;
|
||||
}
|
||||
}
|
||||
List<AuthenticationExecutionModel> executions = findExecutionsToRun();
|
||||
|
||||
for (AuthenticationExecutionModel model : executions) {
|
||||
ClientAuthenticatorFactory factory = (ClientAuthenticatorFactory) processor.getSession().getKeycloakSessionFactory().getProviderFactory(ClientAuthenticator.class, model.getAuthenticator());
|
||||
if (factory == null) {
|
||||
throw new AuthenticationFlowException("Could not find ClientAuthenticatorFactory for: " + model.getAuthenticator(), AuthenticationFlowError.INTERNAL_ERROR);
|
||||
}
|
||||
ClientAuthenticator authenticator = factory.create();
|
||||
AuthenticationProcessor.logger.debugv("client authenticator: {0}", factory.getId());
|
||||
ClientModel authClient = processor.getClient();
|
||||
|
||||
if (authenticator.requiresClient() && authClient == null) {
|
||||
// Continue if it's alternative or optional flow
|
||||
if (model.isAlternative() || model.isOptional()) {
|
||||
AuthenticationProcessor.logger.debugv("client authenticator: {0} requires client, but client not available. Skipping", factory.getId());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (alternativeChallenge != null) {
|
||||
return alternativeChallenge;
|
||||
}
|
||||
throw new AuthenticationFlowException("client authenticator: " + factory.getId(), AuthenticationFlowError.CLIENT_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (authenticator.requiresClient() && authClient != null) {
|
||||
boolean configuredFor = authenticator.configuredFor(processor.getSession(), processor.getRealm(), authClient);
|
||||
if (!configuredFor) {
|
||||
if (model.isRequired()) {
|
||||
throw new AuthenticationFlowException("Client setup required for authenticator " + factory.getId() + " for client " + authClient.getClientId(),
|
||||
AuthenticationFlowError.CLIENT_CREDENTIALS_SETUP_REQUIRED);
|
||||
} else if (model.isOptional()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
AuthenticationProcessor.Result context = processor.createClientAuthenticatorContext(model, authenticator, executions);
|
||||
authenticator.authenticateClient(context);
|
||||
Response response = processResult(context);
|
||||
if (response != null) return response;
|
||||
|
||||
authClient = processor.getClient();
|
||||
if (authClient != null && authClient.isPublicClient()) {
|
||||
AuthenticationProcessor.logger.debugv("Public client {0} identified by {1} . Skip next client authenticators", authClient.getClientId(), factory.getId());
|
||||
logSuccessEvent();
|
||||
return null;
|
||||
ClientModel client = processor.getClient();
|
||||
if (client != null) {
|
||||
|
||||
String expectedClientAuthType = client.getClientAuthenticatorType();
|
||||
|
||||
// Fallback to secret just in case (for backwards compatibility)
|
||||
if (expectedClientAuthType == null) {
|
||||
expectedClientAuthType = KeycloakModelUtils.getDefaultClientAuthenticatorType();
|
||||
AuthenticationProcessor.logger.warnv("Client {0} doesn't have have authentication method configured. Fallback to {1}", client.getClientId(), expectedClientAuthType);
|
||||
}
|
||||
|
||||
// Check if client authentication matches
|
||||
if (factory.getId().equals(expectedClientAuthType)) {
|
||||
AuthenticationProcessor.logger.debugv("Client {0} authenticated by {1}", client.getClientId(), factory.getId());
|
||||
processor.getEvent().detail(Details.CLIENT_AUTH_METHOD, factory.getId());
|
||||
return null;
|
||||
} else {
|
||||
throw new AuthenticationFlowException("Client " + client.getClientId() + " was authenticated by incorrect method " + factory.getId(),
|
||||
AuthenticationFlowError.INVALID_CLIENT_CREDENTIALS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return finishClientAuthentication();
|
||||
// Check if any alternative challenge was identified
|
||||
if (alternativeChallenge != null) {
|
||||
processor.getEvent().error(Errors.INVALID_CLIENT);
|
||||
return alternativeChallenge;
|
||||
}
|
||||
throw new AuthenticationFlowException("Client was not identified by any client authenticator", AuthenticationFlowError.UNKNOWN_CLIENT);
|
||||
}
|
||||
|
||||
protected List<AuthenticationExecutionModel> findExecutionsToRun() {
|
||||
List<AuthenticationExecutionModel> executions = processor.getRealm().getAuthenticationExecutions(flow.getId());
|
||||
List<AuthenticationExecutionModel> executionsToRun = new ArrayList<>();
|
||||
|
||||
public Response processResult(AuthenticationProcessor.Result result) {
|
||||
for (AuthenticationExecutionModel execution : executions) {
|
||||
if (execution.isRequired()) {
|
||||
executionsToRun = Arrays.asList(execution);
|
||||
break;
|
||||
}
|
||||
|
||||
if (execution.isAlternative()) {
|
||||
executionsToRun.add(execution);
|
||||
}
|
||||
}
|
||||
|
||||
if (AuthenticationProcessor.logger.isTraceEnabled()) {
|
||||
List<String> exIds = new ArrayList<>();
|
||||
for (AuthenticationExecutionModel execution : executionsToRun) {
|
||||
exIds.add(execution.getId());
|
||||
}
|
||||
AuthenticationProcessor.logger.tracef("Using executions for client authentication: %s", exIds.toString());
|
||||
}
|
||||
|
||||
return executionsToRun;
|
||||
}
|
||||
|
||||
protected Response processResult(AuthenticationProcessor.Result result) {
|
||||
AuthenticationExecutionModel execution = result.getExecution();
|
||||
FlowStatus status = result.getStatus();
|
||||
|
||||
AuthenticationProcessor.logger.debugv("client authenticator {0}: {1}", status.toString(), execution.getAuthenticator());
|
||||
|
||||
if (status == FlowStatus.SUCCESS) {
|
||||
AuthenticationProcessor.logger.debugv("client authenticator SUCCESS: {0}", execution.getAuthenticator());
|
||||
if (execution.isAlternative()) alternativeSuccessful = true;
|
||||
successAuthenticators.add(execution.getAuthenticator());
|
||||
return null;
|
||||
} else if (status == FlowStatus.FAILED) {
|
||||
AuthenticationProcessor.logger.debugv("client authenticator FAILED: {0}", execution.getAuthenticator());
|
||||
if (result.getChallenge() != null) {
|
||||
return sendChallenge(result, execution);
|
||||
} else {
|
||||
throw new AuthenticationFlowException(result.getError());
|
||||
}
|
||||
throw new AuthenticationFlowException(result.getError());
|
||||
} else if (status == FlowStatus.FORCE_CHALLENGE) {
|
||||
return sendChallenge(result, execution);
|
||||
} else if (status == FlowStatus.CHALLENGE) {
|
||||
AuthenticationProcessor.logger.debugv("client authenticator CHALLENGE: {0}", execution.getAuthenticator());
|
||||
if (execution.isRequired()) {
|
||||
return sendChallenge(result, execution);
|
||||
}
|
||||
ClientModel client = processor.getClient();
|
||||
if (execution.isOptional() && client != null && result.getClientAuthenticator().configuredFor(processor.getSession(), processor.getRealm(), client)) {
|
||||
return sendChallenge(result, execution);
|
||||
}
|
||||
|
||||
// Make sure the first priority alternative challenge is used
|
||||
if (execution.isAlternative() && alternativeChallenge == null) {
|
||||
if (alternativeChallenge == null) {
|
||||
alternativeChallenge = result.getChallenge();
|
||||
}
|
||||
return null;
|
||||
} else if (status == FlowStatus.FAILURE_CHALLENGE) {
|
||||
AuthenticationProcessor.logger.debugv("client authenticator FAILURE_CHALLENGE: {0}", execution.getAuthenticator());
|
||||
return sendChallenge(result, execution);
|
||||
} else if (status == FlowStatus.ATTEMPTED) {
|
||||
AuthenticationProcessor.logger.debugv("client authenticator ATTEMPTED: {0}", execution.getAuthenticator());
|
||||
if (execution.getRequirement() == AuthenticationExecutionModel.Requirement.REQUIRED) {
|
||||
throw new AuthenticationFlowException(AuthenticationFlowError.INVALID_CLIENT_CREDENTIALS);
|
||||
}
|
||||
return null;
|
||||
} else {
|
||||
AuthenticationProcessor.logger.debugv("client authenticator INTERNAL_ERROR: {0}", execution.getAuthenticator());
|
||||
AuthenticationProcessor.logger.error("Unknown result status");
|
||||
throw new AuthenticationFlowException(AuthenticationFlowError.INTERNAL_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Response sendChallenge(AuthenticationProcessor.Result result, AuthenticationExecutionModel execution) {
|
||||
@@ -183,34 +157,4 @@ public class ClientAuthenticationFlow implements AuthenticationFlow {
|
||||
|
||||
return result.getChallenge();
|
||||
}
|
||||
|
||||
private Response finishClientAuthentication() {
|
||||
if (processor.getClient() == null) {
|
||||
// Check if any alternative challenge was identified
|
||||
if (alternativeChallenge != null) {
|
||||
processor.getEvent().error(Errors.INVALID_CLIENT);
|
||||
return alternativeChallenge;
|
||||
}
|
||||
|
||||
throw new AuthenticationFlowException("Client was not identified by any client authenticator", AuthenticationFlowError.UNKNOWN_CLIENT);
|
||||
}
|
||||
|
||||
logSuccessEvent();
|
||||
return null;
|
||||
}
|
||||
|
||||
private void logSuccessEvent() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (String authenticator : successAuthenticators) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
result.append(" ");
|
||||
}
|
||||
result.append(authenticator);
|
||||
}
|
||||
|
||||
processor.getEvent().detail(Details.CLIENT_AUTH_METHOD, result.toString());
|
||||
}
|
||||
}
|
||||
@@ -28,21 +28,4 @@ public interface ClientAuthenticator extends Provider {
|
||||
*/
|
||||
void authenticateClient(ClientAuthenticationFlowContext context);
|
||||
|
||||
|
||||
/**
|
||||
* Does this authenticator require that the client has already been identified? That ClientAuthenticationFlowContext.getClient() is not null?
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean requiresClient();
|
||||
|
||||
/**
|
||||
* Is this authenticator configured for this client?
|
||||
*
|
||||
* @param session
|
||||
* @param realm
|
||||
* @param client
|
||||
* @return
|
||||
*/
|
||||
boolean configuredFor(KeycloakSession session, RealmModel realm, ClientModel client);
|
||||
}
|
||||
-11
@@ -36,7 +36,6 @@ public class ClientIdAndSecretAuthenticator extends AbstractClientAuthenticator
|
||||
public static final String PROVIDER_ID = "client-secret";
|
||||
|
||||
public static final AuthenticationExecutionModel.Requirement[] REQUIREMENT_CHOICES = {
|
||||
AuthenticationExecutionModel.Requirement.REQUIRED,
|
||||
AuthenticationExecutionModel.Requirement.ALTERNATIVE,
|
||||
AuthenticationExecutionModel.Requirement.DISABLED
|
||||
};
|
||||
@@ -133,16 +132,6 @@ public class ClientIdAndSecretAuthenticator extends AbstractClientAuthenticator
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresClient() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configuredFor(KeycloakSession session, RealmModel realm, ClientModel client) {
|
||||
return client.getSecret() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthenticationExecutionModel.Requirement[] getRequirementChoices() {
|
||||
return REQUIREMENT_CHOICES;
|
||||
|
||||
-11
@@ -41,7 +41,6 @@ public class JWTClientAuthenticator extends AbstractClientAuthenticator {
|
||||
public static final String CERTIFICATE_ATTR = "jwt.credential.certificate";
|
||||
|
||||
public static final AuthenticationExecutionModel.Requirement[] REQUIREMENT_CHOICES = {
|
||||
AuthenticationExecutionModel.Requirement.REQUIRED,
|
||||
AuthenticationExecutionModel.Requirement.ALTERNATIVE,
|
||||
AuthenticationExecutionModel.Requirement.DISABLED
|
||||
};
|
||||
@@ -135,16 +134,6 @@ public class JWTClientAuthenticator extends AbstractClientAuthenticator {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresClient() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configuredFor(KeycloakSession session, RealmModel realm, ClientModel client) {
|
||||
return client.getAttribute(CERTIFICATE_ATTR) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayType() {
|
||||
return "Signed Jwt";
|
||||
|
||||
@@ -39,7 +39,7 @@ public class AuthorizeClientUtil {
|
||||
|
||||
ClientModel client = processor.getClient();
|
||||
if (client == null) {
|
||||
throw new ErrorResponseException("invalid_client", "Client authentication was successful, but client is null", Response.Status.BAD_REQUEST);
|
||||
throw new ErrorResponseException("invalid_client", "Client authentication ended, but client is null", Response.Status.BAD_REQUEST);
|
||||
}
|
||||
|
||||
return new ClientAuthResult(client, processor.getClientAuthAttributes());
|
||||
|
||||
Reference in new issue
Block a user