use of javax.naming.ldap.InitialLdapContext in project ART-TIME by Artezio.
the class LdapClient method listDepartments.
public Set<String> listDepartments() {
InitialLdapContext ctx = null;
Filter filter = new Filter(settings.getLdapDepartmentFilter(), new Object[] {});
Set<String> departments = new HashSet<>();
try {
ctx = initializeContext();
SearchControls controls = makeSearchControls();
controls.setReturningAttributes(new String[] { settings.getLdapDepartmentFilterDepartmentAttribute() });
NamingEnumeration<SearchResult> answer = ctx.search(settings.getLdapUserContextDN(), filter.getExpression(), filter.getArgs(), controls);
while (answer.hasMore()) {
SearchResult sr = answer.next();
Attributes attrs = sr.getAttributes();
String department = parseAttribute(attrs.get(settings.getLdapDepartmentFilterDepartmentAttribute()));
departments.add(WordUtils.capitalizeFully(department, new char[] { '-', ' ' }));
}
answer.close();
} catch (NamingException ex) {
throw new RuntimeException("Error getting Departments ", ex);
} finally {
closeContext(ctx);
}
return departments;
}
use of javax.naming.ldap.InitialLdapContext in project uavstack by uavorg.
the class GUISSOLdapClient method ldapApiCheck.
// ======================================init end========================================
// ======================================ldap api begin========================================
private boolean ldapApiCheck(String loginId, String password) {
boolean result = false;
String action = "login";
LdapContext newContext = null;
try {
initLdapContext(action);
Properties actionParam = ldapParams.get(action);
// 替换参数,账号密码验证
actionParam.put(Context.SECURITY_PRINCIPAL, loginId);
actionParam.put(Context.SECURITY_CREDENTIALS, password);
// 密码验证,不报错则为验证成功
newContext = new InitialLdapContext(actionParam, null);
result = true;
loggerInfo("LDAP信息", "登陆校验", "成功", loginId);
} catch (AuthenticationException e) {
// 此异常为用户验证失败
loggerInfo("LDAP信息", "登陆校验", "失败", loginId);
} catch (Exception e1) {
loggerError("LDAP信息校验", loginId, e1);
clearLdapContext(action);
} finally {
try {
if (null != newContext) {
newContext.close();
}
} catch (NamingException e) {
loggerError("LDAP信息校验,链接关闭", loginId, e);
}
}
return result;
}
use of javax.naming.ldap.InitialLdapContext in project iaf by ibissource.
the class LoginFilter method checkUsernamePassword.
private boolean checkUsernamePassword(String username, String password, String authorizePathMode) {
String dnUser = Misc.replace(ldapAuthUserBase, "%UID%", username);
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapAuthUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, dnUser);
env.put(Context.SECURITY_CREDENTIALS, password);
DirContext ctx = null;
try {
try {
ctx = new InitialDirContext(env);
} catch (CommunicationException e) {
log.info("cannot create constructor for DirContext (" + e.getMessage() + "], will try again with dummy SocketFactory");
env.put("java.naming.ldap.factory.socket", DummySSLSocketFactory.class.getName());
ctx = new InitialLdapContext(env, null);
}
if (authorizePathMode == null) {
return true;
} else {
if (authorizePathMode.equals(AUTH_PATH_MODE_OBSERVER)) {
if (isMemberOf(ctx, dnUser, ldapAuthObserverBase)) {
return true;
}
if (isMemberOf(ctx, dnUser, ldapAuthDataAdminBase)) {
return true;
}
}
if (authorizePathMode.equals(AUTH_PATH_MODE_DATAADMIN)) {
if (isMemberOf(ctx, dnUser, ldapAuthDataAdminBase)) {
return true;
}
}
if (authorizePathMode.equals(AUTH_PATH_MODE_TESTER)) {
if (isMemberOf(ctx, dnUser, ldapAuthTesterBase)) {
return true;
}
}
}
} catch (AuthenticationException e) {
return false;
} catch (Exception e) {
log.warn("LoginFilter caught Exception", e);
return false;
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
log.warn("LoginFilter caught Exception", e);
}
}
}
return false;
}
use of javax.naming.ldap.InitialLdapContext in project keycloak by keycloak.
the class LDAPContextManager method createLdapContext.
private void createLdapContext() throws NamingException {
Hashtable<Object, Object> connProp = getConnectionProperties(ldapConfig);
if (!LDAPConstants.AUTH_TYPE_NONE.equals(ldapConfig.getAuthType())) {
vaultCharSecret = getVaultSecret();
if (vaultCharSecret != null && !ldapConfig.isStartTls()) {
connProp.put(SECURITY_CREDENTIALS, vaultCharSecret.getAsArray().orElse(ldapConfig.getBindCredential().toCharArray()));
}
}
ldapContext = new InitialLdapContext(connProp, null);
if (ldapConfig.isStartTls()) {
SSLSocketFactory sslSocketFactory = null;
String useTruststoreSpi = ldapConfig.getUseTruststoreSpi();
if (useTruststoreSpi != null && useTruststoreSpi.equals(LDAPConstants.USE_TRUSTSTORE_ALWAYS)) {
TruststoreProvider provider = session.getProvider(TruststoreProvider.class);
sslSocketFactory = provider.getSSLSocketFactory();
}
tlsResponse = startTLS(ldapContext, ldapConfig.getAuthType(), ldapConfig.getBindDN(), vaultCharSecret.getAsArray().orElse(ldapConfig.getBindCredential().toCharArray()), sslSocketFactory);
// Exception should be already thrown by LDAPContextManager.startTLS if "startTLS" could not be established, but rather do some additional check
if (tlsResponse == null) {
throw new NamingException("Wasn't able to establish LDAP connection through StartTLS");
}
}
}
use of javax.naming.ldap.InitialLdapContext in project scheduling by ow2-proactive.
the class LDAPLoginModule method createLdapContext.
private ContextHandler createLdapContext(String user, String password, boolean requireAuthentication) {
LdapContext ctx = null;
StartTlsResponse tls = null;
Hashtable<String, String> env = createBasicEnvForInitalContext();
try {
if (!START_TLS) {
if (requireAuthentication || !AUTHENTICATION_METHOD.equals(ANONYMOUS_LDAP_CONNECTION)) {
if (requireAuthentication) {
// In case of anonymous bind, when we need to check some user credentials, we must force authentication to be simple
env.put(Context.SECURITY_AUTHENTICATION, ANONYMOUS_LDAP_CONNECTION.equals(AUTHENTICATION_METHOD) ? "simple" : AUTHENTICATION_METHOD);
} else {
env.put(Context.SECURITY_AUTHENTICATION, AUTHENTICATION_METHOD);
}
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, password);
}
}
// Create the initial directory context
ctx = new InitialLdapContext(env, null);
if (START_TLS) {
// Start TLS
tls = (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());
if (ANY_HOSTNAME) {
tls.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
if (ANY_CERTIFICATE) {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, new SecureRandom());
tls.negotiate(context.getSocketFactory());
} else {
tls.negotiate();
}
if (requireAuthentication || !AUTHENTICATION_METHOD.equals(ANONYMOUS_LDAP_CONNECTION)) {
if (requireAuthentication) {
// In case of anonymous bind, when we need to check some user credentials, we must force authentication to be simple
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, ANONYMOUS_LDAP_CONNECTION.equals(AUTHENTICATION_METHOD) ? "simple" : AUTHENTICATION_METHOD);
} else {
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, AUTHENTICATION_METHOD);
}
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, user);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
}
}
return new ContextHandler(ctx, tls);
} catch (NamingException e) {
logger.error("Problem checking user password, user password may be wrong: " + e);
return null;
} catch (Exception e) {
logger.error("Problem when creating the ldap context", e);
return null;
}
}
Aggregations