- // SHARED STATE KEYS
- // compatible with com.sun.security.auth.module.*LoginModule
- // public static final String SHARED_STATE_USERNAME =
- // "javax.security.auth.login.name";
- // public static final String SHARED_STATE_PASSWORD =
- // "javax.security.auth.login.password";
-
- static void registerSessionAuthorization(BundleContext bc, HttpServletRequest request, Subject subject,
- Authorization authorization) {
- String httpSessId = request.getSession().getId();
- if (authorization.getName() != null) {
- request.setAttribute(HttpContext.REMOTE_USER, authorization.getName());
- request.setAttribute(HttpContext.AUTHORIZATION, authorization);
-
- HttpSession httpSession = request.getSession();
- if (httpSession.getAttribute(HttpContext.AUTHORIZATION) == null) {
-
- Collection<ServiceReference<WebCmsSession>> sr;
- try {
- sr = bc.getServiceReferences(WebCmsSession.class,
- "(" + WebCmsSession.CMS_SESSION_ID + "=" + httpSessId + ")");
- } catch (InvalidSyntaxException e) {
- throw new CmsException("Cannot get CMS session for id " + httpSessId, e);
+ @SuppressWarnings("unused")
+ synchronized static void registerSessionAuthorization(RemoteAuthRequest request, Subject subject,
+ Authorization authorization, Locale locale) {
+ // synchronized in order to avoid multiple registrations
+ // TODO move it to a service in order to avoid static synchronization
+ if (request != null) {
+ RemoteAuthSession httpSession = request.getSession();
+ assert httpSession != null;
+ String httpSessId = httpSession.getId();
+ boolean anonymous = authorization.getName() == null;
+ String remoteUser = !anonymous ? authorization.getName() : CmsConstants.ROLE_ANONYMOUS;
+ request.setAttribute(RemoteAuthRequest.REMOTE_USER, remoteUser);
+ request.setAttribute(RemoteAuthRequest.AUTHORIZATION, authorization);
+
+ CmsSessionImpl cmsSession;
+ CmsSessionImpl currentLocalSession = CmsContextImpl.getCmsContext().getCmsSessionByLocalId(httpSessId);
+ if (currentLocalSession != null) {
+ boolean currentLocalSessionAnonymous = currentLocalSession.getAuthorization().getName() == null;
+ if (!anonymous) {
+ if (currentLocalSessionAnonymous) {
+ currentLocalSession.close();
+ // new CMS session
+ UUID cmsSessionUuid = CmsContextImpl.getCmsContext().getUuidFactory().timeUUID();
+ cmsSession = new RemoteCmsSessionImpl(cmsSessionUuid, subject, authorization, locale, request);
+ CmsContextImpl.getCmsContext().registerCmsSession(cmsSession);
+ } else if (!authorization.getName().equals(currentLocalSession.getAuthorization().getName())) {
+ throw new IllegalStateException("Inconsistent user " + authorization.getName()
+ + " for existing CMS session " + currentLocalSession);
+ } else {
+ // keep current session
+ cmsSession = currentLocalSession;
+ // credentials
+ // TODO control it more??
+ subject.getPrivateCredentials().addAll(cmsSession.getSubject().getPrivateCredentials());
+ subject.getPublicCredentials().addAll(cmsSession.getSubject().getPublicCredentials());
+ }
+ } else {// anonymous
+ if (!currentLocalSessionAnonymous) {
+ currentLocalSession.close();
+ throw new IllegalStateException(
+ "Existing CMS session " + currentLocalSession + " was not logged out properly.");
+ }
+ // keep current session
+ cmsSession = currentLocalSession;