public void findLdapDnMemberOfList(LdapTemplate ldapTemplate, String ldapDn, List<String> resultDnList, List<String> dnIgnoreList){ if(dnIgnoreList.contains(ldapDn)){ return; } AndFilter filter = new AndFilter(); filter.and(new EqualsFilter(LdapUtil.getMemberKey(), ldapDn)); List<Object> groupList = ldapTemplate.search("", filter.toString(), new AbstractContextMapper<Object>() { @Override protected Object doMapFromContext(DirContextOperations ctx) { return ctx.getNameInNamespace(); } }); if(groupList.isEmpty()){ dnIgnoreList.add(ldapDn); return; } for(Object groupObj : groupList){ if(groupObj == null || !(groupObj instanceof String)){ continue; } String groupDn = (String)groupObj; if(resultDnList.contains(groupDn)){ continue; } resultDnList.add(groupDn); findLdapDnMemberOfList(ldapTemplate, groupDn, resultDnList, dnIgnoreList); } }
private void update(LdapTemplate ldapTemplate, LdapAccountRefVO ref){ String uid = ref.getLdapUid(); AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("uid", ref.getLdapUid())); List<Object> result = ldapTemplate.search("", filter.toString(), new AbstractContextMapper<Object>() { @Override protected Object doMapFromContext(DirContextOperations ctx) { return ctx.getNameInNamespace(); } }); if(result.size() == 0){ logger.error(String.format("Can not find ldapUid[%s] dn", uid)); return; } if(result.size() > 1){ logger.error(String.format("ldapUid[%s] More than one dn result", uid)); return; } String dn = result.get(0).toString(); ref.setLdapUid(dn); dbf.update(ref); logger.info(String.format("update ldapUid[%s] to ldapDn[%s] success", uid, dn)); }
private ErrorCode testAddLdapServerConnection(LdapServerInventory inv) { Map<String, Object> properties = new HashMap<>(); String timeout = Integer.toString(LdapGlobalProperty.LDAP_ADD_SERVER_CONNECT_TIMEOUT); properties.put("com.sun.jndi.ldap.connect.timeout", timeout); LdapTemplateContextSource ldapTemplateContextSource = new LdapUtil().loadLdap(inv, properties); try { AndFilter filter = new AndFilter(); // Any search conditions filter.and(new EqualsFilter(LdapConstant.LDAP_UID_KEY, "")); ldapTemplateContextSource.getLdapTemplate().authenticate("", filter.toString(), ""); logger.info("LDAP connection was successful"); } catch (Exception e) { logger.debug("Cannot connect to LDAP server", e); return operr("Cannot connect to LDAP server, %s", e.toString()); } return null; }
public boolean isValid(String uid, String password) { LdapTemplateContextSource ldapTemplateContextSource = readLdapServerConfiguration(); String ldapUseAsLoginName = LdapUtil.getLdapUseAsLoginName(); try { boolean valid; String fullUserDn = getFullUserDn(ldapTemplateContextSource.getLdapTemplate(), ldapUseAsLoginName, uid); if (fullUserDn.equals("") || password.equals("")) { return false; } LdapServerVO ldapServerVO = getLdapServer(); LdapServerInventory ldapServerInventory = LdapServerInventory.valueOf(ldapServerVO); ldapServerInventory.setUsername(fullUserDn); ldapServerInventory.setPassword(password); LdapTemplateContextSource ldapTemplateContextSource2 = new LdapUtil().loadLdap(ldapServerInventory); AndFilter filter = new AndFilter(); filter.and(new EqualsFilter(ldapUseAsLoginName, uid)); valid = ldapTemplateContextSource2.getLdapTemplate(). authenticate("", filter.toString(), password); logger.info(String.format("isValid[%s:%s, dn:%s, valid:%s]", ldapUseAsLoginName, uid, fullUserDn, valid)); return valid; } catch (NamingException e) { logger.info("isValid fail userName:" + uid, e); return false; } catch (Exception e) { logger.info("isValid error userName:" + uid, e); return false; } }
private void handle(APIGetCandidateLdapEntryForBindingMsg msg) { APIGetLdapEntryReply reply = new APIGetLdapEntryReply(); AndFilter andFilter = new AndFilter(); andFilter.and(new HardcodedFilter(msg.getLdapFilter())); List<String> boundLdapEntryList = Q.New(LdapAccountRefVO.class) .select(LdapAccountRefVO_.ldapUid) .listValues(); List<Object> result = this.searchLdapEntry(andFilter.toString(), msg.getLimit(), new ResultFilter() { @Override boolean needSelect(String dn) { return !boundLdapEntryList.contains(dn); } }); reply.setInventories(result); bus.reply(msg, reply); }
public static Name getDnOfEntry(LdapTemplate ldapTemplate, String baseDN, String objectClass, String filterAttributeName, String filterAttributeValue) { ContextMapper<Name> mapper = new AbstractContextMapper<Name>() { public Name doMapFromContext(DirContextOperations ctx) { return ctx.getDn(); } }; AndFilter filter = new AndFilter(); filter.and( new EqualsFilter("objectclass", objectClass)).and( new EqualsFilter(filterAttributeName, filterAttributeValue)); List<Name> result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, mapper); if (result != null && !result.isEmpty()) { //not only the first one.... return result.get(0); } return null; }
public static Name getDnOfEntry(LdapTemplate ldapTemplate, String baseDN, String objectClass, String filterAttributeName, String filterAttributeValue) { ContextMapper<Name> mapper = new AbstractContextMapper<Name>() { public Name doMapFromContext(DirContextOperations ctx) { return ctx.getDn(); } }; AndFilter filter = new AndFilter(); filter.and( new EqualsFilter("objectclass", objectClass)).and( new EqualsFilter(filterAttributeName, filterAttributeValue)); List<Name> result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, mapper); if (result != null && !result.isEmpty()) { //not only the first one.... return result.get(0); } return null; }
public static Map<String, Attribute> getAttributesOfEntry(LdapTemplate ldapTemplate, String baseDN, String objectClass, String filterAttributeName, String filterAttributeValue, String[] searchAttributes) { Map<String, Attribute> ldapAttributes = null; AttributesMapper<Map<String, Attribute>> mapper = new AttributesMapper<Map<String, Attribute>>() { public Map<String, Attribute> mapFromAttributes(Attributes attrs) throws NamingException { Map<String, Attribute> map = new HashMap<>(); NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll(); while (attrEnum.hasMore()) { Attribute att = attrEnum.next(); map.put(att.getID(), att); } return map; } }; List<?> result = null; AndFilter filter = new AndFilter(); filter.and( new EqualsFilter("objectclass", objectClass)).and( new EqualsFilter(filterAttributeName, filterAttributeValue)); result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, searchAttributes, mapper); if (result != null && !result.isEmpty()) { ldapAttributes = CastUtils.cast((Map<?, ?>)result.get(0)); } return ldapAttributes; }
result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, searchAttributes, mapper); if (result != null && !result.isEmpty()) {
public static Map<String, Attribute> getAttributesOfEntry(LdapTemplate ldapTemplate, String baseDN, String objectClass, String filterAttributeName, String filterAttributeValue, String[] searchAttributes) { Map<String, Attribute> ldapAttributes = null; AttributesMapper<Map<String, Attribute>> mapper = new AttributesMapper<Map<String, Attribute>>() { public Map<String, Attribute> mapFromAttributes(Attributes attrs) throws NamingException { Map<String, Attribute> map = new HashMap<>(); NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll(); while (attrEnum.hasMore()) { Attribute att = attrEnum.next(); map.put(att.getID(), att); } return map; } }; List<?> result = null; AndFilter filter = new AndFilter(); filter.and( new EqualsFilter("objectclass", objectClass)).and( new EqualsFilter(filterAttributeName, filterAttributeValue)); result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, searchAttributes, mapper); if (result != null && !result.isEmpty()) { ldapAttributes = CastUtils.cast((Map<?, ?>)result.get(0)); } return ldapAttributes; }
result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, searchAttributes, mapper); if (result != null && !result.isEmpty()) {
public boolean authenticate(String uid, String mdpNonCode){ AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", classObjet)); filter.and(new EqualsFilter(id, uid)); return this.getLdapTemplateLectureNonPoolee().authenticate("", filter.toString(), mdpNonCode); }
@Test @Category(NoAdTest.class) public void testAuthenticateWithInvalidPasswordAndCollectedException() { AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3")); final CollectingAuthenticationErrorCallback errorCallback = new CollectingAuthenticationErrorCallback(); assertThat(tested.authenticate("", filter.toString(), "invalidpassword", errorCallback)).isFalse(); final Exception error = errorCallback.getError(); assertThat(error).as("collected error should not be null").isNotNull(); assertThat(error instanceof AuthenticationException).as("expected org.springframework.ldap.AuthenticationException").isTrue(); assertThat(error.getCause() instanceof javax.naming.AuthenticationException).as("expected javax.naming.AuthenticationException").isTrue(); }
@Test @Category(NoAdTest.class) public void testAuthenticateWithInvalidPassword() { AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3")); assertThat(tested.authenticate("", filter.toString(), "invalidpassword")).isFalse(); }
@Test @Category(NoAdTest.class) public void testAuthenticate() { AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3")); assertThat(tested.authenticate("", filter.toString(), "password")).isTrue(); }
@Test @Category(NoAdTest.class) public void testAuthenticateWithFilterThatDoesNotMatchAnything() { AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and( new EqualsFilter("uid", "some.person.that.isnt.there")); assertThat(tested.authenticate("", filter.toString(), "password")).isFalse(); }
@Test @Category(NoAdTest.class) public void testAuthenticate() { AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3")); assertThat(ldapTemplate.authenticate("", filter.toString(), "password")).isTrue(); }
private static Map<String, Attribute> getAttributesOfEntry(LdapTemplate ldapTemplate, String baseDN, String objectClass, String searchFilter, String[] searchAttributes) { Map<String, Attribute> ldapAttributes = null; AttributesMapper<Map<String, Attribute>> mapper = new AttributesMapper<Map<String, Attribute>>() { public Map<String, Attribute> mapFromAttributes(Attributes attrs) throws NamingException { Map<String, Attribute> map = new HashMap<>(); NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll(); while (attrEnum.hasMore()) { Attribute att = attrEnum.next(); map.put(att.getID(), att); } return map; } }; AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", objectClass)).and(new HardcodedFilter(searchFilter)); List<?> result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, searchAttributes, mapper); if (result != null && !result.isEmpty()) { ldapAttributes = CastUtils.cast((Map<?, ?>)result.get(0)); } return ldapAttributes; }
@Test @Category(NoAdTest.class) public void testAuthenticateWithLookupOperationPerformedOnAuthenticatedContext() { AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3")); AuthenticatedLdapEntryContextCallback contextCallback = new AuthenticatedLdapEntryContextCallback() { public void executeWithContext(DirContext ctx, LdapEntryIdentification ldapEntryIdentification) { try { DirContextAdapter adapter = (DirContextAdapter) ctx.lookup(ldapEntryIdentification.getRelativeDn()); assertThat(adapter.getStringAttribute("cn")).isEqualTo("Some Person3"); } catch (NamingException e) { throw new RuntimeException("Failed to lookup " + ldapEntryIdentification.getRelativeDn(), e); } } }; assertThat(tested.authenticate("", filter.toString(), "password", contextCallback)).isTrue(); }
@Test(expected=IncorrectResultSizeDataAccessException.class) @Category(NoAdTest.class) public void testAuthenticateWithFilterThatMatchesSeveralEntries() { AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("cn", "Some Person")); tested.authenticate("", filter.toString(), "password"); }