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
.argeo
.api
.cms
.CmsSession
;
25 import org
.argeo
.api
.cms
.CmsLog
;
26 import org
.argeo
.cms
.security
.NodeSecurityUtils
;
27 import org
.argeo
.api
.cms
.CmsAuth
;
28 import org
.osgi
.framework
.BundleContext
;
29 import org
.osgi
.framework
.FrameworkUtil
;
30 import org
.osgi
.framework
.InvalidSyntaxException
;
31 import org
.osgi
.framework
.ServiceReference
;
32 import org
.osgi
.framework
.ServiceRegistration
;
33 import org
.osgi
.service
.useradmin
.Authorization
;
35 /** Default CMS session implementation. */
36 public class CmsSessionImpl
implements CmsSession
, Serializable
{
37 private static final long serialVersionUID
= 1867719354246307225L;
38 private final static BundleContext bc
= FrameworkUtil
.getBundle(CmsSessionImpl
.class).getBundleContext();
39 private final static CmsLog log
= CmsLog
.getLog(CmsSessionImpl
.class);
41 // private final Subject initialSubject;
42 private transient AccessControlContext accessControlContext
;
43 private final UUID uuid
;
44 private final String localSessionId
;
45 private Authorization authorization
;
46 private final LdapName userDn
;
47 private final boolean anonymous
;
49 private final ZonedDateTime creationTime
;
50 private ZonedDateTime end
;
51 private final Locale locale
;
53 private ServiceRegistration
<CmsSession
> serviceRegistration
;
55 private Map
<String
, Object
> views
= new HashMap
<>();
57 public CmsSessionImpl(Subject initialSubject
, Authorization authorization
, Locale locale
, String localSessionId
) {
58 this.creationTime
= ZonedDateTime
.now();
60 this.accessControlContext
= Subject
.doAs(initialSubject
, new PrivilegedAction
<AccessControlContext
>() {
63 public AccessControlContext
run() {
64 return AccessController
.getContext();
68 // this.initialSubject = initialSubject;
69 this.localSessionId
= localSessionId
;
70 this.authorization
= authorization
;
71 if (authorization
.getName() != null)
73 this.userDn
= new LdapName(authorization
.getName());
74 this.anonymous
= false;
75 } catch (InvalidNameException e
) {
76 throw new IllegalArgumentException("Invalid user name " + authorization
.getName(), e
);
79 this.userDn
= NodeSecurityUtils
.ROLE_ANONYMOUS_NAME
;
80 this.anonymous
= true;
82 this.uuid
= UUID
.randomUUID();
83 // register as service
84 Hashtable
<String
, String
> props
= new Hashtable
<>();
85 props
.put(CmsSession
.USER_DN
, userDn
.toString());
86 props
.put(CmsSession
.SESSION_UUID
, uuid
.toString());
87 props
.put(CmsSession
.SESSION_LOCAL_ID
, localSessionId
);
88 serviceRegistration
= bc
.registerService(CmsSession
.class, this, props
);
92 end
= ZonedDateTime
.now();
93 serviceRegistration
.unregister();
98 lc
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_ANONYMOUS
, getSubject());
100 lc
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_USER
, getSubject());
103 } catch (LoginException e
) {
104 log
.warn("Could not logout " + getSubject() + ": " + e
);
106 accessControlContext
= null;
108 log
.debug("Closed " + this);
111 public Subject
getSubject() {
112 return Subject
.getSubject(accessControlContext
);
115 public Set
<SecretKey
> getSecretKeys() {
117 return getSubject().getPrivateCredentials(SecretKey
.class);
121 public boolean isValid() {
125 private void checkValid() {
127 throw new IllegalStateException("CMS session " + uuid
+ " is not valid since " + end
);
130 final protected boolean isClosed() {
131 return getEnd() != null;
134 public Authorization
getAuthorization() {
136 return authorization
;
140 public String
getDisplayName() {
141 return authorization
.toString();
145 public UUID
getUuid() {
150 public LdapName
getUserDn() {
155 public String
getUserRole() {
156 return new X500Principal(authorization
.getName()).getName();
160 public String
getLocalId() {
161 return localSessionId
;
165 public boolean isAnonymous() {
170 public Locale
getLocale() {
175 public ZonedDateTime
getCreationTime() {
180 public ZonedDateTime
getEnd() {
185 public void registerView(String uid
, Object view
) {
187 if (views
.containsKey(uid
))
188 throw new IllegalArgumentException("View " + uid
+ " is already registered.");
189 views
.put(uid
, view
);
192 public String
toString() {
193 return "CMS Session " + userDn
+ " local=" + localSessionId
+ ", uuid=" + uuid
;
196 public static CmsSessionImpl
getByLocalId(String localId
) {
197 Collection
<ServiceReference
<CmsSession
>> sr
;
199 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_LOCAL_ID
+ "=" + localId
+ ")");
200 } catch (InvalidSyntaxException e
) {
201 throw new IllegalArgumentException("Cannot get CMS session for id " + localId
, e
);
203 ServiceReference
<CmsSession
> cmsSessionRef
;
204 if (sr
.size() == 1) {
205 cmsSessionRef
= sr
.iterator().next();
206 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
207 } else if (sr
.size() == 0) {
210 throw new IllegalStateException(sr
.size() + " CMS sessions registered for " + localId
);
214 public static CmsSessionImpl
getByUuid(Object uuid
) {
215 Collection
<ServiceReference
<CmsSession
>> sr
;
217 sr
= bc
.getServiceReferences(CmsSession
.class, "(" + CmsSession
.SESSION_UUID
+ "=" + uuid
+ ")");
218 } catch (InvalidSyntaxException e
) {
219 throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid
, e
);
221 ServiceReference
<CmsSession
> cmsSessionRef
;
222 if (sr
.size() == 1) {
223 cmsSessionRef
= sr
.iterator().next();
224 return (CmsSessionImpl
) bc
.getService(cmsSessionRef
);
225 } else if (sr
.size() == 0) {
228 throw new IllegalStateException(sr
.size() + " CMS sessions registered for " + uuid
);
232 public static void closeInvalidSessions() {
233 Collection
<ServiceReference
<CmsSession
>> srs
;
235 srs
= bc
.getServiceReferences(CmsSession
.class, null);
236 for (ServiceReference
<CmsSession
> sr
: srs
) {
237 CmsSession cmsSession
= bc
.getService(sr
);
238 if (!cmsSession
.isValid()) {
239 ((CmsSessionImpl
) cmsSession
).close();
240 if (log
.isDebugEnabled())
241 log
.debug("Closed expired CMS session " + cmsSession
);
244 } catch (InvalidSyntaxException e
) {
245 throw new IllegalArgumentException("Cannot get CMS sessions", e
);