Search in sources :

Example 26 with DirContext

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);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext)

Example 27 with DirContext

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;
}
Also used : HashMap(java.util.HashMap) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) MailException(org.springframework.mail.MailException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 28 with DirContext

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;
}
Also used : Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) IOException(java.io.IOException) Socket(java.net.Socket)

Example 29 with DirContext

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);
}
Also used : AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException)

Example 30 with DirContext

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();
}
Also used : NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

DirContext (javax.naming.directory.DirContext)76 NamingException (javax.naming.NamingException)32 InitialDirContext (javax.naming.directory.InitialDirContext)32 SearchResult (javax.naming.directory.SearchResult)26 SearchControls (javax.naming.directory.SearchControls)22 Attributes (javax.naming.directory.Attributes)18 Attribute (javax.naming.directory.Attribute)16 NamingEnumeration (javax.naming.NamingEnumeration)14 Test (org.junit.Test)14 Hashtable (java.util.Hashtable)12 DistinguishedName (org.springframework.ldap.core.DistinguishedName)11 Name (javax.naming.Name)7 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 BasicAttributes (javax.naming.directory.BasicAttributes)6 Authentication (org.springframework.security.core.Authentication)5 Principal (java.security.Principal)3 LdapContext (javax.naming.ldap.LdapContext)3