From 576292a662328c4f91cf118bd3cf0a46b1f47bcc Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Mon, 4 Oct 2021 12:16:40 +0200 Subject: [PATCH] KEYCLOAK-19480 Introduce MapProtocolMapperEntity --- .../models/map/client/MapClientAdapter.java | 59 ++++++++++++++----- .../models/map/client/MapClientEntity.java | 9 ++- .../client/MapClientEntityLazyDelegate.java | 7 +-- .../map/client/MapProtocolMapperEntity.java | 52 ++++++++++++++++ 4 files changed, 102 insertions(+), 25 deletions(-) create mode 100644 model/map/src/main/java/org/keycloak/models/map/client/MapProtocolMapperEntity.java diff --git a/model/map/src/main/java/org/keycloak/models/map/client/MapClientAdapter.java b/model/map/src/main/java/org/keycloak/models/map/client/MapClientAdapter.java index e09db4c1f3..635a7ae91c 100644 --- a/model/map/src/main/java/org/keycloak/models/map/client/MapClientAdapter.java +++ b/model/map/src/main/java/org/keycloak/models/map/client/MapClientAdapter.java @@ -503,10 +503,35 @@ public abstract class MapClientAdapter extends AbstractClientModel getProtocolMappersStream() { - final Map protocolMappers = entity.getProtocolMappers(); - return protocolMappers == null ? Stream.empty() : protocolMappers.values().stream().distinct(); + final Map protocolMappers = entity.getProtocolMappers(); + return protocolMappers == null ? Stream.empty() : protocolMappers.values().stream().distinct() + .map(this::toModel); + } + + private String safeGetProtocol() { + return entity.getProtocol() == null ? "openid-connect" : entity.getProtocol(); } @Override @@ -515,20 +540,17 @@ public abstract class MapClientAdapter extends AbstractClientModel(model.getConfig())); - } else { + MapProtocolMapperEntity pm = fromModel(model); + if (pm.getId() == null) { + String id = KeycloakModelUtils.generateId(); + pm.setId(id); + } + if (model.getConfig() == null) { pm.setConfig(new HashMap<>()); } entity.setProtocolMapper(pm.getId(), pm); - return pm; + return toModel(pm); } @Override @@ -543,20 +565,25 @@ public abstract class MapClientAdapter extends AbstractClientModel protocolMappers = entity.getProtocolMappers(); + final Map protocolMappers = entity.getProtocolMappers(); + if (! Objects.equals(protocol, safeGetProtocol())) { + return null; + } return protocolMappers == null ? null : protocolMappers.values().stream() - .filter(pm -> Objects.equals(pm.getProtocol(), protocol) && Objects.equals(pm.getName(), name)) + .filter(pm -> Objects.equals(pm.getName(), name)) + .map(this::toModel) .findAny() .orElse(null); } diff --git a/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntity.java b/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntity.java index bc39403600..867307c536 100644 --- a/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntity.java +++ b/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntity.java @@ -16,7 +16,6 @@ */ package org.keycloak.models.map.client; -import org.keycloak.models.ProtocolMapperModel; import org.keycloak.models.map.common.AbstractEntity; import org.keycloak.models.map.common.UpdatableEntity; import java.util.Collection; @@ -35,7 +34,7 @@ import org.keycloak.models.map.annotations.GenerateEntityImplementations; @GenerateEntityImplementations(inherits="org.keycloak.models.map.client.MapClientEntity.AbstractClientEntity") public interface MapClientEntity extends AbstractEntity, UpdatableEntity { - static abstract class AbstractClientEntity implements MapClientEntity { + public abstract class AbstractClientEntity implements MapClientEntity { /** * Flag signalizing that any of the setters has been meaningfully used. */ @@ -79,9 +78,9 @@ public interface MapClientEntity extends AbstractEntity, UpdatableEntity { void setClientScope(String id, Boolean defaultScope); void removeClientScope(String id); - ProtocolMapperModel getProtocolMapper(String id); - Map getProtocolMappers(); - void setProtocolMapper(String id, ProtocolMapperModel mapping); + MapProtocolMapperEntity getProtocolMapper(String id); + Map getProtocolMappers(); + void setProtocolMapper(String id, MapProtocolMapperEntity mapping); void removeProtocolMapper(String id); void addRedirectUri(String redirectUri); diff --git a/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntityLazyDelegate.java b/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntityLazyDelegate.java index 1e0bf81287..5ca21853f0 100644 --- a/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntityLazyDelegate.java +++ b/model/map/src/main/java/org/keycloak/models/map/client/MapClientEntityLazyDelegate.java @@ -16,7 +16,6 @@ */ package org.keycloak.models.map.client; -import org.keycloak.models.ProtocolMapperModel; import java.util.Collection; import java.util.List; import java.util.Map; @@ -164,12 +163,12 @@ public class MapClientEntityLazyDelegate implements MapClientEntity { } @Override - public ProtocolMapperModel getProtocolMapper(String id) { + public MapProtocolMapperEntity getProtocolMapper(String id) { return getReadDelegate().getProtocolMapper(id); } @Override - public Map getProtocolMappers() { + public Map getProtocolMappers() { return getReadDelegate().getProtocolMappers(); } @@ -459,7 +458,7 @@ public class MapClientEntityLazyDelegate implements MapClientEntity { } @Override - public void setProtocolMapper(String id, ProtocolMapperModel mapping) { + public void setProtocolMapper(String id, MapProtocolMapperEntity mapping) { getWriteDelegate().setProtocolMapper(id, mapping); } diff --git a/model/map/src/main/java/org/keycloak/models/map/client/MapProtocolMapperEntity.java b/model/map/src/main/java/org/keycloak/models/map/client/MapProtocolMapperEntity.java new file mode 100644 index 0000000000..b8ff1399cf --- /dev/null +++ b/model/map/src/main/java/org/keycloak/models/map/client/MapProtocolMapperEntity.java @@ -0,0 +1,52 @@ +/* + * Copyright 2021 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.models.map.client; + +import org.keycloak.models.map.annotations.GenerateEntityImplementations; +import org.keycloak.models.map.common.UpdatableEntity; +import java.util.Map; + +/** + * + * @author hmlnarik + */ +@GenerateEntityImplementations( + inherits = "org.keycloak.models.map.client.MapProtocolMapperEntity.AbstractProtocolMapperEntity" +) +public interface MapProtocolMapperEntity extends UpdatableEntity { + + public abstract class AbstractProtocolMapperEntity implements MapProtocolMapperEntity { + protected boolean updated; + + @Override + public boolean isUpdated() { + return this.updated; + } + } + + String getId(); + void setId(String id); + + String getName(); + void setName(String name); + + String getProtocolMapper(); + void setProtocolMapper(String protocolMapper); + + Map getConfig(); + void setConfig(Map config); +}