use of javax.naming.directory.DirContext in project nhin-d by DirectProject.
the class LDAPResearchTest method testDummy.
@SuppressWarnings("unchecked")
public void testDummy() throws Exception {
CertCacheFactory.getInstance().flushAll();
DirContext dirContext = createContext("cn=lookupTest");
Attributes attributes = dirContext.getAttributes("");
assertNotNull(attributes);
NamingEnumeration<Attribute> namingEnum = (NamingEnumeration<Attribute>) attributes.getAll();
while (namingEnum.hasMoreElements()) {
Attribute attr = namingEnum.nextElement();
System.out.println("Name: " + attr.getID() + "\r\nValue: " + attr.get() + "\r\n\r\n");
}
Set<SearchResult> results = searchDNs("(email=gm2552@cerner.com)", "", "ou=privKeys, ou=cerner, ou=com", SearchControls.SUBTREE_SCOPE, dirContext);
for (SearchResult result : results) {
System.out.println(result.getName());
// get the priv cert
String privKey = (String) result.getAttributes().get("privKeyStore").get();
System.out.println("Privkey BASE64: " + privKey);
}
}
use of javax.naming.directory.DirContext in project OpenClinica by OpenClinica.
the class SystemController method getLdapModule.
public HashMap<String, Object> getLdapModule(StudyBean studyBean) {
String enabled = CoreResources.getField("ldap.enabled");
String ldapHost = CoreResources.getField("ldap.host");
String username = CoreResources.getField("ldap.userDn");
String password = CoreResources.getField("ldap.password");
String result = "";
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapHost);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// replace with user DN
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
result = "ACTIVE";
} catch (Exception e) {
result = "INACTIVE";
}
HashMap<String, String> mapMetadata = new HashMap<>();
mapMetadata.put("ldap.host", ldapHost);
HashMap<String, Object> mapWebService = new HashMap<>();
mapWebService.put("enabled", enabled.equalsIgnoreCase("true") ? "True" : "False");
mapWebService.put("status", result);
mapWebService.put("metadata", mapMetadata);
HashMap<String, Object> mapModule = new HashMap<>();
mapModule.put("Ldap", mapWebService);
return mapModule;
}
use of javax.naming.directory.DirContext in project OpenAM by OpenRock.
the class AMSetupServlet method getLdapHostAndPort.
// Method to get hostname and port number with the
// provided Domain Name for Active Directory user data store.
private String[] getLdapHostAndPort(String domainName) throws NamingException, IOException {
if (!domainName.endsWith(".")) {
domainName += '.';
}
DirContext ictx;
// The resource record type A is defined in RFC 1035.
try {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
ictx = new InitialDirContext(env);
Attributes attributes = ictx.getAttributes(domainName, new String[] { "A" });
Attribute attrib = attributes.get("A");
if (attrib == null) {
throw new NamingException();
}
} catch (NamingException e) {
// throw exception.
throw e;
}
// then look for the LDAP server
String serverHostName;
String serverPortStr;
String ldapServer = "_ldap._tcp." + domainName;
try {
// Attempting to resolve ldapServer to SRV record.
// This is a mechanism defined in MSDN, querying
// SRV records for _ldap._tcp.DOMAINNAME.
// and get host and port from domain.
Attributes attributes = ictx.getAttributes(ldapServer, new String[] { "SRV" });
Attribute attr = attributes.get("SRV");
if (attr == null) {
throw new NamingException();
}
String[] srv = attr.get().toString().split(" ");
String hostNam = srv[3];
serverHostName = hostNam.substring(0, hostNam.length() - 1);
serverPortStr = srv[2];
} catch (NamingException e) {
// throw exception.
throw e;
}
// try to connect to LDAP port to make sure this machine
// has LDAP service
int serverPort = Integer.parseInt(serverPortStr);
try {
new Socket(serverHostName, serverPort).close();
} catch (IOException e) {
throw e;
}
String[] hostAndPort = new String[2];
hostAndPort[0] = serverHostName;
hostAndPort[1] = serverPortStr;
return hostAndPort;
}
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 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();
}
Aggregations