1 package org
.argeo
.cms
.internal
.auth
;
3 import java
.io
.Serializable
;
4 import java
.security
.AccessControlContext
;
5 import java
.security
.AccessController
;
6 import java
.security
.PrivilegedAction
;
7 import java
.time
.ZonedDateTime
;
8 import java
.util
.Collection
;
9 import java
.util
.HashMap
;
10 import java
.util
.Hashtable
;
11 import java
.util
.Locale
;
14 import java
.util
.UUID
;
16 import javax
.crypto
.SecretKey
;
17 import javax
.naming
.InvalidNameException
;
18 import javax
.naming
.ldap
.LdapName
;
19 import javax
.security
.auth
.Subject
;
20 import javax
.security
.auth
.login
.LoginContext
;
21 import javax
.security
.auth
.login
.LoginException
;
22 import javax
.security
.auth
.x500
.X500Principal
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.api
.NodeConstants
;
27 import org
.argeo
.api
.cms
.CmsSession
;
28 import org
.argeo
.api
.security
.NodeSecurityUtils
;
29 import org
.osgi
.framework
.BundleContext
;
30 import org
.osgi
.framework
.FrameworkUtil
;
31 import org
.osgi
.framework
.InvalidSyntaxException
;
32 import org
.osgi
.framework
.ServiceReference
;
33 import org
.osgi
.framework
.ServiceRegistration
;
34 import org
.osgi
.service
.useradmin
.Authorization
;
36 /** Default CMS session implementation. */
37 public class CmsSessionImpl
implements CmsSession
, Serializable
{
38 private static final long serialVersionUID
= 1867719354246307225L;
39 private final static BundleContext bc
= FrameworkUtil
.getBundle(CmsSessionImpl
.class).getBundleContext();
40 private final static Log log
= LogFactory
.getLog(CmsSessionImpl
.class);
42 // private final Subject initialSubject;
43 private transient AccessControlContext accessControlContext
;
44 private final UUID uuid
;
45 private final String localSessionId
;
46 private Authorization authorization
;
47 private final LdapName userDn
;
48 private final boolean anonymous
;
50 private final ZonedDateTime creationTime
;
51 private ZonedDateTime end
;
52 private final Locale locale
;
54 private ServiceRegistration
<CmsSession
> serviceRegistration
;
56 private Map
<String
, Object
> views
= new HashMap
<>();
58 public CmsSessionImpl(Subject initialSubject
, Authorization authorization
, Locale locale
, String localSessionId
) {
59 this.creationTime
= ZonedDateTime
.now();
61 this.accessControlContext
= Subject
.doAs(initialSubject
, new PrivilegedAction
<AccessControlContext
>() {
64 public AccessControlContext
run() {
65 return AccessController
.getContext();
69 // this.initialSubject = initialSubject;
70 this.localSessionId
= localSessionId
;
71 this.authorization
= authorization
;
72 if (authorization
.getName() != null)
74 this.userDn
= new LdapName(authorization
.getName());
75 this.anonymous
= false;
76 } catch (InvalidNameException e
) {
77 throw new IllegalArgumentException("Invalid user name " + authorization
.getName(), e
);
80 this.userDn
= NodeSecurityUtils
.ROLE_ANONYMOUS_NAME
;
81 this.anonymous
= true;
83 this.uuid
= UUID
.randomUUID();
84 // register as service
85 Hashtable
<String
, String
> props
= new Hashtable
<>();
86 props
.put(CmsSession
.USER_DN
, userDn
.toString());
87 props
.put(CmsSession
.SESSION_UUID
, uuid
.toString());
88 props
.put(CmsSession
.SESSION_LOCAL_ID
, localSessionId
);
89 serviceRegistration
= bc
.registerService(CmsSession
.class, this, props
);
93 end
= ZonedDateTime
.now();
94 serviceRegistration
.unregister();
99 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
, getSubject());
101 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, getSubject());
104 } catch (LoginException e
) {
105 log
.warn("Could not logout " + getSubject() + ": " + e
);
107 accessControlContext
= null;
109 log
.debug("Closed " + this);
112 public Subject
getSubject() {
113 return Subject
.getSubject(accessControlContext
);
116 public Set
<SecretKey
> getSecretKeys() {
118 return getSubject().getPrivateCredentials(SecretKey
.class);
122 public boolean isValid() {
126 private void checkValid() {
128 throw new IllegalStateException("CMS session " + uuid
+ " is not valid since " + end
);
131 final protected boolean isClosed() {
132 return getEnd() != null;
135 public Authorization
getAuthorization() {
137 return authorization
;
141 public String
getDisplayName() {
142 return authorization
.toString();
146 public UUID
getUuid() {
151 public LdapName
getUserDn() {
156 public String
getUserRole() {
157 return new X500Principal(authorization
.getName()).getName();
161 public String
getLocalId() {
162 return localSessionId
;
166 public boolean isAnonymous() {
171 public Locale
getLocale() {
176 public ZonedDateTime
getCreationTime() {
181 public ZonedDateTime
getEnd() {
186 public void registerView(String uid
, Object view
) {
188 if (views
.containsKey(uid
))
189 throw new IllegalArgumentException("View " + uid
+ " is already registered.");
190 views
.put(uid
, view
);
193 public String
toString() {
194 return "CMS Session " + userDn
+ " local=" + localSessionId
+ ", uuid=" + uuid
;
197 public static CmsSessionImpl
getByLocalId(String localId
) {
198 Collection
<ServiceReference
<CmsSession
>> sr
;
200 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_LOCAL_ID
+ "=" + localId
+ ")");
201 } catch (InvalidSyntaxException e
) {
202 throw new IllegalArgumentException("Cannot get CMS session for id " + localId
, e
);
204 ServiceReference
<CmsSession
> cmsSessionRef
;
205 if (sr
.size() == 1) {
206 cmsSessionRef
= sr
.iterator().next();
207 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
208 } else if (sr
.size() == 0) {
211 throw new IllegalStateException(sr
.size() + " CMS sessions registered for " + localId
);
215 public static CmsSessionImpl
getByUuid(Object uuid
) {
216 Collection
<ServiceReference
<CmsSession
>> sr
;
218 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_UUID
+ "=" + uuid
+ ")");
219 } catch (InvalidSyntaxException e
) {
220 throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid
, e
);
222 ServiceReference
<CmsSession
> cmsSessionRef
;
223 if (sr
.size() == 1) {
224 cmsSessionRef
= sr
.iterator().next();
225 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
226 } else if (sr
.size() == 0) {
229 throw new IllegalStateException(sr
.size() + " CMS sessions registered for " + uuid
);
233 public static void closeInvalidSessions() {
234 Collection
<ServiceReference
<CmsSession
>> srs
;
236 srs
= bc
.getServiceReferences(CmsSession
.class, null);
237 for (ServiceReference
<CmsSession
> sr
: srs
) {
238 CmsSession cmsSession
= bc
.getService(sr
);
239 if (!cmsSession
.isValid()) {
240 ((CmsSessionImpl
) cmsSession
).close();
241 if (log
.isDebugEnabled())
242 log
.debug("Closed expired CMS session " + cmsSession
);
245 } catch (InvalidSyntaxException e
) {
246 throw new IllegalArgumentException("Cannot get CMS sessions", e
);