use of javax.naming.directory.DirContext in project geode by apache.
the class LdapUserAuthenticator method authenticate.
@Override
public Principal authenticate(final Properties credentials, final DistributedMember member) {
final String userName = credentials.getProperty(UserPasswordAuthInit.USER_NAME);
if (userName == null) {
throw new AuthenticationFailedException("LdapUserAuthenticator: user name property [" + UserPasswordAuthInit.USER_NAME + "] not provided");
}
String password = credentials.getProperty(UserPasswordAuthInit.PASSWORD);
if (password == null) {
password = "";
}
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.ldap.LdapCtxFactory.class.getName());
env.put(Context.PROVIDER_URL, this.ldapUrlScheme + this.ldapServer + '/' + this.baseDomainName);
env.put(Context.SECURITY_PRINCIPAL, "uid=" + userName + "," + this.baseDomainName);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
final DirContext ctx = new InitialDirContext(env);
ctx.close();
} catch (Exception e) {
throw new AuthenticationFailedException("LdapUserAuthenticator: Failure with provided username, password combination for user name: " + userName, e);
}
return new UsernamePrincipal(userName);
}
use of javax.naming.directory.DirContext in project jmeter by apache.
the class LdapExtClient method connect.
/**
* connect to server
*
* @param host
* name of the server to connect
* @param port
* port of the server to connect
* @param rootdn
* base of the tree to operate on
* @param username
* name of the user to use for binding
* @param password
* password to use for binding
* @param connTimeOut
* connection timeout for connecting the server see
* "com.sun.jndi.ldap.connect.timeout"
* @param secure
* flag whether ssl should be used
* @return newly created {@link DirContext}
* @exception NamingException
* when creating the {@link DirContext} fails
*/
public static DirContext connect(String host, String port, String rootdn, String username, String password, String connTimeOut, boolean secure) throws NamingException {
DirContext dirContext;
Hashtable<String, String> env = new Hashtable<>();
// $NON-NLS-1$
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
StringBuilder sb = new StringBuilder(80);
if (secure) {
// $NON-NLS-1$
sb.append("ldaps://");
} else {
// $NON-NLS-1$
sb.append("ldap://");
}
sb.append(host);
if (port.length() > 0) {
// $NON-NLS-1$
sb.append(":");
sb.append(port);
}
// $NON-NLS-1$
sb.append("/");
sb.append(rootdn);
env.put(Context.PROVIDER_URL, sb.toString());
// $NON-NLS-1$
log.info("prov_url= " + env.get(Context.PROVIDER_URL));
if (connTimeOut.length() > 0) {
// $NON-NLS-1$
env.put("com.sun.jndi.ldap.connect.timeout", connTimeOut);
}
// $NON-NLS-1$
env.put(Context.REFERRAL, "throw");
// $NON-NLS-1$ // $NON-NLS-2$
env.put("java.naming.batchsize", "0");
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.SECURITY_PRINCIPAL, username);
dirContext = new InitialDirContext(env);
return dirContext;
}
use of javax.naming.directory.DirContext in project jmeter by apache.
the class LDAPExtSampler method addTest.
/***************************************************************************
* This will do the add test for the User defined TestCase
*
**************************************************************************/
private void addTest(DirContext dirContext, SampleResult res) throws NamingException {
try {
res.sampleStart();
DirContext ctx = LdapExtClient.createTest(dirContext, getUserAttributes(), getBaseEntryDN());
// the createTest() method creates an extra context which needs to be closed
ctx.close();
} finally {
res.sampleEnd();
}
}
use of javax.naming.directory.DirContext in project karaf by apache.
the class LDAPBackingEngine method listUsers.
@Override
public List<UserPrincipal> listUsers() {
DirContext context = null;
ArrayList<UserPrincipal> users = new ArrayList<>();
try {
context = cache.open();
SearchControls controls = new SearchControls();
if (options.getUserSearchSubtree()) {
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
String filter = options.getUserFilter();
filter = filter.replaceAll(Pattern.quote("%u"), "*");
filter = filter.replace("\\", "\\\\");
LOGGER.debug("Looking for the users in LDAP with ");
LOGGER.debug(" base DN: " + options.getUserBaseDn());
LOGGER.debug(" filter: " + filter);
NamingEnumeration namingEnumeration = context.search(options.getUserBaseDn(), filter, controls);
try {
while (namingEnumeration.hasMore()) {
SearchResult result = (SearchResult) namingEnumeration.next();
// We need to do the following because slashes are handled badly. For example, when searching
// for a user with lots of special characters like cn=admin,=+<>#;\
// SearchResult contains 2 different results:
//
// SearchResult.getName = cn=admin\,\=\+\<\>\#\;\\\\
// SearchResult.getNameInNamespace = cn=admin\,\=\+\<\>#\;\\,ou=people,dc=example,dc=com
//
// the second escapes the slashes correctly.
String userDNNamespace = result.getNameInNamespace();
// handle case where cn, ou, dc case doesn't match
int indexOfUserBaseDN = userDNNamespace.toLowerCase().indexOf("," + options.getUserBaseDn().toLowerCase());
String userDN = (indexOfUserBaseDN > 0) ? userDNNamespace.substring(0, indexOfUserBaseDN) : result.getName();
// we need to pull out the cn=, uid=, ect.. from the user name to get the actual user name
String userName = userDN;
if (userDN.contains("="))
userName = userDN.split("=")[1];
users.add(new UserPrincipal(userName));
}
} finally {
if (namingEnumeration != null) {
try {
namingEnumeration.close();
} catch (NamingException e) {
// Ignore
}
}
}
return users;
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
use of javax.naming.directory.DirContext in project karaf by apache.
the class LDAPCache method doGetUserDnAndNamespace.
protected String[] doGetUserDnAndNamespace(String user) throws NamingException {
DirContext context = open();
SearchControls controls = new SearchControls();
if (options.getUserSearchSubtree()) {
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
String filter = options.getUserFilter();
filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
filter = filter.replace("\\", "\\\\");
LOGGER.debug("Looking for the user in LDAP with ");
LOGGER.debug(" base DN: " + options.getUserBaseDn());
LOGGER.debug(" filter: " + filter);
NamingEnumeration namingEnumeration = context.search(options.getUserBaseDn(), filter, controls);
try {
if (!namingEnumeration.hasMore()) {
LOGGER.warn("User " + user + " not found in LDAP.");
return null;
}
LOGGER.debug("Found the user DN.");
SearchResult result = (SearchResult) namingEnumeration.next();
// We need to do the following because slashes are handled badly. For example, when searching
// for a user with lots of special characters like cn=admin,=+<>#;\
// SearchResult contains 2 different results:
//
// SearchResult.getName = cn=admin\,\=\+\<\>\#\;\\\\
// SearchResult.getNameInNamespace = cn=admin\,\=\+\<\>#\;\\,ou=people,dc=example,dc=com
//
// the second escapes the slashes correctly.
String userDNNamespace = result.getNameInNamespace();
// handle case where cn, ou, dc case doesn't match
int indexOfUserBaseDN = userDNNamespace.toLowerCase().indexOf("," + options.getUserBaseDn().toLowerCase());
String userDN = (indexOfUserBaseDN > 0) ? userDNNamespace.substring(0, indexOfUserBaseDN) : result.getName();
return new String[] { userDN, userDNNamespace };
} finally {
if (namingEnumeration != null) {
try {
namingEnumeration.close();
} catch (NamingException e) {
// Ignore
}
}
}
}
Aggregations