Skip to content

Commit

Permalink
Add Filter to Distinguish SubFlow Authenticators by config alias
Browse files Browse the repository at this point in the history
  • Loading branch information
f11h committed Jan 16, 2024
1 parent 3cbcb41 commit 1b2de41
Show file tree
Hide file tree
Showing 4 changed files with 451 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed
- Allow executions of same provider with different configurations in Sub-Auth-Flows

## [5.10.0] - 2023-12-12
- Updated CI to use Keycloak 23.0.1
- Added correct spelling of "authenticatorFlow" in all import files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public List<AuthenticationExecutionInfoRepresentation> getExecutionFlowsByAlias(
String topLevelFlowAlias,
AuthenticationExecutionExportRepresentation execution) {
List<AuthenticationExecutionInfoRepresentation> executions = searchByAlias(
realmName, topLevelFlowAlias, execution.getAuthenticator(), execution.getFlowAlias());
realmName, topLevelFlowAlias, execution.getAuthenticator(),
execution.getFlowAlias(), execution.getAuthenticatorConfig());

if (executions.isEmpty()) {
String withSubFlow = execution.getFlowAlias() != null
Expand Down Expand Up @@ -147,11 +148,18 @@ private List<AuthenticationExecutionInfoRepresentation> searchByAlias(
String realmName,
String topLevelFlowAlias,
String executionProviderId,
String subFlowAlias
String subFlowAlias,
String authenticationConfig
) {
return getExecutionsByAuthFlow(realmName, topLevelFlowAlias)
.stream()
.filter(f -> Objects.equals(f.getProviderId(), executionProviderId))
.filter(f -> {
if (authenticationConfig != null && f.getAlias() != null) {
return Objects.equals(f.getAlias(), authenticationConfig);
}
return true;
})
.filter(f -> {
if (subFlowAlias != null) {
return Objects.equals(f.getDisplayName(), subFlowAlias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,62 @@ void shouldUpdateMultipleExecutionsWithSameAuthenticatorWithConfig() throws IOEx
assertThat(authConfig.get(0).getConfig(), hasEntry(is("defaultProvider"), is("id4")));
}

@Test
@Order(33)
void shouldCreateMultipleSubFlowExecutionsWithSameAuthenticator() throws IOException {
doImport("33_update_realm__add_multiple_subflow_executions_with_same_authenticator.json");

RealmRepresentation realm = keycloakProvider.getInstance().realm(REALM_NAME).partialExport(true, true);

AuthenticationFlowRepresentation topLevelFlow = getAuthenticationFlow(realm, "my top level auth flow");
assertThat(topLevelFlow.isBuiltIn(), is(false));
assertThat(topLevelFlow.isTopLevel(), is(true));
assertThat(topLevelFlow.getAuthenticationExecutions().size(), is(1));
assertThat(topLevelFlow.getAuthenticationExecutions().get(0).getFlowAlias(), is("my sub auth flow"));

AuthenticationFlowRepresentation subFlow = getAuthenticationFlow(realm, "my sub auth flow");
assertThat(subFlow.isBuiltIn(), is(false));
assertThat(subFlow.isTopLevel(), is(false));
assertThat(subFlow.getAuthenticationExecutions().size(), is(3));

List<AuthenticationExecutionExportRepresentation> execution;
execution = getExecutionFromFlow(subFlow, "identity-provider-redirector");
assertThat(execution, hasSize(2));

List<AuthenticationExecutionExportRepresentation> executionsId1 = execution.stream()
.filter((config) -> config.getAuthenticatorConfig() != null)
.filter((config) -> config.getAuthenticatorConfig().equals("config-1"))
.collect(Collectors.toList());

assertThat(executionsId1, hasSize(1));
assertThat(executionsId1.get(0).getAuthenticator(), is("identity-provider-redirector"));
assertThat(executionsId1.get(0).getAuthenticatorConfig(), is("config-1"));
assertThat(executionsId1.get(0).getRequirement(), is("ALTERNATIVE"));

List<AuthenticationExecutionExportRepresentation> executionsId2 = execution.stream()
.filter((config) -> config.getAuthenticatorConfig() != null)
.filter((config) -> config.getAuthenticatorConfig().equals("config-2"))
.collect(Collectors.toList());

assertThat(executionsId2, hasSize(1));
assertThat(executionsId2.get(0).getAuthenticator(), is("identity-provider-redirector"));
assertThat(executionsId2.get(0).getAuthenticatorConfig(), is("config-2"));
assertThat(executionsId2.get(0).getRequirement(), is("ALTERNATIVE"));

assertThat(executionsId2.get(0).getPriority(), greaterThan(executionsId1.get(0).getPriority()));

List<AuthenticatorConfigRepresentation> authConfig;
authConfig = getAuthenticatorConfig(realm, "config-1");
assertThat(authConfig, hasSize(1));
assertThat(authConfig.get(0).getAlias(), is("config-1"));
assertThat(authConfig.get(0).getConfig(), hasEntry(is("defaultProvider"), is("id1")));

authConfig = getAuthenticatorConfig(realm, "config-2");
assertThat(authConfig, hasSize(1));
assertThat(authConfig.get(0).getAlias(), is("config-2"));
assertThat(authConfig.get(0).getConfig(), hasEntry(is("defaultProvider"), is("id2")));
}

@Test
@Order(40)
void shouldFailWhenTryingToUpdateBuiltInFlow() throws IOException {
Expand Down

0 comments on commit 1b2de41

Please sign in to comment.