You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2020/09/29 16:00:06 UTC
[sling-org-apache-sling-auth-core] branch
feature/SLING-9662-Introduce-SlingUri-Mapping-SPI-v3 updated: SLING-9662 :
No need to use ResourceMapper anymore. Path is resolved by
PathToUriMappingService
This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch feature/SLING-9662-Introduce-SlingUri-Mapping-SPI-v3
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-auth-core.git
The following commit(s) were added to refs/heads/feature/SLING-9662-Introduce-SlingUri-Mapping-SPI-v3 by this push:
new 4324c81 SLING-9662 : No need to use ResourceMapper anymore. Path is resolved by PathToUriMappingService
4324c81 is described below
commit 4324c81be785950bdc5044dc2b7b4cfdfd1edc30
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Sep 29 17:58:53 2020 +0200
SLING-9662 : No need to use ResourceMapper anymore. Path is resolved by PathToUriMappingService
---
.../sling/auth/core/impl/SlingAuthenticator.java | 6 +-
.../impl/SlingAuthenticatorServiceListener.java | 86 ++++--------
.../SlingAuthenticatorServiceListenerTest.java | 153 +++------------------
.../auth/core/impl/SlingAuthenticatorTest.java | 22 ++-
4 files changed, 70 insertions(+), 197 deletions(-)
diff --git a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
index 0f2054a..1786c9f 100644
--- a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
+++ b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
@@ -268,7 +268,7 @@ public class SlingAuthenticator implements Authenticator,
private PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authHandlerCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
// package protected for access in inner class ...
- private final PathBasedHolderCache<AuthenticationRequirementHolder> authRequiredCache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
+ final PathBasedHolderCache<AuthenticationRequirementHolder> authRequiredCache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
/** The name of the impersonation parameter */
private String sudoParameterName;
@@ -360,7 +360,7 @@ public class SlingAuthenticator implements Authenticator,
Servlet.class, plugin, props);
serviceListener = SlingAuthenticatorServiceListener.createListener(
- bundleContext, Executors.newSingleThreadExecutor(), resourceResolverFactory, this.authRequiredCache);
+ bundleContext, Executors.newSingleThreadExecutor(), this.authRequiredCache);
authHandlerTracker = new AuthenticationHandlerTracker(bundleContext,
authHandlerCache);
@@ -958,7 +958,7 @@ public class SlingAuthenticator implements Authenticator,
return false;
}
- private boolean isAnonAllowed(HttpServletRequest request) {
+ boolean isAnonAllowed(HttpServletRequest request) {
String path = getPath(request);
diff --git a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java
index 4bf1ab0..eee6157 100644
--- a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java
+++ b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java
@@ -32,9 +32,6 @@ import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.sling.api.SlingConstants;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.api.resource.mapping.ResourceMapper;
import org.apache.sling.auth.core.AuthConstants;
import org.osgi.framework.AllServiceListener;
import org.osgi.framework.BundleContext;
@@ -69,9 +66,6 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
/** Logger */
private final Logger logger = LoggerFactory.getLogger(SlingAuthenticatorServiceListener.class);
- /** Resource resolver factory */
- private final ResourceResolverFactory resolverFactory;
-
/** Auth requirements cache */
private final PathBasedHolderCache<AuthenticationRequirementHolder> authRequiredCache;
@@ -104,12 +98,10 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
static SlingAuthenticatorServiceListener createListener(
final BundleContext context,
final Executor executor,
- final ResourceResolverFactory factory,
final PathBasedHolderCache<AuthenticationRequirementHolder> authRequiredCache) {
final SlingAuthenticatorServiceListener listener = new SlingAuthenticatorServiceListener(authRequiredCache,
- executor,
- factory);
+ executor);
try {
context.addServiceListener(listener, FILTER_EXPR);
final Dictionary<String, Object> props = new Hashtable<>();
@@ -138,11 +130,9 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
* @param factory The resource resolver factory
*/
private SlingAuthenticatorServiceListener(final PathBasedHolderCache<AuthenticationRequirementHolder> authRequiredCache,
- final Executor executor,
- final ResourceResolverFactory factory) {
+ final Executor executor) {
this.authRequiredCache = authRequiredCache;
this.executor = executor;
- this.resolverFactory = factory;
logger.debug("Started auth requirements listener");
}
@@ -228,60 +218,42 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
* Lazy creation of resource resolver / resource mapper
*/
private void processQueue() {
- ResourceResolver resolver = null;
- ResourceMapper mapper = null;
- try {
- while ( this.backgroundJobRunning.get() ) {
- Map.Entry<Long, Action> entry = null;
- synchronized ( this.processingQueue ) {
- final Iterator<Map.Entry<Long, Action> > iter = this.processingQueue.entrySet().iterator();
- if ( iter.hasNext() ) {
- entry = iter.next();
- iter.remove();
- }
- }
- if ( entry == null ) {
- synchronized ( this.processingQueue ) {
- this.backgroundJobRunning.compareAndSet(true, !this.processingQueue.isEmpty());
- }
- } else {
- logger.debug("Processing action for service {} : {}", entry.getKey(), entry.getValue());
- if ( entry.getValue().type != ActionType.REMOVED && mapper == null ) {
- try {
- resolver = this.resolverFactory.getServiceResourceResolver(null);
- mapper = resolver.adaptTo(ResourceMapper.class);
- } catch ( final org.apache.sling.api.resource.LoginException le) {
- // ignore
- }
- }
- process(mapper, entry.getKey(), entry.getValue());
+ while ( this.backgroundJobRunning.get() ) {
+ Map.Entry<Long, Action> entry = null;
+ synchronized ( this.processingQueue ) {
+ final Iterator<Map.Entry<Long, Action> > iter = this.processingQueue.entrySet().iterator();
+ if ( iter.hasNext() ) {
+ entry = iter.next();
+ iter.remove();
}
}
-
- } finally {
- if ( resolver != null ) {
- resolver.close();
+ if ( entry == null ) {
+ synchronized ( this.processingQueue ) {
+ this.backgroundJobRunning.compareAndSet(true, !this.processingQueue.isEmpty());
+ }
+ } else {
+ logger.debug("Processing action for service {} : {}", entry.getKey(), entry.getValue());
+ process(entry.getKey(), entry.getValue());
}
}
}
/**
* Process a single action
- * @param mapper
* @param id
* @param action
*/
- private void process(final ResourceMapper mapper, final Long id, final Action action) {
+ private void process(final Long id, final Action action) {
switch ( action.type ) {
- case ADDED : this.addService(mapper, action.reference);
+ case ADDED : this.addService(action.reference);
break;
case REMOVED : this.removeService((Long)action.reference.getProperty(Constants.SERVICE_ID));
break;
- case MODIFIED : this.modifiedService(mapper, action.reference);
+ case MODIFIED : this.modifiedService(action.reference);
break;
case UPDATE: final List<AuthenticationRequirementHolder> list = props.get(id);
if (!list.isEmpty() ) {
- this.modifiedService(mapper, list.get(0).serviceReference);
+ this.modifiedService(list.get(0).serviceReference);
}
}
}
@@ -305,7 +277,7 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
}
}
- private Set<String> buildPathsSet(final ResourceMapper mapper, final String[] authReqPaths) {
+ private Set<String> buildPathsSet(final String[] authReqPaths) {
final Set<String> paths = new HashSet<>();
for(String authReq : authReqPaths) {
if (authReq != null ) {
@@ -322,12 +294,6 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
prefix = null;
}
paths.add(prefix == null ? authReq : prefix.concat(authReq));
-
- if ( mapper != null ) {
- for(final String mappedPath : mapper.getAllMappings(authReq)) {
- paths.add(prefix == null ? mappedPath : prefix.concat(mappedPath));
- }
- }
}
}
}
@@ -338,11 +304,11 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
* Process a new service with auth requirements
* @param ref The service reference
*/
- private void addService(final ResourceMapper mapper, final ServiceReference<?> ref) {
+ private void addService(final ServiceReference<?> ref) {
final String[] authReqPaths = Converters.standardConverter().convert(ref.getProperty(AuthConstants.AUTH_REQUIREMENTS)).to(String[].class);
if ( authReqPaths.length > 0 ) {
final Long id = (Long)ref.getProperty(Constants.SERVICE_ID);
- final Set<String> paths = buildPathsSet(mapper, authReqPaths);
+ final Set<String> paths = buildPathsSet(authReqPaths);
if ( !paths.isEmpty() ) {
final List<AuthenticationRequirementHolder> authReqList = new ArrayList<AuthenticationRequirementHolder>();
@@ -363,15 +329,15 @@ public class SlingAuthenticatorServiceListener implements AllServiceListener, Ev
* Process a modified service with auth requirements
* @param ref The service reference
*/
- private void modifiedService(final ResourceMapper mapper, final ServiceReference<?> ref) {
+ private void modifiedService(final ServiceReference<?> ref) {
final String[] authReqPaths = Converters.standardConverter().convert(ref.getProperty(AuthConstants.AUTH_REQUIREMENTS)).to(String[].class);
final Long id = (Long)ref.getProperty(Constants.SERVICE_ID);
if ( authReqPaths.length > 0 ) {
final Set<String> oldPaths = regProps.get(id);
if ( oldPaths == null ) {
- addService(mapper, ref);
+ addService(ref);
} else {
- final Set<String> paths = buildPathsSet(mapper, authReqPaths);
+ final Set<String> paths = buildPathsSet(authReqPaths);
if ( paths.isEmpty() ) {
removeService(id);
} else {
diff --git a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java
index c87a1bc..2ceece9 100644
--- a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java
+++ b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java
@@ -25,13 +25,9 @@ import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.api.resource.mapping.ResourceMapper;
import org.apache.sling.auth.core.AuthConstants;
import org.junit.Test;
import org.osgi.framework.BundleContext;
@@ -95,24 +91,10 @@ public class SlingAuthenticatorServiceListenerTest {
return ref;
}
- private ResourceResolverFactory createFactoryForMapper(final ResourceMapper mapper) throws LoginException {
- final ResourceResolverFactory factory = mock(ResourceResolverFactory.class);
-
- final ResourceResolver resolver = mock(ResourceResolver.class);
-
- when(factory.getServiceResourceResolver(null)).thenReturn(resolver);
-
- when(resolver.adaptTo(ResourceMapper.class)).thenReturn(mapper);
-
- return factory;
- }
-
@Test public void testAddRemoveRegistration() throws LoginException {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Collections.singleton("/path1"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
+ final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), cache);
assertTrue(cache.getHolders().isEmpty());
@@ -130,28 +112,24 @@ public class SlingAuthenticatorServiceListenerTest {
@Test public void testAddUpdateRemoveRegistration() throws LoginException {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", "/path1a"));
- when(mapper.getAllMappings("/path2")).thenReturn(Arrays.asList("/path2", "/path2a"));
- when(mapper.getAllMappings("/path3")).thenReturn(Arrays.asList("/path3", "/path3a"));
-
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
+
+ final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), cache);
// add
final ServiceReference<?> ref = createServiceReference(new String[] {"/path1", "/path2"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
- assertPaths(cache, new String[] {"/path1", "/path1a", "/path2", "/path2a"},
- new ServiceReference<?>[] {ref, ref, ref, ref},
- new boolean[] {true, true, true, true});
+ assertPaths(cache, new String[] {"/path1", "/path2"},
+ new ServiceReference<?>[] {ref, ref},
+ new boolean[] {true, true});
// update
when(ref.getProperty(AuthConstants.AUTH_REQUIREMENTS)).thenReturn(new String[] {"/path2", "/path3"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, ref));
- assertPaths(cache, new String[] {"/path2", "/path2a", "/path3", "/path3a"},
- new ServiceReference<?>[] {ref, ref, ref, ref},
- new boolean[] {true, true, true, true});
+ assertPaths(cache, new String[] {"/path2", "/path3"},
+ new ServiceReference<?>[] {ref, ref},
+ new boolean[] {true, true});
// remmove
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));
@@ -162,11 +140,7 @@ public class SlingAuthenticatorServiceListenerTest {
@Test public void testDuplicateRegistration() throws LoginException {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Collections.singleton("/path1"));
- when(mapper.getAllMappings("/path2")).thenReturn(Collections.singleton("/path2"));
- when(mapper.getAllMappings("/path3")).thenReturn(Collections.singleton("/path3"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
+ final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), cache);
final ServiceReference<?> ref1 = createServiceReference(new String[] {"/path1", "/path1", "/path2"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref1));
@@ -188,13 +162,7 @@ public class SlingAuthenticatorServiceListenerTest {
@Test public void testAddRemoveRegistrations() throws LoginException {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Collections.singleton("/path1"));
- when(mapper.getAllMappings("/path2")).thenReturn(Collections.singleton("/path2"));
- when(mapper.getAllMappings("/path3")).thenReturn(Collections.singleton("/path3"));
- when(mapper.getAllMappings("/path4")).thenReturn(Collections.singleton("/path4"));
- when(mapper.getAllMappings("/path5")).thenReturn(Collections.singleton("/path5"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
+ final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), cache);
final ServiceReference<?> ref1 = createServiceReference(new String[] {"/path1"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref1));
@@ -224,13 +192,7 @@ public class SlingAuthenticatorServiceListenerTest {
@Test public void testModifyRegistration() throws LoginException {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Collections.singleton("/path1"));
- when(mapper.getAllMappings("/path2")).thenReturn(Collections.singleton("/path2"));
- when(mapper.getAllMappings("/path3")).thenReturn(Collections.singleton("/path3"));
- when(mapper.getAllMappings("/path4")).thenReturn(Collections.singleton("/path4"));
- when(mapper.getAllMappings("/path5")).thenReturn(Collections.singleton("/path5"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
+ final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), cache);
final ServiceReference<?> ref1 = createServiceReference(new String[] {"/path1", "/path2", "/path3"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref1));
@@ -247,57 +209,13 @@ public class SlingAuthenticatorServiceListenerTest {
listener.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, ref1));
assertTrue(cache.getHolders().isEmpty());
-
- }
-
- @Test public void testRegistrationWithMapping() throws LoginException {
- final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
- final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", "/path2", "/path3"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
-
- final ServiceReference<?> ref = createServiceReference(new String[] {"/path1"});
- listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
-
- assertPaths(cache, new String[] {"/path1", "/path2", "/path3"},
- new ServiceReference<?>[] {ref, ref, ref});
-
- listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));
-
- assertTrue(cache.getHolders().isEmpty());
- }
-
- @Test public void testRegistrationAndUpdatingMapping() throws LoginException {
- final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
- final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", "/path2", "/path3"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
-
- final ServiceReference<?> ref = createServiceReference(new String[] {"/path1"});
- listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
-
- assertPaths(cache, new String[] {"/path1", "/path2", "/path3"},
- new ServiceReference<?>[] {ref, ref, ref});
-
- // update mapper
- when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", "/path5"));
- listener.handleEvent(null);
-
- assertPaths(cache, new String[] {"/path1", "/path5"},
- new ServiceReference<?>[] {ref, ref});
-
- listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));
-
- assertTrue(cache.getHolders().isEmpty());
}
@Test public void testAllowDeny() throws LoginException {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(null), cache);
+ final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), cache);
final ServiceReference<?> ref = createServiceReference(new String[] {"-/path1", "+/path2", "/path3"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
@@ -307,58 +225,27 @@ public class SlingAuthenticatorServiceListenerTest {
new boolean[] {false, true, true});
}
- @Test public void testAllowDenyWithMapping() throws LoginException {
- final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
- final BundleContext context = mock(BundleContext.class);
-
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", "/path1a", "/path1b"));
- when(mapper.getAllMappings("/path2")).thenReturn(Arrays.asList("/path2", "/path2a", "/path2b"));
- when(mapper.getAllMappings("/path3")).thenReturn(Arrays.asList("/path3", "/path3a", "/path3b"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
-
- final ServiceReference<?> ref = createServiceReference(new String[] {"-/path1", "+/path2", "/path3"});
- listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
-
- assertPaths(cache, new String[] {"/path1", "/path2", "/path3", "/path1a", "/path2a", "/path3a", "/path1b", "/path2b", "/path3b"},
- new ServiceReference<?>[] {ref, ref, ref, ref, ref, ref, ref, ref, ref},
- new boolean[] {false, true, true, false, true, true, false, true, true});
-
- // update mapping
- when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", "/path1c"));
- when(mapper.getAllMappings("/path2")).thenReturn(Arrays.asList("/path2", "/path2c"));
- when(mapper.getAllMappings("/path3")).thenReturn(Arrays.asList("/path3", "/path3c"));
- listener.handleEvent(null);
-
- assertPaths(cache, new String[] {"/path1", "/path2", "/path3", "/path1c", "/path2c", "/path3c"},
- new ServiceReference<?>[] {ref, ref, ref, ref, ref, ref},
- new boolean[] {false, true, true, false, true, true});
- }
-
@Test public void testSwitchAllowDeny() throws LoginException {
final PathBasedHolderCache<AuthenticationRequirementHolder> cache = new PathBasedHolderCache<AuthenticationRequirementHolder>();
final BundleContext context = mock(BundleContext.class);
- final ResourceMapper mapper = mock(ResourceMapper.class);
- when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", "/path1a"));
- when(mapper.getAllMappings("/path2")).thenReturn(Arrays.asList("/path2", "/path2a"));
- final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), createFactoryForMapper(mapper), cache);
+ final SlingAuthenticatorServiceListener listener = SlingAuthenticatorServiceListener.createListener(context, callable -> callable.run(), cache);
// add
final ServiceReference<?> ref = createServiceReference(new String[] {"+/path1", "-/path2"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
- assertPaths(cache, new String[] {"/path1", "/path1a", "/path2", "/path2a"},
- new ServiceReference<?>[] {ref, ref, ref, ref},
- new boolean[] {true, true, false, false});
+ assertPaths(cache, new String[] {"/path1", "/path2"},
+ new ServiceReference<?>[] {ref, ref},
+ new boolean[] {true, false});
// update
when(ref.getProperty(AuthConstants.AUTH_REQUIREMENTS)).thenReturn(new String[] {"-/path1", "/path2"});
listener.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, ref));
- assertPaths(cache, new String[] {"/path1", "/path1a", "/path2", "/path2a"},
- new ServiceReference<?>[] {ref, ref, ref, ref},
- new boolean[] {false, false, true, true});
+ assertPaths(cache, new String[] {"/path1", "/path2"},
+ new ServiceReference<?>[] {ref, ref},
+ new boolean[] {false, true});
// remmove
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));
diff --git a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
index 97cfd51..211c46b 100644
--- a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
+++ b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
@@ -18,6 +18,8 @@
*/
package org.apache.sling.auth.core.impl;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.io.IOException;
@@ -27,6 +29,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.api.resource.mapping.PathToUriMappingService;
+import org.apache.sling.api.resource.mapping.PathToUriMappingService.Result;
import org.apache.sling.api.uri.SlingUriBuilder;
import org.apache.sling.auth.core.spi.AuthenticationFeedbackHandler;
import org.apache.sling.auth.core.spi.AuthenticationInfo;
@@ -364,7 +367,24 @@ public class SlingAuthenticatorTest {
Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
}
-
+ @Test public void testIsAnonAllowedWithMapping() {
+ final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
+ when(req.getScheme()).thenReturn("http");
+ when(req.getServerPort()).thenReturn(80);
+
+ final PathBasedHolderCache<AuthenticationRequirementHolder> cache = this.slingAuthenticator.authRequiredCache;
+ cache.addHolder(AuthenticationRequirementHolder.fromConfig("-/path1", null));
+ cache.addHolder(AuthenticationRequirementHolder.fromConfig("-/path2", null));
+
+ final Result r = Mockito.mock(Result.class);
+ when(this.pathToUriMappingService.resolve(req, null)).thenReturn(r);
+ when(r.getUri()).thenReturn(SlingUriBuilder.create().setPath("/path").build());
+ assertFalse(this.slingAuthenticator.isAnonAllowed(req));
+ when(r.getUri()).thenReturn(SlingUriBuilder.create().setPath("/path1").build());
+ assertTrue(this.slingAuthenticator.isAnonAllowed(req));
+ when(r.getUri()).thenReturn(SlingUriBuilder.create().setPath("/path2").build());
+ assertTrue(this.slingAuthenticator.isAnonAllowed(req));
+ }
//---------------------------- PRIVATE METHODS -----------------------------
/**