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 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";
46 static final String HEADER_AUTHORIZATION
= "Authorization";
48 static void addAuthentication(Subject subject
, Authorization authorization
) {
49 assert subject
!= null;
50 checkSubjectEmpty(subject
);
51 assert authorization
!= null;
53 // required for display name:
54 subject
.getPrivateCredentials().add(authorization
);
56 Set
<Principal
> principals
= subject
.getPrincipals();
58 String authName
= authorization
.getName();
60 // determine user's principal
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());
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
));
76 // Add roles provided by authorization
77 for (String role
: authorization
.getRoles()) {
78 LdapName roleName
= new LdapName(role
);
79 if (roleName
.equals(name
)) {
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());
89 } catch (InvalidNameException e
) {
90 throw new CmsException("Cannot commit", e
);
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
);
105 static void cleanUp(Subject subject
) {
107 subject
.getPrincipals().removeAll(subject
.getPrincipals(X500Principal
.class));
108 subject
.getPrincipals().removeAll(subject
.getPrincipals(ImpliedByPrincipal
.class));
110 // subject.getPrincipals().removeAll(subject.getPrincipals(AdminPrincipal.class));
111 // subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
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";
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
);
128 HttpSession httpSession
= request
.getSession();
129 if (httpSession
.getAttribute(HttpContext
.AUTHORIZATION
) == null) {
131 Collection
<ServiceReference
<WebCmsSession
>> sr
;
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
);
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());
147 throw new CmsException(sr
.size() + " CMS sessions registered for " + httpSessId
);
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()
154 // httpSession.setAttribute(HttpContext.REMOTE_USER,
155 // authorization.getName());
156 // httpSession.setAttribute(HttpContext.AUTHORIZATION,
160 HttpSessionId httpSessionId
= new HttpSessionId(httpSessId
);
161 if (subject
.getPrivateCredentials(HttpSessionId
.class).size() == 0)
162 subject
.getPrivateCredentials().add(httpSessionId
);
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
+ ")");
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();
177 Collection
<ServiceReference
<WebCmsSession
>> srs
;
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
);
185 if (srs
.size() == 0) {
186 if (log
.isTraceEnabled())
187 log
.warn("No CMS web session found for http session " + httpSessionId
);
189 } else if (srs
.size() > 1)
190 throw new CmsException(srs
.size() + " CMS web sessions found for http session " + httpSessionId
);
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
);
200 private CmsAuthUtils() {