1 package org
.argeo
.cms
.auth
;
3 import static org
.argeo
.api
.cms
.CmsConstants
.ROLE_ADMIN
;
4 import static org
.argeo
.api
.cms
.CmsConstants
.ROLE_ANONYMOUS
;
5 import static org
.argeo
.api
.cms
.CmsConstants
.ROLE_USER
;
6 import static org
.argeo
.api
.cms
.CmsConstants
.ROLE_USER_ADMIN
;
8 import java
.security
.Principal
;
9 import java
.util
.Arrays
;
10 import java
.util
.Collections
;
11 import java
.util
.List
;
12 import java
.util
.Locale
;
14 import java
.util
.UUID
;
16 //import javax.naming.InvalidNameException;
17 //import javax.naming.ldap.LdapName;
18 import javax
.security
.auth
.Subject
;
19 import javax
.security
.auth
.x500
.X500Principal
;
21 import org
.argeo
.api
.cms
.AnonymousPrincipal
;
22 import org
.argeo
.api
.cms
.CmsConstants
;
23 import org
.argeo
.api
.cms
.CmsSession
;
24 import org
.argeo
.api
.cms
.CmsSessionId
;
25 import org
.argeo
.api
.cms
.DataAdminPrincipal
;
26 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
27 import org
.argeo
.cms
.internal
.auth
.ImpliedByPrincipal
;
28 import org
.argeo
.cms
.internal
.auth
.RemoteCmsSessionImpl
;
29 import org
.argeo
.cms
.internal
.runtime
.CmsContextImpl
;
30 import org
.argeo
.cms
.osgi
.useradmin
.AuthenticatingUser
;
31 import org
.osgi
.service
.useradmin
.Authorization
;
33 /** Centralises security related registrations. */
36 final static String SHARED_STATE_NAME
= AuthenticatingUser
.SHARED_STATE_NAME
;
37 final static String SHARED_STATE_PWD
= AuthenticatingUser
.SHARED_STATE_PWD
;
38 // final static String HEADER_AUTHORIZATION = "Authorization";
39 // final static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
42 final static String SHARED_STATE_HTTP_REQUEST
= "org.argeo.cms.auth.http.request";
43 final static String SHARED_STATE_SPNEGO_TOKEN
= "org.argeo.cms.auth.spnegoToken";
44 final static String SHARED_STATE_SPNEGO_OUT_TOKEN
= "org.argeo.cms.auth.spnegoOutToken";
45 final static String SHARED_STATE_CERTIFICATE_CHAIN
= "org.argeo.cms.auth.certificateChain";
46 final static String SHARED_STATE_REMOTE_ADDR
= "org.argeo.cms.auth.remote.addr";
47 final static String SHARED_STATE_REMOTE_PORT
= "org.argeo.cms.auth.remote.port";
49 final static String SINGLE_USER_LOCAL_ID
= "single-user";
51 private final static List
<String
> RESERVED_ROLES
= Collections
52 .unmodifiableList(Arrays
.asList(new String
[] { ROLE_ADMIN
, ROLE_ANONYMOUS
, ROLE_USER
, ROLE_USER_ADMIN
}));
54 static void addAuthorization(Subject subject
, Authorization authorization
) {
55 assert subject
!= null;
56 checkSubjectEmpty(subject
);
57 assert authorization
!= null;
59 // required for display name:
60 subject
.getPrivateCredentials().add(authorization
);
62 boolean singleUser
= authorization
instanceof SingleUserAuthorization
;
64 Set
<Principal
> principals
= subject
.getPrincipals();
66 String authName
= authorization
.getName();
68 // determine user's principal
69 // final LdapName name;
70 final Principal userPrincipal
;
71 if (authName
== null) {
72 // name = NodeSecurityUtils.ROLE_ANONYMOUS_NAME;
73 userPrincipal
= new AnonymousPrincipal();
74 principals
.add(userPrincipal
);
76 // name = new LdapName(authName);
77 checkUserName(authName
);
78 userPrincipal
= new X500Principal(authName
.toString());
79 principals
.add(userPrincipal
);
82 principals
.add(new ImpliedByPrincipal(CmsConstants
.ROLE_ADMIN
, userPrincipal
));
83 principals
.add(new DataAdminPrincipal());
87 // Add roles provided by authorization
88 for (String role
: authorization
.getRoles()) {
89 // LdapName roleName = new LdapName(role);
90 if (role
.equals(authName
)) {
92 } else if (role
.equals(CmsConstants
.ROLE_ANONYMOUS
)) {
95 // NodeSecurityUtils.checkImpliedPrincipalName(role);
96 principals
.add(new ImpliedByPrincipal(role
, userPrincipal
));
97 if (role
.equals(CmsConstants
.ROLE_ADMIN
))
98 principals
.add(new DataAdminPrincipal());
102 // } catch (InvalidNameException e) {
103 // throw new IllegalArgumentException("Cannot commit", e);
107 private static void checkSubjectEmpty(Subject subject
) {
108 if (!subject
.getPrincipals(AnonymousPrincipal
.class).isEmpty())
109 throw new IllegalStateException("Already logged in as anonymous: " + subject
);
110 if (!subject
.getPrincipals(X500Principal
.class).isEmpty())
111 throw new IllegalStateException("Already logged in as user: " + subject
);
112 if (!subject
.getPrincipals(DataAdminPrincipal
.class).isEmpty())
113 throw new IllegalStateException("Already logged in as data admin: " + subject
);
114 if (!subject
.getPrincipals(ImpliedByPrincipal
.class).isEmpty())
115 throw new IllegalStateException("Already authorized: " + subject
);
118 static void cleanUp(Subject subject
) {
120 subject
.getPrincipals().removeAll(subject
.getPrincipals(X500Principal
.class));
121 subject
.getPrincipals().removeAll(subject
.getPrincipals(ImpliedByPrincipal
.class));
122 subject
.getPrincipals().removeAll(subject
.getPrincipals(AnonymousPrincipal
.class));
123 subject
.getPrincipals().removeAll(subject
.getPrincipals(DataAdminPrincipal
.class));
125 subject
.getPrivateCredentials().removeAll(subject
.getPrivateCredentials(CmsSessionId
.class));
126 subject
.getPrivateCredentials().removeAll(subject
.getPrivateCredentials(Authorization
.class));
128 // subject.getPrincipals().removeAll(subject.getPrincipals(AdminPrincipal.class));
129 // subject.getPrincipals().removeAll(subject.getPrincipals(AnonymousPrincipal.class));
132 @SuppressWarnings("unused")
133 synchronized static void registerSessionAuthorization(RemoteAuthRequest request
, Subject subject
,
134 Authorization authorization
, Locale locale
) {
135 // synchronized in order to avoid multiple registrations
136 // TODO move it to a service in order to avoid static synchronization
137 if (request
!= null) {
138 RemoteAuthSession httpSession
= request
.getSession();
139 String httpSessId
= httpSession
!= null ? httpSession
.getId() : null;
140 boolean anonymous
= authorization
.getName() == null;
141 String remoteUser
= !anonymous ? authorization
.getName() : CmsConstants
.ROLE_ANONYMOUS
;
142 request
.setAttribute(RemoteAuthRequest
.REMOTE_USER
, remoteUser
);
143 request
.setAttribute(RemoteAuthRequest
.AUTHORIZATION
, authorization
);
145 CmsSessionImpl cmsSession
;
146 CmsSessionImpl currentLocalSession
= CmsContextImpl
.getCmsContext().getCmsSessionByLocalId(httpSessId
);
147 if (currentLocalSession
!= null) {
148 boolean currentLocalSessionAnonymous
= currentLocalSession
.isAnonymous();
150 if (currentLocalSessionAnonymous
) {
151 currentLocalSession
.close();
153 UUID cmsSessionUuid
= CmsContextImpl
.getCmsContext().getUuidFactory().timeUUID();
154 cmsSession
= new RemoteCmsSessionImpl(cmsSessionUuid
, subject
, authorization
, locale
, request
);
155 CmsContextImpl
.getCmsContext().registerCmsSession(cmsSession
);
156 } else if (!authorization
.getName().equals(currentLocalSession
.getAuthorization().getName())) {
157 throw new IllegalStateException("Inconsistent user " + authorization
.getName()
158 + " for existing CMS session " + currentLocalSession
);
160 // keep current session
161 cmsSession
= currentLocalSession
;
163 // TODO control it more??
164 subject
.getPrivateCredentials().addAll(cmsSession
.getSubject().getPrivateCredentials());
165 subject
.getPublicCredentials().addAll(cmsSession
.getSubject().getPublicCredentials());
168 if (!currentLocalSessionAnonymous
) {
169 currentLocalSession
.close();
170 throw new IllegalStateException(
171 "Existing CMS session " + currentLocalSession
+ " was not logged out properly.");
173 // keep current session
174 cmsSession
= currentLocalSession
;
178 UUID cmsSessionUuid
= CmsContextImpl
.getCmsContext().getUuidFactory().timeUUID();
179 cmsSession
= new RemoteCmsSessionImpl(cmsSessionUuid
, subject
, authorization
, locale
, request
);
180 CmsContextImpl
.getCmsContext().registerCmsSession(cmsSession
);
183 if (cmsSession
== null)// should be dead code (cf. SuppressWarning of the method)
184 throw new IllegalStateException("CMS session cannot be null");
186 CmsSessionId nodeSessionId
= new CmsSessionId(cmsSession
.getUuid());
187 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 0) {
188 subject
.getPrivateCredentials().add(nodeSessionId
);
190 UUID storedSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next().getUuid();
191 if (!storedSessionId
.equals(nodeSessionId
.getUuid()))
192 throw new IllegalStateException(
193 "Subject already logged with session " + storedSessionId
+ " (not " + nodeSessionId
+ ")");
195 request
.setAttribute(CmsSession
.class.getName(), cmsSession
);
197 CmsSessionImpl cmsSession
= CmsContextImpl
.getCmsContext().getCmsSessionByLocalId(SINGLE_USER_LOCAL_ID
);
198 if (cmsSession
== null) {
199 UUID cmsSessionUuid
= CmsContextImpl
.getCmsContext().getUuidFactory().timeUUID();
200 cmsSession
= new CmsSessionImpl(cmsSessionUuid
, subject
, authorization
, locale
, SINGLE_USER_LOCAL_ID
);
201 CmsContextImpl
.getCmsContext().registerCmsSession(cmsSession
);
203 CmsSessionId nodeSessionId
= new CmsSessionId(cmsSession
.getUuid());
204 subject
.getPrivateCredentials().add(nodeSessionId
);
208 // public static CmsSessionImpl cmsSessionFromHttpSession(BundleContext bc, String httpSessionId) {
209 // Authorization authorization = null;
210 // Collection<ServiceReference<CmsSession>> sr;
212 // sr = bc.getServiceReferences(CmsSession.class,
213 // "(" + CmsSession.SESSION_LOCAL_ID + "=" + httpSessionId + ")");
214 // } catch (InvalidSyntaxException e) {
215 // throw new IllegalArgumentException("Cannot get CMS session for id " + httpSessionId, e);
217 // CmsSessionImpl cmsSession;
218 // if (sr.size() == 1) {
219 // cmsSession = (CmsSessionImpl) bc.getService(sr.iterator().next());
220 //// locale = cmsSession.getLocale();
221 // authorization = cmsSession.getAuthorization();
222 // if (authorization.getName() == null)
223 // return null;// anonymous is not sufficient
224 // } else if (sr.size() == 0)
227 // throw new IllegalStateException(sr.size() + ">1 web sessions detected for http session " + httpSessionId);
228 // return cmsSession;
231 public static <T
extends Principal
> T
getSinglePrincipal(Subject subject
, Class
<T
> clss
) {
232 Set
<T
> principals
= subject
.getPrincipals(clss
);
233 if (principals
.isEmpty())
235 if (principals
.size() > 1)
236 throw new IllegalStateException("Only one " + clss
+ " principal expected in " + subject
);
237 return principals
.iterator().next();
240 private static void checkUserName(String name
) throws IllegalArgumentException
{
241 if (RESERVED_ROLES
.contains(name
))
242 throw new IllegalArgumentException(name
+ " is a reserved name");
245 private CmsAuthUtils() {