Make user/anonymous semantics more consistent with Authorization
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CmsAuthUtils.java
index 65ccbd6aba15c7c2cfa71fcb7f39d7107eb5d7a0..661cc6905fb9a33753348cfa1e81d5b47a508e47 100644 (file)
@@ -19,29 +19,27 @@ import org.argeo.cms.CmsException;
 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.kernel.Activator;
 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 HEADER_AUTHORIZATION = "Authorization";
-       final static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
+       final static String SHARED_STATE_CERTIFICATE_CHAIN = "org.argeo.cms.auth.certificateChain";
 
        static void addAuthorization(Subject subject, Authorization authorization, Locale locale,
                        HttpServletRequest request) {
@@ -52,6 +50,10 @@ class CmsAuthUtils {
                // required for display name:
                subject.getPrivateCredentials().add(authorization);
 
+               if (Activator.isSingleUser()) {
+                       subject.getPrincipals().add(new DataAdminPrincipal());
+               }
+
                Set<Principal> principals = subject.getPrincipals();
                try {
                        String authName = authorization.getName();
@@ -63,13 +65,13 @@ class CmsAuthUtils {
                                name = NodeSecurityUtils.ROLE_ANONYMOUS_NAME;
                                userPrincipal = new AnonymousPrincipal();
                                principals.add(userPrincipal);
-                               // principals.add(new AnonymousPrincipal());
                        } else {
                                name = new LdapName(authName);
                                NodeSecurityUtils.checkUserName(name);
                                userPrincipal = new X500Principal(name.toString());
                                principals.add(userPrincipal);
-                               principals.add(new ImpliedByPrincipal(NodeSecurityUtils.ROLE_USER_NAME, userPrincipal));
+                               // principals.add(new ImpliedByPrincipal(NodeSecurityUtils.ROLE_USER_NAME,
+                               // userPrincipal));
                        }
 
                        // Add roles provided by authorization
@@ -77,6 +79,8 @@ class CmsAuthUtils {
                                LdapName roleName = new LdapName(role);
                                if (roleName.equals(name)) {
                                        // skip
+                               } else if (roleName.equals(NodeSecurityUtils.ROLE_ANONYMOUS_NAME)) {
+                                       // skip
                                } else {
                                        NodeSecurityUtils.checkImpliedPrincipalName(roleName);
                                        principals.add(new ImpliedByPrincipal(roleName.toString(), userPrincipal));