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
.jcr
.Repository
;
19 import javax
.jcr
.Session
;
20 import javax
.naming
.InvalidNameException
;
21 import javax
.naming
.ldap
.LdapName
;
22 import javax
.security
.auth
.Subject
;
23 import javax
.security
.auth
.login
.LoginContext
;
24 import javax
.security
.auth
.login
.LoginException
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.cms
.CmsException
;
29 import org
.argeo
.cms
.auth
.CmsSession
;
30 import org
.argeo
.jcr
.JcrUtils
;
31 import org
.argeo
.node
.NodeConstants
;
32 import org
.argeo
.node
.security
.NodeSecurityUtils
;
33 import org
.osgi
.framework
.BundleContext
;
34 import org
.osgi
.framework
.FrameworkUtil
;
35 import org
.osgi
.framework
.InvalidSyntaxException
;
36 import org
.osgi
.framework
.ServiceReference
;
37 import org
.osgi
.framework
.ServiceRegistration
;
38 import org
.osgi
.service
.useradmin
.Authorization
;
40 public class CmsSessionImpl
implements CmsSession
{
41 private final static BundleContext bc
= FrameworkUtil
.getBundle(CmsSessionImpl
.class).getBundleContext();
42 private final static Log log
= LogFactory
.getLog(CmsSessionImpl
.class);
44 // private final Subject initialSubject;
45 private final AccessControlContext initialContext
;
46 private final UUID uuid
;
47 private final String localSessionId
;
48 private final Authorization authorization
;
49 private final LdapName userDn
;
50 private final boolean anonymous
;
52 private final ZonedDateTime creationTime
;
53 private ZonedDateTime end
;
54 private final Locale locale
;
56 private ServiceRegistration
<CmsSession
> serviceRegistration
;
58 private Map
<String
, Session
> dataSessions
= new HashMap
<>();
59 private Set
<String
> dataSessionsInUse
= new HashSet
<>();
60 private LinkedHashSet
<Session
> additionalDataSessions
= new LinkedHashSet
<>();
62 public CmsSessionImpl(Subject initialSubject
, Authorization authorization
, Locale locale
, String localSessionId
) {
63 this.creationTime
= ZonedDateTime
.now();
65 this.initialContext
= Subject
.doAs(initialSubject
, new PrivilegedAction
<AccessControlContext
>() {
68 public AccessControlContext
run() {
69 return AccessController
.getContext();
73 // this.initialSubject = initialSubject;
74 this.localSessionId
= localSessionId
;
75 this.authorization
= authorization
;
76 if (authorization
.getName() != null)
78 this.userDn
= new LdapName(authorization
.getName());
79 this.anonymous
= false;
80 } catch (InvalidNameException e
) {
81 throw new CmsException("Invalid user name " + authorization
.getName(), e
);
84 this.userDn
= NodeSecurityUtils
.ROLE_ANONYMOUS_NAME
;
85 this.anonymous
= true;
87 this.uuid
= UUID
.randomUUID();
88 // register as service
89 Hashtable
<String
, String
> props
= new Hashtable
<>();
90 props
.put(CmsSession
.USER_DN
, userDn
.toString());
91 props
.put(CmsSession
.SESSION_UUID
, uuid
.toString());
92 props
.put(CmsSession
.SESSION_LOCAL_ID
, localSessionId
);
93 serviceRegistration
= bc
.registerService(CmsSession
.class, this, props
);
97 end
= ZonedDateTime
.now();
98 serviceRegistration
.unregister();
100 synchronized (this) {
101 // TODO check data session in use ?
102 for (String path
: dataSessions
.keySet())
103 JcrUtils
.logoutQuietly(dataSessions
.get(path
));
104 for (Session session
: additionalDataSessions
)
105 JcrUtils
.logoutQuietly(session
);
111 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
, getSubject());
113 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, getSubject());
116 } catch (LoginException e
) {
117 log
.warn("Could not logout " + getSubject() + ": " + e
);
119 log
.debug("Closed " + this);
122 private Subject
getSubject() {
123 return Subject
.getSubject(initialContext
);
126 public synchronized Session
getDataSession(String cn
, String workspace
, Repository repository
) {
127 // FIXME make it more robust
128 if (workspace
== null)
130 String path
= cn
+ '/' + workspace
;
131 if (dataSessionsInUse
.contains(path
)) {
134 if (dataSessionsInUse
.contains(path
)) {
135 Session session
= login(repository
, workspace
);
136 additionalDataSessions
.add(session
);
137 if (log
.isTraceEnabled())
138 log
.trace("Additional data session " + path
+ " for " + userDn
);
141 } catch (InterruptedException e
) {
146 Session session
= null;
147 if (dataSessions
.containsKey(path
)) {
148 session
= dataSessions
.get(path
);
150 session
= login(repository
, workspace
);
151 dataSessions
.put(path
, session
);
152 if (log
.isTraceEnabled())
153 log
.trace("New data session " + path
+ " for " + userDn
);
155 dataSessionsInUse
.add(path
);
159 private Session
login(Repository repository
, String workspace
) {
161 return Subject
.doAs(getSubject(), new PrivilegedExceptionAction
<Session
>() {
163 public Session
run() throws Exception
{
164 return repository
.login(workspace
);
167 } catch (Exception e
) {
168 throw new CmsException("Cannot log in " + userDn
+ " to JCR", e
);
172 public synchronized void releaseDataSession(String cn
, Session session
) {
173 if (additionalDataSessions
.contains(session
)) {
174 JcrUtils
.logoutQuietly(session
);
175 additionalDataSessions
.remove(session
);
178 String path
= cn
+ '/' + session
.getWorkspace().getName();
179 if (!dataSessionsInUse
.contains(path
))
180 log
.warn("Data session " + path
+ " was not in use for " + userDn
);
181 dataSessionsInUse
.remove(path
);
182 Session registeredSession
= dataSessions
.get(path
);
183 if (session
!= registeredSession
)
184 log
.warn("Data session " + path
+ " not consistent for " + userDn
);
189 public boolean isValid() {
193 protected boolean isClosed() {
194 return getEnd() != null;
198 public Authorization
getAuthorization() {
199 return authorization
;
203 public UUID
getUuid() {
208 public LdapName
getUserDn() {
213 public String
getLocalId() {
214 return localSessionId
;
218 public boolean isAnonymous() {
223 public Locale
getLocale() {
228 public ZonedDateTime
getCreationTime() {
233 public ZonedDateTime
getEnd() {
237 public String
toString() {
238 return "CMS Session " + userDn
+ " local=" + localSessionId
+ ", uuid=" + uuid
;
241 public static CmsSessionImpl
getByLocalId(String localId
) {
242 Collection
<ServiceReference
<CmsSession
>> sr
;
244 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_LOCAL_ID
+ "=" + localId
+ ")");
245 } catch (InvalidSyntaxException e
) {
246 throw new CmsException("Cannot get CMS session for id " + localId
, e
);
248 ServiceReference
<CmsSession
> cmsSessionRef
;
249 if (sr
.size() == 1) {
250 cmsSessionRef
= sr
.iterator().next();
251 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
252 } else if (sr
.size() == 0) {
255 throw new CmsException(sr
.size() + " CMS sessions registered for " + localId
);
259 public static CmsSessionImpl
getByUuid(Object uuid
) {
260 Collection
<ServiceReference
<CmsSession
>> sr
;
262 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_UUID
+ "=" + uuid
+ ")");
263 } catch (InvalidSyntaxException e
) {
264 throw new CmsException("Cannot get CMS session for uuid " + uuid
, e
);
266 ServiceReference
<CmsSession
> cmsSessionRef
;
267 if (sr
.size() == 1) {
268 cmsSessionRef
= sr
.iterator().next();
269 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
270 } else if (sr
.size() == 0) {
273 throw new CmsException(sr
.size() + " CMS sessions registered for " + uuid
);
277 public static void closeInvalidSessions() {
278 Collection
<ServiceReference
<CmsSession
>> srs
;
280 srs
= bc
.getServiceReferences(CmsSession
.class, null);
281 for (ServiceReference
<CmsSession
> sr
: srs
) {
282 CmsSession cmsSession
= bc
.getService(sr
);
283 if (!cmsSession
.isValid()) {
284 ((CmsSessionImpl
) cmsSession
).close();
285 if (log
.isDebugEnabled())
286 log
.debug("Closed expired CMS session " + cmsSession
);
289 } catch (InvalidSyntaxException e
) {
290 throw new CmsException("Cannot get CMS sessions", e
);