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
.api
.uuid
.UuidIdentified
;
25 import org
.argeo
.cms
.internal
.runtime
.CmsContextImpl
;
26 import org
.osgi
.service
.useradmin
.Authorization
;
28 /** Default CMS session implementation. */
29 public class CmsSessionImpl
implements CmsSession
, Serializable
, UuidIdentified
{
30 private static final long serialVersionUID
= 1867719354246307225L;
31 private final static CmsLog log
= CmsLog
.getLog(CmsSessionImpl
.class);
33 private transient Subject subject
;
34 private final UUID uuid
;
35 private final String localSessionId
;
36 private Authorization authorization
;
37 // private final LdapName userDn;
38 private final String userDn
;
39 private final boolean anonymous
;
41 private final ZonedDateTime creationTime
;
42 private ZonedDateTime end
;
43 private final Locale locale
;
45 private Map
<String
, Object
> views
= new HashMap
<>();
47 private List
<Consumer
<CmsSession
>> onCloseCallbacks
= Collections
.synchronizedList(new ArrayList
<>());
49 public CmsSessionImpl(UUID uuid
, Subject initialSubject
, Authorization authorization
, Locale locale
,
50 String localSessionId
) {
51 Objects
.requireNonNull(uuid
);
53 this.creationTime
= ZonedDateTime
.now();
55 this.subject
= initialSubject
;
56 this.localSessionId
= localSessionId
;
57 this.authorization
= authorization
;
58 if (authorization
.getName() != null) {
59 this.userDn
= authorization
.getName();
60 this.anonymous
= false;
62 this.userDn
= CmsConstants
.ROLE_ANONYMOUS
;
63 this.anonymous
= true;
69 end
= ZonedDateTime
.now();
70 CmsContextImpl
.getCmsContext().unregisterCmsSession(this);
71 // serviceRegistration.unregister();
73 for (Consumer
<CmsSession
> onClose
: onCloseCallbacks
) {
80 lc
= CmsAuth
.ANONYMOUS
.newLoginContext(getSubject());
82 lc
= CmsAuth
.USER
.newLoginContext(getSubject());
85 } catch (LoginException e
) {
86 log
.warn("Could not logout " + getSubject() + ": " + e
);
90 log
.debug("Closed " + this);
94 public void addOnCloseCallback(Consumer
<CmsSession
> onClose
) {
95 onCloseCallbacks
.add(onClose
);
98 public Subject
getSubject() {
102 // public Set<SecretKey> getSecretKeys() {
104 // return getSubject().getPrivateCredentials(SecretKey.class);
108 public boolean isValid() {
112 private void checkValid() {
114 throw new IllegalStateException("CMS session " + uuid
+ " is not valid since " + end
);
117 final protected boolean isClosed() {
118 return getEnd() != null;
121 public Authorization
getAuthorization() {
123 return authorization
;
127 public String
getDisplayName() {
128 return authorization
.toString();
137 public String
getUserDn() {
142 public String
getUserRole() {
143 return new X500Principal(authorization
.getName()).getName();
147 public String
getLocalId() {
148 return localSessionId
;
152 public boolean isAnonymous() {
157 public Locale
getLocale() {
162 public ZonedDateTime
getCreationTime() {
167 public ZonedDateTime
getEnd() {
172 public void registerView(String uid
, Object view
) {
174 if (views
.containsKey(uid
))
175 throw new IllegalArgumentException("View " + uid
+ " is already registered.");
176 views
.put(uid
, view
);
184 public boolean equals(Object o
) {
185 return UuidIdentified
.equals(this, o
);
189 public int hashCode() {
190 return UuidIdentified
.hashCode(this);
194 public String
toString() {
195 return "CMS Session " + userDn
+ " localId=" + localSessionId
+ ", uuid=" + uuid
;