]> git.argeo.org Git - lgpl/argeo-commons.git/blob - argeo/cms/auth/CmsAuthUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.Set;
6
7 import javax.naming.InvalidNameException;
8 import javax.naming.ldap.LdapName;
9 import javax.security.auth.Subject;
10 import javax.security.auth.x500.X500Principal;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpSession;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 //import org.apache.jackrabbit.core.security.AnonymousPrincipal;
17 //import org.apache.jackrabbit.core.security.SecurityConstants;
18 //import org.apache.jackrabbit.core.security.principal.AdminPrincipal;
19 import org.argeo.cms.CmsException;
20 import org.argeo.cms.internal.auth.ImpliedByPrincipal;
21 import org.argeo.cms.internal.kernel.WebCmsSessionImpl;
22 import org.argeo.node.security.AnonymousPrincipal;
23 import org.argeo.node.security.DataAdminPrincipal;
24 import org.argeo.node.security.NodeSecurityUtils;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.InvalidSyntaxException;
27 import org.osgi.framework.ServiceReference;
28 import org.osgi.service.http.HttpContext;
29 import org.osgi.service.useradmin.Authorization;
30
31 class CmsAuthUtils {
32 private final static Log log = LogFactory.getLog(CmsAuthUtils.class);
33
34 /** Shared HTTP request */
35 static final String SHARED_STATE_HTTP_REQUEST = "org.argeo.cms.auth.http.request";
36 /** From org.osgi.service.http.HttpContext */
37 static final String SHARED_STATE_AUTHORIZATION = "org.osgi.service.useradmin.authorization";
38 /** From com.sun.security.auth.module.*LoginModule */
39 static final String SHARED_STATE_NAME = "javax.security.auth.login.name";
40 /** From com.sun.security.auth.module.*LoginModule */
41 static final String SHARED_STATE_PWD = "javax.security.auth.login.password";
42
43 static final String SHARED_STATE_SPNEGO_TOKEN = "org.argeo.cms.auth.spnegoToken";
44 static final String SHARED_STATE_SPNEGO_OUT_TOKEN = "org.argeo.cms.auth.spnegoOutToken";
45
46 static final String HEADER_AUTHORIZATION = "Authorization";
47
48 static void addAuthentication(Subject subject, Authorization authorization) {
49 assert subject != null;
50 checkSubjectEmpty(subject);
51 assert authorization != null;
52
53 // required for display name:
54 subject.getPrivateCredentials().add(authorization);
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 // principals.add(new AnonymousPrincipal());
68 } else {
69 name = new LdapName(authName);
70 NodeSecurityUtils.checkUserName(name);
71 userPrincipal = new X500Principal(name.toString());
72 principals.add(userPrincipal);
73 principals.add(new ImpliedByPrincipal(NodeSecurityUtils.ROLE_USER_NAME, userPrincipal));
74 }
75
76 // Add roles provided by authorization
77 for (String role : authorization.getRoles()) {
78 LdapName roleName = new LdapName(role);
79 if (roleName.equals(name)) {
80 // skip
81 } else {
82 NodeSecurityUtils.checkImpliedPrincipalName(roleName);
83 principals.add(new ImpliedByPrincipal(roleName.toString(), userPrincipal));
84 if (roleName.equals(NodeSecurityUtils.ROLE_ADMIN_NAME))
85 principals.add(new DataAdminPrincipal());
86 }
87 }
88
89 } catch (InvalidNameException e) {
90 throw new CmsException("Cannot commit", e);
91 }
92 }
93
94 private static void checkSubjectEmpty(Subject subject) {
95 if (!subject.getPrincipals(AnonymousPrincipal.class).isEmpty())
96 throw new IllegalStateException("Already logged in as anonymous: " + subject);
97 if (!subject.getPrincipals(X500Principal.class).isEmpty())
98 throw new IllegalStateException("Already logged in as user: " + subject);
99 if (!subject.getPrincipals(DataAdminPrincipal.class).isEmpty())
100 throw new IllegalStateException("Already logged in as data admin: " + subject);
101 if (!subject.getPrincipals(ImpliedByPrincipal.class).isEmpty())
102 throw new IllegalStateException("Already authorized: " + subject);
103 }
104
105 static void cleanUp(Subject subject) {
106 // Argeo
107 subject.getPrincipals().removeAll(subject.getPrincipals(X500Principal.class));
108 subject.getPrincipals().removeAll(subject.getPrincipals(ImpliedByPrincipal.class));
109 // Jackrabbit
110 // subject.getPrincipals().removeAll(subject.getPrincipals(AdminPrincipal.class));
111 // subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
112 }
113
114 // SHARED STATE KEYS
115 // compatible with com.sun.security.auth.module.*LoginModule
116 // public static final String SHARED_STATE_USERNAME =
117 // "javax.security.auth.login.name";
118 // public static final String SHARED_STATE_PASSWORD =
119 // "javax.security.auth.login.password";
120
121 static void registerSessionAuthorization(BundleContext bc, HttpServletRequest request, Subject subject,
122 Authorization authorization) {
123 String httpSessId = request.getSession().getId();
124 if (authorization.getName() != null) {
125 request.setAttribute(HttpContext.REMOTE_USER, authorization.getName());
126 request.setAttribute(HttpContext.AUTHORIZATION, authorization);
127
128 HttpSession httpSession = request.getSession();
129 if (httpSession.getAttribute(HttpContext.AUTHORIZATION) == null) {
130
131 Collection<ServiceReference<WebCmsSession>> sr;
132 try {
133 sr = bc.getServiceReferences(WebCmsSession.class,
134 "(" + WebCmsSession.CMS_SESSION_ID + "=" + httpSessId + ")");
135 } catch (InvalidSyntaxException e) {
136 throw new CmsException("Cannot get CMS session for id " + httpSessId, e);
137 }
138 ServiceReference<WebCmsSession> cmsSessionRef;
139 if (sr.size() == 1) {
140 cmsSessionRef = sr.iterator().next();
141 } else if (sr.size() == 0) {
142 WebCmsSessionImpl cmsSessionImpl = new WebCmsSessionImpl(httpSessId, authorization);
143 cmsSessionRef = cmsSessionImpl.getServiceRegistration().getReference();
144 if (log.isDebugEnabled())
145 log.debug("Initialized " + cmsSessionImpl + " for " + authorization.getName());
146 } else
147 throw new CmsException(sr.size() + " CMS sessions registered for " + httpSessId);
148
149 WebCmsSessionImpl cmsSession = (WebCmsSessionImpl) bc.getService(cmsSessionRef);
150 cmsSession.addHttpSession(request);
151 if (log.isTraceEnabled())
152 log.trace("Added " + request.getServletPath() + " to " + cmsSession + " (" + request.getRequestURI()
153 + ")");
154 // httpSession.setAttribute(HttpContext.REMOTE_USER,
155 // authorization.getName());
156 // httpSession.setAttribute(HttpContext.AUTHORIZATION,
157 // authorization);
158 }
159 }
160 HttpSessionId httpSessionId = new HttpSessionId(httpSessId);
161 if (subject.getPrivateCredentials(HttpSessionId.class).size() == 0)
162 subject.getPrivateCredentials().add(httpSessionId);
163 else {
164 String storedSessionId = subject.getPrivateCredentials(HttpSessionId.class).iterator().next().getValue();
165 // if (storedSessionId.equals(httpSessionId.getValue()))
166 throw new CmsException(
167 "Subject already logged with session " + storedSessionId + " (not " + httpSessionId + ")");
168 }
169 }
170
171 static boolean logoutSession(BundleContext bc, Subject subject) {
172 String httpSessionId;
173 if (subject.getPrivateCredentials(HttpSessionId.class).size() == 1)
174 httpSessionId = subject.getPrivateCredentials(HttpSessionId.class).iterator().next().getValue();
175 else
176 return false;
177 Collection<ServiceReference<WebCmsSession>> srs;
178 try {
179 srs = bc.getServiceReferences(WebCmsSession.class,
180 "(" + WebCmsSession.CMS_SESSION_ID + "=" + httpSessionId + ")");
181 } catch (InvalidSyntaxException e) {
182 throw new CmsException("Cannot retrieve CMS session #" + httpSessionId, e);
183 }
184
185 if (srs.size() == 0) {
186 if (log.isTraceEnabled())
187 log.warn("No CMS web session found for http session " + httpSessionId);
188 return false;
189 } else if (srs.size() > 1)
190 throw new CmsException(srs.size() + " CMS web sessions found for http session " + httpSessionId);
191
192 WebCmsSessionImpl cmsSession = (WebCmsSessionImpl) bc.getService(srs.iterator().next());
193 cmsSession.cleanUp();
194 subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(HttpSessionId.class));
195 if (log.isDebugEnabled())
196 log.debug("Cleaned up " + cmsSession);
197 return true;
198 }
199
200 private CmsAuthUtils() {
201
202 }
203
204 }