use of javax.naming.directory.InitialDirContext in project platformlayer by platformlayer.
the class ITOpenLdapService method testLdap.
private void testLdap(String ldapUrl, Secret adminPassword) throws NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
String sp = "com.sun.jndi.ldap.LdapCtxFactory";
env.put(Context.INITIAL_CONTEXT_FACTORY, sp);
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=test,dc=platformlayer,dc=org");
env.put(Context.SECURITY_CREDENTIALS, adminPassword.plaintext());
DirContext ctx = new InitialDirContext(env);
NamingEnumeration results = ctx.list("dc=test,dc=platformlayer,dc=org");
while (results.hasMore()) {
NameClassPair sr = (NameClassPair) results.next();
System.out.println(sr.getNameInNamespace());
}
ctx.close();
}
use of javax.naming.directory.InitialDirContext in project jforum2 by rafaelsteil.
the class LDAPAuthenticator method validateLogin.
/**
* @see net.jforum.sso.LoginAuthenticator#validateLogin(java.lang.String, java.lang.String, java.util.Map)
*/
public User validateLogin(String username, String password, Map extraParams) {
Hashtable environment = this.prepareEnvironment();
StringBuffer principal = new StringBuffer(256).append(SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX)).append(username).append(',').append(SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX));
environment.put(Context.SECURITY_PRINCIPAL, principal.toString());
environment.put(Context.SECURITY_CREDENTIALS, password);
DirContext dir = null;
try {
dir = new InitialDirContext(environment);
String lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_PREFIX);
String lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_SUFFIX);
if (lookupPrefix == null || lookupPrefix.length() == 0) {
lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX);
}
if (lookupSuffix == null || lookupSuffix.length() == 0) {
lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX);
}
String lookupPrincipal = lookupPrefix + username + "," + lookupSuffix;
Attribute att = dir.getAttributes(lookupPrincipal).get(SystemGlobals.getValue(ConfigKeys.LDAP_FIELD_EMAIL));
SSOUtils utils = new SSOUtils();
if (!utils.userExists(username)) {
String email = att != null ? (String) att.get() : "noemail";
utils.register("ldap", email);
}
return utils.getUser();
} catch (AuthenticationException e) {
return null;
} catch (NamingException e) {
return null;
} finally {
if (dir != null) {
try {
dir.close();
} catch (NamingException e) {
//close jndi context
}
}
}
}
use of javax.naming.directory.InitialDirContext in project nhin-d by DirectProject.
the class LdapPublicCertUtilImpl method getDirContext.
/**
* Creates the LDAP directory context from an SRV lookup name.
* @param lookupName The SRV record name used to discover the LDAP services.
* @return And InitialDirContext object that is connected to a discoverd LDAP service.
* @throws Exception
*/
protected InitialDirContext getDirContext(String lookupName) throws Exception {
// try the configured servers first
InitialDirContext ctx = null;
Lookup lu = LookupFactory.getFactory().getInstance(new Name(lookupName), Type.SRV);
// default retries is 3, limit to 2
lu.setResolver(createExResolver(servers.toArray(new String[servers.size()]), 2, 3));
final Record[] retRecords = lu.run();
if (retRecords != null && retRecords.length > 0) {
String ldapURL = createLDAPUrl(retRecords);
final Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, LDAP_FACTORY);
env.put(Context.PROVIDER_URL, ldapURL);
env.put(Context.SECURITY_AUTHENTICATION, "none");
env.put(LDAP_TIMEOUT, DEFAULT_LDAP_TIMEOUT);
env.put(LDAP_CONNECT_TIMEOUT, DEFAULT_LDAP_CONNECT_TIMEOUT);
env.put("java.naming.ldap.attributes.binary", "userCertificate, usercertificate");
ctx = new InitialDirContext(env);
}
return ctx;
}
use of javax.naming.directory.InitialDirContext 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.InitialDirContext 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;
}
Aggregations