Path: blob/master/test/jdk/com/sun/security/auth/module/LdapLoginModule/CheckConfigs.java
51748 views
/*1* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @author Vincent Ryan26* @bug 481452227* @summary Check that an LdapLoginModule can be initialized using various28* JAAS configurations.29* (LdapLoginModule replaces the JndiLoginModule for LDAP access)30*31* Run this test twice, once using the default security manager:32*33* @run main/othervm CheckConfigs34* @run main/othervm/policy=CheckConfigs.policy CheckConfigs35*/3637import java.io.IOException;38import java.util.Collections;39import java.util.Map;40import java.util.HashMap;4142import javax.naming.CommunicationException;43import javax.security.auth.*;44import javax.security.auth.login.*;45import javax.security.auth.callback.*;46import com.sun.security.auth.module.LdapLoginModule;4748public class CheckConfigs {4950public static void main(String[] args) throws Exception {51SecurityManager securityManager = System.getSecurityManager();52System.out.println(securityManager == null53? "[security manager is not running]"54: "[security manager is running: " +55securityManager.getClass().getName() + "]");56init();57checkConfigModes();58}5960private static void init() throws Exception {61}6263private static void checkConfigModes() throws Exception {6465LoginContext ldapLogin;6667// search-first mode68System.out.println("Testing search-first mode...");69try {70ldapLogin = new LoginContext(LdapConfiguration.LOGIN_CONFIG_NAME,71null, new TestCallbackHandler(), new SearchFirstMode());72ldapLogin.login();73throw new SecurityException("expected a LoginException");7475} catch (LoginException le) {76// expected behaviour (because no LDAP server is available)77if (!(le.getCause() instanceof CommunicationException)) {78throw le;79}80}8182// authentication-first mode83System.out.println("\nTesting authentication-first mode...");84try {85ldapLogin = new LoginContext(LdapConfiguration.LOGIN_CONFIG_NAME,86null, new TestCallbackHandler(), new AuthFirstMode());87ldapLogin.login();88throw new SecurityException("expected a LoginException");8990} catch (LoginException le) {91// expected behaviour (because no LDAP server is available)92if (!(le.getCause() instanceof CommunicationException)) {93throw le;94}95}9697// authentication-only mode98System.out.println("\nTesting authentication-only mode...");99try {100ldapLogin = new LoginContext(LdapConfiguration.LOGIN_CONFIG_NAME,101null, new TestCallbackHandler(), new AuthOnlyMode());102ldapLogin.login();103throw new SecurityException("expected a LoginException");104105} catch (LoginException le) {106// expected behaviour (because no LDAP server is available)107if (!(le.getCause() instanceof CommunicationException)) {108throw le;109}110}111}112113private static class TestCallbackHandler implements CallbackHandler {114115public void handle(Callback[] callbacks)116throws IOException, UnsupportedCallbackException {117118for (int i = 0; i < callbacks.length; i++) {119if (callbacks[i] instanceof NameCallback) {120((NameCallback)callbacks[i]).setName("myname");121122} else if (callbacks[i] instanceof PasswordCallback) {123((PasswordCallback)callbacks[i])124.setPassword("mypassword".toCharArray());125126} else {127throw new UnsupportedCallbackException128(callbacks[i], "Unrecognized callback");129}130}131}132}133}134135class LdapConfiguration extends Configuration {136137// The JAAS configuration name for ldap-based authentication138public static final String LOGIN_CONFIG_NAME = "TestAuth";139140// The JAAS configuration for ldap-based authentication141protected static AppConfigurationEntry[] entries;142143// The classname of the login module for ldap-based authentication144protected static final String LDAP_LOGIN_MODULE =145LdapLoginModule.class.getName();146147/**148* Gets the JAAS configuration for ldap-based authentication149*/150public AppConfigurationEntry[] getAppConfigurationEntry(String name) {151return name.equals(LOGIN_CONFIG_NAME) ? entries : null;152}153154/**155* Refreshes the configuration.156*/157public void refresh() {158// the configuration is fixed159}160}161162/**163* This class defines the JAAS configuration for ldap-based authentication.164* It is equivalent to the following textual configuration entry:165* <pre>166* TestAuth {167* com.sun.security.auth.module.LdapLoginModule REQUIRED168* userProvider="ldap://localhost:23456/dc=example,dc=com"169* userFilter="(&(uid={USERNAME})(objectClass=inetOrgPerson))"170* authzIdentity="{EMPLOYEENUMBER}"171* debug=true;172* };173* </pre>174*/175class SearchFirstMode extends LdapConfiguration {176177public SearchFirstMode() {178super();179180Map<String, String> options = new HashMap<>(4);181options.put("userProvider", "ldap://localhost:23456/dc=example,dc=com");182options.put("userFilter",183"(&(uid={USERNAME})(objectClass=inetOrgPerson))");184options.put("authzIdentity", "{EMPLOYEENUMBER}");185options.put("debug", "true");186187entries = new AppConfigurationEntry[] {188new AppConfigurationEntry(LDAP_LOGIN_MODULE,189AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,190options)191};192}193194}195196/**197* This class defines the JAAS configuration for ldap-based authentication.198* It is equivalent to the following textual configuration entry:199* <pre>200* TestAuth {201* com.sun.security.auth.module.LdapLoginModule REQUIRED202* userProvider="ldap://localhost:23456/dc=example,dc=com"203* authIdentity="{USERNAME}"204* userFilter="(&(|(samAccountName={USERNAME})(userPrincipalName={USERNAME})(cn={USERNAME}))(objectClass=user))"205* useSSL=false206* debug=true;207* };208* </pre>209*/210class AuthFirstMode extends LdapConfiguration {211212public AuthFirstMode() {213super();214215Map<String, String> options = new HashMap<>(5);216options.put("userProvider", "ldap://localhost:23456/dc=example,dc=com");217options.put("authIdentity", "{USERNAME}");218options.put("userFilter",219"(&(|(samAccountName={USERNAME})(userPrincipalName={USERNAME})" +220"(cn={USERNAME}))(objectClass=user))");221options.put("useSSL", "false");222options.put("debug", "true");223224entries = new AppConfigurationEntry[] {225new AppConfigurationEntry(LDAP_LOGIN_MODULE,226AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,227options)228};229}230}231232/**233* This class defines the JAAS configuration for ldap-based authentication.234* It is equivalent to the following textual configuration entry:235* <pre>236* TestAuth {237* com.sun.security.auth.module.LdapLoginModule REQUIRED238* userProvider="ldap://localhost:23456 ldap://localhost:23457"239* authIdentity="cn={USERNAME},ou=people,dc=example,dc=com"240* authzIdentity="staff"241* debug=true;242* };243* </pre>244*/245class AuthOnlyMode extends LdapConfiguration {246247public AuthOnlyMode() {248super();249250Map<String, String> options = new HashMap<>(4);251options.put("userProvider",252"ldap://localhost:23456 ldap://localhost:23457");253options.put("authIdentity",254"cn={USERNAME},ou=people,dc=example,dc=com");255options.put("authzIdentity", "staff");256options.put("debug", "true");257258entries = new AppConfigurationEntry[] {259new AppConfigurationEntry(LDAP_LOGIN_MODULE,260AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,261options)262};263}264265}266267268