use of javax.naming.directory.DirContext in project karaf by apache.
the class LDAPCache method doGetUserRoles.
private String[] doGetUserRoles(String user, String userDn, String userDnNamespace) throws NamingException {
DirContext context = open();
SearchControls controls = new SearchControls();
if (options.getRoleSearchSubtree()) {
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
String filter = options.getRoleFilter();
if (filter != null) {
filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
filter = filter.replaceAll(Pattern.quote("%dn"), Matcher.quoteReplacement(userDn));
filter = filter.replaceAll(Pattern.quote("%fqdn"), Matcher.quoteReplacement(userDnNamespace));
filter = filter.replace("\\", "\\\\");
LOGGER.debug("Looking for the user roles in LDAP with ");
LOGGER.debug(" base DN: " + options.getRoleBaseDn());
LOGGER.debug(" filter: " + filter);
NamingEnumeration namingEnumeration = context.search(options.getRoleBaseDn(), filter, controls);
try {
List<String> rolesList = new ArrayList<>();
while (namingEnumeration.hasMore()) {
SearchResult result = (SearchResult) namingEnumeration.next();
Attributes attributes = result.getAttributes();
Attribute roles1 = attributes.get(options.getRoleNameAttribute());
if (roles1 != null) {
for (int i = 0; i < roles1.size(); i++) {
String role = (String) roles1.get(i);
if (role != null) {
LOGGER.debug("User {} is a member of role {}", user, role);
// handle role mapping
Set<String> roleMappings = tryMappingRole(role);
if (roleMappings.isEmpty()) {
rolesList.add(role);
} else {
for (String roleMapped : roleMappings) {
rolesList.add(roleMapped);
}
}
}
}
}
}
return rolesList.toArray(new String[rolesList.size()]);
} finally {
if (namingEnumeration != null) {
try {
namingEnumeration.close();
} catch (NamingException e) {
// Ignore
}
}
}
} else {
LOGGER.debug("The user role filter is null so no roles are retrieved");
return new String[] {};
}
}
use of javax.naming.directory.DirContext in project jmeter by apache.
the class LDAPExtSampler method testEnded.
// Ensure any remaining contexts are closed
@Override
public void testEnded(String host) {
for (Map.Entry<String, DirContext> entry : ldapContexts.entrySet()) {
DirContext dc = entry.getValue();
try {
log.warn("Tidying old Context for thread: " + entry.getKey());
dc.close();
} catch (NamingException ignored) {
// ignored
}
}
ldapContexts.clear();
}
use of javax.naming.directory.DirContext 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();
}
use of javax.naming.directory.DirContext in project jetcd by coreos.
the class DnsSrvNameResolver method getServers.
@Override
protected List<ResolvedServerInfo> getServers() {
try {
DirContext ctx = new InitialDirContext(ENV);
NamingEnumeration<?> resolved = ctx.getAttributes(name, ATTRIBUTE_IDS).get("srv").getAll();
List<ResolvedServerInfo> servers = new LinkedList<>();
while (resolved.hasMore()) {
servers.add(srvRecordToServerInfo((String) resolved.next()));
}
return servers;
} catch (Exception e) {
LOGGER.warn("", e);
}
return Collections.emptyList();
}
use of javax.naming.directory.DirContext in project jdk8u_jdk by JetBrains.
the class ldapURLContextFactory method getUsingURLIgnoreRootDN.
static ResolveResult getUsingURLIgnoreRootDN(String url, Hashtable<?, ?> env) throws NamingException {
LdapURL ldapUrl = new LdapURL(url);
DirContext ctx = new LdapCtx("", ldapUrl.getHost(), ldapUrl.getPort(), env, ldapUrl.useSsl());
String dn = (ldapUrl.getDN() != null ? ldapUrl.getDN() : "");
// Represent DN as empty or single-component composite name.
CompositeName remaining = new CompositeName();
if (!"".equals(dn)) {
// if nonempty, add component
remaining.add(dn);
}
return new ResolveResult(ctx, remaining);
}
Aggregations