]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/CmsAuthUtils.java
Fix MANIFEST generation issues.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CmsAuthUtils.java
1 package org.argeo.cms.auth;
2
3 import java.security.Principal;
4 import java.util.Collection;
5 import java.util.Locale;
6 import java.util.Set;
7 import java.util.UUID;
8
9 import javax.naming.InvalidNameException;
10 import javax.naming.ldap.LdapName;
11 import javax.security.auth.Subject;
12 import javax.security.auth.x500.X500Principal;
13
14 import org.argeo.api.cms.CmsSession;
15 import org.argeo.api.cms.CmsSessionId;
16 import org.argeo.api.cms.DataAdminPrincipal;
17 import org.argeo.api.cms.AnonymousPrincipal;
18 import org.argeo.api.cms.CmsConstants;
19 import org.argeo.cms.internal.auth.CmsSessionImpl;
20 import org.argeo.cms.internal.auth.ImpliedByPrincipal;
21 import org.argeo.cms.internal.http.WebCmsSessionImpl;
22 import org.argeo.cms.security.NodeSecurityUtils;
23 import org.argeo.osgi.useradmin.AuthenticatingUser;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.InvalidSyntaxException;
26 import org.osgi.framework.ServiceReference;
27 import org.osgi.service.http.HttpContext;
28 import org.osgi.service.useradmin.Authorization;
29
30 /** Centralises security related registrations. */
31 class CmsAuthUtils {
32 // Standard
33 final static String SHARED_STATE_NAME = AuthenticatingUser.SHARED_STATE_NAME;
34 final static String SHARED_STATE_PWD = AuthenticatingUser.SHARED_STATE_PWD;
35 final static String HEADER_AUTHORIZATION = "Authorization";
36 final static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
37
38 // Argeo specific
39 final static String SHARED_STATE_HTTP_REQUEST = "org.argeo.cms.auth.http.request";
40 final static String SHARED_STATE_SPNEGO_TOKEN = "org.argeo.cms.auth.spnegoToken";
41 final static String SHARED_STATE_SPNEGO_OUT_TOKEN = "org.argeo.cms.auth.spnegoOutToken";
42 final static String SHARED_STATE_CERTIFICATE_CHAIN = "org.argeo.cms.auth.certificateChain";
43 final static String SHARED_STATE_REMOTE_ADDR = "org.argeo.cms.auth.remote.addr";
44 final static String SHARED_STATE_REMOTE_PORT = "org.argeo.cms.auth.remote.port";
45
46 static void addAuthorization(Subject subject, Authorization authorization) {
47 assert subject != null;
48 checkSubjectEmpty(subject);
49 assert authorization != null;
50
51 // required for display name:
52 subject.getPrivateCredentials().add(authorization);
53
54 boolean singleUser = authorization instanceof SingleUserAuthorization;
55
56 Set<Principal> principals = subject.getPrincipals();
57 try {
58 String authName = authorization.getName();
59
60 // determine user's principal
61 final LdapName name;
62 final Principal userPrincipal;
63 if (authName == null) {
64 name = NodeSecurityUtils.ROLE_ANONYMOUS_NAME;
65 userPrincipal = new AnonymousPrincipal();
66 principals.add(userPrincipal);
67 } else {
68 name = new LdapName(authName);
69 NodeSecurityUtils.checkUserName(name);
70 userPrincipal = new X500Principal(name.toString());
71 principals.add(userPrincipal);
72
73 if (singleUser) {
74 principals.add(new ImpliedByPrincipal(NodeSecurityUtils.ROLE_ADMIN_NAME, userPrincipal));
75 principals.add(new DataAdminPrincipal());
76 }
77 }
78
79 // Add roles provided by authorization
80 for (String role : authorization.getRoles()) {
81 LdapName roleName = new LdapName(role);
82 if (roleName.equals(name)) {
83 // skip
84 } else if (roleName.equals(NodeSecurityUtils.ROLE_ANONYMOUS_NAME)) {
85 // skip
86 } else {
87 NodeSecurityUtils.checkImpliedPrincipalName(roleName);
88 principals.add(new ImpliedByPrincipal(roleName.toString(), userPrincipal));
89 if (roleName.equals(NodeSecurityUtils.ROLE_ADMIN_NAME))
90 principals.add(new DataAdminPrincipal());
91 }
92 }
93
94 } catch (InvalidNameException e) {
95 throw new IllegalArgumentException("Cannot commit", e);
96 }
97 }
98
99 private static void checkSubjectEmpty(Subject subject) {
100 if (!subject.getPrincipals(AnonymousPrincipal.class).isEmpty())
101 throw new IllegalStateException("Already logged in as anonymous: " + subject);
102 if (!subject.getPrincipals(X500Principal.class).isEmpty())
103 throw new IllegalStateException("Already logged in as user: " + subject);
104 if (!subject.getPrincipals(DataAdminPrincipal.class).isEmpty())
105 throw new IllegalStateException("Already logged in as data admin: " + subject);
106 if (!subject.getPrincipals(ImpliedByPrincipal.class).isEmpty())
107 throw new IllegalStateException("Already authorized: " + subject);
108 }
109
110 static void cleanUp(Subject subject) {
111 // Argeo
112 subject.getPrincipals().removeAll(subject.getPrincipals(X500Principal.class));
113 subject.getPrincipals().removeAll(subject.getPrincipals(ImpliedByPrincipal.class));
114 subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
115 subject.getPrincipals().removeAll(subject.getPrincipals(DataAdminPrincipal.class));
116
117 subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(CmsSessionId.class));
118 subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(Authorization.class));
119 // Jackrabbit
120 // subject.getPrincipals().removeAll(subject.getPrincipals(AdminPrincipal.class));
121 // subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
122 }
123
124 @SuppressWarnings("unused")
125 synchronized static void registerSessionAuthorization(RemoteAuthRequest request, Subject subject,
126 Authorization authorization, Locale locale) {
127 // synchronized in order to avoid multiple registrations
128 // TODO move it to a service in order to avoid static synchronization
129 if (request != null) {
130 RemoteAuthSession httpSession = request.getSession();
131 assert httpSession != null;
132 String httpSessId = httpSession.getId();
133 boolean anonymous = authorization.getName() == null;
134 String remoteUser = !anonymous ? authorization.getName() : CmsConstants.ROLE_ANONYMOUS;
135 request.setAttribute(HttpContext.REMOTE_USER, remoteUser);
136 request.setAttribute(HttpContext.AUTHORIZATION, authorization);
137
138 CmsSessionImpl cmsSession;
139 CmsSessionImpl currentLocalSession = CmsSessionImpl.getByLocalId(httpSessId);
140 if (currentLocalSession != null) {
141 boolean currentLocalSessionAnonymous = currentLocalSession.getAuthorization().getName() == null;
142 if (!anonymous) {
143 if (currentLocalSessionAnonymous) {
144 currentLocalSession.close();
145 // new CMS session
146 cmsSession = new WebCmsSessionImpl(subject, authorization, locale, request);
147 } else if (!authorization.getName().equals(currentLocalSession.getAuthorization().getName())) {
148 throw new IllegalStateException("Inconsistent user " + authorization.getName()
149 + " for existing CMS session " + currentLocalSession);
150 } else {
151 // keep current session
152 cmsSession = currentLocalSession;
153 // keyring
154 subject.getPrivateCredentials().addAll(cmsSession.getSecretKeys());
155 }
156 } else {// anonymous
157 if (!currentLocalSessionAnonymous) {
158 currentLocalSession.close();
159 throw new IllegalStateException(
160 "Existing CMS session " + currentLocalSession + " was not logged out properly.");
161 }
162 // keep current session
163 cmsSession = currentLocalSession;
164 }
165 } else {
166 // new CMS session
167 cmsSession = new WebCmsSessionImpl(subject, authorization, locale, request);
168 }
169
170 if (cmsSession == null)// should be dead code (cf. SuppressWarning of the method)
171 throw new IllegalStateException("CMS session cannot be null");
172
173 CmsSessionId nodeSessionId = new CmsSessionId(cmsSession.getUuid());
174 if (subject.getPrivateCredentials(CmsSessionId.class).size() == 0) {
175 subject.getPrivateCredentials().add(nodeSessionId);
176 } else {
177 UUID storedSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next().getUuid();
178 // if (storedSessionId.equals(httpSessionId.getValue()))
179 throw new IllegalStateException(
180 "Subject already logged with session " + storedSessionId + " (not " + nodeSessionId + ")");
181 }
182 } else {
183 CmsSessionImpl cmsSession = new CmsSessionImpl(subject, authorization, locale, "desktop");
184 CmsSessionId nodeSessionId = new CmsSessionId(cmsSession.getUuid());
185 subject.getPrivateCredentials().add(nodeSessionId);
186 }
187 }
188
189 public static CmsSessionImpl cmsSessionFromHttpSession(BundleContext bc, String httpSessionId) {
190 Authorization authorization = null;
191 Collection<ServiceReference<CmsSession>> sr;
192 try {
193 sr = bc.getServiceReferences(CmsSession.class,
194 "(" + CmsSession.SESSION_LOCAL_ID + "=" + httpSessionId + ")");
195 } catch (InvalidSyntaxException e) {
196 throw new IllegalArgumentException("Cannot get CMS session for id " + httpSessionId, e);
197 }
198 CmsSessionImpl cmsSession;
199 if (sr.size() == 1) {
200 cmsSession = (CmsSessionImpl) bc.getService(sr.iterator().next());
201 // locale = cmsSession.getLocale();
202 authorization = cmsSession.getAuthorization();
203 if (authorization.getName() == null)
204 return null;// anonymous is not sufficient
205 } else if (sr.size() == 0)
206 return null;
207 else
208 throw new IllegalStateException(sr.size() + ">1 web sessions detected for http session " + httpSessionId);
209 return cmsSession;
210 }
211
212 public static <T extends Principal> T getSinglePrincipal(Subject subject, Class<T> clss) {
213 Set<T> principals = subject.getPrincipals(clss);
214 if (principals.isEmpty())
215 return null;
216 if (principals.size() > 1)
217 throw new IllegalStateException("Only one " + clss + " principal expected in " + subject);
218 return principals.iterator().next();
219 }
220
221 private CmsAuthUtils() {
222
223 }
224
225 }