Improve Jackrabbit security
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / jcr / JcrAuthenticationToken.java
index 865508317e9a12d68b9476a30783a64904bb8db5..27b7ee85b174a222300c79c36d9f260dffe9369d 100644 (file)
@@ -2,33 +2,63 @@ package org.argeo.security.jcr;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 
 import org.argeo.ArgeoException;
 import org.argeo.security.SiteAuthenticationToken;
 import org.springframework.security.GrantedAuthority;
 
+/** An authenticated authentication based on a JCR session. */
 public class JcrAuthenticationToken extends SiteAuthenticationToken {
        private static final long serialVersionUID = -2736830165315486169L;
-       private final transient Node userHome;
+
+       private final transient Session session;
+       private final String userHomePath;
 
        public JcrAuthenticationToken(Object principal, Object credentials,
                        GrantedAuthority[] authorities, String url, Node userHome) {
                super(principal, credentials, authorities, url,
                                extractWorkspace(userHome));
-               this.userHome = userHome;
+               try {
+                       this.session = userHome.getSession();
+                       this.userHomePath = userHome.getPath();
+               } catch (RepositoryException e) {
+                       throw new ArgeoException("Cannot extract path from " + userHome, e);
+               }
        }
 
        private static String extractWorkspace(Node userHome) {
                try {
                        return userHome.getSession().getWorkspace().getName();
                } catch (RepositoryException e) {
-                       throw new ArgeoException("Cannot extract workspace of " + userHome,
-                                       e);
+                       throw new ArgeoException("Cannot extract workspace from "
+                                       + userHome, e);
                }
        }
 
-       public Node getUserHome() {
-               return userHome;
+       /** The path to the authenticated user home node. */
+       public String getUserHomePath() {
+               return userHomePath;
+       }
+
+       /** The session used to create this authentication. */
+       public Session getSession() {
+               return session;
+       }
+
+       @Override
+       public boolean isAuthenticated() {
+               if (session == null || !session.isLive())
+                       setAuthenticated(false);
+               return super.isAuthenticated();
+       }
+
+       @Override
+       public void setAuthenticated(boolean isAuthenticated)
+                       throws IllegalArgumentException {
+               super.setAuthenticated(isAuthenticated);
+               if (!isAuthenticated && session != null)
+                       session.logout();
        }
 
 }