1 package org
.argeo
.cms
.internal
.auth
;
3 import java
.io
.Serializable
;
4 import java
.time
.ZonedDateTime
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Collections
;
7 import java
.util
.HashMap
;
9 import java
.util
.Locale
;
11 import java
.util
.Objects
;
12 import java
.util
.UUID
;
13 import java
.util
.function
.Consumer
;
15 import javax
.security
.auth
.Subject
;
16 import javax
.security
.auth
.login
.LoginContext
;
17 import javax
.security
.auth
.login
.LoginException
;
18 import javax
.security
.auth
.x500
.X500Principal
;
20 import org
.argeo
.api
.cms
.CmsAuth
;
21 import org
.argeo
.api
.cms
.CmsConstants
;
22 import org
.argeo
.api
.cms
.CmsLog
;
23 import org
.argeo
.api
.cms
.CmsSession
;
24 import org
.argeo
.cms
.internal
.runtime
.CmsContextImpl
;
25 import org
.osgi
.service
.useradmin
.Authorization
;
27 /** Default CMS session implementation. */
28 public class CmsSessionImpl
implements CmsSession
, Serializable
{
29 private static final long serialVersionUID
= 1867719354246307225L;
30 private final static CmsLog log
= CmsLog
.getLog(CmsSessionImpl
.class);
32 private transient Subject subject
;
33 private final UUID uuid
;
34 private final String localSessionId
;
35 private Authorization authorization
;
36 // private final LdapName userDn;
37 private final String userDn
;
38 private final boolean anonymous
;
40 private final ZonedDateTime creationTime
;
41 private ZonedDateTime end
;
42 private final Locale locale
;
44 private Map
<String
, Object
> views
= new HashMap
<>();
46 private List
<Consumer
<CmsSession
>> onCloseCallbacks
= Collections
.synchronizedList(new ArrayList
<>());
48 public CmsSessionImpl(UUID uuid
, Subject initialSubject
, Authorization authorization
, Locale locale
,
49 String localSessionId
) {
50 Objects
.requireNonNull(uuid
);
52 this.creationTime
= ZonedDateTime
.now();
54 this.subject
= initialSubject
;
55 this.localSessionId
= localSessionId
;
56 this.authorization
= authorization
;
57 if (authorization
.getName() != null) {
58 this.userDn
= authorization
.getName();
59 this.anonymous
= false;
61 this.userDn
= CmsConstants
.ROLE_ANONYMOUS
;
62 this.anonymous
= true;
68 end
= ZonedDateTime
.now();
69 CmsContextImpl
.getCmsContext().unregisterCmsSession(this);
70 // serviceRegistration.unregister();
72 for (Consumer
<CmsSession
> onClose
: onCloseCallbacks
) {
79 lc
= CmsAuth
.ANONYMOUS
.newLoginContext(getSubject());
81 lc
= CmsAuth
.USER
.newLoginContext(getSubject());
84 } catch (LoginException e
) {
85 log
.warn("Could not logout " + getSubject() + ": " + e
);
89 log
.debug("Closed " + this);
93 public void addOnCloseCallback(Consumer
<CmsSession
> onClose
) {
94 onCloseCallbacks
.add(onClose
);
97 public Subject
getSubject() {
101 // public Set<SecretKey> getSecretKeys() {
103 // return getSubject().getPrivateCredentials(SecretKey.class);
107 public boolean isValid() {
111 private void checkValid() {
113 throw new IllegalStateException("CMS session " + uuid
+ " is not valid since " + end
);
116 final protected boolean isClosed() {
117 return getEnd() != null;
120 public Authorization
getAuthorization() {
122 return authorization
;
126 public String
getDisplayName() {
127 return authorization
.toString();
131 public UUID
getUuid() {
136 public String
getUserDn() {
141 public String
getUserRole() {
142 return new X500Principal(authorization
.getName()).getName();
146 public String
getLocalId() {
147 return localSessionId
;
151 public boolean isAnonymous() {
156 public Locale
getLocale() {
161 public ZonedDateTime
getCreationTime() {
166 public ZonedDateTime
getEnd() {
171 public void registerView(String uid
, Object view
) {
173 if (views
.containsKey(uid
))
174 throw new IllegalArgumentException("View " + uid
+ " is already registered.");
175 views
.put(uid
, view
);
178 public String
toString() {
179 return "CMS Session " + userDn
+ " localId=" + localSessionId
+ ", uuid=" + uuid
;