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
.PrivilegedExceptionAction
;
7 import java
.time
.ZonedDateTime
;
8 import java
.util
.Collection
;
9 import java
.util
.HashMap
;
10 import java
.util
.HashSet
;
11 import java
.util
.Hashtable
;
12 import java
.util
.LinkedHashSet
;
13 import java
.util
.Locale
;
16 import java
.util
.UUID
;
18 import javax
.crypto
.SecretKey
;
19 import javax
.jcr
.Repository
;
20 import javax
.jcr
.Session
;
21 import javax
.naming
.InvalidNameException
;
22 import javax
.naming
.ldap
.LdapName
;
23 import javax
.security
.auth
.Subject
;
24 import javax
.security
.auth
.login
.LoginContext
;
25 import javax
.security
.auth
.login
.LoginException
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.argeo
.cms
.CmsException
;
30 import org
.argeo
.cms
.auth
.CmsSession
;
31 import org
.argeo
.jcr
.JcrUtils
;
32 import org
.argeo
.node
.NodeConstants
;
33 import org
.argeo
.node
.security
.NodeSecurityUtils
;
34 import org
.osgi
.framework
.BundleContext
;
35 import org
.osgi
.framework
.FrameworkUtil
;
36 import org
.osgi
.framework
.InvalidSyntaxException
;
37 import org
.osgi
.framework
.ServiceReference
;
38 import org
.osgi
.framework
.ServiceRegistration
;
39 import org
.osgi
.service
.useradmin
.Authorization
;
41 public class CmsSessionImpl
implements CmsSession
{
42 private final static BundleContext bc
= FrameworkUtil
.getBundle(CmsSessionImpl
.class).getBundleContext();
43 private final static Log log
= LogFactory
.getLog(CmsSessionImpl
.class);
45 // private final Subject initialSubject;
46 private final AccessControlContext initialContext
;
47 private final UUID uuid
;
48 private final String localSessionId
;
49 private final Authorization authorization
;
50 private final LdapName userDn
;
51 private final boolean anonymous
;
53 private final ZonedDateTime creationTime
;
54 private ZonedDateTime end
;
55 private final Locale locale
;
57 private ServiceRegistration
<CmsSession
> serviceRegistration
;
59 private Map
<String
, Session
> dataSessions
= new HashMap
<>();
60 private Set
<String
> dataSessionsInUse
= new HashSet
<>();
61 private LinkedHashSet
<Session
> additionalDataSessions
= new LinkedHashSet
<>();
63 public CmsSessionImpl(Subject initialSubject
, Authorization authorization
, Locale locale
, String localSessionId
) {
64 this.creationTime
= ZonedDateTime
.now();
66 this.initialContext
= Subject
.doAs(initialSubject
, new PrivilegedAction
<AccessControlContext
>() {
69 public AccessControlContext
run() {
70 return AccessController
.getContext();
74 // this.initialSubject = initialSubject;
75 this.localSessionId
= localSessionId
;
76 this.authorization
= authorization
;
77 if (authorization
.getName() != null)
79 this.userDn
= new LdapName(authorization
.getName());
80 this.anonymous
= false;
81 } catch (InvalidNameException e
) {
82 throw new CmsException("Invalid user name " + authorization
.getName(), e
);
85 this.userDn
= NodeSecurityUtils
.ROLE_ANONYMOUS_NAME
;
86 this.anonymous
= true;
88 this.uuid
= UUID
.randomUUID();
89 // register as service
90 Hashtable
<String
, String
> props
= new Hashtable
<>();
91 props
.put(CmsSession
.USER_DN
, userDn
.toString());
92 props
.put(CmsSession
.SESSION_UUID
, uuid
.toString());
93 props
.put(CmsSession
.SESSION_LOCAL_ID
, localSessionId
);
94 serviceRegistration
= bc
.registerService(CmsSession
.class, this, props
);
98 end
= ZonedDateTime
.now();
99 serviceRegistration
.unregister();
101 synchronized (this) {
102 // TODO check data session in use ?
103 for (String path
: dataSessions
.keySet())
104 JcrUtils
.logoutQuietly(dataSessions
.get(path
));
105 for (Session session
: additionalDataSessions
)
106 JcrUtils
.logoutQuietly(session
);
112 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
, getSubject());
114 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, getSubject());
117 } catch (LoginException e
) {
118 log
.warn("Could not logout " + getSubject() + ": " + e
);
120 log
.debug("Closed " + this);
123 private Subject
getSubject() {
124 return Subject
.getSubject(initialContext
);
127 public Set
<SecretKey
> getSecretKeys() {
128 return getSubject().getPrivateCredentials(SecretKey
.class);
131 public synchronized Session
getDataSession(String cn
, String workspace
, Repository repository
) {
132 // FIXME make it more robust
133 if (workspace
== null)
135 String path
= cn
+ '/' + workspace
;
136 if (dataSessionsInUse
.contains(path
)) {
139 if (dataSessionsInUse
.contains(path
)) {
140 Session session
= login(repository
, workspace
);
141 additionalDataSessions
.add(session
);
142 if (log
.isTraceEnabled())
143 log
.trace("Additional data session " + path
+ " for " + userDn
);
146 } catch (InterruptedException e
) {
151 Session session
= null;
152 if (dataSessions
.containsKey(path
)) {
153 session
= dataSessions
.get(path
);
155 session
= login(repository
, workspace
);
156 dataSessions
.put(path
, session
);
157 if (log
.isTraceEnabled())
158 log
.trace("New data session " + path
+ " for " + userDn
);
160 dataSessionsInUse
.add(path
);
164 private Session
login(Repository repository
, String workspace
) {
166 return Subject
.doAs(getSubject(), new PrivilegedExceptionAction
<Session
>() {
168 public Session
run() throws Exception
{
169 return repository
.login(workspace
);
172 } catch (Exception e
) {
173 throw new CmsException("Cannot log in " + userDn
+ " to JCR", e
);
177 public synchronized void releaseDataSession(String cn
, Session session
) {
178 if (additionalDataSessions
.contains(session
)) {
179 JcrUtils
.logoutQuietly(session
);
180 additionalDataSessions
.remove(session
);
181 if (log
.isTraceEnabled())
182 log
.trace("Remove additional data session " + session
);
185 String path
= cn
+ '/' + session
.getWorkspace().getName();
186 if (!dataSessionsInUse
.contains(path
))
187 log
.warn("Data session " + path
+ " was not in use for " + userDn
);
188 dataSessionsInUse
.remove(path
);
189 Session registeredSession
= dataSessions
.get(path
);
190 if (session
!= registeredSession
)
191 log
.warn("Data session " + path
+ " not consistent for " + userDn
);
192 if (log
.isTraceEnabled())
193 log
.trace("Released data session " + session
+ " for " + path
);
198 public boolean isValid() {
202 protected boolean isClosed() {
203 return getEnd() != null;
207 public Authorization
getAuthorization() {
208 return authorization
;
212 public UUID
getUuid() {
217 public LdapName
getUserDn() {
222 public String
getLocalId() {
223 return localSessionId
;
227 public boolean isAnonymous() {
232 public Locale
getLocale() {
237 public ZonedDateTime
getCreationTime() {
242 public ZonedDateTime
getEnd() {
246 public String
toString() {
247 return "CMS Session " + userDn
+ " local=" + localSessionId
+ ", uuid=" + uuid
;
250 public static CmsSessionImpl
getByLocalId(String localId
) {
251 Collection
<ServiceReference
<CmsSession
>> sr
;
253 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_LOCAL_ID
+ "=" + localId
+ ")");
254 } catch (InvalidSyntaxException e
) {
255 throw new CmsException("Cannot get CMS session for id " + localId
, e
);
257 ServiceReference
<CmsSession
> cmsSessionRef
;
258 if (sr
.size() == 1) {
259 cmsSessionRef
= sr
.iterator().next();
260 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
261 } else if (sr
.size() == 0) {
264 throw new CmsException(sr
.size() + " CMS sessions registered for " + localId
);
268 public static CmsSessionImpl
getByUuid(Object uuid
) {
269 Collection
<ServiceReference
<CmsSession
>> sr
;
271 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_UUID
+ "=" + uuid
+ ")");
272 } catch (InvalidSyntaxException e
) {
273 throw new CmsException("Cannot get CMS session for uuid " + uuid
, e
);
275 ServiceReference
<CmsSession
> cmsSessionRef
;
276 if (sr
.size() == 1) {
277 cmsSessionRef
= sr
.iterator().next();
278 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
279 } else if (sr
.size() == 0) {
282 throw new CmsException(sr
.size() + " CMS sessions registered for " + uuid
);
286 public static void closeInvalidSessions() {
287 Collection
<ServiceReference
<CmsSession
>> srs
;
289 srs
= bc
.getServiceReferences(CmsSession
.class, null);
290 for (ServiceReference
<CmsSession
> sr
: srs
) {
291 CmsSession cmsSession
= bc
.getService(sr
);
292 if (!cmsSession
.isValid()) {
293 ((CmsSessionImpl
) cmsSession
).close();
294 if (log
.isDebugEnabled())
295 log
.debug("Closed expired CMS session " + cmsSession
);
298 } catch (InvalidSyntaxException e
) {
299 throw new CmsException("Cannot get CMS sessions", e
);