LdapAuthenticator extends Authenticator { public boolean authenticate(String login, String password) { for (String ldapKey : userMappings.keySet()) { final String principal; if (contextFactories.get(ldapKey).isSasl()) { principal = login; } else { final SearchResult result; try { result = userMappings.get(ldapKey).createSearch(contextFactories.get(ldapKey), login).findUnique(); } catch (NamingException e) { LOG.debug("User {} not found in server {}: {}", login, ldapKey, e.getMessage()); continue; } if (result == null) { LOG.debug("User {} not found in {}", login, ldapKey); continue; } principal = result.getNameInNamespace(); } boolean passwordValid; if (contextFactories.get(ldapKey).isGssapi()) { passwordValid = checkPasswordUsingGssapi(principal, password, ldapKey); } else { passwordValid = checkPasswordUsingBind(principal, password, ldapKey); } if (passwordValid) { return true; } } LOG.debug("User {} not found", login); return false; } LdapAuthenticator(Map<String, LdapContextFactory> contextFactories, Map<String, LdapUserMapping> userMappings); @Override boolean doAuthenticate(Context context); boolean authenticate(String login, String password);  }
@Test public void testNoConnection() { exampleServer.disableAnonymousAccess(); try { LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_SIMPLE), new LdapAutodiscovery()); LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings()); authenticator.authenticate("godin", "secret1"); } finally { exampleServer.enableAnonymousAccess(); } }
@Test public void testSimple() { LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_SIMPLE), new LdapAutodiscovery()); LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings()); assertThat(authenticator.authenticate("godin", "secret1")).isTrue(); assertThat(authenticator.authenticate("godin", "wrong")).isFalse(); assertThat(authenticator.authenticate("tester", "secret2")).isTrue(); assertThat(authenticator.authenticate("tester", "wrong")).isFalse(); assertThat(authenticator.authenticate("notfound", "wrong")).isFalse(); assertThat(authenticator.authenticate("godin", "")).isFalse(); assertThat(authenticator.authenticate("godin", null)).isFalse(); }
@Test public void testSimpleMultiLdap() { LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, infosupportServer, LdapContextFactory.AUTH_METHOD_SIMPLE), new LdapAutodiscovery()); LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings()); assertThat(authenticator.authenticate("godin", "secret1")).isTrue(); assertThat(authenticator.authenticate("godin", "wrong")).isFalse(); assertThat(authenticator.authenticate("tester", "secret2")).isTrue(); assertThat(authenticator.authenticate("tester", "wrong")).isFalse(); assertThat(authenticator.authenticate("notfound", "wrong")).isFalse(); assertThat(authenticator.authenticate("godin", "")).isFalse(); assertThat(authenticator.authenticate("godin", null)).isFalse(); assertThat(authenticator.authenticate("robby", "secret1")).isTrue(); assertThat(authenticator.authenticate("robby", "wrong")).isFalse(); }
@Test public void testSasl() { LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_CRAM_MD5), new LdapAutodiscovery()); LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings()); assertThat(authenticator.authenticate("godin", "secret1")).isTrue(); assertThat(authenticator.authenticate("godin", "wrong")).isFalse(); assertThat(authenticator.authenticate("tester", "secret2")).isTrue(); assertThat(authenticator.authenticate("tester", "wrong")).isFalse(); assertThat(authenticator.authenticate("notfound", "wrong")).isFalse(); }
@Test public void testSaslMultipleLdap() { LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, infosupportServer, LdapContextFactory.AUTH_METHOD_CRAM_MD5), new LdapAutodiscovery()); LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings()); assertThat(authenticator.authenticate("godin", "secret1")).isTrue(); assertThat(authenticator.authenticate("godin", "wrong")).isFalse(); assertThat(authenticator.authenticate("tester", "secret2")).isTrue(); assertThat(authenticator.authenticate("tester", "wrong")).isFalse(); assertThat(authenticator.authenticate("notfound", "wrong")).isFalse(); assertThat(authenticator.authenticate("robby", "secret1")).isTrue(); assertThat(authenticator.authenticate("robby", "wrong")).isFalse(); }