Search in sources :

Example 41 with SearchResult

use of javax.naming.directory.SearchResult in project traccar by tananaev.

the class LdapProvider method getUser.

public User getUser(String accountName) {
    SearchResult ldapUser;
    User user = new User();
    try {
        ldapUser = lookupUser(accountName);
        if (ldapUser != null) {
            Attribute attribute = ldapUser.getAttributes().get(idAttribute);
            if (attribute != null) {
                user.setLogin((String) attribute.get());
            } else {
                user.setLogin(accountName);
            }
            attribute = ldapUser.getAttributes().get(nameAttribute);
            if (attribute != null) {
                user.setName((String) attribute.get());
            } else {
                user.setName(accountName);
            }
            attribute = ldapUser.getAttributes().get(mailAttribute);
            if (attribute != null) {
                user.setEmail((String) attribute.get());
            } else {
                user.setEmail(accountName);
            }
        }
        user.setAdmin(isAdmin(accountName));
    } catch (NamingException e) {
        user.setLogin(accountName);
        user.setName(accountName);
        user.setEmail(accountName);
        Log.warning(e);
    }
    return user;
}
Also used : User(org.traccar.model.User) Attribute(javax.naming.directory.Attribute) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 42 with SearchResult

use of javax.naming.directory.SearchResult in project cxf by apache.

the class LdapCertificateRepo method getCertificatesFromLdap.

private List<X509Certificate> getCertificatesFromLdap(String tmpRootDN, String tmpFilter, String tmpAttrName) {
    try {
        List<X509Certificate> certificates = new ArrayList<>();
        NamingEnumeration<SearchResult> answer = ldapSearch.searchSubTree(tmpRootDN, tmpFilter);
        while (answer.hasMore()) {
            SearchResult sr = answer.next();
            Attributes attrs = sr.getAttributes();
            Attribute attribute = attrs.get(tmpAttrName);
            if (attribute != null) {
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                X509Certificate certificate = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream((byte[]) attribute.get()));
                certificates.add(certificate);
            }
        }
        return certificates;
    } catch (CertificateException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NamingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) CertificateException(java.security.cert.CertificateException) NamingException(javax.naming.NamingException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 43 with SearchResult

use of javax.naming.directory.SearchResult in project cxf by apache.

the class LdapCertificateRepo method getCRLsFromLdap.

private List<X509CRL> getCRLsFromLdap(String tmpRootDN, String tmpFilter, String tmpAttrName) {
    try {
        List<X509CRL> crls = new ArrayList<>();
        NamingEnumeration<SearchResult> answer = ldapSearch.searchSubTree(tmpRootDN, tmpFilter);
        while (answer.hasMore()) {
            SearchResult sr = answer.next();
            Attributes attrs = sr.getAttributes();
            Attribute attribute = attrs.get(tmpAttrName);
            if (attribute != null) {
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                X509CRL crl = (X509CRL) cf.generateCRL(new ByteArrayInputStream((byte[]) attribute.get()));
                crls.add(crl);
            }
        }
        return crls;
    } catch (CertificateException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NamingException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (CRLException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : X509CRL(java.security.cert.X509CRL) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) NamingException(javax.naming.NamingException) CRLException(java.security.cert.CRLException)

Example 44 with SearchResult

use of javax.naming.directory.SearchResult in project opentheso by miledrousset.

the class LDAPAuthenticator method dnFromUser.

private String dnFromUser(String username) throws NamingException {
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    props.put(Context.PROVIDER_URL, "ldap://ldap.mondomaine.fr");
    props.put(Context.REFERRAL, "ignore");
    InitialDirContext context = new InitialDirContext(props);
    SearchControls ctrls = new SearchControls();
    ctrls.setReturningAttributes(new String[] { authBean.getFirstnameLdap(), authBean.getNameLdap() });
    ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration<SearchResult> answers = context.search(authBean.getScope(), "(" + authBean.getUidLdap() + "=" + username + ")", ctrls);
    if (answers != null) {
        SearchResult result = answers.next();
        return result.getNameInNamespace();
    }
    return null;
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties)

Example 45 with SearchResult

use of javax.naming.directory.SearchResult in project opentheso by miledrousset.

the class LDAPAuthenticator method login.

/* (non-Javadoc)
     * @see fr.persee.aldo.auth.Authenticator#login(java.lang.String, java.lang.String)
     */
public Account login(String login, String password) {
    Account acc = null;
    try {
        String dn = dnFromUser(login);
        if (dn == null) {
            // TODO gerer exception
            return null;
        }
        env.put(Context.SECURITY_PRINCIPAL, dn);
        env.put(Context.SECURITY_CREDENTIALS, password);
        InitialDirContext context = new InitialDirContext(env);
        SearchControls ctrls = new SearchControls();
        ctrls.setReturningAttributes(new String[] { authBean.getFirstnameLdap(), authBean.getNameLdap(), authBean.getMailLdap() });
        ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration<SearchResult> answers;
        answers = context.search(authBean.getScope(), "(" + authBean.getUidLdap() + "=" + login + ")", ctrls);
        SearchResult result;
        result = answers.next();
        // System.out.println();
        String firstname = result.getAttributes().get(authBean.getFirstnameLdap()).get().toString();
        String name = result.getAttributes().get(authBean.getNameLdap()).get().toString();
        String mail = result.getAttributes().get(authBean.getMailLdap()).get().toString();
        // User
        User user = new User();
        user.setUser(login);
        user.setFirstname(firstname);
        user.setLastname(name);
        user.setMail(mail);
        user.setUid(login);
        // Account
        acc = new Account();
        acc.setBaseId(authBean.getBaseId());
        acc.setUser(user);
    } catch (NamingException e) {
    }
    return acc;
}
Also used : Account(mom.trd.opentheso.bdd.account.Account) User(mom.trd.opentheso.bdd.account.User) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

SearchResult (javax.naming.directory.SearchResult)248 SearchControls (javax.naming.directory.SearchControls)146 NamingException (javax.naming.NamingException)113 Attributes (javax.naming.directory.Attributes)96 Attribute (javax.naming.directory.Attribute)86 ArrayList (java.util.ArrayList)75 LdapContext (javax.naming.ldap.LdapContext)39 NamingEnumeration (javax.naming.NamingEnumeration)36 DirContext (javax.naming.directory.DirContext)35 Test (org.junit.Test)32 BasicAttributes (javax.naming.directory.BasicAttributes)30 HashSet (java.util.HashSet)28 InitialDirContext (javax.naming.directory.InitialDirContext)27 InitialLdapContext (javax.naming.ldap.InitialLdapContext)23 PagedResultsControl (javax.naming.ldap.PagedResultsControl)22 HashMap (java.util.HashMap)20 IOException (java.io.IOException)19 BasicAttribute (javax.naming.directory.BasicAttribute)19 Control (javax.naming.ldap.Control)16 PagedResultsResponseControl (javax.naming.ldap.PagedResultsResponseControl)15