import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.security.auth.Subject;
-import javax.security.auth.login.CredentialNotFoundException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER,
new HttpRequestCallbackHandler(UiContext.getHttpRequest()));
loginContext.login();
- } catch (CredentialNotFoundException e) {
+ } catch (LoginException e) {
try {
loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER);
loginContext.login();
} catch (LoginException e1) {
throw new CmsException("Cannot log in as anonymous", e1);
}
- } catch (LoginException e) {
- throw new CmsException("Cannot initialize subject", e);
}
authChange(loginContext);
anonymousLc.login();
authChange(anonymousLc);
} catch (LoginException e) {
- throw new CmsException("Cannot logout", e);
+ log.error("Cannot logout", e);
}
}
package org.argeo.cms.auth;
import java.security.Principal;
+import java.util.Collection;
import java.util.Set;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.security.auth.Subject;
import javax.security.auth.x500.X500Principal;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
//import org.apache.jackrabbit.core.security.AnonymousPrincipal;
//import org.apache.jackrabbit.core.security.SecurityConstants;
//import org.apache.jackrabbit.core.security.principal.AdminPrincipal;
import org.argeo.cms.CmsException;
import org.argeo.cms.internal.auth.ImpliedByPrincipal;
+import org.argeo.cms.internal.kernel.WebCmsSessionImpl;
import org.argeo.node.security.AnonymousPrincipal;
import org.argeo.node.security.DataAdminPrincipal;
import org.argeo.node.security.NodeSecurityUtils;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.HttpContext;
import org.osgi.service.useradmin.Authorization;
class CmsAuthUtils {
+ private final static Log log = LogFactory.getLog(CmsAuthUtils.class);
+
+ /** Shared HTTP request */
+ static final String SHARED_STATE_HTTP_REQUEST = "org.argeo.cms.auth.http.request";
/** From org.osgi.service.http.HttpContext */
static final String SHARED_STATE_AUTHORIZATION = "org.osgi.service.useradmin.authorization";
/** From com.sun.security.auth.module.*LoginModule */
// 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);
+ }
+ ServiceReference<WebCmsSession> cmsSessionRef;
+ if (sr.size() == 1) {
+ cmsSessionRef = sr.iterator().next();
+ } else if (sr.size() == 0) {
+ WebCmsSessionImpl cmsSessionImpl = new WebCmsSessionImpl(httpSessId, authorization);
+ cmsSessionRef = cmsSessionImpl.getServiceRegistration().getReference();
+ if (log.isDebugEnabled())
+ log.debug("Initialized " + cmsSessionImpl + " for " + authorization.getName());
+ } else
+ throw new CmsException(sr.size() + " CMS sessions registered for " + httpSessId);
+
+ WebCmsSessionImpl cmsSession = (WebCmsSessionImpl) bc.getService(cmsSessionRef);
+ cmsSession.addHttpSession(request);
+ if (log.isTraceEnabled())
+ log.trace("Added " + request.getServletPath() + " to " + cmsSession + " (" + request.getRequestURI()
+ + ")");
+ // httpSession.setAttribute(HttpContext.REMOTE_USER,
+ // authorization.getName());
+ // httpSession.setAttribute(HttpContext.AUTHORIZATION,
+ // authorization);
+ }
+ }
+ HttpSessionId httpSessionId = new HttpSessionId(httpSessId);
+ if (subject.getPrivateCredentials(HttpSessionId.class).size() == 0)
+ subject.getPrivateCredentials().add(httpSessionId);
+ else {
+ String storedSessionId = subject.getPrivateCredentials(HttpSessionId.class).iterator().next().getValue();
+ // if (storedSessionId.equals(httpSessionId.getValue()))
+ throw new CmsException(
+ "Subject already logged with session " + storedSessionId + " (not " + httpSessionId + ")");
+ }
+ }
+
+ static boolean logoutSession(BundleContext bc, Subject subject) {
+ String httpSessionId;
+ if (subject.getPrivateCredentials(HttpSessionId.class).size() == 1)
+ httpSessionId = subject.getPrivateCredentials(HttpSessionId.class).iterator().next().getValue();
+ else
+ return false;
+ Collection<ServiceReference<WebCmsSession>> srs;
+ try {
+ srs = bc.getServiceReferences(WebCmsSession.class,
+ "(" + WebCmsSession.CMS_SESSION_ID + "=" + httpSessionId + ")");
+ } catch (InvalidSyntaxException e) {
+ throw new CmsException("Cannot retrieve CMS session #" + httpSessionId, e);
+ }
+
+ if (srs.size() == 0) {
+ if (log.isTraceEnabled())
+ log.warn("No CMS web session found for http session " + httpSessionId);
+ return false;
+ } else if (srs.size() > 1)
+ throw new CmsException(srs.size() + " CMS web sessions found for http session " + httpSessionId);
+
+ WebCmsSessionImpl cmsSession = (WebCmsSessionImpl) bc.getService(srs.iterator().next());
+ cmsSession.cleanUp();
+ subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(HttpSessionId.class));
+ if (log.isDebugEnabled())
+ log.debug("Cleaned up " + cmsSession);
+ return true;
+ }
+
private CmsAuthUtils() {
}
+
}
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
throw new CmsException(sr.size() + ">1 web sessions detected for http session " + httpSessionId);
}
+ sharedState.put(CmsAuthUtils.SHARED_STATE_HTTP_REQUEST, request);
if (authorization == null)
return false;
sharedState.put(CmsAuthUtils.SHARED_STATE_AUTHORIZATION, authorization);
}
if (request == null)
return false;
- HttpSessionId httpSessionId = registerAuthorization(request, authorizationToRegister);
- if (subject.getPrivateCredentials(HttpSessionId.class).size() == 0)
- subject.getPrivateCredentials().add(httpSessionId);
- else {
- String storedSessionId = subject.getPrivateCredentials(HttpSessionId.class).iterator().next().getValue();
- // if (storedSessionId.equals(httpSessionId.getValue()))
- throw new LoginException(
- "Subject already logged with session " + storedSessionId + " (not " + httpSessionId + ")");
- }
+ CmsAuthUtils.registerSessionAuthorization(bc, request, subject, authorizationToRegister);
if (authorization != null) {
// CmsAuthUtils.addAuthentication(subject, authorization);
}
}
- private HttpSessionId registerAuthorization(HttpServletRequest request, Authorization authorization) {
- String httpSessionId = 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 + "=" + httpSessionId + ")");
- } catch (InvalidSyntaxException e) {
- throw new CmsException("Cannot get CMS session for id " + httpSessionId, e);
- }
- ServiceReference<WebCmsSession> cmsSessionRef;
- if (sr.size() == 1) {
- cmsSessionRef = sr.iterator().next();
- } else if (sr.size() == 0) {
- WebCmsSessionImpl cmsSessionImpl = new WebCmsSessionImpl(httpSessionId, authorization);
- cmsSessionRef = cmsSessionImpl.getServiceRegistration().getReference();
- if (log.isDebugEnabled())
- log.debug("Initialized " + cmsSessionImpl + " for " + authorization.getName());
- } else
- throw new CmsException(sr.size() + " CMS sessions registered for " + httpSessionId);
-
- WebCmsSessionImpl cmsSession = (WebCmsSessionImpl) bc.getService(cmsSessionRef);
- cmsSession.addHttpSession(request);
- if (log.isTraceEnabled())
- log.trace("Added " + request.getServletPath() + " to " + cmsSession + " (" + request.getRequestURI()
- + ")");
- // httpSession.setAttribute(HttpContext.REMOTE_USER,
- // authorization.getName());
- // httpSession.setAttribute(HttpContext.AUTHORIZATION,
- // authorization);
- }
- }
- return new HttpSessionId(httpSessionId);
- }
-
@Override
public boolean abort() throws LoginException {
cleanUp();
@Override
public boolean logout() throws LoginException {
- String httpSessionId;
- if (subject.getPrivateCredentials(HttpSessionId.class).size() == 1)
- httpSessionId = subject.getPrivateCredentials(HttpSessionId.class).iterator().next().getValue();
- else
- return false;
- Collection<ServiceReference<WebCmsSession>> srs;
- try {
- srs = bc.getServiceReferences(WebCmsSession.class,
- "(" + WebCmsSession.CMS_SESSION_ID + "=" + httpSessionId + ")");
- } catch (InvalidSyntaxException e) {
- throw new CmsException("Cannot retrieve CMS session #" + httpSessionId, e);
- }
-
- if (srs.size() == 0)
- throw new CmsException("No CMS web session found for http session " + httpSessionId);
- else if (srs.size() > 1)
- throw new CmsException(srs.size() + " CMS web sessions found for http session " + httpSessionId);
-
- WebCmsSessionImpl cmsSession = (WebCmsSessionImpl) bc.getService(srs.iterator().next());
- cmsSession.cleanUp();
- subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(HttpSessionId.class));
- if (log.isDebugEnabled())
- log.debug("Cleaned up " + cmsSession);
- return true;
+ return CmsAuthUtils.logoutSession(bc, subject);
}
}
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
+import javax.servlet.http.HttpServletRequest;
import org.argeo.cms.CmsException;
import org.argeo.naming.LdapAttrs;
public class IpaLoginModule implements LoginModule {
private BundleContext bc;
private Subject subject;
+ private Map<String, Object> sharedState = null;
+ private CallbackHandler callbackHandler;
@Override
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
Map<String, ?> options) {
this.subject = subject;
+ this.sharedState = (Map<String, Object>) sharedState;
+ this.callbackHandler = callbackHandler;
try {
bc = FrameworkUtil.getBundle(IpaLoginModule.class).getBundleContext();
assert bc != null;
Authorization authorization = null;
Set<KerberosPrincipal> kerberosPrincipals = subject.getPrincipals(KerberosPrincipal.class);
if (kerberosPrincipals.isEmpty()) {
+ if(callbackHandler!=null)
+ throw new LoginException("Cannot be anonymous if callback handler is set");
authorization = userAdmin.getAuthorization(null);
} else {
KerberosPrincipal kerberosPrincipal = kerberosPrincipals.iterator().next();
if (authorization == null)
return false;
CmsAuthUtils.addAuthentication(subject, authorization);
+ HttpServletRequest request = (HttpServletRequest) sharedState.get(CmsAuthUtils.SHARED_STATE_HTTP_REQUEST);
+ if (request != null) {
+ CmsAuthUtils.registerSessionAuthorization(bc, request, subject, authorization);
+ }
return true;
}
@Override
public boolean logout() throws LoginException {
- // TODO Auto-generated method stub
- return false;
+ return CmsAuthUtils.logoutSession(bc, subject);
}
}
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;
+import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.http.HttpService;
ConfigurationAdmin configurationAdmin = bc.getService(reference);
deployConfig = new DeployConfig(configurationAdmin, cleanState);
httpExpected = deployConfig.getProps(KernelConstants.JETTY_FACTORY_PID, "default") != null;
+ try {
+ Configuration[] configs= configurationAdmin.listConfigurations("(service.factoryPid="+NodeConstants.NODE_REPOS_FACTORY_PID+")");
+ for(Configuration config:configs){
+ Object cn = config.getProperties().get(NodeConstants.CN);
+ log.debug("Standalone repo cn: "+cn);
+ }
+ } catch (Exception e) {
+ throw new CmsException("Cannot initialize config", e);
+ }
return super.addingService(reference);
}
}.open();
USER {
- com.sun.security.auth.module.Krb5LoginModule required clearPass=true;
+ org.argeo.cms.auth.HttpSessionLoginModule sufficient;
+ com.sun.security.auth.module.Krb5LoginModule optional clearPass=true;
org.argeo.cms.auth.IpaLoginModule requisite;
};