forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a conditional authenticator to check if a sub-flow was exec…
…uted or not previously in the process (keycloak#35668) Closes keycloak#35231 Signed-off-by: rmartinc <[email protected]> Co-authored-by: Marek Posolda <[email protected]> Co-authored-by: andymunro <[email protected]>
- Loading branch information
1 parent
8d934de
commit bbca611
Showing
12 changed files
with
513 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
113 changes: 113 additions & 0 deletions
113
...ak/authentication/authenticators/conditional/ConditionalSubFlowExecutedAuthenticator.java
This file contains 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,113 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.authentication.authenticators.conditional; | ||
|
||
import java.util.stream.Stream; | ||
import org.jboss.logging.Logger; | ||
import org.keycloak.authentication.AuthenticationFlowContext; | ||
import org.keycloak.models.AuthenticationExecutionModel; | ||
import org.keycloak.models.AuthenticationFlowModel; | ||
import org.keycloak.models.AuthenticatorConfigModel; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.sessions.AuthenticationSessionModel; | ||
|
||
/** | ||
* <p>Conditional authenticator to know if a sub-flow was executed successfully in the authentication flow.</p> | ||
* | ||
* @author rmartinc | ||
*/ | ||
public class ConditionalSubFlowExecutedAuthenticator implements ConditionalAuthenticator { | ||
|
||
protected static final ConditionalSubFlowExecutedAuthenticator SINGLETON = new ConditionalSubFlowExecutedAuthenticator(); | ||
private static final Logger logger = Logger.getLogger(ConditionalSubFlowExecutedAuthenticator.class); | ||
|
||
@Override | ||
public boolean matchCondition(AuthenticationFlowContext context) { | ||
final AuthenticatorConfigModel configModel = context.getAuthenticatorConfig(); | ||
if (configModel == null || configModel.getConfig() == null) { | ||
logger.warnf("No configuration defined for the conditional flow executed. Nothing executed."); | ||
return false; | ||
} | ||
|
||
final String flowAlias = configModel.getConfig().get(ConditionalSubFlowExecutedAuthenticatorFactory.FLOW_TO_CHECK); | ||
final boolean executed = !ConditionalSubFlowExecutedAuthenticatorFactory.CHECK_RESULT_NOT_EXECUTED.equals( | ||
configModel.getConfig().get(ConditionalSubFlowExecutedAuthenticatorFactory.CHECK_RESULT)); | ||
if (flowAlias == null) { | ||
logger.warnf("No flow configured in the option '%s'. Nothing executed.", ConditionalSubFlowExecutedAuthenticatorFactory.FLOW_TO_CHECK); | ||
return !executed; | ||
} | ||
|
||
final RealmModel realm = context.getRealm(); | ||
final AuthenticationFlowModel flow = realm.getFlowByAlias(flowAlias); | ||
if (flow == null) { | ||
logger.warnf("No flow '%s' defined in the realm. Nothing executed.", flowAlias); | ||
return !executed; | ||
} | ||
|
||
final AuthenticationExecutionModel exec = locateExecutionFlowToCheck(realm, context.getTopLevelFlow().getId(), flow.getId()); | ||
if (exec == null) { | ||
logger.warnf("Cannot locate execution for flow '%s' in the top level flow '%s'. Nothing executed.", flowAlias, context.getTopLevelFlow().getAlias()); | ||
return !executed; | ||
} | ||
|
||
AuthenticationSessionModel.ExecutionStatus status = context.getAuthenticationSession().getExecutionStatus().get(exec.getId()); | ||
logger.tracef("The authentication status for the flow '%s' is %s", flowAlias, status); | ||
return executed | ||
? AuthenticationSessionModel.ExecutionStatus.SUCCESS.equals(status) | ||
: !AuthenticationSessionModel.ExecutionStatus.SUCCESS.equals(status); | ||
} | ||
|
||
@Override | ||
public void action(AuthenticationFlowContext context) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public boolean requiresUser() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void setRequiredActions(KeycloakSession session, RealmModel realm, UserModel user) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// no-op | ||
} | ||
|
||
private Stream<AuthenticationExecutionModel> flattened(RealmModel realm, AuthenticationExecutionModel flowExec) { | ||
// flatten the execution model recursively only for flows | ||
return Stream.concat(Stream.of(flowExec), | ||
realm.getAuthenticationExecutionsStream(flowExec.getFlowId()) | ||
.filter(AuthenticationExecutionModel::isAuthenticatorFlow) | ||
.flatMap(exec -> flattened(realm, exec))); | ||
} | ||
|
||
private AuthenticationExecutionModel locateExecutionFlowToCheck(RealmModel realm, String topFlowId, String flowId) { | ||
return realm.getAuthenticationExecutionsStream(topFlowId) | ||
.filter(AuthenticationExecutionModel::isAuthenticatorFlow) | ||
.flatMap(exec -> flattened(realm, exec)) | ||
.filter(exec -> flowId.equals(exec.getFlowId())) | ||
.findAny() | ||
.orElse(null); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...entication/authenticators/conditional/ConditionalSubFlowExecutedAuthenticatorFactory.java
This file contains 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,118 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.authentication.authenticators.conditional; | ||
|
||
import java.util.List; | ||
import org.keycloak.Config.Scope; | ||
import org.keycloak.models.AuthenticationExecutionModel; | ||
import org.keycloak.models.AuthenticationExecutionModel.Requirement; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
import org.keycloak.provider.ProviderConfigurationBuilder; | ||
|
||
/** | ||
* <p>Conditional factory to know if a sub-flow was executed successfully in the authentication flow.</p> | ||
* | ||
* @author rmartinc | ||
*/ | ||
public class ConditionalSubFlowExecutedAuthenticatorFactory implements ConditionalAuthenticatorFactory { | ||
|
||
public static final String PROVIDER_ID = "conditional-sub-flow-executed"; | ||
public static final String FLOW_TO_CHECK = "flow_to_check"; | ||
public static final String CHECK_RESULT = "check_result"; | ||
public static final String CHECK_RESULT_EXECUTED = "executed"; | ||
public static final String CHECK_RESULT_NOT_EXECUTED = "not-executed"; | ||
|
||
@Override | ||
public void init(Scope config) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void postInit(KeycloakSessionFactory factory) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public String getDisplayType() { | ||
return "Condition - sub-flow executed"; | ||
} | ||
|
||
@Override | ||
public boolean isConfigurable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Requirement[] getRequirementChoices() { | ||
return new Requirement[]{AuthenticationExecutionModel.Requirement.REQUIRED, AuthenticationExecutionModel.Requirement.DISABLED}; | ||
} | ||
|
||
@Override | ||
public boolean isUserSetupAllowed() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getHelpText() { | ||
return "Condition to evaluate if a sub-flow was executed successfully during the authentication process"; | ||
} | ||
|
||
@Override | ||
public List<ProviderConfigProperty> getConfigProperties() { | ||
return ProviderConfigurationBuilder.create() | ||
.property() | ||
.name(FLOW_TO_CHECK) | ||
.type(ProviderConfigProperty.STRING_TYPE) | ||
.label("Flow name") | ||
.helpText("The sub-flow name to check if it was executed.") | ||
.required(true) | ||
.add() | ||
.property() | ||
.name(CHECK_RESULT) | ||
.type(ProviderConfigProperty.LIST_TYPE) | ||
.label("Check result") | ||
.helpText( | ||
""" | ||
When the condition evaluates to true. | ||
If 'executed' returns true when the configured sub-flow was executed with output success, false otherwise. | ||
If 'not-executed' returns false when the sub-flow was executed with output success, true otherwise. | ||
""" | ||
) | ||
.required(true) | ||
.options(List.of(CHECK_RESULT_EXECUTED, CHECK_RESULT_NOT_EXECUTED)) | ||
.defaultValue(CHECK_RESULT_EXECUTED) | ||
.add() | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ConditionalAuthenticator getSingleton() { | ||
return ConditionalSubFlowExecutedAuthenticator.SINGLETON; | ||
} | ||
} |
This file contains 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 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 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.