Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAUTH2-282 Allow the default ScopeDescriptor to describe scopes that are left from deleted SAP entries #174

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public SAPEntryScope(SAPEntry sapEntry, String scope) {
_scope = scope;
}

public SAPEntry getSapEntry() {
return _sapEntry;
}

public String getSapEntryName() {
return _sapEntry.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,37 @@

import com.liferay.oauth2.provider.scope.spi.scope.descriptor.ScopeDescriptor;
import com.liferay.oauth2.provider.scope.spi.scope.finder.ScopeFinder;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.security.service.access.policy.model.SAPEntry;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
* @author Tomas Polesovsky
*/
public class SAPEntryScopeDescriptorFinder
implements ScopeDescriptor, ScopeFinder {

public SAPEntryScopeDescriptorFinder(List<SAPEntryScope> sapEntryScopes) {
public SAPEntryScopeDescriptorFinder(
List<SAPEntryScope> sapEntryScopes,
ScopeDescriptor defaultScopeDescriptor) {

_defaultScopeDescriptor = defaultScopeDescriptor;

for (SAPEntryScope sapEntryScope : sapEntryScopes) {
SAPEntry sapEntry = sapEntryScope.getSapEntry();

if (sapEntry.isEnabled()) {
_scopes.add(sapEntryScope.getScope());
}

_sapEntryScopes.put(sapEntryScope.getScope(), sapEntryScope);
}
}
Expand All @@ -44,24 +56,26 @@ public String describeScope(String scope, Locale locale) {
SAPEntryScope sapEntryScope = _sapEntryScopes.get(scope);

if (sapEntryScope == null) {
if (_log.isWarnEnabled()) {
_log.warn("Unable to get SAP entry scope " + scope);
if (_log.isDebugEnabled()) {
_log.debug("Unable to get SAP entry scope " + scope);
}

return StringPool.BLANK;
return _defaultScopeDescriptor.describeScope(scope, locale);
}

return sapEntryScope.getTitle(locale);
}

@Override
public Collection<String> findScopes() {
return new HashSet<>(_sapEntryScopes.keySet());
return new HashSet<>(_scopes);
}

private static final Log _log = LogFactoryUtil.getLog(
SAPEntryScopeDescriptorFinder.class);

private final ScopeDescriptor _defaultScopeDescriptor;
private final Map<String, SAPEntryScope> _sapEntryScopes = new HashMap<>();
private final Set<String> _scopes = new HashSet<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void register(long companyId) {
List<SAPEntryScope> sapEntryScopes = loadSAPEntryScopes(companyId);

SAPEntryScopeDescriptorFinder sapEntryScopeDescriptorFinder =
new SAPEntryScopeDescriptorFinder(sapEntryScopes);
new SAPEntryScopeDescriptorFinder(
sapEntryScopes, _defaultScopeDescriptor);

_scopeDescriptorServiceRegistrations.compute(
companyId,
Expand Down Expand Up @@ -189,8 +190,6 @@ protected List<SAPEntryScope> loadSAPEntryScopes(long companyId) {

return stream.filter(
this::isOAuth2ExportedSAPEntry
).filter(
SAPEntry::isEnabled
).map(
sapEntry -> new SAPEntryScope(sapEntry, _parseScope(sapEntry))
).collect(
Expand Down Expand Up @@ -242,6 +241,10 @@ private String _parseScope(SAPEntry sapEntry) {
SAPEntryScopeDescriptorFinderRegistrator.class);

private BundleContext _bundleContext;

@Reference(target = "(default=true)")
private ScopeDescriptor _defaultScopeDescriptor;

private final Set<String> _jaxRsApplicationNames =
Collections.newSetFromMap(new ConcurrentHashMap<>());
private final Map<Long, List<SAPEntryScope>> _registeredSAPEntryScopes =
Expand Down