diff --git a/openid-connect-common/src/main/java/org/mitre/oauth2/model/SystemScope.java b/openid-connect-common/src/main/java/org/mitre/oauth2/model/SystemScope.java index 0807b160e8..83e84dfd13 100644 --- a/openid-connect-common/src/main/java/org/mitre/oauth2/model/SystemScope.java +++ b/openid-connect-common/src/main/java/org/mitre/oauth2/model/SystemScope.java @@ -38,11 +38,13 @@ @Table(name = "system_scope") @NamedQueries({ @NamedQuery(name = SystemScope.QUERY_ALL, query = "select s from SystemScope s ORDER BY s.id"), - @NamedQuery(name = SystemScope.QUERY_BY_VALUE, query = "select s from SystemScope s WHERE s.value = :" + SystemScope.PARAM_VALUE) + @NamedQuery(name = SystemScope.QUERY_BY_VALUE, query = "select s from SystemScope s WHERE s.value = :" + SystemScope.PARAM_VALUE), + @NamedQuery(name = SystemScope.QUERY_BY_VALUES, query = "select s from SystemScope s WHERE s.value in :" + SystemScope.PARAM_VALUE) }) public class SystemScope { public static final String QUERY_BY_VALUE = "SystemScope.getByValue"; + public static final String QUERY_BY_VALUES = "SystemScope.getByValues"; public static final String QUERY_ALL = "SystemScope.findAll"; public static final String PARAM_VALUE = "value"; diff --git a/openid-connect-common/src/main/java/org/mitre/oauth2/repository/SystemScopeRepository.java b/openid-connect-common/src/main/java/org/mitre/oauth2/repository/SystemScopeRepository.java index 8c891d566d..dd41ace47a 100644 --- a/openid-connect-common/src/main/java/org/mitre/oauth2/repository/SystemScopeRepository.java +++ b/openid-connect-common/src/main/java/org/mitre/oauth2/repository/SystemScopeRepository.java @@ -36,6 +36,8 @@ public interface SystemScopeRepository { public SystemScope getByValue(String value); + public Set getByValues(Set values); + public void remove(SystemScope scope); public SystemScope save(SystemScope scope); diff --git a/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaSystemScopeRepository.java b/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaSystemScopeRepository.java index f646b57243..ad0d2da47e 100644 --- a/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaSystemScopeRepository.java +++ b/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaSystemScopeRepository.java @@ -23,6 +23,8 @@ import static org.mitre.util.jpa.JpaUtil.getSingleResult; import static org.mitre.util.jpa.JpaUtil.saveOrUpdate; +import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; @@ -76,6 +78,17 @@ public SystemScope getByValue(String value) { return getSingleResult(query.getResultList()); } + @Override + public Set getByValues(Set values) { + if(values.isEmpty()) { + return Collections.emptySet(); + } + + TypedQuery query = em.createNamedQuery(SystemScope.QUERY_BY_VALUES, SystemScope.class); + query.setParameter(SystemScope.PARAM_VALUE, values); + return new HashSet<>(query.getResultList()); + } + /* (non-Javadoc) * @see org.mitre.oauth2.repository.SystemScopeRepository#remove(org.mitre.oauth2.model.SystemScope) */ diff --git a/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java b/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java index 21474fe6e0..d5182de93a 100644 --- a/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java +++ b/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java @@ -20,8 +20,11 @@ */ package org.mitre.oauth2.service.impl; +import java.util.HashSet; import java.util.LinkedHashSet; +import java.util.Objects; import java.util.Set; +import java.util.stream.Collectors; import org.mitre.oauth2.model.SystemScope; import org.mitre.oauth2.repository.SystemScopeRepository; @@ -32,7 +35,6 @@ import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.base.Strings; import com.google.common.collect.Collections2; import com.google.common.collect.Sets; @@ -67,24 +69,6 @@ public boolean apply(SystemScope input) { } }; - private Function stringToSystemScope = new Function() { - @Override - public SystemScope apply(String input) { - if (Strings.isNullOrEmpty(input)) { - return null; - } else { - // get the real scope if it's available - SystemScope s = getByValue(input); - if (s == null) { - // make a fake one otherwise - s = new SystemScope(input); - } - - return s; - } - } - }; - private Function systemScopeToString = new Function() { @Override public String apply(SystemScope input) { @@ -120,6 +104,10 @@ public SystemScope getByValue(String value) { return repository.getByValue(value); } + private Set getByValues(Set values) { + return repository.getByValues(values); + } + /* (non-Javadoc) * @see org.mitre.oauth2.service.SystemScopeService#remove(org.mitre.oauth2.model.SystemScope) */ @@ -149,7 +137,19 @@ public Set fromStrings(Set scope) { if (scope == null) { return null; } else { - return new LinkedHashSet<>(Collections2.filter(Collections2.transform(scope, stringToSystemScope), Predicates.notNull())); + Set scopeValues = scope.stream().filter(Objects::nonNull).collect(Collectors.toSet()); + Set scopesFromDB = getByValues(scopeValues); + Set scopesFromDBValues = scopesFromDB.stream().map(SystemScope::getValue).collect(Collectors.toSet()); + Set missingScopesFromDB = scopeValues + .stream() + .filter(sv -> !scopesFromDBValues.contains(sv)) + .map(sv -> new SystemScope(sv)) + .collect(Collectors.toSet()); + + Set allScopes = new HashSet(); + allScopes.addAll(scopesFromDB); + allScopes.addAll(missingScopesFromDB); + return allScopes; } }