Search in sources :

Example 6 with DirContextAdapter

use of org.springframework.ldap.core.DirContextAdapter in project spring-security by spring-projects.

the class InetOrgPersonTests method createUserContext.

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;
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 7 with DirContextAdapter

use of org.springframework.ldap.core.DirContextAdapter in project spring-security by spring-projects.

the class LdapUserDetailsServiceTests method nullPopulatorConstructorReturnsEmptyAuthoritiesList.

@Test
public void nullPopulatorConstructorReturnsEmptyAuthoritiesList() throws Exception {
    DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
    LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData));
    UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
    assertThat(user.getAuthorities()).isEmpty();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) DistinguishedName(org.springframework.ldap.core.DistinguishedName) MockUserSearch(org.springframework.security.ldap.authentication.MockUserSearch) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 8 with DirContextAdapter

use of org.springframework.ldap.core.DirContextAdapter in project spring-security by spring-projects.

the class LdapUserDetailsServiceTests method correctAuthoritiesAreReturned.

@Test
public void correctAuthoritiesAreReturned() {
    DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
    LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData), new MockAuthoritiesPopulator());
    service.setUserDetailsMapper(new LdapUserDetailsMapper());
    UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
    Set<String> authorities = AuthorityUtils.authorityListToSet(user.getAuthorities());
    assertThat(authorities).hasSize(1);
    assertThat(authorities.contains("ROLE_FROM_POPULATOR")).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) DistinguishedName(org.springframework.ldap.core.DistinguishedName) MockUserSearch(org.springframework.security.ldap.authentication.MockUserSearch) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 9 with DirContextAdapter

use of org.springframework.ldap.core.DirContextAdapter in project spring-security by spring-projects.

the class SpringSecurityLdapTemplateTests method searchForSingleEntryInternalAllowsReferrals.

// SEC-2405
@Test
public void searchForSingleEntryInternalAllowsReferrals() throws Exception {
    String base = "";
    String filter = "";
    String searchResultName = "ldap://example.com/dc=springframework,dc=org";
    Object[] params = new Object[] {};
    DirContextAdapter searchResultObject = mock(DirContextAdapter.class);
    when(ctx.search(any(DistinguishedName.class), eq(filter), eq(params), searchControls.capture())).thenReturn(resultsEnum);
    when(resultsEnum.hasMore()).thenReturn(true, false);
    when(resultsEnum.next()).thenReturn(searchResult);
    when(searchResult.getName()).thenReturn(searchResultName);
    when(searchResult.getObject()).thenReturn(searchResultObject);
    SpringSecurityLdapTemplate.searchForSingleEntryInternal(ctx, mock(SearchControls.class), base, filter, params);
    assertThat(searchControls.getValue().getReturningObjFlag()).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchControls(javax.naming.directory.SearchControls) Test(org.junit.Test)

Example 10 with DirContextAdapter

use of org.springframework.ldap.core.DirContextAdapter in project spring-security by spring-projects.

the class LdapUserDetailsManager method updateUser.

public void updateUser(UserDetails user) {
    DistinguishedName dn = usernameMapper.buildDn(user.getUsername());
    logger.debug("Updating user '" + user.getUsername() + "' with DN '" + dn + "'");
    List<GrantedAuthority> authorities = getUserAuthorities(dn, user.getUsername());
    DirContextAdapter ctx = loadUserAsContext(dn, user.getUsername());
    ctx.setUpdateMode(true);
    copyToContext(user, ctx);
    // Remove the objectclass attribute from the list of mods (if present).
    List<ModificationItem> mods = new LinkedList<ModificationItem>(Arrays.asList(ctx.getModificationItems()));
    ListIterator<ModificationItem> modIt = mods.listIterator();
    while (modIt.hasNext()) {
        ModificationItem mod = (ModificationItem) modIt.next();
        Attribute a = mod.getAttribute();
        if ("objectclass".equalsIgnoreCase(a.getID())) {
            modIt.remove();
        }
    }
    template.modifyAttributes(dn, mods.toArray(new ModificationItem[mods.size()]));
    // template.rebind(dn, ctx, null);
    // Remove the old authorities and replace them with the new one
    removeAuthorities(dn, authorities);
    addAuthorities(dn, user.getAuthorities());
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) DistinguishedName(org.springframework.ldap.core.DistinguishedName) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) LinkedList(java.util.LinkedList)

Aggregations

DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)39 DistinguishedName (org.springframework.ldap.core.DistinguishedName)28 Test (org.junit.Test)19 GrantedAuthority (org.springframework.security.core.GrantedAuthority)11 DirContext (javax.naming.directory.DirContext)7 SearchControls (javax.naming.directory.SearchControls)7 SearchResult (javax.naming.directory.SearchResult)7 Name (javax.naming.Name)4 Authentication (org.springframework.security.core.Authentication)4 HashSet (java.util.HashSet)3 BasicAttribute (javax.naming.directory.BasicAttribute)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)3 List (java.util.List)2 Attribute (javax.naming.directory.Attribute)2 BasicAttributes (javax.naming.directory.BasicAttributes)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 MockUserSearch (org.springframework.security.ldap.authentication.MockUserSearch)2 ArrayList (java.util.ArrayList)1