use of com.novell.ldap.LDAPJSSESecureSocketFactory in project ldapchai by ldapchai.
the class JLDAPProviderImpl method init.
public void init(final ChaiConfiguration chaiConfig, final ChaiProviderFactory providerFactory) throws ChaiUnavailableException, IllegalStateException {
super.init(chaiConfig, providerFactory);
try {
// grab the first URL from the list.
final URI ldapURL = URI.create(chaiConfig.bindURLsAsList().get(0));
if (ldapURL.getScheme().equalsIgnoreCase("ldaps")) {
final boolean usePromiscuousSSL = Boolean.parseBoolean(chaiConfig.getSetting(ChaiSetting.PROMISCUOUS_SSL));
if (usePromiscuousSSL) {
try {
final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new X509TrustManager[] { new PromiscuousTrustManager() }, new java.security.SecureRandom());
ldapConnection = new LDAPConnection(new LDAPJSSESecureSocketFactory(sc.getSocketFactory()));
} catch (Exception e) {
LOGGER.error("error creating promiscuous ssl ldap socket factory: " + e.getMessage());
}
} else if (chaiConfig.getTrustManager() != null) {
try {
final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, chaiConfig.getTrustManager(), new java.security.SecureRandom());
ldapConnection = new LDAPConnection(new LDAPJSSESecureSocketFactory(sc.getSocketFactory()));
} catch (Exception e) {
LOGGER.error("error creating configured ssl ldap socket factory: " + e.getMessage());
}
} else {
ldapConnection = new LDAPConnection(new LDAPJSSESecureSocketFactory());
}
} else {
ldapConnection = new LDAPConnection();
}
ldapConnection.connect(ldapURL.getHost(), ldapURL.getPort());
if (chaiConfig.getBooleanSetting(ChaiSetting.LDAP_FOLLOW_REFERRALS)) {
final LDAPConstraints ldapConstraints = new LDAPConstraints();
ldapConstraints.setReferralFollowing(true);
ldapConnection.setConstraints(ldapConstraints);
}
final String characterEncoding = chaiConfig.getSetting(ChaiSetting.LDAP_CHARACTER_ENCODING);
final byte[] bindPassword = chaiConfig.getSetting(ChaiSetting.BIND_PASSWORD).getBytes(Charset.forName(characterEncoding));
final String bindDN = chaiConfig.getSetting(ChaiSetting.BIND_DN);
ldapConnection.bind(LDAPConnection.LDAP_V3, bindDN, bindPassword);
} catch (LDAPException e) {
final String message = e.getMessage();
if (message.contains("Connect Error")) {
throw new ChaiUnavailableException(message, ChaiError.COMMUNICATION, false, false);
}
throw ChaiUnavailableException.forErrorMessage(message);
}
}
Aggregations