1 package org
.argeo
.cms
.internal
.auth
;
3 import java
.security
.AccessControlContext
;
4 import java
.security
.AccessController
;
5 import java
.security
.PrivilegedAction
;
6 import java
.security
.PrivilegedActionException
;
7 import java
.security
.PrivilegedExceptionAction
;
8 import java
.time
.ZonedDateTime
;
9 import java
.util
.Collection
;
10 import java
.util
.HashMap
;
11 import java
.util
.HashSet
;
12 import java
.util
.Hashtable
;
13 import java
.util
.LinkedHashSet
;
14 import java
.util
.Locale
;
17 import java
.util
.UUID
;
19 import javax
.crypto
.SecretKey
;
20 import javax
.jcr
.Repository
;
21 import javax
.jcr
.Session
;
22 import javax
.naming
.InvalidNameException
;
23 import javax
.naming
.ldap
.LdapName
;
24 import javax
.security
.auth
.Subject
;
25 import javax
.security
.auth
.login
.LoginContext
;
26 import javax
.security
.auth
.login
.LoginException
;
27 import javax
.security
.auth
.x500
.X500Principal
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
31 import org
.argeo
.api
.NodeConstants
;
32 import org
.argeo
.api
.security
.NodeSecurityUtils
;
33 import org
.argeo
.cms
.auth
.CmsSession
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.osgi
.framework
.BundleContext
;
36 import org
.osgi
.framework
.FrameworkUtil
;
37 import org
.osgi
.framework
.InvalidSyntaxException
;
38 import org
.osgi
.framework
.ServiceReference
;
39 import org
.osgi
.framework
.ServiceRegistration
;
40 import org
.osgi
.service
.useradmin
.Authorization
;
42 /** Default CMS session implementation. */
43 public class CmsSessionImpl
implements CmsSession
{
44 private final static BundleContext bc
= FrameworkUtil
.getBundle(CmsSessionImpl
.class).getBundleContext();
45 private final static Log log
= LogFactory
.getLog(CmsSessionImpl
.class);
47 // private final Subject initialSubject;
48 private final AccessControlContext initialContext
;
49 private final UUID uuid
;
50 private final String localSessionId
;
51 private final Authorization authorization
;
52 private final LdapName userDn
;
53 private final boolean anonymous
;
55 private final ZonedDateTime creationTime
;
56 private ZonedDateTime end
;
57 private final Locale locale
;
59 private ServiceRegistration
<CmsSession
> serviceRegistration
;
61 private Map
<String
, Session
> dataSessions
= new HashMap
<>();
62 private Set
<String
> dataSessionsInUse
= new HashSet
<>();
63 private LinkedHashSet
<Session
> additionalDataSessions
= new LinkedHashSet
<>();
65 private Map
<String
, Object
> views
= new HashMap
<>();
67 public CmsSessionImpl(Subject initialSubject
, Authorization authorization
, Locale locale
, String localSessionId
) {
68 this.creationTime
= ZonedDateTime
.now();
70 this.initialContext
= Subject
.doAs(initialSubject
, new PrivilegedAction
<AccessControlContext
>() {
73 public AccessControlContext
run() {
74 return AccessController
.getContext();
78 // this.initialSubject = initialSubject;
79 this.localSessionId
= localSessionId
;
80 this.authorization
= authorization
;
81 if (authorization
.getName() != null)
83 this.userDn
= new LdapName(authorization
.getName());
84 this.anonymous
= false;
85 } catch (InvalidNameException e
) {
86 throw new IllegalArgumentException("Invalid user name " + authorization
.getName(), e
);
89 this.userDn
= NodeSecurityUtils
.ROLE_ANONYMOUS_NAME
;
90 this.anonymous
= true;
92 this.uuid
= UUID
.randomUUID();
93 // register as service
94 Hashtable
<String
, String
> props
= new Hashtable
<>();
95 props
.put(CmsSession
.USER_DN
, userDn
.toString());
96 props
.put(CmsSession
.SESSION_UUID
, uuid
.toString());
97 props
.put(CmsSession
.SESSION_LOCAL_ID
, localSessionId
);
98 serviceRegistration
= bc
.registerService(CmsSession
.class, this, props
);
101 public void close() {
102 end
= ZonedDateTime
.now();
103 serviceRegistration
.unregister();
105 synchronized (this) {
106 // TODO check data session in use ?
107 for (String path
: dataSessions
.keySet())
108 JcrUtils
.logoutQuietly(dataSessions
.get(path
));
109 for (Session session
: additionalDataSessions
)
110 JcrUtils
.logoutQuietly(session
);
116 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
, getSubject());
118 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, getSubject());
121 } catch (LoginException e
) {
122 log
.warn("Could not logout " + getSubject() + ": " + e
);
124 log
.debug("Closed " + this);
127 private Subject
getSubject() {
128 return Subject
.getSubject(initialContext
);
131 public Set
<SecretKey
> getSecretKeys() {
132 return getSubject().getPrivateCredentials(SecretKey
.class);
135 public Session
newDataSession(String cn
, String workspace
, Repository repository
) {
136 return login(repository
, workspace
);
139 public synchronized Session
getDataSession(String cn
, String workspace
, Repository repository
) {
140 // FIXME make it more robust
141 if (workspace
== null)
142 workspace
= NodeConstants
.SYS_WORKSPACE
;
143 String path
= cn
+ '/' + workspace
;
144 if (dataSessionsInUse
.contains(path
)) {
147 if (dataSessionsInUse
.contains(path
)) {
148 Session session
= login(repository
, workspace
);
149 additionalDataSessions
.add(session
);
150 if (log
.isTraceEnabled())
151 log
.trace("Additional data session " + path
+ " for " + userDn
);
154 } catch (InterruptedException e
) {
159 Session session
= null;
160 if (dataSessions
.containsKey(path
)) {
161 session
= dataSessions
.get(path
);
163 session
= login(repository
, workspace
);
164 dataSessions
.put(path
, session
);
165 if (log
.isTraceEnabled())
166 log
.trace("New data session " + path
+ " for " + userDn
);
168 dataSessionsInUse
.add(path
);
172 private Session
login(Repository repository
, String workspace
) {
174 return Subject
.doAs(getSubject(), new PrivilegedExceptionAction
<Session
>() {
176 public Session
run() throws Exception
{
177 return repository
.login(workspace
);
180 } catch (PrivilegedActionException e
) {
181 throw new IllegalStateException("Cannot log in " + userDn
+ " to JCR", e
);
185 public synchronized void releaseDataSession(String cn
, Session session
) {
186 if (additionalDataSessions
.contains(session
)) {
187 JcrUtils
.logoutQuietly(session
);
188 additionalDataSessions
.remove(session
);
189 if (log
.isTraceEnabled())
190 log
.trace("Remove additional data session " + session
);
193 String path
= cn
+ '/' + session
.getWorkspace().getName();
194 if (!dataSessionsInUse
.contains(path
))
195 log
.warn("Data session " + path
+ " was not in use for " + userDn
);
196 dataSessionsInUse
.remove(path
);
197 Session registeredSession
= dataSessions
.get(path
);
198 if (session
!= registeredSession
)
199 log
.warn("Data session " + path
+ " not consistent for " + userDn
);
200 if (log
.isTraceEnabled())
201 log
.trace("Released data session " + session
+ " for " + path
);
206 public boolean isValid() {
210 protected boolean isClosed() {
211 return getEnd() != null;
215 public Authorization
getAuthorization() {
216 return authorization
;
220 public UUID
getUuid() {
225 public LdapName
getUserDn() {
230 public String
getUserRole() {
231 return new X500Principal(authorization
.getName()).getName();
235 public String
getLocalId() {
236 return localSessionId
;
240 public boolean isAnonymous() {
245 public Locale
getLocale() {
250 public ZonedDateTime
getCreationTime() {
255 public ZonedDateTime
getEnd() {
260 public void registerView(String uid
, Object view
) {
261 if (views
.containsKey(uid
))
262 throw new IllegalArgumentException("View " + uid
+ " is already registered.");
263 views
.put(uid
, view
);
266 public String
toString() {
267 return "CMS Session " + userDn
+ " local=" + localSessionId
+ ", uuid=" + uuid
;
270 public static CmsSessionImpl
getByLocalId(String localId
) {
271 Collection
<ServiceReference
<CmsSession
>> sr
;
273 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_LOCAL_ID
+ "=" + localId
+ ")");
274 } catch (InvalidSyntaxException e
) {
275 throw new IllegalArgumentException("Cannot get CMS session for id " + localId
, e
);
277 ServiceReference
<CmsSession
> cmsSessionRef
;
278 if (sr
.size() == 1) {
279 cmsSessionRef
= sr
.iterator().next();
280 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
281 } else if (sr
.size() == 0) {
284 throw new IllegalStateException(sr
.size() + " CMS sessions registered for " + localId
);
288 public static CmsSessionImpl
getByUuid(Object uuid
) {
289 Collection
<ServiceReference
<CmsSession
>> sr
;
291 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_UUID
+ "=" + uuid
+ ")");
292 } catch (InvalidSyntaxException e
) {
293 throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid
, e
);
295 ServiceReference
<CmsSession
> cmsSessionRef
;
296 if (sr
.size() == 1) {
297 cmsSessionRef
= sr
.iterator().next();
298 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
299 } else if (sr
.size() == 0) {
302 throw new IllegalStateException(sr
.size() + " CMS sessions registered for " + uuid
);
306 public static void closeInvalidSessions() {
307 Collection
<ServiceReference
<CmsSession
>> srs
;
309 srs
= bc
.getServiceReferences(CmsSession
.class, null);
310 for (ServiceReference
<CmsSession
> sr
: srs
) {
311 CmsSession cmsSession
= bc
.getService(sr
);
312 if (!cmsSession
.isValid()) {
313 ((CmsSessionImpl
) cmsSession
).close();
314 if (log
.isDebugEnabled())
315 log
.debug("Closed expired CMS session " + cmsSession
);
318 } catch (InvalidSyntaxException e
) {
319 throw new IllegalArgumentException("Cannot get CMS sessions", e
);