Author: justin
Date: Tue Jan 10 20:34:41 2012
New Revision: 1229715
URL: http://svn.apache.org/viewvc?rev=1229715&view=rev
Log:
SLING-2349 - adding login and logout events
Modified:
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/AuthConstants.java
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AbstractAuthenticationHandlerHolder.java
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/engine/EngineAuthenticationHandlerHolder.java
Modified:
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/AuthConstants.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/AuthConstants.java?rev=1229715&r1=1229714&r2=1229715&view=diff
==============================================================================
---
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/AuthConstants.java
(original)
+++
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/AuthConstants.java
Tue Jan 10 20:34:41 2012
@@ -77,6 +77,25 @@ public final class AuthConstants {
*/
public static final String AUTH_HANDLER_BROWSER_ONLY =
"sling.auth.browser-only";
+ /**
+ * The topic for the OSGi event which is sent when a user has logged in
successfully.
+ * The event contains at least the {@link
org.apache.sling.api.SlingConstants#PROPERTY_USERID},
+ * and {@link #PROPERTY_AUTH_HANDLER_CLASS} properties.
+ */
+ public static final String TOPIC_LOGIN =
"org/apache/sling/auth/core/Authenticator/LOGIN";
+
+ /**
+ * The topic for the OSGi event which is sent when a user has logged out.
+ * The event contains at least the {@link
org.apache.sling.api.SlingConstants#PROPERTY_USERID}
+ * property.
+ */
+ public static final String TOPIC_LOGOUT =
"org/apache/sling/auth/core/Authenticator/LOGOUT";
+
+ /**
+ * The name of the event property holding the authentication handler class
name.
+ */
+ public static final String PROPERTY_AUTH_HANDLER_CLASS =
"authHandlerClassName";
+
private AuthConstants() {
}
Modified:
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AbstractAuthenticationHandlerHolder.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AbstractAuthenticationHandlerHolder.java?rev=1229715&r1=1229714&r2=1229715&view=diff
==============================================================================
---
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AbstractAuthenticationHandlerHolder.java
(original)
+++
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AbstractAuthenticationHandlerHolder.java
Tue Jan 10 20:34:41 2012
@@ -153,6 +153,13 @@ public abstract class AbstractAuthentica
protected abstract void doDropCredentials(HttpServletRequest request,
HttpServletResponse response) throws IOException;
+ /**
+ * Return the held handler.
+ *
+ * @return the held handler
+ */
+ protected abstract Object getHandler();
+
// ---------- internal
/**
Modified:
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java?rev=1229715&r1=1229714&r2=1229715&view=diff
==============================================================================
---
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
(original)
+++
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/AuthenticationHandlerHolder.java
Tue Jan 10 20:34:41 2012
@@ -148,4 +148,9 @@ final class AuthenticationHandlerHolder
final String requestLogin = AuthUtil.getAttributeOrParameter(request,
REQUEST_LOGIN_PARAMETER, null);
return requestLogin == null || authType.equals(requestLogin);
}
+
+ @Override
+ protected AuthenticationHandler getHandler() {
+ return handler;
+ }
}
\ No newline at end of file
Modified:
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java?rev=1229715&r1=1229714&r2=1229715&view=diff
==============================================================================
---
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
(original)
+++
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
Tue Jan 10 20:34:41 2012
@@ -20,6 +20,7 @@ package org.apache.sling.auth.core.impl;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
@@ -41,12 +42,15 @@ import org.apache.felix.scr.annotations.
import org.apache.felix.scr.annotations.PropertyOption;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.auth.Authenticator;
import org.apache.sling.api.auth.NoAuthenticationHandlerException;
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.auth.core.AuthConstants;
import org.apache.sling.auth.core.AuthUtil;
import org.apache.sling.auth.core.AuthenticationSupport;
import
org.apache.sling.auth.core.impl.engine.EngineAuthenticationHandlerHolder;
@@ -64,6 +68,8 @@ import org.osgi.framework.InvalidSyntaxE
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
import org.osgi.service.http.HttpContext;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
@@ -190,6 +196,14 @@ public class SlingAuthenticator implemen
*/
private static final String ATTR_RESOURCE_RESOLVER_SKIP_CLOSE =
"org.apache.sling.api.resource.ResourceResolver.skip.close";
+ /**
+ * The name of the {@link AuthenticationInfo} property providing the
+ * handler which extracted the credentials. May be an instance of either
+ * {@link org.apache.sling.auth.core.spi.AuthenticationHandler} or
+ * {@link org.apache.sling.auth.sling.engine.auth.AuthenticationHandler}
+ */
+ private static final String AUTH_INFO_PROP_AUTHENTICATION_HANDLER =
"$$sling.auth.AuthenticationHandler$$";
+
@Reference
private ResourceResolverFactory resourceResolverFactory;
@@ -260,6 +274,12 @@ public class SlingAuthenticator implemen
*/
private ServiceTracker authInfoPostProcessorTracker;
+ /**
+ * The event admin service.
+ */
+ @Reference(policy=ReferencePolicy.DYNAMIC)
+ private EventAdmin eventAdmin;
+
// ---------- SCR integration
@SuppressWarnings("unused")
@@ -562,6 +582,8 @@ public class SlingAuthenticator implemen
throw new IllegalStateException("Response already committed");
}
+ String userId = request.getRemoteUser();
+
final String path = getHandlerSelectionPath(request);
final List<AbstractAuthenticationHandlerHolder>[] holderListArray =
this.authHandlerCache.findApplicableHolder(request);
for (int m = 0; m < holderListArray.length; m++) {
@@ -589,6 +611,8 @@ public class SlingAuthenticator implemen
httpBasicHandler.dropCredentials(request, response);
}
+ postLogoutEvent(userId);
+
redirectAfterLogout(request, response);
}
@@ -682,6 +706,9 @@ public class SlingAuthenticator implemen
request, response);
if (authInfo != null) {
+ authInfo.put(AUTH_INFO_PROP_AUTHENTICATION_HANDLER,
+ holder.getHandler());
+
// add the feedback handler to the info (may be
null)
authInfo.put(AUTH_INFO_PROP_FEEDBACK_HANDLER,
holder.getFeedbackHandler());
@@ -789,6 +816,9 @@ public class SlingAuthenticator implemen
// no redirect desired, so continue processing by first setting
// the request attributes and then returning true
setAttributes(resolver, authInfo.getAuthType(), request);
+
+ postLoginEvent(authInfo);
+
return true;
} catch (LoginException re) {
@@ -1339,6 +1369,27 @@ public class SlingAuthenticator implemen
}
}
+ private void postLoginEvent(final AuthenticationInfo authInfo) {
+ final Dictionary<String, Object> properties = new Hashtable<String,
Object>();
+ properties.put(SlingConstants.PROPERTY_USERID, authInfo.getUser());
+ properties.put(AuthConstants.PROPERTY_AUTH_HANDLER_CLASS,
authInfo.get(AUTH_INFO_PROP_AUTHENTICATION_HANDLER).getClass().getName());
+
+ EventAdmin localEA = this.eventAdmin;
+ if (localEA != null) {
+ localEA.postEvent(new Event(AuthConstants.TOPIC_LOGIN,
properties));
+ }
+ }
+
+ private void postLogoutEvent(final String userId) {
+ final Dictionary<String, Object> properties = new Hashtable<String,
Object>();
+ properties.put(SlingConstants.PROPERTY_USERID, userId);
+
+ EventAdmin localEA = this.eventAdmin;
+ if (localEA != null) {
+ localEA.postEvent(new Event(AuthConstants.TOPIC_LOGOUT,
properties));
+ }
+ }
+
/**
* Ensures the cookie value is properly quoted for transmission to the
* client.
Modified:
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/engine/EngineAuthenticationHandlerHolder.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/engine/EngineAuthenticationHandlerHolder.java?rev=1229715&r1=1229714&r2=1229715&view=diff
==============================================================================
---
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/engine/EngineAuthenticationHandlerHolder.java
(original)
+++
sling/trunk/bundles/auth/core/src/main/java/org/apache/sling/auth/core/impl/engine/EngineAuthenticationHandlerHolder.java
Tue Jan 10 20:34:41 2012
@@ -105,6 +105,11 @@ public final class EngineAuthenticationH
}
@Override
+ protected AuthenticationHandler getHandler() {
+ return handler;
+ }
+
+ @Override
public String toString() {
return handler.toString() + " (Legacy API Handler)";
}