X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fauth%2FSingleUserLoginModule.java;h=0b163bac3fb9c12c98f5f85c8a2d36dde0e9abb2;hb=11c9710b1d2456c8304a5841d775af008a794431;hp=6147fc77aa5cead990718870d389576cce77d4a9;hpb=d039711d38e91f8d419e784f9b88f3a86bfc8538;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/auth/SingleUserLoginModule.java b/org.argeo.cms/src/org/argeo/cms/auth/SingleUserLoginModule.java index 6147fc77a..0b163bac3 100644 --- a/org.argeo.cms/src/org/argeo/cms/auth/SingleUserLoginModule.java +++ b/org.argeo.cms/src/org/argeo/cms/auth/SingleUserLoginModule.java @@ -1,41 +1,89 @@ package org.argeo.cms.auth; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.security.Principal; +import java.util.Locale; import java.util.Map; import java.util.Set; +import javax.naming.ldap.LdapName; import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.kerberos.KerberosPrincipal; import javax.security.auth.login.LoginException; import javax.security.auth.spi.LoginModule; import javax.security.auth.x500.X500Principal; +import javax.servlet.http.HttpServletRequest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.api.NodeConstants; +import org.argeo.api.security.DataAdminPrincipal; import org.argeo.cms.internal.auth.ImpliedByPrincipal; -import org.argeo.node.NodeConstants; -import org.argeo.node.security.DataAdminPrincipal; +import org.argeo.naming.LdapAttrs; +import org.argeo.osgi.useradmin.IpaUtils; +import org.osgi.service.useradmin.Authorization; + +/** Login module for when the system is owned by a single user. */ +public class SingleUserLoginModule implements LoginModule { + private final static Log log = LogFactory.getLog(SingleUserLoginModule.class); -public class SingleUserLoginModule implements LoginModule, AuthConstants { private Subject subject; + private Map sharedState = null; + @SuppressWarnings("unchecked") @Override public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { this.subject = subject; + this.sharedState = (Map) sharedState; } @Override public boolean login() throws LoginException { + String username = System.getProperty("user.name"); + if (!sharedState.containsKey(CmsAuthUtils.SHARED_STATE_NAME)) + sharedState.put(CmsAuthUtils.SHARED_STATE_NAME, username); return true; } @Override public boolean commit() throws LoginException { - String username = System.getProperty("user.name"); - X500Principal principal = new X500Principal("uid=" + username + ",dc=localhost,dc=localdomain"); + X500Principal principal; + KerberosPrincipal kerberosPrincipal = CmsAuthUtils.getSinglePrincipal(subject, KerberosPrincipal.class); + if (kerberosPrincipal != null) { + LdapName userDn = IpaUtils.kerberosToDn(kerberosPrincipal.getName()); + principal = new X500Principal(userDn.toString()); + } else { + Object username = sharedState.get(CmsAuthUtils.SHARED_STATE_NAME); + if (username == null) + throw new LoginException("No username available"); + String hostname; + try { + hostname = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + log.warn("Using localhost as hostname", e); + hostname = "localhost"; + } + String baseDn = ("." + hostname).replaceAll("\\.", ",dc="); + principal = new X500Principal(LdapAttrs.uid + "=" + username + baseDn); + } Set principals = subject.getPrincipals(); principals.add(principal); principals.add(new ImpliedByPrincipal(NodeConstants.ROLE_ADMIN, principal)); principals.add(new DataAdminPrincipal()); + + HttpServletRequest request = (HttpServletRequest) sharedState.get(CmsAuthUtils.SHARED_STATE_HTTP_REQUEST); + Locale locale = Locale.getDefault(); + if (request != null) + locale = request.getLocale(); + if (locale == null) + locale = Locale.getDefault(); + Authorization authorization = new SingleUserAuthorization(); + CmsAuthUtils.addAuthorization(subject, authorization); + CmsAuthUtils.registerSessionAuthorization(request, subject, authorization, locale); + return true; } @@ -46,7 +94,7 @@ public class SingleUserLoginModule implements LoginModule, AuthConstants { @Override public boolean logout() throws LoginException { - // TODO Auto-generated method stub + CmsAuthUtils.cleanUp(subject); return true; }