Search in sources :

Example 26 with DistinguishedName

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

the class LdapUserDetailsManager method createUser.

public void createUser(UserDetails user) {
    DirContextAdapter ctx = new DirContextAdapter();
    copyToContext(user, ctx);
    DistinguishedName dn = usernameMapper.buildDn(user.getUsername());
    logger.debug("Creating new user '" + user.getUsername() + "' with DN '" + dn + "'");
    template.bind(dn, ctx, null);
    // Check for any existing authorities which might be set for this DN and remove
    // them
    List<GrantedAuthority> authorities = getUserAuthorities(dn, user.getUsername());
    if (authorities.size() > 0) {
        removeAuthorities(dn, authorities);
    }
    addAuthorities(dn, user.getAuthorities());
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 27 with DistinguishedName

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

the class LdapUserDetailsManager method deleteUser.

public void deleteUser(String username) {
    DistinguishedName dn = usernameMapper.buildDn(username);
    removeAuthorities(dn, getUserAuthorities(dn, username));
    template.unbind(dn);
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName)

Example 28 with DistinguishedName

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

the class ActiveDirectoryLdapAuthenticationProviderTests method nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal.

@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"));
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) DirContext(javax.naming.directory.DirContext) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 29 with DistinguishedName

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

the class ActiveDirectoryLdapAuthenticationProviderTests method checkAuthentication.

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);
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) Authentication(org.springframework.security.core.Authentication) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext)

Example 30 with DistinguishedName

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

the class InetOrgPersonTests method mappingBackToContextMatchesOriginalData.

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

Aggregations

DistinguishedName (org.springframework.ldap.core.DistinguishedName)32 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)23 Test (org.junit.Test)8 GrantedAuthority (org.springframework.security.core.GrantedAuthority)7 DirContext (javax.naming.directory.DirContext)5 BasicAttribute (javax.naming.directory.BasicAttribute)4 SearchResult (javax.naming.directory.SearchResult)4 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)4 BasicAttributes (javax.naming.directory.BasicAttributes)2 ModificationItem (javax.naming.directory.ModificationItem)2 SearchControls (javax.naming.directory.SearchControls)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 MockUserSearch (org.springframework.security.ldap.authentication.MockUserSearch)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 NamingException (javax.naming.NamingException)1