public Object mapFromContext(Object ctx) { DirContextAdapter adapter = (DirContextAdapter) ctx; Map<String, List<String>> record = new HashMap<String, List<String>>(); if (attributeNames == null || attributeNames.length == 0) { try { for (NamingEnumeration ae = adapter.getAttributes().getAll(); ae .hasMore();) { Attribute attr = (Attribute) ae.next(); extractStringAttributeValues(adapter, record, attr.getID()); } } catch (NamingException x) { org.springframework.ldap.support.LdapUtils .convertLdapException(x); } } else { for (String attributeName : attributeNames) { extractStringAttributeValues(adapter, record, attributeName); } } record.put(DN_KEY, Arrays.asList(getAdapterDN(adapter))); set.add(record); return null; } };
List<String> attributeNames = Collections.list(adapter.getAttributes().getIDs()); for (String attributeName : attributeNames) { try {
@Test public void defaultSearchFilter() throws Exception { // given final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))"; DirContext ctx = mock(DirContext.class); when(ctx.getNameInNamespace()).thenReturn(""); DirContextAdapter dca = new DirContextAdapter(); SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); when( ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class))).thenReturn( new MockNamingEnumeration(sr)); ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider( "mydomain.eu", "ldap://192.168.1.200/"); customProvider.contextFactory = createContextFactoryReturning(ctx); // when Authentication result = customProvider.authenticate(joe); // then assertThat(result.isAuthenticated()).isTrue(); verify(ctx).search(any(DistinguishedName.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class)); }
@Test public void bindPrincipalAndUsernameUsed() throws Exception { // given final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))"; ArgumentCaptor<Object[]> captor = ArgumentCaptor.forClass(Object[].class); DirContext ctx = mock(DirContext.class); when(ctx.getNameInNamespace()).thenReturn(""); DirContextAdapter dca = new DirContextAdapter(); SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); when( ctx.search(any(Name.class), eq(defaultSearchFilter), captor.capture(), any(SearchControls.class))).thenReturn( new MockNamingEnumeration(sr)); ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider( "mydomain.eu", "ldap://192.168.1.200/"); customProvider.contextFactory = createContextFactoryReturning(ctx); // when Authentication result = customProvider.authenticate(joe); // then assertThat(captor.getValue()).containsExactly("joe@mydomain.eu", "joe"); assertThat(result.isAuthenticated()).isTrue(); }
private void checkAuthentication(String rootDn, ActiveDirectoryLdapAuthenticationProvider provider) throws NamingException { DirContext ctx = mock(DirContext.class); when(ctx.getNameInNamespace()).thenReturn(""); DirContextAdapter dca = new DirContextAdapter(); SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); @SuppressWarnings("deprecation") DistinguishedName searchBaseDn = new DistinguishedName(rootDn); when( ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class), any(SearchControls.class))).thenReturn( new MockNamingEnumeration(sr)).thenReturn(new MockNamingEnumeration(sr)); provider.contextFactory = createContextFactoryReturning(ctx); Authentication result = provider.authenticate(joe); assertThat(result.getAuthorities()).isEmpty(); dca.addAttributeValue("memberOf", "CN=Admin,CN=Users,DC=mydomain,DC=eu"); result = provider.authenticate(joe); assertThat(result.getAuthorities()).hasSize(1); }
@Test public void customSearchFilterIsUsedForSuccessfulAuthentication() throws Exception { // given String customSearchFilter = "(&(objectClass=user)(sAMAccountName={0}))"; DirContext ctx = mock(DirContext.class); when(ctx.getNameInNamespace()).thenReturn(""); DirContextAdapter dca = new DirContextAdapter(); SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); when( ctx.search(any(Name.class), eq(customSearchFilter), any(Object[].class), any(SearchControls.class))).thenReturn( new MockNamingEnumeration(sr)); ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider( "mydomain.eu", "ldap://192.168.1.200/"); customProvider.contextFactory = createContextFactoryReturning(ctx); // when customProvider.setSearchFilter(customSearchFilter); Authentication result = customProvider.authenticate(joe); // then assertThat(result.isAuthenticated()).isTrue(); }
@Test public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws Exception { provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://192.168.1.200/"); DirContext ctx = mock(DirContext.class); when(ctx.getNameInNamespace()).thenReturn(""); DirContextAdapter dca = new DirContextAdapter(); SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes()); when( ctx.search(eq(new DistinguishedName("DC=mydomain,DC=eu")), any(String.class), any(Object[].class), any(SearchControls.class))) .thenReturn(new MockNamingEnumeration(sr)); provider.contextFactory = createContextFactoryReturning(ctx); try { provider.authenticate(joe); fail("Expected BadCredentialsException for user with no domain information"); } catch (BadCredentialsException expected) { } provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu", "password")); }
/** * {@inheritDoc} */ @Override public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { return getAttributes(name.toString(), attrIds); }
/** * {@inheritDoc} */ @Override public Attributes getAttributes(Name name) throws NamingException { return getAttributes(name.toString()); }
/** * {@inheritDoc} */ @Override public Attributes getAttributes(Name name) throws NamingException { return getAttributes(name.toString()); }
/** * {@inheritDoc} */ @Override public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { return getAttributes(name.toString(), attrIds); }
@Test public void testAddAttributeValue() throws NamingException { // Perform test tested.addAttributeValue("abc", "123"); Attributes attrs = tested.getAttributes(); Attribute attr = attrs.get("abc"); assertThat((String) attr.get()).isEqualTo("123"); }
@Test public void testAddAttributeValueAttributeWithOtherValueExists() throws NamingException { tested.setAttribute(new BasicAttribute("abc", "321")); // Perform test tested.addAttributeValue("abc", "123"); Attributes attrs = tested.getAttributes(); Attribute attr = attrs.get("abc"); assertThat((String) attr.get(0)).isEqualTo("321"); assertThat((String) attr.get(1)).isEqualTo("123"); }
@Test public void testNewLdapNameWithLdapName() throws NamingException { tested.addAttributeValue("member", "CN=test,DC=root"); tested.addAttributeValue("member2", LdapUtils.newLdapName("CN=test2,DC=root")); Attributes attrs = tested.getAttributes(); assertThat(attrs.get("member").get()).isEqualTo("CN=test,DC=root"); assertThat(attrs.get("member2").get()).isEqualTo(LdapUtils.newLdapName("CN=test2,DC=root")); }
@Test public void testRemoveAttributeValueAttributeWithOtherValueExists() throws NamingException { tested.setAttribute(new BasicAttribute("abc", "321")); // Perform test tested.removeAttributeValue("abc", "123"); Attributes attributes = tested.getAttributes(); Attribute attr = attributes.get("abc"); assertThat(attr).isNotNull(); assertThat(attr.size()).isEqualTo(1); assertThat(attr.get()).isEqualTo("321"); }
@Test public void testAddAttributeValueAttributeWithSameValueExists() throws NamingException { tested.setAttribute(new BasicAttribute("abc", "123")); // Perform test tested.addAttributeValue("abc", "123"); Attributes attrs = tested.getAttributes(); Attribute attr = attrs.get("abc"); assertThat(attr.size()).isEqualTo(1); assertThat((String) attr.get(0)).isEqualTo("123"); }
@Test public void testRemoveAttributeValueAttributeDoesntExist() { // Perform test tested.removeAttributeValue("abc", "123"); Attributes attributes = tested.getAttributes(); assertThat(attributes.get("abc")).isNull(); }
@Test public void testGetObjectInstance_nullObject() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(null, DN, null, new Hashtable(), expectedAttributes); assertThat(adapter.getDn()).isEqualTo(DN); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }
@Test public void testRemoveAttributeValueInUpdateMode() { tested.setUpdateMode(true); // Perform test tested.removeAttributeValue("abc", "123"); assertThat(tested.getAttributes().get("abc")).isNull(); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(0); }
@Test public void testGetObjectInstance_CompositeName() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); CompositeName name = new CompositeName(); name.add(DN_STRING); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, name, null, new Hashtable(), expectedAttributes); verify(contextMock).close(); assertThat(adapter.getDn()).isEqualTo(DN); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }