]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrAuthenticationToken.java
Introduce AsyncSystemTaskExecutor
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / jcr / JcrAuthenticationToken.java
1 package org.argeo.security.jcr;
2
3 import javax.jcr.Node;
4 import javax.jcr.RepositoryException;
5 import javax.jcr.Session;
6
7 import org.argeo.ArgeoException;
8 import org.argeo.security.SiteAuthenticationToken;
9 import org.springframework.security.GrantedAuthority;
10
11 /** An authenticated authentication based on a JCR session. */
12 public class JcrAuthenticationToken extends SiteAuthenticationToken {
13 private static final long serialVersionUID = -2736830165315486169L;
14
15 private final transient Session session;
16 private final String userHomePath;
17
18 public JcrAuthenticationToken(Object principal, Object credentials,
19 GrantedAuthority[] authorities, String url, Node userHome) {
20 super(principal, credentials, authorities, url,
21 extractWorkspace(userHome));
22 try {
23 this.session = userHome.getSession();
24 this.userHomePath = userHome.getPath();
25 } catch (RepositoryException e) {
26 throw new ArgeoException("Cannot extract path from " + userHome, e);
27 }
28 }
29
30 private static String extractWorkspace(Node userHome) {
31 try {
32 return userHome.getSession().getWorkspace().getName();
33 } catch (RepositoryException e) {
34 throw new ArgeoException("Cannot extract workspace from "
35 + userHome, e);
36 }
37 }
38
39 /** The path to the authenticated user home node. */
40 public String getUserHomePath() {
41 return userHomePath;
42 }
43
44 /** The session used to create this authentication. */
45 public Session getSession() {
46 return session;
47 }
48
49 @Override
50 public boolean isAuthenticated() {
51 if (session == null || !session.isLive())
52 setAuthenticated(false);
53 return super.isAuthenticated();
54 }
55
56 @Override
57 public void setAuthenticated(boolean isAuthenticated)
58 throws IllegalArgumentException {
59 super.setAuthenticated(isAuthenticated);
60 if (!isAuthenticated && session != null)
61 session.logout();
62 }
63
64 }