use of javax.naming.ldap.InitialLdapContext in project bw-calendar-engine by Bedework.
the class UserGroupsLdapImpl method getGroups.
/* Return all groups for principal == null or all groups for which principal
* is a member
*
*/
private Collection<BwGroup> getGroups(final DirConfigProperties dirProps, final BwPrincipal principal) throws CalFacadeException {
final ArrayList<BwGroup> groups = new ArrayList<>();
final LdapConfigProperties props = (LdapConfigProperties) dirProps;
if (props.getGroupMemberAttr() == null) {
warn("No group member attribute set - assuming no groups");
return groups;
}
InitialLdapContext ctx = null;
String member = null;
if (principal != null) {
if (principal.getKind() == WhoDefs.whoTypeUser) {
member = getUserEntryValue(props, principal);
} else if (principal.getKind() == WhoDefs.whoTypeGroup) {
member = getGroupEntryValue(props, principal);
}
}
try {
try {
ctx = createLdapInitContext(props);
} catch (final Throwable t) {
warn("*******************************************");
warn("No group information available");
error(t);
return groups;
}
final BasicAttributes matchAttrs = new BasicAttributes(true);
if (member != null) {
matchAttrs.put(props.getGroupMemberAttr(), member);
}
final String[] idAttr = { props.getGroupIdAttr() };
final NamingEnumeration response = ctx.search(props.getGroupContextDn(), matchAttrs, idAttr);
while (response.hasMore()) {
final SearchResult sr = (SearchResult) response.next();
final Attributes attrs = sr.getAttributes();
final Attribute nmAttr = attrs.get(props.getGroupIdAttr());
if (nmAttr.size() != 1) {
throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
}
final BwGroup group = new BwGroup();
group.setAccount(nmAttr.get(0).toString());
group.setPrincipalRef(makePrincipalUri(group.getAccount(), WhoDefs.whoTypeGroup));
groups.add(group);
}
return groups;
} catch (final Throwable t) {
if (debug) {
error(t);
}
throw new CalFacadeException(t);
} finally {
// Close the context to release the connection
if (ctx != null) {
closeContext(ctx);
}
}
}
use of javax.naming.ldap.InitialLdapContext in project teiid by teiid.
the class LDAPConnectionImpl method initializeLDAPContext.
/**
* Setup a standard initial LDAP context using JNDI's context factory.
* This method may be extended to support Sun-specific and AD-specific
* contexts, in order to support the different paging implementations they provide.
* @return the initial LDAP Context
*/
private InitialLdapContext initializeLDAPContext() throws ResourceException {
// Create the root context.
InitialLdapContext initContext;
Hashtable connenv = new Hashtable();
connenv.put(Context.INITIAL_CONTEXT_FACTORY, this.config.getLdapContextFactory());
connenv.put(Context.PROVIDER_URL, this.config.getLdapUrl());
connenv.put(Context.REFERRAL, LDAP_REFERRAL_MODE);
String userName = this.config.getLdapAdminUserDN();
String password = this.config.getLdapAdminUserPassword();
String authType = this.config.getLdapAuthType();
// if security-domain is specified and caller identity is used; then use
// credentials from subject
Subject subject = ConnectionContext.getSubject();
if (subject != null) {
userName = ConnectionContext.getUserName(subject, this.config, userName);
password = ConnectionContext.getPassword(subject, this.config, userName, password);
}
connenv.put(Context.SECURITY_AUTHENTICATION, authType);
if (!authType.equals("none")) {
if (userName == null) {
// $NON-NLS-1$
final String msg = LDAPPlugin.Util.getString("LDAPConnection.adminUserDNPropNotFound");
throw new ResourceException(msg);
}
if (password == null) {
// $NON-NLS-1$
final String msg = LDAPPlugin.Util.getString("LDAPConnection.adminUserPassPropNotFound");
throw new ResourceException(msg);
}
connenv.put(Context.SECURITY_PRINCIPAL, userName);
connenv.put(Context.SECURITY_CREDENTIALS, password);
}
if (this.config.getLdapTxnTimeoutInMillis() != null && this.config.getLdapTxnTimeoutInMillis() != -1) {
// $NON-NLS-1$
connenv.put("com.sun.jndi.ldap.connect.timeout", this.config.getLdapTxnTimeoutInMillis().toString());
}
// Enable connection pooling for the Initial context.
// $NON-NLS-1$ //$NON-NLS-2$
connenv.put("com.sun.jndi.ldap.connect.pool", "true");
// $NON-NLS-1$ //$NON-NLS-2$
connenv.put("com.sun.jndi.ldap.connect.pool.debug", "fine");
try {
initContext = new InitialLdapContext(connenv, null);
} catch (NamingException ne) {
// $NON-NLS-1$
final String msg = LDAPPlugin.Util.getString("LDAPConnection.directoryNamingError", ne.getExplanation());
throw new ResourceException(msg, ne);
}
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Successfully obtained initial LDAP context.");
return initContext;
}
use of javax.naming.ldap.InitialLdapContext in project OpenOLAT by OpenOLAT.
the class LDAPLoginManagerImpl method bindUser.
/**
* Connect to LDAP with the User-Name and Password given as parameters
*
* Configuration: LDAP URL = ldapContext.xml (property=ldapURL) LDAP Base =
* ldapContext.xml (property=ldapBase) LDAP Attributes Map =
* ldapContext.xml (property=userAttrs)
*
* @param uid The users LDAP login name (can't be null)
* @param pwd The users LDAP password (can't be null)
*
* @return After successful bind Attributes otherwise NULL
*
* @throws NamingException
*/
@Override
public Attributes bindUser(String login, String pwd, LDAPError errors) {
// get user name, password and attributes
String ldapUrl = ldapLoginModule.getLdapUrl();
String[] userAttr = syncConfiguration.getUserAttributes();
if (login == null || pwd == null) {
if (log.isDebug())
log.debug("Error when trying to bind user, missing username or password. Username::" + login + " pwd::" + pwd);
errors.insert("Username and password must be selected");
return null;
}
LdapContext ctx = bindSystem();
if (ctx == null) {
errors.insert("LDAP connection error");
return null;
}
String userDN = ldapDao.searchUserForLogin(login, ctx);
if (userDN == null) {
log.info("Error when trying to bind user with username::" + login + " - user not found on LDAP server" + (ldapLoginModule.isCacheLDAPPwdAsOLATPwdOnLogin() ? ", trying with OLAT login provider" : ""));
errors.insert("Username or password incorrect");
return null;
}
// Ok, so far so good, user exists. Now try to fetch attributes using the
// users credentials
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, userDN);
env.put(Context.SECURITY_CREDENTIALS, pwd);
if (ldapLoginModule.getLdapConnectionTimeout() != null) {
env.put(TIMEOUT_KEY, ldapLoginModule.getLdapConnectionTimeout().toString());
}
if (ldapLoginModule.isSslEnabled()) {
enableSSL(env);
}
try {
Control[] connectCtls = new Control[] {};
LdapContext userBind = new InitialLdapContext(env, connectCtls);
Attributes attributes = userBind.getAttributes(userDN, userAttr);
userBind.close();
return attributes;
} catch (AuthenticationException e) {
log.info("Error when trying to bind user with username::" + login + " - invalid LDAP password");
errors.insert("Username or password incorrect");
return null;
} catch (NamingException e) {
log.error("NamingException when trying to get attributes after binding user with username::" + login, e);
errors.insert("Username or password incorrect");
return null;
}
}
use of javax.naming.ldap.InitialLdapContext in project goodies by sonatype.
the class LdapTestEnvironmentTest method smoke.
@Test
public void smoke() throws Exception {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:" + getLdapServer().getPort() + "/o=sonatype");
env.put(Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN);
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// Let's open a connection on this partition
InitialContext initialContext = new InitialLdapContext(env, null);
// We should be able to read it
DirContext appRoot = (DirContext) initialContext.lookup("");
assertThat(appRoot, notNullValue());
// Let's get the entry associated to the top level
Attributes attributes = appRoot.getAttributes("");
assertThat(attributes, notNullValue());
assertThat((String) attributes.get("o").get(), equalTo("sonatype"));
Attribute attribute = attributes.get("objectClass");
assertThat(attribute, notNullValue());
assertThat(attribute.contains("top"), is(true));
assertThat(attribute.contains("organization"), is(true));
}
use of javax.naming.ldap.InitialLdapContext in project wildfly by wildfly.
the class LdapUrlTestServlet method runSearch.
/**
* Try to search in LDAP with search base containing URL. Also try to retrieve RequestControls from LdapContext.
*
* @param hostname
* @return
* @throws Exception
*/
public static String runSearch(final String hostname, boolean testLdapCtx) throws Exception {
final StringBuilder result = new StringBuilder();
final String ldapUrl = "ldap://" + (hostname == null ? "localhost" : hostname) + ":10389";
final Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
final SearchControls ctl = new SearchControls();
ctl.setReturningAttributes(new String[] { "cn" });
DirContext dirCtx = null;
if (testLdapCtx) {
// LdapContext must also work
LdapContext ldapCtx = new InitialLdapContext(env, null);
// next line tests if the LdapContext works
ldapCtx.getRequestControls();
dirCtx = ldapCtx;
} else {
dirCtx = new InitialDirContext(env);
}
final NamingEnumeration<SearchResult> nenum = dirCtx.search(ldapUrl + "/dc=jboss,dc=org", "(uid=jduke)", ctl);
while (nenum.hasMore()) {
SearchResult sr = nenum.next();
Attributes attrs = sr.getAttributes();
result.append("cn=").append(attrs.get("cn").get());
}
dirCtx.close();
return result.toString();
}
Aggregations