]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/OsJcrAuthenticationProvider.java
Big cleanup of the security layers
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / jcr / OsJcrAuthenticationProvider.java
index 9abac5972a7f584fc1aaca81326d49877e4c41af..e6f90b165c08cc9efc6d498d88d16f68d5bf5e73 100644 (file)
 package org.argeo.security.jcr;
 
-import java.util.Map;
-import java.util.concurrent.Executor;
-
 import javax.jcr.Node;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.version.VersionManager;
 
 import org.argeo.ArgeoException;
+import org.argeo.jcr.ArgeoNames;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.security.OsAuthenticationToken;
-import org.argeo.security.SystemExecutionService;
 import org.argeo.security.core.OsAuthenticationProvider;
 import org.springframework.security.Authentication;
 import org.springframework.security.AuthenticationException;
-import org.springframework.security.userdetails.UserDetails;
 
+/** Relies on OS to authenticate and additionaly setup JCR */
 public class OsJcrAuthenticationProvider extends OsAuthenticationProvider {
-       private Executor systemExecutor;
-       private String homeBasePath = "/home";
        private Repository repository;
-       private String workspace = null;
+       private String securityWorkspace = "security";
+       private Session securitySession;
+
+       public void init() {
+               try {
+                       securitySession = repository.login(securityWorkspace);
+               } catch (RepositoryException e) {
+                       throw new ArgeoException("Cannot initialize", e);
+               }
+       }
 
-       private Long timeout = 5 * 60 * 1000l;
+       public void destroy() {
+               JcrUtils.logoutQuietly(securitySession);
+       }
 
        public Authentication authenticate(Authentication authentication)
                        throws AuthenticationException {
                final OsAuthenticationToken authen = (OsAuthenticationToken) super
                                .authenticate(authentication);
-               final Repository repository = getRepositoryBlocking();
-               systemExecutor.execute(new Runnable() {
-                       public void run() {
-                               Session session = null;
-                               try {
-                                       session = repository.login(workspace);
-                                       // WARNING: at this stage we assume that the java properties
-                                       // will have the same value
-                                       String userName = System.getProperty("user.name");
-                                       Node userHome = JcrUtils.getUserHome(session, userName);
-                                       if (userHome == null)
-                                               userHome = JcrUtils.createUserHome(session,
-                                                               homeBasePath, userName);
-                                       // authen.setDetails(getUserDetails(userHome, authen));
-                               } catch (RepositoryException e) {
-                                       JcrUtils.discardQuietly(session);
-                                       throw new ArgeoException(
-                                                       "Unexpected exception when synchronizing OS and JCR security ",
-                                                       e);
-                               } finally {
-                                       JcrUtils.logoutQuietly(session);
-                               }
-                       }
-               });
-               return authen;
-       }
-
-       /** Builds user details based on the authentication and the user home. */
-       protected UserDetails getUserDetails(Node userHome, Authentication authen) {
                try {
-                       // TODO: loads enabled, locked, etc. from the home node.
-                       return new JcrUserDetails(userHome.getPath(), authen.getPrincipal()
-                                       .toString(), authen.getCredentials().toString(),
-                                       isEnabled(userHome), true, true, true,
-                                       authen.getAuthorities());
-               } catch (Exception e) {
-                       throw new ArgeoException("Cannot get user details for " + userHome,
+                       // WARNING: at this stage we assume that the java properties
+                       // will have the same value
+                       String username = System.getProperty("user.name");
+                       Node userHome = JcrUtils.createUserHomeIfNeeded(securitySession,
+                                       username);
+                       Node userProfile = userHome.hasNode(ArgeoNames.ARGEO_PROFILE) ? userHome
+                                       .getNode(ArgeoNames.ARGEO_PROFILE) : JcrUtils
+                                       .createUserProfile(securitySession, username);
+                       if (securitySession.hasPendingChanges())
+                               securitySession.save();
+                       VersionManager versionManager = securitySession.getWorkspace()
+                                       .getVersionManager();
+                       if (versionManager.isCheckedOut(userProfile.getPath()))
+                               versionManager.checkin(userProfile.getPath());
+
+                       JcrUserDetails.checkAccountStatus(userProfile);
+                       // user details
+                       JcrUserDetails userDetails = new JcrUserDetails(userProfile, authen
+                                       .getCredentials().toString(), getBaseAuthorities());
+                       authen.setDetails(userDetails);
+               } catch (RepositoryException e) {
+                       JcrUtils.discardQuietly(securitySession);
+                       throw new ArgeoException(
+                                       "Unexpected exception when synchronizing OS and JCR security ",
                                        e);
+               } finally {
+                       JcrUtils.logoutQuietly(securitySession);
                }
+               return authen;
        }
 
-       protected Boolean isEnabled(Node userHome) {
-               return true;
-       }
-
-       protected Repository getRepositoryBlocking() {
-               long begin = System.currentTimeMillis();
-               while (repository == null) {
-                       synchronized (this) {
-                               try {
-                                       wait(500);
-                               } catch (InterruptedException e) {
-                                       // silent
-                               }
-                       }
-                       if (System.currentTimeMillis() - begin > timeout)
-                               throw new ArgeoException("No repository registered after "
-                                               + timeout + " ms");
-               }
-               return repository;
+       public void setSecurityWorkspace(String securityWorkspace) {
+               this.securityWorkspace = securityWorkspace;
        }
 
-       public synchronized void register(Repository repository,
-                       Map<String, String> parameters) {
+       public void setRepository(Repository repository) {
                this.repository = repository;
-               notifyAll();
-       }
-
-       public synchronized void unregister(Repository repository,
-                       Map<String, String> parameters) {
-               this.repository = null;
-               notifyAll();
-       }
-
-       public void register(SystemExecutionService systemExecutor,
-                       Map<String, String> parameters) {
-               this.systemExecutor = systemExecutor;
-       }
-
-       public void unregister(SystemExecutionService systemExecutor,
-                       Map<String, String> parameters) {
-               this.systemExecutor = null;
-       }
-
-       public void setHomeBasePath(String homeBasePath) {
-               this.homeBasePath = homeBasePath;
        }
-
-       public void setWorkspace(String workspace) {
-               this.workspace = workspace;
-       }
-
 }