Support SSL client authentication
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CmsAuthUtils.java
index 4d59c5263a76afe974b600244c8ad6b848c98243..4762eb96c765578396650ba1044c0c92b9818e20 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.cms.auth;
 
 import java.security.Principal;
+import java.util.Locale;
 import java.util.Set;
 import java.util.UUID;
 
@@ -22,27 +23,25 @@ import org.argeo.node.NodeConstants;
 import org.argeo.node.security.AnonymousPrincipal;
 import org.argeo.node.security.DataAdminPrincipal;
 import org.argeo.node.security.NodeSecurityUtils;
+import org.argeo.osgi.useradmin.AuthenticatingUser;
 import org.osgi.service.http.HttpContext;
 import org.osgi.service.useradmin.Authorization;
 
 class CmsAuthUtils {
-       /** Shared HTTP request */
-       final static String SHARED_STATE_HTTP_REQUEST = "org.argeo.cms.auth.http.request";
-       /** From org.osgi.service.http.HttpContext */
-       // final static String SHARED_STATE_AUTHORIZATION =
-       // "org.osgi.service.useradmin.authorization";
-       /** From com.sun.security.auth.module.*LoginModule */
-       final static String SHARED_STATE_NAME = "javax.security.auth.login.name";
-       /** From com.sun.security.auth.module.*LoginModule */
-       final static String SHARED_STATE_PWD = "javax.security.auth.login.password";
+       // Standard
+       final static String SHARED_STATE_NAME = AuthenticatingUser.SHARED_STATE_NAME;
+       final static String SHARED_STATE_PWD = AuthenticatingUser.SHARED_STATE_PWD;
+       final static String HEADER_AUTHORIZATION = "Authorization";
+       final static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
 
+       // Argeo specific
+       final static String SHARED_STATE_HTTP_REQUEST = "org.argeo.cms.auth.http.request";
        final static String SHARED_STATE_SPNEGO_TOKEN = "org.argeo.cms.auth.spnegoToken";
        final static String SHARED_STATE_SPNEGO_OUT_TOKEN = "org.argeo.cms.auth.spnegoOutToken";
+       final static String SHARED_STATE_CERTIFICATE_CHAIN = "org.argeo.cms.auth.certificateChain";
 
-       final static String HEADER_AUTHORIZATION = "Authorization";
-       final static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
-
-       static void addAuthorization(Subject subject, Authorization authorization, HttpServletRequest request) {
+       static void addAuthorization(Subject subject, Authorization authorization, Locale locale,
+                       HttpServletRequest request) {
                assert subject != null;
                checkSubjectEmpty(subject);
                assert authorization != null;
@@ -87,7 +86,7 @@ class CmsAuthUtils {
                        throw new CmsException("Cannot commit", e);
                }
 
-               registerSessionAuthorization(request, subject, authorization);
+               registerSessionAuthorization(request, subject, authorization, locale);
        }
 
        private static void checkSubjectEmpty(Subject subject) {
@@ -115,22 +114,24 @@ class CmsAuthUtils {
                // subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
        }
 
-       private static void registerSessionAuthorization(HttpServletRequest request, Subject subject,
-                       Authorization authorization) {
+       private synchronized static void registerSessionAuthorization(HttpServletRequest request, Subject subject,
+                       Authorization authorization, Locale locale) {
+               // synchronized in order to avoid multiple registrations
+               // TODO move it to a service in order to avoid static synchronization
                if (request != null) {
-                       HttpSession httpSession = request.getSession();
+                       HttpSession httpSession = request.getSession(false);
+                       assert httpSession != null;
                        String httpSessId = httpSession.getId();
                        String remoteUser = authorization.getName() != null ? authorization.getName()
                                        : NodeConstants.ROLE_ANONYMOUS;
                        request.setAttribute(HttpContext.REMOTE_USER, remoteUser);
                        request.setAttribute(HttpContext.AUTHORIZATION, authorization);
 
-                       CmsSession cmsSession = CmsSessionImpl.getByLocalId(httpSessId);
+                       CmsSessionImpl cmsSession = CmsSessionImpl.getByLocalId(httpSessId);
                        if (cmsSession != null) {
                                if (authorization.getName() != null) {
                                        if (cmsSession.getAuthorization().getName() == null) {
-                                               // FIXME make it more generic
-                                               ((WebCmsSessionImpl) cmsSession).cleanUp();
+                                               cmsSession.close();
                                                cmsSession = null;
                                        } else if (!authorization.getName().equals(cmsSession.getAuthorization().getName())) {
                                                throw new CmsException("Inconsistent user " + authorization.getName()
@@ -138,15 +139,15 @@ class CmsAuthUtils {
                                        }
                                } else {// anonymous
                                        if (cmsSession.getAuthorization().getName() != null) {
-                                               // FIXME make it more generic
-                                               ((WebCmsSessionImpl) cmsSession).cleanUp();
+                                               cmsSession.close();
+                                               // TODO rather throw an exception ? log a warning ?
                                                cmsSession = null;
                                        }
                                }
                        }
 
                        if (cmsSession == null)
-                               cmsSession = new WebCmsSessionImpl(subject, authorization, httpSessId);
+                               cmsSession = new WebCmsSessionImpl(subject, authorization, locale, request);
                        // request.setAttribute(CmsSession.class.getName(), cmsSession);
                        CmsSessionId nodeSessionId = new CmsSessionId(cmsSession.getUuid());
                        if (subject.getPrivateCredentials(CmsSessionId.class).size() == 0)