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;
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations