Search in sources :

Example 21 with DistinguishedName

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

the class PasswordComparisonAuthenticatorTests method testWithUserSearch.

@Test
public void testWithUserSearch() {
    authenticator = new PasswordComparisonAuthenticator(getContextSource());
    authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
    assertThat(authenticator.getUserDns("Bob")).withFailMessage("User DN matches shouldn't be available").isEmpty();
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("uid=Bob,ou=people"));
    ctx.setAttributeValue("userPassword", "bobspassword");
    authenticator.setUserSearch(new MockUserSearch(ctx));
    authenticator.authenticate(new UsernamePasswordAuthenticationToken("shouldntbeused", "bobspassword"));
}
Also used : PlaintextPasswordEncoder(org.springframework.security.authentication.encoding.PlaintextPasswordEncoder) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 22 with DistinguishedName

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

the class DefaultLdapAuthoritiesPopulatorTests method subGroupRolesAreNotFoundByDefault.

@Test
public void subGroupRolesAreNotFoundByDefault() {
    populator.setGroupRoleAttribute("ou");
    populator.setConvertToUpperCase(true);
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("uid=ben,ou=people,dc=springframework,dc=org"));
    Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "manager"));
    assertThat(authorities).as("Should have 2 roles").hasSize(2);
    assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
    assertThat(authorities.contains("ROLE_DEVELOPER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 23 with DistinguishedName

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

the class DefaultLdapAuthoritiesPopulatorTests method userDnWithEscapedCharacterParameterReturnsExpectedRoles.

@Test
public void userDnWithEscapedCharacterParameterReturnsExpectedRoles() {
    populator.setGroupRoleAttribute("ou");
    populator.setConvertToUpperCase(true);
    populator.setGroupSearchFilter("(member={0})");
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("cn=mouse\\, jerry,ou=people,dc=springframework,dc=org"));
    Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "notused"));
    assertThat(authorities).as("Should have 1 role").hasSize(1);
    assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 24 with DistinguishedName

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

the class LdapUserDetailsManager method loadUserByUsername.

public UserDetails loadUserByUsername(String username) {
    DistinguishedName dn = usernameMapper.buildDn(username);
    List<GrantedAuthority> authorities = getUserAuthorities(dn, username);
    logger.debug("Loading user '" + username + "' with DN '" + dn + "'");
    DirContextAdapter userCtx = loadUserAsContext(dn, username);
    return userDetailsMapper.mapUserFromContext(userCtx, username, authorities);
}
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 25 with DistinguishedName

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

the class LdapUserDetailsManager method getUserAuthorities.

/**
	 *
	 * @param dn the distinguished name of the entry - may be either relative to the base
	 * context or a complete DN including the name of the context (either is supported).
	 * @param username the user whose roles are required.
	 * @return the granted authorities returned by the group search
	 */
@SuppressWarnings("unchecked")
List<GrantedAuthority> getUserAuthorities(final DistinguishedName dn, final String username) {
    SearchExecutor se = new SearchExecutor() {

        public NamingEnumeration<SearchResult> executeSearch(DirContext ctx) throws NamingException {
            DistinguishedName fullDn = LdapUtils.getFullDn(dn, ctx);
            SearchControls ctrls = new SearchControls();
            ctrls.setReturningAttributes(new String[] { groupRoleAttributeName });
            return ctx.search(groupSearchBase, groupSearchFilter, new String[] { fullDn.toUrl(), username }, ctrls);
        }
    };
    AttributesMapperCallbackHandler roleCollector = new AttributesMapperCallbackHandler(roleMapper);
    template.search(se, roleCollector);
    return roleCollector.getList();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) DirContext(javax.naming.directory.DirContext) SearchExecutor(org.springframework.ldap.core.SearchExecutor) AttributesMapperCallbackHandler(org.springframework.ldap.core.AttributesMapperCallbackHandler)

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