]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/auth/CmsAuthUtils.java
Move CMS session management to CMS Context.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CmsAuthUtils.java
index 4c09650d4b0546bdc5c6220a23f23de99903cf95..46adcf8a516db80a509476947cbcb4c9ee5cd364 100644 (file)
@@ -10,16 +10,17 @@ import javax.naming.InvalidNameException;
 import javax.naming.ldap.LdapName;
 import javax.security.auth.Subject;
 import javax.security.auth.x500.X500Principal;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
 
-import org.argeo.api.NodeConstants;
-import org.argeo.api.security.AnonymousPrincipal;
-import org.argeo.api.security.DataAdminPrincipal;
-import org.argeo.api.security.NodeSecurityUtils;
+import org.argeo.api.cms.CmsSession;
+import org.argeo.api.cms.CmsSessionId;
+import org.argeo.api.cms.DataAdminPrincipal;
+import org.argeo.api.cms.AnonymousPrincipal;
+import org.argeo.api.cms.CmsConstants;
 import org.argeo.cms.internal.auth.CmsSessionImpl;
 import org.argeo.cms.internal.auth.ImpliedByPrincipal;
 import org.argeo.cms.internal.http.WebCmsSessionImpl;
+import org.argeo.cms.internal.runtime.CmsContextImpl;
+import org.argeo.cms.security.NodeSecurityUtils;
 import org.argeo.osgi.useradmin.AuthenticatingUser;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
@@ -122,21 +123,21 @@ class CmsAuthUtils {
        }
 
        @SuppressWarnings("unused")
-       synchronized static void registerSessionAuthorization(HttpServletRequest request, Subject subject,
+       synchronized static void registerSessionAuthorization(RemoteAuthRequest 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(false);
+                       RemoteAuthSession httpSession = request.getSession();
                        assert httpSession != null;
                        String httpSessId = httpSession.getId();
                        boolean anonymous = authorization.getName() == null;
-                       String remoteUser = !anonymous ? authorization.getName() : NodeConstants.ROLE_ANONYMOUS;
+                       String remoteUser = !anonymous ? authorization.getName() : CmsConstants.ROLE_ANONYMOUS;
                        request.setAttribute(HttpContext.REMOTE_USER, remoteUser);
                        request.setAttribute(HttpContext.AUTHORIZATION, authorization);
 
                        CmsSessionImpl cmsSession;
-                       CmsSessionImpl currentLocalSession = CmsSessionImpl.getByLocalId(httpSessId);
+                       CmsSessionImpl currentLocalSession = CmsContextImpl.getCmsContext().getCmsSessionByLocalId(httpSessId);
                        if (currentLocalSession != null) {
                                boolean currentLocalSessionAnonymous = currentLocalSession.getAuthorization().getName() == null;
                                if (!anonymous) {
@@ -165,6 +166,7 @@ class CmsAuthUtils {
                        } else {
                                // new CMS session
                                cmsSession = new WebCmsSessionImpl(subject, authorization, locale, request);
+                               CmsContextImpl.getCmsContext().registerCmsSession(cmsSession);
                        }
 
                        if (cmsSession == null)// should be dead code (cf. SuppressWarning of the method)
@@ -181,33 +183,34 @@ class CmsAuthUtils {
                        }
                } else {
                        CmsSessionImpl cmsSession = new CmsSessionImpl(subject, authorization, locale, "desktop");
+                       CmsContextImpl.getCmsContext().registerCmsSession(cmsSession);
                        CmsSessionId nodeSessionId = new CmsSessionId(cmsSession.getUuid());
                        subject.getPrivateCredentials().add(nodeSessionId);
                }
        }
 
-       public static CmsSession cmsSessionFromHttpSession(BundleContext bc, String httpSessionId) {
-               Authorization authorization = null;
-               Collection<ServiceReference<CmsSession>> sr;
-               try {
-                       sr = bc.getServiceReferences(CmsSession.class,
-                                       "(" + CmsSession.SESSION_LOCAL_ID + "=" + httpSessionId + ")");
-               } catch (InvalidSyntaxException e) {
-                       throw new IllegalArgumentException("Cannot get CMS session for id " + httpSessionId, e);
-               }
-               CmsSession cmsSession;
-               if (sr.size() == 1) {
-                       cmsSession = bc.getService(sr.iterator().next());
-//                     locale = cmsSession.getLocale();
-                       authorization = cmsSession.getAuthorization();
-                       if (authorization.getName() == null)
-                               return null;// anonymous is not sufficient
-               } else if (sr.size() == 0)
-                       return null;
-               else
-                       throw new IllegalStateException(sr.size() + ">1 web sessions detected for http session " + httpSessionId);
-               return cmsSession;
-       }
+//     public static CmsSessionImpl cmsSessionFromHttpSession(BundleContext bc, String httpSessionId) {
+//             Authorization authorization = null;
+//             Collection<ServiceReference<CmsSession>> sr;
+//             try {
+//                     sr = bc.getServiceReferences(CmsSession.class,
+//                                     "(" + CmsSession.SESSION_LOCAL_ID + "=" + httpSessionId + ")");
+//             } catch (InvalidSyntaxException e) {
+//                     throw new IllegalArgumentException("Cannot get CMS session for id " + httpSessionId, e);
+//             }
+//             CmsSessionImpl cmsSession;
+//             if (sr.size() == 1) {
+//                     cmsSession = (CmsSessionImpl) bc.getService(sr.iterator().next());
+////                   locale = cmsSession.getLocale();
+//                     authorization = cmsSession.getAuthorization();
+//                     if (authorization.getName() == null)
+//                             return null;// anonymous is not sufficient
+//             } else if (sr.size() == 0)
+//                     return null;
+//             else
+//                     throw new IllegalStateException(sr.size() + ">1 web sessions detected for http session " + httpSessionId);
+//             return cmsSession;
+//     }
 
        public static <T extends Principal> T getSinglePrincipal(Subject subject, Class<T> clss) {
                Set<T> principals = subject.getPrincipals(clss);