]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/OsJcrAuthenticationProvider.java
Update license headers
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / jcr / OsJcrAuthenticationProvider.java
index 192d2fdb2f801ee51a0beed4a77079c3d270edf4..4f3e6a18e5a5ff0e3c4bf058c3664d915b79cbc2 100644 (file)
@@ -1,12 +1,25 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 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.RepositoryFactory;
 import javax.jcr.Session;
+import javax.jcr.security.Privilege;
 
 import org.argeo.ArgeoException;
 import org.argeo.jcr.JcrUtils;
@@ -14,83 +27,70 @@ import org.argeo.security.OsAuthenticationToken;
 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 additionally setup JCR */
 public class OsJcrAuthenticationProvider extends OsAuthenticationProvider {
-       private RepositoryFactory repositoryFactory;
-       private Executor systemExecutor;
-       private String homeBasePath = "/home";
-       private String repositoryAlias = "node";
-       private String workspace = null;
+       private Repository repository;
+       private String securityWorkspace = "security";
+       private Session securitySession;
+       private Session nodeSession;
 
-       public Authentication authenticate(Authentication authentication)
-                       throws AuthenticationException {
-               final OsAuthenticationToken authen = (OsAuthenticationToken) super
-                               .authenticate(authentication);
-               systemExecutor.execute(new Runnable() {
-                       public void run() {
-                               try {
-                                       Session session = JcrUtils.getRepositoryByAlias(
-                                                       repositoryFactory, repositoryAlias)
-                                                       .login(workspace);
-                                       Node userHome = JcrUtils.getUserHome(session,
-                                                       authen.getName());
-                                       if (userHome == null)
-                                               JcrUtils.createUserHome(session, homeBasePath,
-                                                               authen.getName());
-                                       authen.setDetails(getUserDetails(userHome, authen));
-                               } catch (RepositoryException e) {
-                                       throw new ArgeoException(
-                                                       "Unexpected exception when synchronizing OS and JCR security ",
-                                                       e);
-                               }
-                       }
-               });
-               return authen;
-       }
-
-       /** Builds user details based on the authentication and the user home. */
-       protected UserDetails getUserDetails(Node userHome, Authentication authen) {
+       public void init() {
                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,
-                                       e);
+                       securitySession = repository.login(securityWorkspace);
+                       nodeSession = repository.login();
+               } catch (RepositoryException e) {
+                       throw new ArgeoException("Cannot initialize", e);
                }
        }
 
-       protected Boolean isEnabled(Node userHome) {
-               return true;
-       }
-
-       public void register(RepositoryFactory repositoryFactory,
-                       Map<String, String> parameters) {
-               this.repositoryFactory = repositoryFactory;
+       public void destroy() {
+               JcrUtils.logoutQuietly(securitySession);
+               JcrUtils.logoutQuietly(nodeSession);
        }
 
-       public void unregister(RepositoryFactory repositoryFactory,
-                       Map<String, String> parameters) {
-               this.repositoryFactory = null;
-       }
+       public Authentication authenticate(Authentication authentication)
+                       throws AuthenticationException {
+               final OsAuthenticationToken authen = (OsAuthenticationToken) super
+                               .authenticate(authentication);
+               try {
+                       // WARNING: at this stage we assume that the java properties
+                       // will have the same value
+                       String username = System.getProperty("user.name");
+                       Node userProfile = JcrUtils.createUserProfileIfNeeded(
+                                       securitySession, username);
+                       JcrUserDetails.checkAccountStatus(userProfile);
 
-       public void setSystemExecutor(Executor systemExecutor) {
-               this.systemExecutor = systemExecutor;
-       }
+                       // each user should have a writable area in the default workspace of
+                       // the node
+                       Node userNodeHome = JcrUtils.createUserHomeIfNeeded(nodeSession,
+                                       username);
+                       // FIXME how to set user home privileges *before* it is created ?
+                       // JcrUtils.addPrivilege(nodeSession, userNodeHome.getPath(),
+                       // username, Privilege.JCR_ALL);
+                       // if (nodeSession.hasPendingChanges())
+                       // nodeSession.save();
 
-       public void setHomeBasePath(String homeBasePath) {
-               this.homeBasePath = homeBasePath;
+                       // 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;
        }
 
-       public void setRepositoryAlias(String repositoryAlias) {
-               this.repositoryAlias = repositoryAlias;
+       public void setSecurityWorkspace(String securityWorkspace) {
+               this.securityWorkspace = securityWorkspace;
        }
 
-       public void setWorkspace(String workspace) {
-               this.workspace = workspace;
+       public void setRepository(Repository repository) {
+               this.repository = repository;
        }
-
 }