LdapBasicAuthenticator implements Authenticator<BasicCredentials, User> { Hashtable<String, String> getContextEnvironment(String sanitizedUsername, String password) { String providerUrl = settings.getProviderUrl(); if (providerUrl == null) { throw new IllegalArgumentException("providerUrl of the ldap service shouldn't be null"); } Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, LDAP_LDAP_CTX_FACTORY_NAME); env.put(Context.PROVIDER_URL, providerUrl); env.put(LDAP_CONNECT_TIMEOUT_KEY, String.valueOf(settings.getConnectingTimeout().toMilliseconds())); env.put(LDAP_READ_TIMEOUT_KEY, String.valueOf(settings.getReadingTimeout().toMilliseconds())); String strategy = settings.getStrategy(); if (!"".equals(strategy)) { env.put(Context.SECURITY_AUTHENTICATION, strategy); } if (providerUrl.toLowerCase().startsWith(LDAPS_URL_PREFIX)) { env.put(Context.SECURITY_PROTOCOL, SSL_PROTOCOL_VALUE); String certificateAbsolutePath = settings.getCertificateAbsolutePath(); if (certificateAbsolutePath == null || "".equals(certificateAbsolutePath)) { throw new RuntimeException("The attribute 'certificateAbsolutePath' must be set when using ldap over ssl to authenticate."); } if (!new File(certificateAbsolutePath).exists()) { throw new RuntimeException(String.format("The file specified not existing: %s", certificateAbsolutePath)); } System.setProperty(SYS_PROP_SSL_KEY_STORE, certificateAbsolutePath); System.setProperty(SYS_PROP_SSL_TRUST_STORE, certificateAbsolutePath); } env.put(Context.SECURITY_PRINCIPAL, comprisePrincipal(sanitizedUsername)); env.put(Context.SECURITY_CREDENTIALS, password); return env; } LdapBasicAuthenticator(LdapConfig settings); Optional<User> authenticate(BasicCredentials credentials);  }
@Test public void testGetContextEnvironmentNormal() { String username = "username"; String secretPhrase = "secret-phrase"; Hashtable<String, String> env = AUTHENTICATOR_FOR_UTIL_METHODS_WITHOUT_SSL.getContextEnvironment(username, secretPhrase); Assert.assertEquals("unexpected ldap context factory name", LDAP_CTX_FACTORY_NAME, env.get(Context.INITIAL_CONTEXT_FACTORY)); Assert.assertEquals("unexpected ldap service provider url", LDAP_SERVICE_PROVIDER_URL, env.get(Context.PROVIDER_URL)); Assert.assertEquals("unexpected connecting timeout value", String.valueOf(Duration.parse(CONNECTING_TIMEOUT_VALUE).toMilliseconds()), env.get(LDAP_CONNECT_TIMEOUT_KEY)); Assert.assertEquals("unexpected reading timeout value", String.valueOf(Duration.parse(READING_TIMEOUT_VALUE).toMilliseconds()), env.get(LDAP_READ_TIMEOUT_KEY)); Assert.assertEquals("unexpected username", username+USERNAME_SUFFIX, env.get(Context.SECURITY_PRINCIPAL)); Assert.assertEquals("unexpected secret credentials", secretPhrase, env.get(Context.SECURITY_CREDENTIALS)); Assert.assertEquals("unexpected strategy", STRATEGY_SIMPLE, env.get(Context.SECURITY_AUTHENTICATION)); }
@Test public void testGetContextEnvironmentBlankStrategy() { String username = "username"; String secretPhrase = "secret-phrase"; LdapBasicAuthenticator blankStrategyAuthenticator = new LdapBasicAuthenticator(getNonSSLPreSettings().setStrategy("")); String strategyMaybeBlank = blankStrategyAuthenticator.getContextEnvironment(username, secretPhrase).get(Context.SECURITY_AUTHENTICATION); Assert.assertNull("unexpected strategy", strategyMaybeBlank); }
@Test public void testGetContextEnvironmentNormalWithSSL() { String username = "username"; String secretPhrase = "secret-phrase"; Hashtable<String, String> env = AUTHENTICATOR_FOR_UTIL_METHODS_WITH_SSL.getContextEnvironment(username, secretPhrase); Assert.assertEquals("unexpected ldap context factory name", LDAP_CTX_FACTORY_NAME, env.get(Context.INITIAL_CONTEXT_FACTORY)); Assert.assertEquals("unexpected ldap service provider url", LDAP_SERVICE_PROVIDER_SSL_URL, env.get(Context.PROVIDER_URL)); Assert.assertEquals("unexpected connecting timeout value", String.valueOf(Duration.parse(CONNECTING_TIMEOUT_VALUE).toMilliseconds()), env.get(LDAP_CONNECT_TIMEOUT_KEY)); Assert.assertEquals("unexpected reading timeout value", String.valueOf(Duration.parse(READING_TIMEOUT_VALUE).toMilliseconds()), env.get(LDAP_READ_TIMEOUT_KEY)); Assert.assertEquals("unexpected username", username+USERNAME_SUFFIX, env.get(Context.SECURITY_PRINCIPAL)); Assert.assertEquals("unexpected secret credentials", secretPhrase, env.get(Context.SECURITY_CREDENTIALS)); Assert.assertEquals("unexpected strategy", STRATEGY_SIMPLE, env.get(Context.SECURITY_AUTHENTICATION)); Assert.assertEquals("unexpected key-store path", EXISTING_MOCK_FILE_PATH, System.getProperty(SYS_PROP_SSL_KEY_STORE)); Assert.assertEquals("unexpected trust-store path", EXISTING_MOCK_FILE_PATH, System.getProperty(SYS_PROP_SSL_TRUST_STORE)); }
@Test public void testGetContextEnvironmentMeaninglessCAPathSSL() { String username = "username"; String secretPhrase = "secret-phrase"; try { LdapBasicAuthenticator blankStrategyAuthenticator = new LdapBasicAuthenticator(getSSLPreSettings().setCertificateAbsolutePath(null)); blankStrategyAuthenticator.getContextEnvironment(username, secretPhrase).get(Context.SECURITY_AUTHENTICATION); } catch (Exception e) { Assert.assertEquals("unexpected exception thrown", RuntimeException.class, e.getClass()); Assert.assertEquals("unexpected exception message", "The attribute 'certificateAbsolutePath' must be set when using ldap over ssl to authenticate.", e.getMessage()); } try { LdapBasicAuthenticator blankStrategyAuthenticator = new LdapBasicAuthenticator(getSSLPreSettings().setCertificateAbsolutePath("")); blankStrategyAuthenticator.getContextEnvironment(username, secretPhrase).get(Context.SECURITY_AUTHENTICATION); } catch (Exception e) { Assert.assertEquals("unexpected exception thrown", RuntimeException.class, e.getClass()); Assert.assertEquals("unexpected exception message", "The attribute 'certificateAbsolutePath' must be set when using ldap over ssl to authenticate.", e.getMessage()); } }
@Test public void testGetContextEnvironmentUnexistingCA_SSL() { String username = "username"; String secretPhrase = "secret-phrase"; String wrongCAPath = String.format("%s/this/cannot/be/existing", System.getProperty("user.dir")); try { LdapBasicAuthenticator blankStrategyAuthenticator = new LdapBasicAuthenticator(getSSLPreSettings().setCertificateAbsolutePath(wrongCAPath)); blankStrategyAuthenticator.getContextEnvironment(username, secretPhrase).get(Context.SECURITY_AUTHENTICATION); } catch (Exception e) { Assert.assertEquals("unexpected exception thrown", RuntimeException.class, e.getClass()); Assert.assertEquals("unexpected exception message", String.format("The file specified not existing: %s", wrongCAPath), e.getMessage()); } }