1 package org
.argeo
.cms
.auth
;
3 import java
.security
.Principal
;
4 import java
.util
.Collection
;
5 import java
.util
.Locale
;
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 import javax
.servlet
.http
.HttpServletRequest
;
14 import javax
.servlet
.http
.HttpSession
;
16 import org
.argeo
.api
.NodeConstants
;
17 import org
.argeo
.api
.security
.AnonymousPrincipal
;
18 import org
.argeo
.api
.security
.DataAdminPrincipal
;
19 import org
.argeo
.api
.security
.NodeSecurityUtils
;
20 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
21 import org
.argeo
.cms
.internal
.auth
.ImpliedByPrincipal
;
22 import org
.argeo
.cms
.internal
.http
.WebCmsSessionImpl
;
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
;
30 /** Centralises security related registrations. */
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";
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";
46 static void addAuthorization(Subject subject
, Authorization authorization
) {
47 assert subject
!= null;
48 checkSubjectEmpty(subject
);
49 assert authorization
!= null;
51 // required for display name:
52 subject
.getPrivateCredentials().add(authorization
);
54 boolean singleUser
= authorization
instanceof SingleUserAuthorization
;
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
);
68 name
= new LdapName(authName
);
69 NodeSecurityUtils
.checkUserName(name
);
70 userPrincipal
= new X500Principal(name
.toString());
71 principals
.add(userPrincipal
);
74 principals
.add(new ImpliedByPrincipal(NodeSecurityUtils
.ROLE_ADMIN_NAME
, userPrincipal
));
75 principals
.add(new DataAdminPrincipal());
79 // Add roles provided by authorization
80 for (String role
: authorization
.getRoles()) {
81 LdapName roleName
= new LdapName(role
);
82 if (roleName
.equals(name
)) {
84 } else if (roleName
.equals(NodeSecurityUtils
.ROLE_ANONYMOUS_NAME
)) {
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());
94 } catch (InvalidNameException e
) {
95 throw new IllegalArgumentException("Cannot commit", e
);
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
);
110 static void cleanUp(Subject subject
) {
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));
117 subject
.getPrivateCredentials().removeAll(subject
.getPrivateCredentials(CmsSessionId
.class));
118 subject
.getPrivateCredentials().removeAll(subject
.getPrivateCredentials(Authorization
.class));
120 // subject.getPrincipals().removeAll(subject.getPrincipals(AdminPrincipal.class));
121 // subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
124 @SuppressWarnings("unused")
125 synchronized static void registerSessionAuthorization(HttpServletRequest 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 HttpSession httpSession
= request
.getSession(false);
131 assert httpSession
!= null;
132 String httpSessId
= httpSession
.getId();
133 boolean anonymous
= authorization
.getName() == null;
134 String remoteUser
= !anonymous ? authorization
.getName() : NodeConstants
.ROLE_ANONYMOUS
;
135 request
.setAttribute(HttpContext
.REMOTE_USER
, remoteUser
);
136 request
.setAttribute(HttpContext
.AUTHORIZATION
, authorization
);
138 CmsSessionImpl cmsSession
;
139 CmsSessionImpl currentLocalSession
= CmsSessionImpl
.getByLocalId(httpSessId
);
140 if (currentLocalSession
!= null) {
141 boolean currentLocalSessionAnonymous
= currentLocalSession
.getAuthorization().getName() == null;
143 if (currentLocalSessionAnonymous
) {
144 currentLocalSession
.close();
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
);
151 // keep current session
152 cmsSession
= currentLocalSession
;
154 subject
.getPrivateCredentials().addAll(cmsSession
.getSecretKeys());
157 if (!currentLocalSessionAnonymous
) {
158 currentLocalSession
.close();
159 throw new IllegalStateException(
160 "Existing CMS session " + currentLocalSession
+ " was not logged out properly.");
162 // keep current session
163 cmsSession
= currentLocalSession
;
167 cmsSession
= new WebCmsSessionImpl(subject
, authorization
, locale
, request
);
170 if (cmsSession
== null)// should be dead code (cf. SuppressWarning of the method)
171 throw new IllegalStateException("CMS session cannot be null");
173 CmsSessionId nodeSessionId
= new CmsSessionId(cmsSession
.getUuid());
174 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 0) {
175 subject
.getPrivateCredentials().add(nodeSessionId
);
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
+ ")");
183 CmsSessionImpl cmsSession
= new CmsSessionImpl(subject
, authorization
, locale
, "desktop");
184 CmsSessionId nodeSessionId
= new CmsSessionId(cmsSession
.getUuid());
185 subject
.getPrivateCredentials().add(nodeSessionId
);
189 public static CmsSession
cmsSessionFromHttpSession(BundleContext bc
, String httpSessionId
) {
190 Authorization authorization
= null;
191 Collection
<ServiceReference
<CmsSession
>> sr
;
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
);
198 CmsSession cmsSession
;
199 if (sr
.size() == 1) {
200 cmsSession
= 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)
208 throw new IllegalStateException(sr
.size() + ">1 web sessions detected for http session " + httpSessionId
);
212 public static <T
extends Principal
> T
getSinglePrincipal(Subject subject
, Class
<T
> clss
) {
213 Set
<T
> principals
= subject
.getPrincipals(clss
);
214 if (principals
.isEmpty())
216 if (principals
.size() > 1)
217 throw new IllegalStateException("Only one " + clss
+ " principal expected in " + subject
);
218 return principals
.iterator().next();
221 private CmsAuthUtils() {