/** * This file Copyright (c) 2010 Magnolia International * Ltd. (http://www.magnolia-cms.com). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia-cms.com/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.jaas.sp.ldap; import info.magnolia.cms.core.SystemProperty; import info.magnolia.cms.security.AccessDeniedException; import info.magnolia.cms.security.DelegatingUserManager; import info.magnolia.cms.security.ExternalUserManager; import info.magnolia.cms.security.Group; import info.magnolia.cms.security.GroupManager; import info.magnolia.cms.security.Role; import info.magnolia.cms.security.RoleManager; import info.magnolia.cms.security.SecuritySupport; import info.magnolia.cms.security.User; import info.magnolia.cms.security.UserManager; import info.magnolia.cms.security.auth.callback.CredentialsCallbackHandler; import info.magnolia.cms.security.auth.login.LoginResult; import info.magnolia.jaas.sp.UserAwareLoginModule; import javax.security.auth.Subject; import javax.security.auth.spi.LoginModule; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * * @author gjoseph * @version $Revision: $ ($Author: $) */ public class Tester { public static void main(String[] args) throws Exception { if (args.length != 4) { System.err.println("Usage: " + Tester.class.getName() + " "); System.exit(-1); } test(args[0], args[1], args[2], args[3]); } public static void test(String authModuleClass, String configFile, String username, String password) throws Exception { // setup some Magnolia black magic ... SystemProperty.setProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR, "/tmp"); SystemProperty.setProperty(SecuritySupport.class.getName(), DummySecuritySupport.class.getName()); SystemProperty.setProperty(LDAPAuthenticationModule.JNDI_CONFIG_FILE, configFile); final Class lm = (Class) Class.forName(authModuleClass); final Subject subject = new Subject(); final HashMap state = new HashMap(); final Map options = new HashMap(); // if we do this, CONFIG_BASE_PATH is prepended; options.put(LDAPAuthenticationModule.JNDI_CONFIG_FILE, configFile); final CredentialsCallbackHandler credentialsCallbackHandler = new CredentialsCallbackHandler(username, password.toCharArray()); final LoginModule loginModule = lm.newInstance(); loginModule.initialize(subject, credentialsCallbackHandler, state, options); final boolean lres = loginModule.login(); final boolean cres = loginModule.commit(); System.out.println("Login result: " + lres); System.out.println("Commit result: " + cres); System.out.println("Subject: " + subject); System.out.println("User: " + ((UserAwareLoginModule) loginModule).getUser()); System.out.println("State: " + state); final Set groupNames = (Set) state.get("groupNames"); System.out.println(" * group names: "); for (String groupName : groupNames) { System.out.println(" " + groupName); } final Set roleNames = (Set) state.get("roleNames"); System.out.println(" * role names: "); for (String roleName : roleNames) { System.out.println(" " + roleName); } } public final static class DummySecuritySupport implements SecuritySupport { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DummySecuritySupport.class); public UserManager getUserManager() { return new DummyUserManager(null); } public UserManager getUserManager(String realmName) { return new DummyUserManager(realmName); } public GroupManager getGroupManager() { return new GroupManager() { public Group createGroup(String name) throws UnsupportedOperationException, AccessDeniedException { throw new UnsupportedOperationException(); } public Group getGroup(String name) throws UnsupportedOperationException, AccessDeniedException { log.warn("Getting group {}", name); return null; } public Collection getAllGroups() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } }; } public RoleManager getRoleManager() { return new RoleManager() { public Role createRole(String name) throws UnsupportedOperationException, Exception { throw new UnsupportedOperationException(); } public Role getRole(String name) throws UnsupportedOperationException { log.warn("Getting role {}", name); return null; } }; } public LoginResult authenticate(CredentialsCallbackHandler callbackHandler, String jaasModuleName) { throw new UnsupportedOperationException(); } private static class DummyUserManager implements UserManager { private final String realmName; private DummyUserManager(String realmName) { this.realmName = realmName; } public User getUser(String name) throws UnsupportedOperationException { log.warn("Getting user {} from realm {}", name, realmName); return null; } public User getUser(Subject subject) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } public User getSystemUser() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } public User getAnonymousUser() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } public Collection getAllUsers() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } public User createUser(String name, String pw) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } public void changePassword(User user, String newPassword) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } } }