rest interface for claim mapping

This commit is contained in:
Bill Burke committed 2015-02-25 11:34:51 -05:00
1 parent 1704a6c643
commit 9f759ed77c
20 files changed
+435 -59

No files matched your search

@@ -105,7 +105,7 @@ public interface ClientModel {
boolean hasIdentityProvider(String providerId);
Set<ProtocolMapperModel> getProtocolMappers();
void addProtocolMappers(Set<String> mapperNames);
void removeProtocolMappers(Set<String> mapperNames);
void setProtocolMappers(Set<String> mapperNames);
void addProtocolMappers(Set<String> mapperIds);
void removeProtocolMappers(Set<String> mapperIds);
void setProtocolMappers(Set<String> mapperIds);
}
@@ -249,7 +249,7 @@ public interface RealmModel extends RoleContainerModel {
void removeProtocolMapper(ProtocolMapperModel mapping);
void updateProtocolMapper(ProtocolMapperModel mapping);
public ProtocolMapperModel getProtocolMapperById(String id);
public ProtocolMapperModel getProtocolMapperByName(String name);
public ProtocolMapperModel getProtocolMapperByName(String protocol, String name);
}
@@ -19,6 +19,7 @@ import org.keycloak.models.UserSessionModel;
import org.keycloak.representations.idm.ApplicationRepresentation;
import org.keycloak.representations.idm.ClaimRepresentation;
import org.keycloak.representations.idm.ClaimTypeRepresentation;
import org.keycloak.representations.idm.ClientProtocolMappingRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.FederatedIdentityRepresentation;
import org.keycloak.representations.idm.IdentityProviderRepresentation;
@@ -266,8 +267,12 @@ public class ModelToRepresentation {
}
if (!applicationModel.getProtocolMappers().isEmpty()) {
Set<String> mappings = new HashSet<String>();
for (ProtocolMapperModel model : applicationModel.getProtocolMappers()) mappings.add(model.getName());
List<ClientProtocolMappingRepresentation> mappings = new LinkedList<ClientProtocolMappingRepresentation>();
for (ProtocolMapperModel model : applicationModel.getProtocolMappers()) {
ClientProtocolMappingRepresentation map = new ClientProtocolMappingRepresentation();
map.setProtocol(model.getProtocol());
map.setName(model.getName());
}
rep.setProtocolMappers(mappings);
}
@@ -301,10 +306,15 @@ public class ModelToRepresentation {
}
if (!model.getProtocolMappers().isEmpty()) {
Set<String> mappings = new HashSet<String>();
for (ProtocolMapperModel mappingModel : model.getProtocolMappers()) mappings.add(mappingModel.getName());
rep.setProtocolClaimMappings(mappings);
List<ClientProtocolMappingRepresentation> mappings = new LinkedList<ClientProtocolMappingRepresentation>();
for (ProtocolMapperModel mapping : model.getProtocolMappers()) {
ClientProtocolMappingRepresentation map = new ClientProtocolMappingRepresentation();
map.setProtocol(mapping.getProtocol());
map.setName(mapping.getName());
}
rep.setProtocolMappers(mappings);
}
return rep;
}
@@ -23,6 +23,7 @@ import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.ApplicationRepresentation;
import org.keycloak.representations.idm.ClaimRepresentation;
import org.keycloak.representations.idm.ClaimTypeRepresentation;
import org.keycloak.representations.idm.ClientProtocolMappingRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.FederatedIdentityRepresentation;
import org.keycloak.representations.idm.IdentityProviderRepresentation;
@@ -461,9 +462,18 @@ public class RepresentationToModel {
}
if (resourceRep.getProtocolMappers() != null) {
applicationModel.setProtocolMappers(resourceRep.getProtocolMappers());
Set<String> ids = new HashSet<String>();
for (ClientProtocolMappingRepresentation map : resourceRep.getProtocolMappers()) {
ProtocolMapperModel mapperModel = applicationModel.getRealm().getProtocolMapperByName(map.getProtocol(), map.getName());
if (mapperModel != null) {
ids.add(mapperModel.getId());
}
}
applicationModel.setProtocolMappers(ids);
}
return applicationModel;
}
@@ -637,8 +647,16 @@ public class RepresentationToModel {
model.updateAllowedIdentityProviders(rep.getAllowedIdentityProviders());
}
if (rep.getProtocolClaimMappings() != null) {
model.addProtocolMappers(rep.getProtocolClaimMappings());
if (rep.getProtocolMappers() != null) {
Set<String> ids = new HashSet<String>();
for (ClientProtocolMappingRepresentation map : rep.getProtocolMappers()) {
ProtocolMapperModel mapperModel = model.getRealm().getProtocolMapperByName(map.getProtocol(), map.getName());
if (mapperModel != null) {
ids.add(mapperModel.getId());
}
}
model.setProtocolMappers(ids);
}
}
@@ -777,7 +795,7 @@ public class RepresentationToModel {
// we make sure we don't recreate mappers that are automatically created by the protocol providers.
Set<ProtocolMapperModel> mappers = newRealm.getProtocolMappers();
for (ProtocolMapperRepresentation representation : rep.getProtocolMappers()) {
ProtocolMapperModel existing = newRealm.getProtocolMapperByName(representation.getName());
ProtocolMapperModel existing = newRealm.getProtocolMapperByName(representation.getProtocol(), representation.getName());
if (existing == null) {
newRealm.addProtocolMapper(toModel(representation));
} else {
@@ -920,9 +920,9 @@ public class RealmAdapter implements RealmModel {
}
@Override
public ProtocolMapperModel getProtocolMapperByName(String name) {
public ProtocolMapperModel getProtocolMapperByName(String protocol, String name) {
for (ProtocolMapperModel mapping : cached.getClaimMappings()) {
if (mapping.getName().equals(name)) return mapping;
if (mapping.getProtocol().equals(protocol) && mapping.getName().equals(name)) return mapping;
}
return null;
}
@@ -380,9 +380,10 @@ public abstract class ClientAdapter implements ClientModel {
return mappings;
}
protected ProtocolMapperEntity findProtocolMapperByName(String name) {
TypedQuery<ProtocolMapperEntity> query = em.createNamedQuery("getProtocolMapperByName", ProtocolMapperEntity.class);
protected ProtocolMapperEntity findProtocolMapperByName(String protocol, String name) {
TypedQuery<ProtocolMapperEntity> query = em.createNamedQuery("getProtocolMapperByNameProtocol", ProtocolMapperEntity.class);
query.setParameter("name", name);
query.setParameter("protocol", protocol);
query.setParameter("realm", entity.getRealm());
List<ProtocolMapperEntity> entities = query.getResultList();
if (entities.size() == 0) return null;
@@ -396,11 +397,11 @@ public abstract class ClientAdapter implements ClientModel {
Collection<ProtocolMapperEntity> entities = entity.getProtocolMappers();
Set<String> already = new HashSet<String>();
for (ProtocolMapperEntity rel : entities) {
already.add(rel.getName());
already.add(rel.getId());
}
for (String name : mappings) {
if (!already.contains(name)) {
ProtocolMapperEntity mapping = findProtocolMapperByName(name);
for (String id : mappings) {
if (!already.contains(id)) {
ProtocolMapperEntity mapping = em.find(ProtocolMapperEntity.class, id);
if (mapping != null) {
entities.add(mapping);
}
@@ -414,7 +415,7 @@ public abstract class ClientAdapter implements ClientModel {
Collection<ProtocolMapperEntity> entities = entity.getProtocolMappers();
List<ProtocolMapperEntity> remove = new LinkedList<ProtocolMapperEntity>();
for (ProtocolMapperEntity rel : entities) {
if (mappings.contains(rel.getName())) remove.add(rel);
if (mappings.contains(rel.getId())) remove.add(rel);
}
for (ProtocolMapperEntity entity : remove) {
entities.remove(entity);
@@ -428,15 +429,15 @@ public abstract class ClientAdapter implements ClientModel {
Set<String> already = new HashSet<String>();
while (it.hasNext()) {
ProtocolMapperEntity mapper = it.next();
if (mappings.contains(mapper.getName())) {
already.add(mapper.getName());
if (mappings.contains(mapper.getId())) {
already.add(mapper.getId());
continue;
}
it.remove();
}
for (String name : mappings) {
if (!already.contains(name)) {
ProtocolMapperEntity mapping = findProtocolMapperByName(name);
for (String id : mappings) {
if (!already.contains(id)) {
ProtocolMapperEntity mapping = em.find(ProtocolMapperEntity.class, id);
if (mapping != null) {
entities.add(mapping);
}
@@ -632,7 +632,7 @@ public class RealmAdapter implements RealmModel {
Set<String> adding = new HashSet<String>();
for (ProtocolMapperEntity mapper : realm.getProtocolMappers()) {
if (mapper.isAppliedByDefault()) {
adding.add(mapper.getName());
adding.add(mapper.getId());
}
}
client.setProtocolMappers(adding);
@@ -1295,8 +1295,8 @@ public class RealmAdapter implements RealmModel {
@Override
public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) {
if (getProtocolMapperByName(model.getName()) != null) {
throw new RuntimeException("Duplicate protocol mapper with name: " + model.getName());
if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) {
throw new RuntimeException("protocol mapper name must be unique per protocol");
}
String id = KeycloakModelUtils.generateId();
ProtocolMapperEntity entity = new ProtocolMapperEntity();
@@ -1325,9 +1325,9 @@ public class RealmAdapter implements RealmModel {
}
protected ProtocolMapperEntity getProtocolMapperEntityByName(String name) {
protected ProtocolMapperEntity getProtocolMapperEntityByName(String protocol, String name) {
for (ProtocolMapperEntity entity : realm.getProtocolMappers()) {
if (entity.getName().equals(name)) {
if (entity.getProtocol().equals(protocol) && entity.getName().equals(name)) {
return entity;
}
}
@@ -1370,8 +1370,8 @@ public class RealmAdapter implements RealmModel {
}
@Override
public ProtocolMapperModel getProtocolMapperByName(String name) {
ProtocolMapperEntity entity = getProtocolMapperEntityByName(name);
public ProtocolMapperModel getProtocolMapperByName(String protocol, String name) {
ProtocolMapperEntity entity = getProtocolMapperEntityByName(protocol, name);
if (entity == null) return null;
return entityToModel(entity);
}
@@ -20,7 +20,7 @@ import java.util.Map;
*/
@Entity
@NamedQueries({
@NamedQuery(name="getProtocolMapperByName", query="select mapper from ProtocolMapperEntity mapper where mapper.name = :name and mapper.realm = :realm")
@NamedQuery(name="getProtocolMapperByNameProtocol", query="select mapper from ProtocolMapperEntity mapper where mapper.protocol = :protocol and mapper.name = :name and mapper.realm = :realm")
})
@Table(name="PROTOCOL_MAPPER")
public class ProtocolMapperEntity {
@@ -303,22 +303,22 @@ public abstract class ClientAdapter<T extends MongoIdentifiableEntity> extends A
}
@Override
public void addProtocolMappers(Set<String> mapperNames) {
getMongoEntityAsClient().getProtocolMappers().addAll(mapperNames);
public void addProtocolMappers(Set<String> mapperIds) {
getMongoEntityAsClient().getProtocolMappers().addAll(mapperIds);
updateMongoEntity();
}
@Override
public void removeProtocolMappers(Set<String> mapperNames) {
getMongoEntityAsClient().getProtocolMappers().removeAll(mapperNames);
public void removeProtocolMappers(Set<String> mapperIds) {
getMongoEntityAsClient().getProtocolMappers().removeAll(mapperIds);
updateMongoEntity();
}
@Override
public void setProtocolMappers(Set<String> mapperNames) {
public void setProtocolMappers(Set<String> mapperIds) {
getMongoEntityAsClient().getProtocolMappers().clear();
getMongoEntityAsClient().getProtocolMappers().addAll(mapperNames);
getMongoEntityAsClient().getProtocolMappers().addAll(mapperIds);
updateMongoEntity();
}
@@ -619,7 +619,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
public void addDefaultClientProtocolMappers(ClientModel client) {
Set<String> adding = new HashSet<String>();
for (ProtocolMapperEntity mapper : realm.getProtocolMappers()) {
if (mapper.isAppliedByDefault()) adding.add(mapper.getName());
if (mapper.isAppliedByDefault()) adding.add(mapper.getId());
}
client.setProtocolMappers(adding);
@@ -820,6 +820,9 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
@Override
public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) {
if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) {
throw new RuntimeException("protocol mapper name must be unique per protocol");
}
ProtocolMapperEntity entity = new ProtocolMapperEntity();
entity.setId(KeycloakModelUtils.generateId());
entity.setProtocol(model.getProtocol());
@@ -855,9 +858,9 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
return null;
}
protected ProtocolMapperEntity getProtocolMapperyEntityByName(String name) {
protected ProtocolMapperEntity getProtocolMapperEntityByName(String protocol, String name) {
for (ProtocolMapperEntity entity : realm.getProtocolMappers()) {
if (entity.getName().equals(name)) {
if (entity.getProtocol().equals(protocol) && entity.getName().equals(name)) {
return entity;
}
}
@@ -891,8 +894,8 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
}
@Override
public ProtocolMapperModel getProtocolMapperByName(String name) {
ProtocolMapperEntity entity = getProtocolMapperyEntityById(name);
public ProtocolMapperModel getProtocolMapperByName(String protocol, String name) {
ProtocolMapperEntity entity = getProtocolMapperEntityByName(protocol, name);
if (entity == null) return null;
return entityToModel(entity);
}