Workbench anonymous entry point
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 25 Feb 2015 21:02:27 +0000 (21:02 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 25 Feb 2015 21:02:27 +0000 (21:02 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@7964 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/auth/ArgeoLoginContext.java
org.argeo.cms/src/org/argeo/cms/internal/auth/AbstractLoginModule.java
org.argeo.cms/src/org/argeo/cms/internal/auth/AnonymousLoginModule.java
org.argeo.security.ui.rap/plugin.xml
org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/AnonymousEntryPoint.java
org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/SecureEntryPoint.java
org.argeo.security.ui/plugin.xml

index 3a536672a02b437a0bafa200f3736e3febe21adf..1a06486550130c52a015edacae54cfa9753ec603 100644 (file)
@@ -28,6 +28,15 @@ public class ArgeoLoginContext extends LoginContext {
                currentContextClassLoader.remove();
        }
 
+       public ArgeoLoginContext(String name, Subject subject)
+                       throws LoginException {
+               super(setContextClassLoaderForName(name), subject);
+               // reset current context classloader
+               Thread.currentThread().setContextClassLoader(
+                               currentContextClassLoader.get());
+               currentContextClassLoader.remove();
+       }
+
        /**
         * Set the context classloader
         * 
index 77f0d165aaede68f7b3c852654882cdd244f5572..427ec83188f2db7d70b17620dbe109513065a114 100644 (file)
@@ -80,8 +80,8 @@ public abstract class AbstractLoginModule implements LoginModule {
                                return true;
                        }
 
-                       if (callbackHandler == null)
-                               throw new LoginException("No callback handler available");
+                       // if (callbackHandler == null)
+                       // throw new LoginException("No callback handler available");
 
                        authentication = processLogin(callbackHandler);
                        if (authentication != null) {
index 372f27e60bc0205ff54d77c8cc729b4430e2bf4d..855524961c86e390fe14b742e90c53ce95362c86 100644 (file)
@@ -43,19 +43,22 @@ public class AnonymousLoginModule extends AbstractLoginModule {
                        InterruptedException {
                Locale selectedLocale = null;
                // multi locale
-               if (availableLocales != null && !availableLocales.trim().equals("")) {
-                       LocaleCallback localeCallback = new LocaleCallback(availableLocales);
-                       callbackHandler.handle(new Callback[] { localeCallback });
-                       selectedLocale = localeCallback.getSelectedLocale();
-               } else {
-                       callbackHandler.handle(new Callback[] {});
-               }
+               if (callbackHandler != null)
+                       if (availableLocales != null && !availableLocales.trim().equals("")) {
+                               LocaleCallback localeCallback = new LocaleCallback(
+                                               availableLocales);
+                               callbackHandler.handle(new Callback[] { localeCallback });
+                               selectedLocale = localeCallback.getSelectedLocale();
+                       } else {
+                               callbackHandler.handle(new Callback[] {});
+                       }
 
                List<GrantedAuthorityPrincipal> authorities = Collections
                                .singletonList(new GrantedAuthorityPrincipal(
                                                KernelHeader.ROLE_ANONYMOUS));
                AnonymousAuthenticationToken anonymousToken = new AnonymousAuthenticationToken(
-                               Activator.getSystemKey(), null, authorities);
+                               Activator.getSystemKey(), KernelHeader.USERNAME_ANONYMOUS,
+                               authorities);
 
                Authentication auth = getAuthenticationManager().authenticate(
                                anonymousToken);
index f42eb1a5c87a18905fceda0c167be2c0f60a215e..84df522b00c8156f75514bad6f51b343ee5cd575 100644 (file)
@@ -9,29 +9,12 @@
             path="/node"
             brandingId="org.argeo.security.ui.rap.defaultBranding">
       </entrypoint>
-      <entrypoint
-            id="org.argeo.security.ui.rap.secureEntryPoint"
-            class="org.argeo.security.ui.rap.SecureEntryPoint"
-            path="/secureWebUi"
-            brandingId="org.argeo.security.ui.rap.defaultBranding">
-      </entrypoint>
       <entrypoint
             id="org.argeo.security.ui.rap.anonymousEntryPoint"
             class="org.argeo.security.ui.rap.AnonymousEntryPoint"
-            path="/publicWebUi"
+            path="/public"
             brandingId="org.argeo.security.ui.rap.defaultBranding">
       </entrypoint>
-    <!--   <entrypoint
-            id="org.argeo.security.ui.rap.logoutEntryPoint"
-            class="org.argeo.security.ui.rap.LogoutEntryPoint"
-            path="/logout"
-            brandingId="org.argeo.security.ui.rap.logoutBranding">
-      </entrypoint> -->
-      <entrypoint
-            id="org.argeo.security.ui.rap.nullEntryPoint"
-            class="org.argeo.security.ui.rap.NullEntryPoint"
-            path="/nullEP">
-      </entrypoint>
    </extension>
 
        <!-- COMMANDS --> 
index ac0007acfa9af3bac041d356c40fefd07d01b901..99536faa11827574aa64ba2bd791a8fec786ac16 100644 (file)
  */
 package org.argeo.security.ui.rap;
 
+import java.security.PrivilegedAction;
+
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.ArgeoException;
+import org.argeo.cms.KernelHeader;
+import org.argeo.cms.auth.ArgeoLoginContext;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.application.EntryPoint;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
 
 /**
  * RAP entry point which authenticates the subject as anonymous, for public
  * unauthenticated access.
  */
 public class AnonymousEntryPoint implements EntryPoint {
-       // private final static Log log =
-       // LogFactory.getLog(AnonymousEntryPoint.class);
+       private final static Log log = LogFactory.getLog(AnonymousEntryPoint.class);
 
        /**
         * How many seconds to wait before invalidating the session if the user has
         * not yet logged in.
         */
-       private Integer loginTimeout = 1 * 60;
+       private Integer sessionTimeout = 5 * 60;
 
        @Override
        public int createUI() {
-               // Short login timeout so that the modal dialog login doesn't hang
-               // around too long
-               RWT.getRequest().getSession().setMaxInactiveInterval(loginTimeout);
+               RWT.getRequest().getSession().setMaxInactiveInterval(sessionTimeout);
 
                // if (log.isDebugEnabled())
                // log.debug("Anonymous THREAD=" + Thread.currentThread().getId()
                // + ", sessionStore=" + RWT.getSessionStore().getId());
 
-               // create display
-               // final Display display = PlatformUI.createDisplay();
+               final Display display = PlatformUI.createDisplay();
+               Subject subject = new Subject();
+
+               final LoginContext loginContext;
+               try {
+                       loginContext = new ArgeoLoginContext(
+                                       KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject);
+                       loginContext.login();
+               } catch (LoginException e1) {
+                       throw new ArgeoException("Cannot initialize login context", e1);
+               }
+
+               // identify after successful login
+               if (log.isDebugEnabled())
+                       log.debug("Authenticated " + subject);
+               final String username = subject.getPrincipals().iterator().next()
+                               .getName();
+
+               // Logout callback when the display is disposed
+               display.disposeExec(new Runnable() {
+                       public void run() {
+                               log.debug("Display disposed");
+                               logout(loginContext, username);
+                       }
+               });
 
-               // log in
-               // final ILoginContext loginContext = SecureRapActivator
-               // .createLoginContext(SecureRapActivator.CONTEXT_SPRING_ANONYMOUS);
-               // Subject subject = null;
-               // try {
-               // loginContext.login();
-               // subject = loginContext.getSubject();
-               // } catch (LoginException e) {
-               // throw new ArgeoException(
-               // "Unexpected exception during authentication", e);
-               // }
-               //
-               // // identify after successful login
-               // if (log.isDebugEnabled())
-               // log.debug("Authenticated " + subject);
-               // final String username = subject.getPrincipals().iterator().next()
-               // .getName();
-               //
-               // // Once the user is logged in, she can have a longer session timeout
-               // RWT.getRequest().getSession().setMaxInactiveInterval(sessionTimeout);
                //
-               // // Logout callback when the display is disposed
-               // display.disposeExec(new Runnable() {
-               // public void run() {
-               // log.debug("Display disposed");
-               // logout(loginContext, username);
-               // }
-               // });
+               // RUN THE WORKBENCH
                //
-               // //
-               // // RUN THE WORKBENCH
-               // //
-               // Integer returnCode = null;
-               // try {
-               // returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
-               // public Integer run() {
-               // RapWorkbenchAdvisor workbenchAdvisor = new RapWorkbenchAdvisor(
-               // null);
-               // int result = PlatformUI.createAndRunWorkbench(display,
-               // workbenchAdvisor);
-               // return new Integer(result);
-               // }
-               // });
-               // logout(loginContext, username);
-               // } finally {
-               // display.dispose();
-               // }
+               Integer returnCode = null;
+               try {
+                       returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
+                               public Integer run() {
+                                       RapWorkbenchAdvisor workbenchAdvisor = new RapWorkbenchAdvisor(
+                                                       null);
+                                       int result = PlatformUI.createAndRunWorkbench(display,
+                                                       workbenchAdvisor);
+                                       return new Integer(result);
+                               }
+                       });
+                       logout(loginContext, username);
+                       if (log.isTraceEnabled())
+                               log.trace("Return code " + returnCode);
+               } finally {
+                       display.dispose();
+               }
                return 1;
        }
 
-       // private void logout(ILoginContext secureContext, String username) {
-       // try {
-       // secureContext.logout();
-       // log.info("Logged out " + (username != null ? username : "")
-       // + " (THREAD=" + Thread.currentThread().getId() + ")");
-       // } catch (LoginException e) {
-       // log.error("Erorr when logging out", e);
-       // }
-       // }
+       private void logout(LoginContext loginContext, String username) {
+               try {
+                       loginContext.logout();
+                       log.info("Logged out " + (username != null ? username : "")
+                                       + " (THREAD=" + Thread.currentThread().getId() + ")");
+               } catch (LoginException e) {
+                       log.error("Erorr when logging out", e);
+               }
+       }
 }
index 7f92ab7cb913965a54fff22fb17647164824f111..67e76ceae92c3be835fe54743da102b0ce605064 100644 (file)
@@ -86,18 +86,9 @@ public class SecureEntryPoint implements EntryPoint {
                        SecurityContextHolder
                                        .setContext((SecurityContext) contextFromSessionObject);
 
-               // if (log.isDebugEnabled())
-               // log.debug("THREAD=" + Thread.currentThread().getId()
-               // + ", sessionStore=" + RWT.getSessionStore().getId()
-               // + ", remote user=" + httpRequest.getRemoteUser());
-
-               // create display
                final Display display = PlatformUI.createDisplay();
                Subject subject = new Subject();
 
-               // log in
-               // Thread.currentThread().setContextClassLoader(
-               // getClass().getClassLoader());
                final LoginContext loginContext;
                try {
                        CallbackHandler callbackHandler = new DefaultLoginDialog(
@@ -111,10 +102,6 @@ public class SecureEntryPoint implements EntryPoint {
                tryLogin: while (subject.getPrincipals(Authentication.class).size() == 0) {
                        try {
                                loginContext.login();
-                               // if () {
-                               // throw new ArgeoException("Login failed");
-                               // }
-
                                if (subject.getPrincipals(Authentication.class).size() == 0)
                                        throw new ArgeoException("Login succeeded but no auth");// fatal
 
@@ -122,12 +109,13 @@ public class SecureEntryPoint implements EntryPoint {
                                if (httpSession.getAttribute(SPRING_SECURITY_CONTEXT_KEY) == null)
                                        httpSession.setAttribute(SPRING_SECURITY_CONTEXT_KEY,
                                                        SecurityContextHolder.getContext());
+
                                // add thread locale to RWT session
                                if (log.isTraceEnabled())
                                        log.trace("Locale " + LocaleUtils.threadLocale.get());
                                RWT.setLocale(LocaleUtils.threadLocale.get());
 
-                               // Once the user is logged in, longer session timeout
+                               // once the user is logged in, longer session timeout
                                RWT.getRequest().getSession()
                                                .setMaxInactiveInterval(sessionTimeout);
 
index 7d18541af3589eeaee7f194791e3ce4316997c22..a1e1e9bdf18f7903bd1b0c258d40da25a7555d2b 100644 (file)
    </extension>
   <extension
            point="org.eclipse.ui.activities">
+        <activity
+              description="Authenticated users"
+              id="org.argeo.security.ui.userActivity"
+              name="User">
+                 <enabledWhen>
+                   <with variable="roles">
+                     <iterate ifEmpty="false" operator="or">
+                       <equals value="ROLE_USER" />
+                     </iterate>
+                   </with>
+                 </enabledWhen>
+        </activity>
         <activity
               description="Admins"
               id="org.argeo.security.ui.adminActivity"
               isEqualityPattern="true"
               pattern="org.argeo.security.ui/org.argeo.security.ui.adminLogView">
         </activityPatternBinding>
+        <activityPatternBinding
+              activityId="org.argeo.security.ui.userActivity"
+              isEqualityPattern="true"
+              pattern="org.argeo.security.ui/org.argeo.security.ui.userHomePerspective">
+        </activityPatternBinding>
+        <activityPatternBinding
+              activityId="org.argeo.security.ui.userActivity"
+              isEqualityPattern="true"
+              pattern="org.argeo.security.ui/org.argeo.security.ui.userProfile">
+        </activityPatternBinding>
      </extension>
  </plugin>