Fix KcCustomOidcBrokerTest

Fixes: #20541
This commit is contained in:
Hynek Mlnarik 2023-03-19 11:25:45 +01:00 committed by Michal Hajas
parent 0f2ff9c235
commit fc0e47caa4
3 changed files with 29 additions and 6 deletions

View file

@ -31,6 +31,9 @@ import java.util.stream.Stream;
import org.jboss.logging.Logger;
import org.keycloak.Config;
import org.keycloak.broker.provider.IdentityProvider;
import org.keycloak.broker.provider.IdentityProviderFactory;
import org.keycloak.broker.social.SocialIdentityProvider;
import org.keycloak.common.enums.SslRequired;
import org.keycloak.component.ComponentFactory;
import org.keycloak.component.ComponentModel;
@ -943,7 +946,8 @@ public class MapRealmAdapter extends AbstractRealmModel<MapRealmEntity> implemen
@Override
public Stream<IdentityProviderModel> getIdentityProvidersStream() {
Set<MapIdentityProviderEntity> ips = entity.getIdentityProviders();
return ips == null ? Stream.empty() : ips.stream().map(MapIdentityProviderEntity::toModel);
return ips == null ? Stream.empty() : ips.stream()
.map(e -> MapIdentityProviderEntity.toModel(e, () -> this.getModelFromProviderFactory(e.getProviderId())));
}
@Override
@ -952,10 +956,27 @@ public class MapRealmAdapter extends AbstractRealmModel<MapRealmEntity> implemen
return ips == null ? null : ips.stream()
.filter(identityProvider -> Objects.equals(identityProvider.getAlias(), alias))
.findFirst()
.map(MapIdentityProviderEntity::toModel)
.map(e -> MapIdentityProviderEntity.toModel(e, () -> this.getModelFromProviderFactory(e.getProviderId())))
.orElse(null);
}
// This is a violation of layering requirements, this should NOT be in store code.
// However, there is no easy way around this given the current number of IdentityProviderModel implementations
private IdentityProviderModel getModelFromProviderFactory(String providerId) {
Optional<IdentityProviderFactory> factory = Stream.concat(session.getKeycloakSessionFactory().getProviderFactoriesStream(IdentityProvider.class),
session.getKeycloakSessionFactory().getProviderFactoriesStream(SocialIdentityProvider.class))
.filter(providerFactory -> Objects.equals(providerFactory.getId(), providerId))
.map(IdentityProviderFactory.class::cast)
.findFirst();
if (factory.isPresent()) {
return factory.get().createConfig();
} else {
LOG.warn("Couldn't find a suitable identity provider factory for " + providerId);
return new IdentityProviderModel();
}
}
@Override
public void addIdentityProvider(IdentityProviderModel model) {
if (getIdentityProviderByAlias(model.getAlias()) != null) {

View file

@ -26,6 +26,7 @@ import org.keycloak.models.utils.KeycloakModelUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
@GenerateEntityImplementations
@DeepCloner.Root
@ -50,9 +51,9 @@ public interface MapIdentityProviderEntity extends UpdatableEntity, AbstractEnti
return entity;
}
static IdentityProviderModel toModel(MapIdentityProviderEntity entity) {
static IdentityProviderModel toModel(MapIdentityProviderEntity entity, Supplier<IdentityProviderModel> instanceCreator) {
if (entity == null) return null;
IdentityProviderModel model = new IdentityProviderModel();
IdentityProviderModel model = instanceCreator.get();
model.setInternalId(entity.getId());
model.setAlias(entity.getAlias());
model.setDisplayName(entity.getDisplayName());

View file

@ -20,11 +20,12 @@ import static org.keycloak.testsuite.broker.BrokerTestConstants.IDP_OIDC_ALIAS;
import static org.keycloak.testsuite.broker.BrokerTestTools.createIdentityProvider;
import static org.keycloak.testsuite.broker.BrokerTestTools.getConsumerRoot;
import org.junit.Assert;
import org.junit.Test;
import org.keycloak.models.IdentityProviderSyncMode;
import org.keycloak.representations.idm.IdentityProviderRepresentation;
import org.keycloak.testsuite.broker.oidc.TestKeycloakOidcIdentityProviderFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
/**
* Test methods for testing a custom OIDC broker
@ -51,6 +52,6 @@ public class KcCustomOidcBrokerTest extends AbstractInitializedBaseBrokerTest {
@Test
public void testCustomDisplayIcon() {
driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
Assert.assertTrue(driver.getPageSource().contains("my-custom-idp-icon"));
assertThat(driver.getPageSource(), containsString("my-custom-idp-icon"));
}
}