@Test public void testMultipleRoleAttributeValuesAreMappedToAuthorities() throws Exception { LdapUserDetailsMapper mapper = new LdapUserDetailsMapper(); mapper.setConvertToUpperCase(false); mapper.setRolePrefix(""); mapper.setRoleAttributes(new String[] { "userRole" }); DirContextAdapter ctx = new DirContextAdapter(); ctx.setAttributeValues("userRole", new String[] { "X", "Y", "Z" }); ctx.setAttributeValue("uid", "ani"); LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES); assertThat(user.getAuthorities()).hasSize(3); }
@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)); }
List<String> attributeNames = Collections.list(adapter.getAttributes().getIDs()); for (String attributeName : attributeNames) { try { Object[] objValues = adapter.getObjectAttributes(attributeName); String[] values = new String[objValues != null ? objValues.length : 0]; for (int i = 0; i < values.length; i++) { record.put(DN_KEY, new String[]{adapter.getDn().toString()}); String mailAttr = configureMailAttribute(username, record); ExtendedLdapUserImpl result = new ExtendedLdapUserImpl(ldapUserDetails, record);
protected void populateContext(DirContextAdapter adapter) { super.populateContext(adapter); adapter.setAttributeValue("carLicense", carLicense); adapter.setAttributeValue("departmentNumber", departmentNumber); adapter.setAttributeValue("destinationIndicator", destinationIndicator); adapter.setAttributeValue("displayName", displayName); adapter.setAttributeValue("employeeNumber", employeeNumber); adapter.setAttributeValue("homePhone", homePhone); adapter.setAttributeValue("homePostalAddress", homePostalAddress); adapter.setAttributeValue("initials", initials); adapter.setAttributeValue("mail", mail); adapter.setAttributeValue("mobile", mobile); adapter.setAttributeValue("postalAddress", postalAddress); adapter.setAttributeValue("postalCode", postalCode); adapter.setAttributeValue("ou", ou); adapter.setAttributeValue("o", o); adapter.setAttributeValue("roomNumber", roomNumber); adapter.setAttributeValue("street", street); adapter.setAttributeValue("uid", uid); adapter.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" }); }
public Object mapFromContext(Object ctx) { DirContextAdapter adapter = (DirContextAdapter) ctx; Map<String, String[]> record = new HashMap<String, String[]>(); for (String attributeName : attributeNames) { String[] values = adapter.getStringAttributes(attributeName); if (values == null || values.length == 0) { logger.debug("No attribute value found for '" + attributeName + "'"); } else { record.put(attributeName, values); } } record.put(DN_KEY, new String[] {adapter.getDn().toString()}); set.add(record); return null; } };
private DirContextAdapter createUserContext() { DirContextAdapter ctx = new DirContextAdapter(); ctx.setDn(new DistinguishedName("ignored=ignored")); ctx.setAttributeValue("uid", "ghengis"); ctx.setAttributeValue("userPassword", "pillage"); ctx.setAttributeValue("carLicense", "HORS1"); ctx.setAttributeValue("cn", "Ghengis Khan"); ctx.setAttributeValue("description", "Scary"); ctx.setAttributeValue("destinationIndicator", "West"); ctx.setAttributeValue("displayName", "Ghengis McCann"); ctx.setAttributeValue("givenName", "Ghengis"); ctx.setAttributeValue("homePhone", "+467575436521"); ctx.setAttributeValue("initials", "G"); ctx.setAttributeValue("employeeNumber", "00001"); ctx.setAttributeValue("homePostalAddress", "Steppes"); ctx.setAttributeValue("mail", "ghengis@mongolia"); ctx.setAttributeValue("mobile", "always"); ctx.setAttributeValue("o", "Hordes"); ctx.setAttributeValue("ou", "Horde1"); ctx.setAttributeValue("postalAddress", "On the Move"); ctx.setAttributeValue("postalCode", "Changes Frequently"); ctx.setAttributeValue("roomNumber", "Yurt 1"); ctx.setAttributeValue("roomNumber", "Yurt 1"); ctx.setAttributeValue("sn", "Khan"); ctx.setAttributeValue("street", "Westward Avenue"); ctx.setAttributeValue("telephoneNumber", "+442075436521"); return ctx; }
@Test public void testAddDnAttributeSyntacticallyEqual() { BasicAttributes attributes = new BasicAttributes(); attributes.put("uniqueMember", "cn=john doe,OU=company"); DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups")); tested.setUpdateMode(true); tested.addAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company")); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(0); }
public Object executeWithContext(DirContext ctx) throws NamingException { try { Attributes attrs = ctx.getAttributes(dn, attributesToRetrieve); return new DirContextAdapter(attrs, LdapUtils.getFullDn(dn, ctx)); } catch (NameNotFoundException notFound) { throw new UsernameNotFoundException( "User " + username + " not found", notFound); } } });
@Test public void testRemoveAttributeValueInUpdateModeSameValueExistsInUpdatedAttrs() { tested.setUpdateMode(true); tested.setAttributeValue("abc", "123"); // Perform test tested.removeAttributeValue("abc", "123"); assertThat(tested.getAttributes().get("abc")).isNull(); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(0); }
@Test public void testGetModificationItemsOnAddAttributeValueInUpdateModeAttributeWhenSameValueExistsInOrigAttrs() throws NamingException { tested.setAttribute(new BasicAttribute("abc", "123")); tested.setUpdateMode(true); // Perform test tested.addAttributeValue("abc", "123"); Attributes attrs = tested.getAttributes(); assertThat(attrs.get("abc")).isNotNull(); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(0); }
@Test public void testPasswordAttributeIsMappedCorrectly() throws Exception { LdapUserDetailsMapper mapper = new LdapUserDetailsMapper(); mapper.setPasswordAttributeName("myappsPassword"); BasicAttributes attrs = new BasicAttributes(); attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes())); DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName")); ctx.setAttributeValue("uid", "ani"); LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES); assertThat(user.getPassword()).isEqualTo("mypassword"); }
@Test public void mappingBackToContextMatchesOriginalData() { DirContextAdapter ctx1 = createUserContext(); DirContextAdapter ctx2 = new DirContextAdapter(); ctx1.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" }); ctx2.setDn(new DistinguishedName("ignored=ignored")); InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)) .createUserDetails(); p.populateContext(ctx2); assertThat(ctx2).isEqualTo(ctx1); }
@Test public void testSetDnAttributesValueIdentical() { BasicAttributes attributes = new BasicAttributes(); attributes.put("uniqueMember", "cn=john doe, ou=company"); DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups")); tested.setUpdateMode(true); tested.setAttributeValues("uniqueMember", new Object[]{LdapUtils.newLdapName("cn=john doe, ou=company")}); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(0); }
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 testGetObjectInstance() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, DN, null, new Hashtable(), expectedAttributes); verify(contextMock).close(); assertThat(adapter.getDn()).isEqualTo(DN); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }
@Test public void testSetDnAttributeValueIdentical() { BasicAttributes attributes = new BasicAttributes(); attributes.put("uniqueMember", "cn=john doe, ou=company"); DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups")); tested.setUpdateMode(true); tested.setAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company")); ModificationItem[] modificationItems = tested.getModificationItems(); assertThat(modificationItems.length).isEqualTo(0); }
@Test public void testGetDn() throws Exception { DirContextAdapter tested = new DirContextAdapter(DUMMY_NAME); Name result = tested.getDn(); assertThat(result).isEqualTo(DUMMY_NAME); }
public String get(String attribute, DirContextAdapter context) { String attributeName = cfg.getGroupAttribute(attribute); if (attributeName != null) { return context.getStringAttribute(attributeName); } return null; }
private void createOu() { DirContextAdapter ctx = new DirContextAdapter(OU_DN); ctx.addAttributeValue("objectClass", "top"); ctx.addAttributeValue("objectClass", "organizationalUnit"); ctx.setAttributeValue("ou", "dummy"); ctx.setAttributeValue("description", "dummy description"); ldapTemplate.bind(ctx); }
@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"); }