]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/internal/auth/CmsSessionImpl.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / internal / auth / CmsSessionImpl.java
1 package org.argeo.cms.internal.auth;
2
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;
8 import java.util.List;
9 import java.util.Locale;
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.UUID;
13 import java.util.function.Consumer;
14
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;
19
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;
27
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);
32
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;
40
41 private final ZonedDateTime creationTime;
42 private ZonedDateTime end;
43 private final Locale locale;
44
45 private Map<String, Object> views = new HashMap<>();
46
47 private List<Consumer<CmsSession>> onCloseCallbacks = Collections.synchronizedList(new ArrayList<>());
48
49 public CmsSessionImpl(UUID uuid, Subject initialSubject, Authorization authorization, Locale locale,
50 String localSessionId) {
51 Objects.requireNonNull(uuid);
52
53 this.creationTime = ZonedDateTime.now();
54 this.locale = locale;
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;
61 } else {
62 this.userDn = CmsConstants.ROLE_ANONYMOUS;
63 this.anonymous = true;
64 }
65 this.uuid = uuid;
66 }
67
68 public void close() {
69 end = ZonedDateTime.now();
70 CmsContextImpl.getCmsContext().unregisterCmsSession(this);
71 // serviceRegistration.unregister();
72
73 for (Consumer<CmsSession> onClose : onCloseCallbacks) {
74 onClose.accept(this);
75 }
76
77 try {
78 LoginContext lc;
79 if (isAnonymous()) {
80 lc = CmsAuth.ANONYMOUS.newLoginContext(getSubject());
81 } else {
82 lc = CmsAuth.USER.newLoginContext(getSubject());
83 }
84 lc.logout();
85 } catch (LoginException e) {
86 log.warn("Could not logout " + getSubject() + ": " + e);
87 } finally {
88 subject = null;
89 }
90 log.debug("Closed " + this);
91 }
92
93 @Override
94 public void addOnCloseCallback(Consumer<CmsSession> onClose) {
95 onCloseCallbacks.add(onClose);
96 }
97
98 public Subject getSubject() {
99 return subject;
100 }
101
102 // public Set<SecretKey> getSecretKeys() {
103 // checkValid();
104 // return getSubject().getPrivateCredentials(SecretKey.class);
105 // }
106
107 @Override
108 public boolean isValid() {
109 return !isClosed();
110 }
111
112 private void checkValid() {
113 if (!isValid())
114 throw new IllegalStateException("CMS session " + uuid + " is not valid since " + end);
115 }
116
117 final protected boolean isClosed() {
118 return getEnd() != null;
119 }
120
121 public Authorization getAuthorization() {
122 checkValid();
123 return authorization;
124 }
125
126 @Override
127 public String getDisplayName() {
128 return authorization.toString();
129 }
130
131 @Override
132 public UUID uuid() {
133 return uuid;
134 }
135
136 @Override
137 public String getUserDn() {
138 return userDn;
139 }
140
141 @Override
142 public String getUserRole() {
143 return new X500Principal(authorization.getName()).getName();
144 }
145
146 @Override
147 public String getLocalId() {
148 return localSessionId;
149 }
150
151 @Override
152 public boolean isAnonymous() {
153 return anonymous;
154 }
155
156 @Override
157 public Locale getLocale() {
158 return locale;
159 }
160
161 @Override
162 public ZonedDateTime getCreationTime() {
163 return creationTime;
164 }
165
166 @Override
167 public ZonedDateTime getEnd() {
168 return end;
169 }
170
171 @Override
172 public void registerView(String uid, Object view) {
173 checkValid();
174 if (views.containsKey(uid))
175 throw new IllegalArgumentException("View " + uid + " is already registered.");
176 views.put(uid, view);
177 }
178
179 /*
180 * OBJECT METHODS
181 */
182
183 @Override
184 public boolean equals(Object o) {
185 return UuidIdentified.equals(this, o);
186 }
187
188 @Override
189 public int hashCode() {
190 return UuidIdentified.hashCode(this);
191 }
192
193 @Override
194 public String toString() {
195 return "CMS Session " + userDn + " localId=" + localSessionId + ", uuid=" + uuid;
196 }
197 }