This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-auth-core.git
The following commit(s) were added to refs/heads/master by this push:
new 1b7c761 SLING-10248 : Refactor: Move auth requirements handling into
separate component
1b7c761 is described below
commit 1b7c761e898617fb977b66144371308c58631252
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Tue Mar 23 14:57:26 2021 +0100
SLING-10248 : Refactor: Move auth requirements handling into separate
component
---
.../core/impl/AuthenticationHandlerHolder.java | 2 +-
.../impl/AuthenticationRequirementsManager.java | 104 ++++++++++++---------
.../core/impl/AuthenticatorWebConsolePlugin.java | 5 +-
.../sling/auth/core/impl/PathBasedHolderCache.java | 2 +-
.../sling/auth/core/impl/SlingAuthenticator.java | 48 ++--------
.../AuthenticationRequirementsManagerTest.java | 91 +++++++++++-------
.../auth/core/impl/SlingAuthenticatorOsgiTest.java | 3 +
.../auth/core/impl/SlingAuthenticatorTest.java | 51 ++++++----
8 files changed, 169 insertions(+), 137 deletions(-)
diff --git
a/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
b/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
index a36ccb6..7c751ea 100644
---
a/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
+++
b/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
@@ -50,7 +50,7 @@ final class AuthenticationHandlerHolder extends
AuthenticationHandlerHolder(final String fullPath,
final AuthenticationHandler handler,
- final ServiceReference serviceReference) {
+ final ServiceReference<?> serviceReference) {
super(fullPath, serviceReference);
final String browserOnly =
Converters.standardConverter().convert(serviceReference.getProperty(AuthConstants.AUTH_HANDLER_BROWSER_ONLY)).to(String.class);
diff --git
a/src/main/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManager.java
b/src/main/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManager.java
index 69bee91..4ed10d7 100644
---
a/src/main/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManager.java
+++
b/src/main/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManager.java
@@ -19,9 +19,7 @@
package org.apache.sling.auth.core.impl;
import java.util.ArrayList;
-import java.util.Dictionary;
import java.util.HashSet;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@@ -29,6 +27,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.sling.api.SlingConstants;
@@ -42,7 +41,11 @@ import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
@@ -55,6 +58,11 @@ import org.slf4j.LoggerFactory;
* the service registry.
*
*/
+@Component(service = {AuthenticationRequirementsManager.class,
EventHandler.class},
+ configurationPid = SlingAuthenticator.PID,
+ property = {
+ EventConstants.EVENT_TOPIC + "=" +
SlingConstants.TOPIC_RESOURCE_RESOLVER_MAPPING_CHANGED
+ })
public class AuthenticationRequirementsManager
extends PathBasedHolderCache<AuthenticationRequirementHolder>
implements AllServiceListener, EventHandler {
@@ -80,9 +88,6 @@ public class AuthenticationRequirementsManager
/** Cache for registered holders for an auth requirement */
private final Map<Long, List<AuthenticationRequirementHolder>> props = new
ConcurrentHashMap<>();
- /** Service registration for the event handler */
- private ServiceRegistration<EventHandler> serviceRegistration;
-
/** Processing queue for changes */
private final Map<Long, Action> processingQueue = new LinkedHashMap<>();
@@ -93,65 +98,76 @@ public class AuthenticationRequirementsManager
private final AtomicBoolean backgroundJobRunning = new
AtomicBoolean(false);
/**
- * Create a new listener
- * @param context Bundle context
- * @param executor Executor service
- * @param factory Resource resolver factory
- * @param authRequiredCache The cache for the auth requirements
- * @return
+ * Create a new manager
+ * @param executor For updating
+ * @param factory The resource resolver factory
*/
- static AuthenticationRequirementsManager createListener(
- final BundleContext context,
- final Executor executor,
- final ResourceResolverFactory factory) {
+ @Activate
+ public AuthenticationRequirementsManager(final BundleContext context,
+ @Reference ResourceResolverFactory factory,
+ final SlingAuthenticator.Config config) {
+ this(context, factory, config, Executors.newSingleThreadExecutor());
+ }
- final AuthenticationRequirementsManager listener = new
AuthenticationRequirementsManager(executor,
- factory);
+ /**
+ * Create a new manager
+ * @param executor For updating
+ * @param factory The resource resolver factory
+ */
+ AuthenticationRequirementsManager(
+ final BundleContext context,
+ final ResourceResolverFactory factory,
+ final SlingAuthenticator.Config config,
+ final Executor executor) {
+ this.executor = executor;
+ this.resolverFactory = factory;
+ this.modified(config);
try {
- context.addServiceListener(listener, FILTER_EXPR);
- final Dictionary<String, Object> props = new Hashtable<>();
- props.put(EventConstants.EVENT_TOPIC,
SlingConstants.TOPIC_RESOURCE_RESOLVER_MAPPING_CHANGED);
-
listener.setServiceRegistration(context.registerService(EventHandler.class,
listener, props));
+ context.addServiceListener(this, FILTER_EXPR);
ServiceReference<?>[] refs = context.getAllServiceReferences(null,
FILTER_EXPR);
if (refs != null) {
for (final ServiceReference<?> ref : refs) {
final Long id =
(Long)ref.getProperty(Constants.SERVICE_ID);
- listener.queue(id, new Action(ActionType.ADDED, ref));
+ this.queue(id, new Action(ActionType.ADDED, ref));
}
}
- listener.schedule();
+ this.schedule();
- return listener;
- } catch (InvalidSyntaxException ise) {
+ } catch (final InvalidSyntaxException ise) {
+ // the filter expression is constants
}
- return null;
- }
-
- /**
- * Create a new listener
- * @param executor For updating
- * @param factory The resource resolver factory
- */
- private AuthenticationRequirementsManager(final Executor executor,
- final ResourceResolverFactory factory) {
- this.executor = executor;
- this.resolverFactory = factory;
logger.debug("Started auth requirements listener");
}
- void setServiceRegistration(final ServiceRegistration<EventHandler> reg) {
- this.serviceRegistration = reg;
+ @Modified
+ private void modified(final SlingAuthenticator.Config config) {
+ this.clear();
+ this.addHolder(new AuthenticationRequirementHolder("/",
!config.auth_annonymous(), null));
+
+ if (config.sling_auth_requirements() != null) {
+ for (String authReq : config.sling_auth_requirements()) {
+ if (authReq != null && authReq.length() > 0) {
+ this.addHolder(AuthenticationRequirementHolder.fromConfig(
+ authReq, null));
+ }
+ }
+ }
+ // don't require authentication for login/logout servlets
+ this.addHolder(new AuthenticationRequirementHolder(
+ LoginServlet.SERVLET_PATH, false, null));
+ this.addHolder(new AuthenticationRequirementHolder(
+ LogoutServlet.SERVLET_PATH, false, null));
+
+ // add all registered services
+ this.registerAllServices();
}
+ @Deactivate
public void stop(final BundleContext bundleContext) {
this.clear();
bundleContext.removeServiceListener(this);
- if ( this.serviceRegistration != null ) {
- this.serviceRegistration.unregister();
- this.serviceRegistration = null;
- }
queue(CLEAR, null);
backgroundJobRunning.set(false);
logger.debug("Stopped auth requirements listener");
diff --git
a/src/main/java/org/apache/sling/auth/core/impl/AuthenticatorWebConsolePlugin.java
b/src/main/java/org/apache/sling/auth/core/impl/AuthenticatorWebConsolePlugin.java
index f55f6c3..bc5702c 100644
---
a/src/main/java/org/apache/sling/auth/core/impl/AuthenticatorWebConsolePlugin.java
+++
b/src/main/java/org/apache/sling/auth/core/impl/AuthenticatorWebConsolePlugin.java
@@ -51,6 +51,9 @@ public class AuthenticatorWebConsolePlugin extends
HttpServlet {
/** The title for the web console */
public static final String TITLE = "Authenticator";
+ @Reference(service = AuthenticationRequirementsManager.class)
+ private PathBasedHolderCache<AuthenticationRequirementHolder>
authenticationRequirementsManager;
+
@Reference
private SlingAuthenticator slingAuthenticator;
@@ -115,7 +118,7 @@ public class AuthenticatorWebConsolePlugin extends
HttpServlet {
pw.println("<th class='content'>Defining Service (Description or
ID)</td>");
pw.println("</tr>");
- final List<AuthenticationRequirementHolder> holderList =
slingAuthenticator.getAuthenticationRequirements();
+ final List<AuthenticationRequirementHolder> holderList =
authenticationRequirementsManager.getHolders();
for (final AuthenticationRequirementHolder req : holderList) {
pw.println("<tr class='content'>");
diff --git
a/src/main/java/org/apache/sling/auth/core/impl/PathBasedHolderCache.java
b/src/main/java/org/apache/sling/auth/core/impl/PathBasedHolderCache.java
index e166fe6..37427c8 100644
--- a/src/main/java/org/apache/sling/auth/core/impl/PathBasedHolderCache.java
+++ b/src/main/java/org/apache/sling/auth/core/impl/PathBasedHolderCache.java
@@ -37,7 +37,7 @@ public class PathBasedHolderCache<Type extends
PathBasedHolder> {
*/
private final Map<String, Map<String, SortedSet<Type>>> cache = new
ConcurrentHashMap<>();
- public void clear() {
+ protected void clear() {
cache.clear();
}
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 4811d09..6f03a97 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
@@ -31,7 +31,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Executors;
import java.util.function.Function;
import javax.jcr.SimpleCredentials;
@@ -68,7 +67,6 @@ import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.FieldOption;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
@@ -105,7 +103,7 @@ import org.slf4j.LoggerFactory;
* Currently this class does not support multiple handlers for any one request
* URL.
*/
-@Component(name = "org.apache.sling.engine.impl.auth.SlingAuthenticator",
+@Component(name = SlingAuthenticator.PID,
service = {Authenticator.class, AuthenticationSupport.class,
ServletRequestListener.class, SlingAuthenticator.class })
@HttpWhiteboardContextSelect("(" +
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=*)")
@HttpWhiteboardListener
@@ -115,6 +113,9 @@ import org.slf4j.LoggerFactory;
public class SlingAuthenticator implements Authenticator,
AuthenticationSupport, ServletRequestListener {
+ /** The pid for the configuration */
+ public static final String PID =
"org.apache.sling.engine.impl.auth.SlingAuthenticator";
+
@ObjectClassDefinition(name = "Apache Sling Authentication Service",
description = "Extracts user authentication details from the request
with" +
" the help of authentication handlers registered as separate
services. One" +
@@ -301,7 +302,7 @@ public class SlingAuthenticator implements Authenticator,
* The listener for services registered with "sling.auth.requirements" to
* update the internal authentication requirements
*/
- private final AuthenticationRequirementsManager serviceListener;
+ private final PathBasedHolderCache<AuthenticationRequirementHolder>
authenticationRequirementsManager;
/**
* AuthenticationInfoPostProcessor services
@@ -326,14 +327,14 @@ public class SlingAuthenticator implements Authenticator,
@Activate
public SlingAuthenticator(@Reference(policyOption =
ReferencePolicyOption.GREEDY) final MetricsService metricsService,
+ @Reference AuthenticationRequirementsManager authReqManager,
@Reference(policyOption = ReferencePolicyOption.GREEDY) final
ResourceResolverFactory resourceResolverFactory,
final BundleContext bundleContext,
final Config config) {
this.metrics = new SlingAuthenticationMetrics(metricsService);
this.resourceResolverFactory = resourceResolverFactory;
- this.serviceListener =
AuthenticationRequirementsManager.createListener(
- bundleContext, Executors.newSingleThreadExecutor(),
resourceResolverFactory);
+ this.authenticationRequirementsManager = authReqManager;
this.modified(config);
}
@@ -377,35 +378,6 @@ public class SlingAuthenticator implements Authenticator,
} else {
this.httpBasicHandler = new
HttpBasicAuthenticationHandler(config.auth_http_realm(),
HTTP_AUTH_ENABLED.equals(http));
}
-
- // update auth requirments based on configuration
- this.serviceListener.clear();
- this.serviceListener.addHolder(new
AuthenticationRequirementHolder("/", !config.auth_annonymous(), null));
-
- if (config.sling_auth_requirements() != null) {
- for (String authReq : config.sling_auth_requirements()) {
- if (authReq != null && authReq.length() > 0) {
-
this.serviceListener.addHolder(AuthenticationRequirementHolder.fromConfig(
- authReq, null));
- }
- }
- }
-
- // don't require authentication for login/logout servlets
- this.serviceListener.addHolder(new AuthenticationRequirementHolder(
- LoginServlet.SERVLET_PATH, false, null));
- this.serviceListener.addHolder(new AuthenticationRequirementHolder(
- LogoutServlet.SERVLET_PATH, false, null));
-
- // add all registered services
- if (serviceListener != null) {
- serviceListener.registerAllServices();
- }
- }
-
- @Deactivate
- private void deactivate(final BundleContext bundleContext) {
- this.serviceListener.stop(bundleContext);
}
// --------- AuthenticationSupport interface
@@ -671,10 +643,6 @@ public class SlingAuthenticator implements Authenticator,
return handlerMap;
}
- List<AuthenticationRequirementHolder> getAuthenticationRequirements() {
- return this.serviceListener.getHolders();
- }
-
/**
* Returns the name of the user to assume for requests without credentials.
* This may be <code>null</code> if not configured and the default
anonymous
@@ -914,7 +882,7 @@ public class SlingAuthenticator implements Authenticator,
String path = getPath(request);
- final Collection<AuthenticationRequirementHolder>[] holderSetArray =
this.serviceListener.findApplicableHolders(request);
+ final Collection<AuthenticationRequirementHolder>[] holderSetArray =
this.authenticationRequirementsManager.findApplicableHolders(request);
for (int m = 0; m < holderSetArray.length; m++) {
final Collection<AuthenticationRequirementHolder> holders =
holderSetArray[m];
if (holders != null) {
diff --git
a/src/test/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManagerTest.java
b/src/test/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManagerTest.java
index e814e7a..115c961 100644
---
a/src/test/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManagerTest.java
+++
b/src/test/java/org/apache/sling/auth/core/impl/AuthenticationRequirementsManagerTest.java
@@ -41,6 +41,19 @@ import org.osgi.framework.ServiceReference;
public class AuthenticationRequirementsManagerTest {
+ private SlingAuthenticator.Config createDefaultConfig() {
+ final SlingAuthenticator.Config config =
mock(SlingAuthenticator.Config.class);
+
+ when(config.auth_sudo_cookie()).thenReturn("sling.sudo");
+ when(config.auth_sudo_parameter()).thenReturn("sudo");
+ when(config.auth_annonymous()).thenReturn(true);
+
when(config.auth_http()).thenReturn(SlingAuthenticator.HTTP_AUTH_PREEMPTIVE);
+ when(config.auth_http_realm()).thenReturn("Sling (Development)");
+ when(config.auth_uri_suffix()).thenReturn(new String[]
{SlingAuthenticator.DEFAULT_AUTH_URI_SUFFIX});
+
+ return config;
+ }
+
private void assertPaths(final
PathBasedHolderCache<AuthenticationRequirementHolder> cache,
final String[] paths,
final ServiceReference<?>[] refs) {
@@ -56,22 +69,25 @@ public class AuthenticationRequirementsManagerTest {
assertEquals("Wrong input to assert paths", paths.length,
requireAuth.length);
}
- assertEquals(paths.length, cache.getHolders().size());
+ // there are three default entries
+ assertEquals(3 + paths.length, cache.getHolders().size());
for(final AuthenticationRequirementHolder h : cache.getHolders()) {
- boolean found = false;
- int index = 0;
- while ( !found && index < paths.length ) {
- if (paths[index].equals(h.path) &&
refs[index].equals(h.serviceReference) ) {
- found = true;
-
- if ( requireAuth != null ) {
- assertEquals(requireAuth[index],
h.requiresAuthentication());
+ boolean found = Arrays.asList(LoginServlet.SERVLET_PATH,
LogoutServlet.SERVLET_PATH, "/").contains(h.path);
+ if ( !found ) {
+ int index = 0 ;
+ while ( !found && index < paths.length ) {
+ if (paths[index].equals(h.path) &&
refs[index].equals(h.serviceReference) ) {
+ found = true;
+
+ if ( requireAuth != null ) {
+ assertEquals(requireAuth[index],
h.requiresAuthentication());
+ }
+ } else {
+ index++;
}
- } else {
- index++;
}
}
- assertTrue(Arrays.toString(paths) + " should contain " + h.path,
found);
+ assertTrue(Arrays.toString(paths) + " should contain " + h.path,
found);
}
}
@@ -111,9 +127,10 @@ public class AuthenticationRequirementsManagerTest {
final BundleContext context = mock(BundleContext.class);
final ResourceMapper mapper = mock(ResourceMapper.class);
when(mapper.getAllMappings("/path1")).thenReturn(Collections.singleton("/path1"));
- final AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
final ServiceReference<?> ref = createServiceReference(new String[]
{"/path1"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
@@ -123,7 +140,7 @@ public class AuthenticationRequirementsManagerTest {
manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref));
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
}
@Test public void testAddUpdateRemoveRegistration() throws LoginException {
@@ -133,7 +150,8 @@ public class AuthenticationRequirementsManagerTest {
when(mapper.getAllMappings("/path2")).thenReturn(Arrays.asList("/path2",
"/path2a"));
when(mapper.getAllMappings("/path3")).thenReturn(Arrays.asList("/path3",
"/path3a"));
- final AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
// add
final ServiceReference<?> ref = createServiceReference(new String[]
{"/path1", "/path2"});
@@ -154,7 +172,7 @@ public class AuthenticationRequirementsManagerTest {
// remmove
manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref));
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
}
@Test public void testDuplicateRegistration() throws LoginException {
@@ -163,7 +181,8 @@ public class AuthenticationRequirementsManagerTest {
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 AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
final ServiceReference<?> ref1 = createServiceReference(new String[]
{"/path1", "/path1", "/path2"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
ref1));
@@ -179,7 +198,7 @@ public class AuthenticationRequirementsManagerTest {
new ServiceReference<?>[] {ref1, ref1});
manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref1));
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
}
@Test public void testAddRemoveRegistrations() throws LoginException {
@@ -190,7 +209,8 @@ public class AuthenticationRequirementsManagerTest {
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 AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
final ServiceReference<?> ref1 = createServiceReference(new String[]
{"/path1"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
ref1));
@@ -204,7 +224,7 @@ public class AuthenticationRequirementsManagerTest {
assertPaths(manager, new String[] { "/path1", "/path2", "/path3",
"/path4", "/path5"},
new ServiceReference<?>[] {ref1, ref2, ref2, ref3,
ref3});
- manager.serviceChanged(new
ServiceEvent(ServiceEvent.UNREGISTERING, ref2));
+ manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref2));
assertPaths(manager, new String[] { "/path1", "/path4", "/path5"},
new ServiceReference<?>[] {ref1, ref3, ref3});
@@ -214,7 +234,7 @@ public class AuthenticationRequirementsManagerTest {
new ServiceReference<?>[] {ref3, ref3});
manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref3));
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
}
@Test public void testModifyRegistration() throws LoginException {
@@ -225,7 +245,8 @@ public class AuthenticationRequirementsManagerTest {
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 AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
final ServiceReference<?> ref1 = createServiceReference(new String[]
{"/path1", "/path2", "/path3"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
ref1));
@@ -241,15 +262,15 @@ public class AuthenticationRequirementsManagerTest {
new ServiceReference<?>[] {ref1, ref1, ref1});
manager.serviceChanged(new
ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, ref1));
- assertTrue(manager.getHolders().isEmpty());
-
+ assertEquals(3, manager.getHolders().size());
}
@Test public void testRegistrationWithMapping() throws LoginException {
final BundleContext context = mock(BundleContext.class);
final ResourceMapper mapper = mock(ResourceMapper.class);
when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1",
"/path2", "/path3"));
- final AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
final ServiceReference<?> ref = createServiceReference(new String[]
{"/path1"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
@@ -259,14 +280,15 @@ public class AuthenticationRequirementsManagerTest {
manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref));
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
}
@Test public void testRegistrationAndUpdatingMapping() throws
LoginException {
final BundleContext context = mock(BundleContext.class);
final ResourceMapper mapper = mock(ResourceMapper.class);
when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1",
"/path2", "/path3"));
- final AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
final ServiceReference<?> ref = createServiceReference(new String[]
{"/path1"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
@@ -283,13 +305,14 @@ public class AuthenticationRequirementsManagerTest {
manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref));
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
}
@Test public void testAllowDeny() throws LoginException {
final BundleContext context = mock(BundleContext.class);
- final AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(null));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(null),
+ createDefaultConfig(), callable -> callable.run());
final ServiceReference<?> ref = createServiceReference(new String[]
{"-/path1", "+/path2", "/path3"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
@@ -306,7 +329,8 @@ public class AuthenticationRequirementsManagerTest {
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 AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
final ServiceReference<?> ref = createServiceReference(new String[]
{"-/path1", "+/path2", "/path3"});
manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, ref));
@@ -332,7 +356,8 @@ public class AuthenticationRequirementsManagerTest {
when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1",
"/path1a"));
when(mapper.getAllMappings("/path2")).thenReturn(Arrays.asList("/path2",
"/path2a"));
- final AuthenticationRequirementsManager manager =
AuthenticationRequirementsManager.createListener(context, callable ->
callable.run(), createFactoryForMapper(mapper));
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(context, createFactoryForMapper(mapper),
+ createDefaultConfig(), callable -> callable.run());
// add
final ServiceReference<?> ref = createServiceReference(new String[]
{"+/path1", "-/path2"});
@@ -353,6 +378,6 @@ public class AuthenticationRequirementsManagerTest {
// remmove
manager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,
ref));
- assertTrue(manager.getHolders().isEmpty());
+ assertEquals(3, manager.getHolders().size());
}
}
diff --git
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java
index 5777107..a6ce7d8 100644
---
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java
+++
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorOsgiTest.java
@@ -57,6 +57,7 @@ public class SlingAuthenticatorOsgiTest {
public void before() throws Exception {
ResourceResolver rr = mock(ResourceResolver.class);
ResourceResolverFactory resourceResolverFactory =
mock(ResourceResolverFactory.class);
+
when(resourceResolverFactory.getResourceResolver(any(AuthenticationInfo.class))).thenReturn(rr);
when(timer.time()).thenReturn(ctx);
@@ -66,6 +67,8 @@ public class SlingAuthenticatorOsgiTest {
context.registerService(ResourceResolverFactory.class,
resourceResolverFactory);
context.registerService(MetricsService.class, metricsService);
+
context.registerInjectActivateService(AuthenticationRequirementsManager.class);
+
authenticator =
context.registerInjectActivateService(SlingAuthenticator.class);
}
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 23e3440..84c6efc 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
@@ -100,7 +100,8 @@ public class SlingAuthenticatorTest {
// anon is allowed by default
final SlingAuthenticator.Config config = createDefaultConfig();
- final SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
config, callable -> callable.run());
+ final SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), config);
final HttpServletRequest request =
Mockito.mock(HttpServletRequest.class);
@@ -117,7 +118,8 @@ public class SlingAuthenticatorTest {
final SlingAuthenticator.Config config = createDefaultConfig();
Mockito.when(config.auth_annonymous()).thenReturn(false);
- final SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
config, callable -> callable.run());
+ final SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), config);
final HttpServletRequest request =
Mockito.mock(HttpServletRequest.class);
@@ -138,7 +140,8 @@ public class SlingAuthenticatorTest {
final String PROTECTED_PATH = "/content/en/test";
final String REQUEST_CHILD_NODE = "/content/en/test/childnodetest";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>
authRequiredCache = new
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
@@ -166,7 +169,8 @@ public class SlingAuthenticatorTest {
final String PROTECTED_PATH = "/content/en/test";
final String REQUEST_CHILD_NODE = "/content/en/test";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>
authRequiredCache = new
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
@@ -194,7 +198,8 @@ public class SlingAuthenticatorTest {
final String PROTECTED_PATH = "/content/en/test";
final String REQUEST_CHILD_NODE = "/content/en/test/";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>
authRequiredCache = new
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
@@ -222,7 +227,8 @@ public class SlingAuthenticatorTest {
final String PROTECTED_PATH = "/content/en/test";
final String REQUEST_CHILD_NODE = "/content/en/test.html";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>
authRequiredCache = new
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
@@ -246,7 +252,8 @@ public class SlingAuthenticatorTest {
final String PROTECTED_PATH = "/";
final String REQUEST_CHILD_NODE = "/content/en/test";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>
authRequiredCache = new
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
@@ -272,7 +279,8 @@ public class SlingAuthenticatorTest {
final String PROTECTED_PATH_LONGER = "/resource1.test2";
final String REQUEST_CHILD_NODE = "/resource1.test2";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>
authRequiredCache = new
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
@@ -314,7 +322,8 @@ public class SlingAuthenticatorTest {
final String PROTECTED_PATH = "/content/en/test";
final String REQUEST_NOT_PROTECTED_PATH = "/content/en/test2";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>
authRequiredCache = new
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
@@ -336,7 +345,8 @@ public class SlingAuthenticatorTest {
public void test_childNodeAuthenticationHandlerPath() throws Throwable {
final String requestPath = "/content/test/test2";
final String handlerPath = "/content/test";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));
@@ -346,7 +356,8 @@ public class SlingAuthenticatorTest {
public void test_siblingNodeAuthenticationHandlerPath() throws Throwable {
final String requestPath =
"/content/test2.html/en/2016/09/19/test.html";
final String handlerPath = "/content/test";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertFalse(
(boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));
@@ -356,7 +367,8 @@ public class SlingAuthenticatorTest {
public void test_actualNodeAuthenticationHandlerPath() throws Throwable {
final String requestPath = "/content/test";
final String handlerPath = "/content/test";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));
@@ -366,7 +378,8 @@ public class SlingAuthenticatorTest {
public void test_rootNodeAuthenticationHandlerPath() throws Throwable {
final String requestPath = "/content/test";
final String handlerPath = "/";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));
@@ -376,7 +389,8 @@ public class SlingAuthenticatorTest {
public void test_requestPathSelectorsAreTakenInConsideration() throws
Throwable {
final String requestPath =
"/content/test.selector1.selector2.html/en/2016/test.html";
final String handlerPath = "/content/test";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));
@@ -386,7 +400,8 @@ public class SlingAuthenticatorTest {
public void test_requestPathSelectorsSiblingAreTakenInConsideration()
throws Throwable {
final String requestPath =
"/content/test.selector1.selector2.html/en/2016/09/19/test.html";
final String handlerPath = "/content/test2";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertFalse(
(boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));
@@ -396,7 +411,8 @@ public class SlingAuthenticatorTest {
public void test_requestPathBackSlash() throws Throwable {
final String requestPath = "/page1\\somesubepage";
final String handlerPath = "/page";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertFalse(
(boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));
@@ -406,7 +422,8 @@ public class SlingAuthenticatorTest {
public void test_emptyNodeAuthenticationHandlerPath() throws Throwable {
final String requestPath = "/content/test";
final String handlerPath = "";
- SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class),
+ final AuthenticationRequirementsManager manager = new
AuthenticationRequirementsManager(Mockito.mock(BundleContext.class), null,
createDefaultConfig(), callable -> callable.run());
+ SlingAuthenticator slingAuthenticator = new
SlingAuthenticator(Mockito.mock(MetricsService.class), manager,
null, Mockito.mock(BundleContext.class), createDefaultConfig());
Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator,
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new
Object[] {requestPath, handlerPath}));