1 package org
.argeo
.cms
.auth
;
3 import java
.security
.Principal
;
4 import java
.util
.Collection
;
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
;
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
;
32 private final static Log log
= LogFactory
.getLog(CmsAuthUtils
.class);
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";
43 static void addAuthentication(Subject subject
, Authorization authorization
) {
44 assert subject
!= null;
45 checkSubjectEmpty(subject
);
46 assert authorization
!= null;
48 // required for display name:
49 subject
.getPrivateCredentials().add(authorization
);
51 Set
<Principal
> principals
= subject
.getPrincipals();
53 String authName
= authorization
.getName();
55 // determine user's principal
57 final Principal userPrincipal
;
58 if (authName
== null) {
59 name
= NodeSecurityUtils
.ROLE_ANONYMOUS_NAME
;
60 userPrincipal
= new AnonymousPrincipal();
61 principals
.add(userPrincipal
);
62 // principals.add(new AnonymousPrincipal());
64 name
= new LdapName(authName
);
65 NodeSecurityUtils
.checkUserName(name
);
66 userPrincipal
= new X500Principal(name
.toString());
67 principals
.add(userPrincipal
);
68 principals
.add(new ImpliedByPrincipal(NodeSecurityUtils
.ROLE_USER_NAME
, userPrincipal
));
71 // Add roles provided by authorization
72 for (String role
: authorization
.getRoles()) {
73 LdapName roleName
= new LdapName(role
);
74 if (roleName
.equals(name
)) {
77 NodeSecurityUtils
.checkImpliedPrincipalName(roleName
);
78 principals
.add(new ImpliedByPrincipal(roleName
.toString(), userPrincipal
));
79 if (roleName
.equals(NodeSecurityUtils
.ROLE_ADMIN_NAME
))
80 principals
.add(new DataAdminPrincipal());
84 } catch (InvalidNameException e
) {
85 throw new CmsException("Cannot commit", e
);
89 private static void checkSubjectEmpty(Subject subject
) {
90 if (!subject
.getPrincipals(AnonymousPrincipal
.class).isEmpty())
91 throw new IllegalStateException("Already logged in as anonymous: " + subject
);
92 if (!subject
.getPrincipals(X500Principal
.class).isEmpty())
93 throw new IllegalStateException("Already logged in as user: " + subject
);
94 if (!subject
.getPrincipals(DataAdminPrincipal
.class).isEmpty())
95 throw new IllegalStateException("Already logged in as data admin: " + subject
);
96 if (!subject
.getPrincipals(ImpliedByPrincipal
.class).isEmpty())
97 throw new IllegalStateException("Already authorized: " + subject
);
100 static void cleanUp(Subject subject
) {
102 subject
.getPrincipals().removeAll(subject
.getPrincipals(X500Principal
.class));
103 subject
.getPrincipals().removeAll(subject
.getPrincipals(ImpliedByPrincipal
.class));
105 // subject.getPrincipals().removeAll(subject.getPrincipals(AdminPrincipal.class));
106 // subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
110 // compatible with com.sun.security.auth.module.*LoginModule
111 // public static final String SHARED_STATE_USERNAME =
112 // "javax.security.auth.login.name";
113 // public static final String SHARED_STATE_PASSWORD =
114 // "javax.security.auth.login.password";
116 static void registerSessionAuthorization(BundleContext bc
, HttpServletRequest request
, Subject subject
,
117 Authorization authorization
) {
118 String httpSessId
= request
.getSession().getId();
119 if (authorization
.getName() != null) {
120 request
.setAttribute(HttpContext
.REMOTE_USER
, authorization
.getName());
121 request
.setAttribute(HttpContext
.AUTHORIZATION
, authorization
);
123 HttpSession httpSession
= request
.getSession();
124 if (httpSession
.getAttribute(HttpContext
.AUTHORIZATION
) == null) {
126 Collection
<ServiceReference
<WebCmsSession
>> sr
;
128 sr
= bc
.getServiceReferences(WebCmsSession
.class,
129 "(" + WebCmsSession
.CMS_SESSION_ID
+ "=" + httpSessId
+ ")");
130 } catch (InvalidSyntaxException e
) {
131 throw new CmsException("Cannot get CMS session for id " + httpSessId
, e
);
133 ServiceReference
<WebCmsSession
> cmsSessionRef
;
134 if (sr
.size() == 1) {
135 cmsSessionRef
= sr
.iterator().next();
136 } else if (sr
.size() == 0) {
137 WebCmsSessionImpl cmsSessionImpl
= new WebCmsSessionImpl(httpSessId
, authorization
);
138 cmsSessionRef
= cmsSessionImpl
.getServiceRegistration().getReference();
139 if (log
.isDebugEnabled())
140 log
.debug("Initialized " + cmsSessionImpl
+ " for " + authorization
.getName());
142 throw new CmsException(sr
.size() + " CMS sessions registered for " + httpSessId
);
144 WebCmsSessionImpl cmsSession
= (WebCmsSessionImpl
) bc
.getService(cmsSessionRef
);
145 cmsSession
.addHttpSession(request
);
146 if (log
.isTraceEnabled())
147 log
.trace("Added " + request
.getServletPath() + " to " + cmsSession
+ " (" + request
.getRequestURI()
149 // httpSession.setAttribute(HttpContext.REMOTE_USER,
150 // authorization.getName());
151 // httpSession.setAttribute(HttpContext.AUTHORIZATION,
155 HttpSessionId httpSessionId
= new HttpSessionId(httpSessId
);
156 if (subject
.getPrivateCredentials(HttpSessionId
.class).size() == 0)
157 subject
.getPrivateCredentials().add(httpSessionId
);
159 String storedSessionId
= subject
.getPrivateCredentials(HttpSessionId
.class).iterator().next().getValue();
160 // if (storedSessionId.equals(httpSessionId.getValue()))
161 throw new CmsException(
162 "Subject already logged with session " + storedSessionId
+ " (not " + httpSessionId
+ ")");
166 static boolean logoutSession(BundleContext bc
, Subject subject
) {
167 String httpSessionId
;
168 if (subject
.getPrivateCredentials(HttpSessionId
.class).size() == 1)
169 httpSessionId
= subject
.getPrivateCredentials(HttpSessionId
.class).iterator().next().getValue();
172 Collection
<ServiceReference
<WebCmsSession
>> srs
;
174 srs
= bc
.getServiceReferences(WebCmsSession
.class,
175 "(" + WebCmsSession
.CMS_SESSION_ID
+ "=" + httpSessionId
+ ")");
176 } catch (InvalidSyntaxException e
) {
177 throw new CmsException("Cannot retrieve CMS session #" + httpSessionId
, e
);
180 if (srs
.size() == 0) {
181 if (log
.isTraceEnabled())
182 log
.warn("No CMS web session found for http session " + httpSessionId
);
184 } else if (srs
.size() > 1)
185 throw new CmsException(srs
.size() + " CMS web sessions found for http session " + httpSessionId
);
187 WebCmsSessionImpl cmsSession
= (WebCmsSessionImpl
) bc
.getService(srs
.iterator().next());
188 cmsSession
.cleanUp();
189 subject
.getPrivateCredentials().removeAll(subject
.getPrivateCredentials(HttpSessionId
.class));
190 if (log
.isDebugEnabled())
191 log
.debug("Cleaned up " + cmsSession
);
195 private CmsAuthUtils() {