Search in sources :

Example 1 with LdapUserDetailsMapper

use of org.springframework.security.ldap.userdetails.LdapUserDetailsMapper in project spring-security by spring-projects.

the class LdapAuthenticationProviderTests method normalUsage.

@Test
public void normalUsage() {
    MockAuthoritiesPopulator populator = new MockAuthoritiesPopulator();
    LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(), populator);
    LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
    userMapper.setRoleAttributes(new String[] { "ou" });
    ldapProvider.setUserDetailsContextMapper(userMapper);
    assertThat(ldapProvider.getAuthoritiesPopulator()).isNotNull();
    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben", "benspassword");
    Object authDetails = new Object();
    authRequest.setDetails(authDetails);
    Authentication authResult = ldapProvider.authenticate(authRequest);
    assertThat(authResult.getCredentials()).isEqualTo("benspassword");
    assertThat(authResult.getDetails()).isSameAs(authDetails);
    UserDetails user = (UserDetails) authResult.getPrincipal();
    assertThat(user.getAuthorities()).hasSize(2);
    assertThat(user.getPassword()).isEqualTo("{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
    assertThat(user.getUsername()).isEqualTo("ben");
    assertThat(populator.getRequestedUsername()).isEqualTo("ben");
    assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_ENTRY");
    assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_POPULATOR");
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) LdapUserDetailsMapper(org.springframework.security.ldap.userdetails.LdapUserDetailsMapper) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 2 with LdapUserDetailsMapper

use of org.springframework.security.ldap.userdetails.LdapUserDetailsMapper in project spring-security by spring-projects.

the class LdapUserDetailsImplMixinTests method serializeWhenMixinRegisteredThenSerializes.

@Test
public void serializeWhenMixinRegisteredThenSerializes() throws Exception {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    LdapUserDetailsImpl p = (LdapUserDetailsImpl) mapper.mapUserFromContext(createUserContext(), "ghengis", AuthorityUtils.NO_AUTHORITIES);
    String json = this.mapper.writeValueAsString(p);
    JSONAssert.assertEquals(USER_JSON, json, true);
}
Also used : LdapUserDetailsImpl(org.springframework.security.ldap.userdetails.LdapUserDetailsImpl) LdapUserDetailsMapper(org.springframework.security.ldap.userdetails.LdapUserDetailsMapper) Test(org.junit.jupiter.api.Test)

Example 3 with LdapUserDetailsMapper

use of org.springframework.security.ldap.userdetails.LdapUserDetailsMapper in project spring-security by spring-projects.

the class LdapAuthenticationProviderTests method useWithNullAuthoritiesPopulatorReturnsCorrectRole.

@Test
public void useWithNullAuthoritiesPopulatorReturnsCorrectRole() {
    LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator());
    LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
    userMapper.setRoleAttributes(new String[] { "ou" });
    ldapProvider.setUserDetailsContextMapper(userMapper);
    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben", "benspassword");
    UserDetails user = (UserDetails) ldapProvider.authenticate(authRequest).getPrincipal();
    assertThat(user.getAuthorities()).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_ENTRY");
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) LdapUserDetailsMapper(org.springframework.security.ldap.userdetails.LdapUserDetailsMapper) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 4 with LdapUserDetailsMapper

use of org.springframework.security.ldap.userdetails.LdapUserDetailsMapper in project spring-security by spring-projects.

the class LdapUserDetailsImplMixinTests method deserializeWhenMixinRegisteredThenDeserializes.

@Test
public void deserializeWhenMixinRegisteredThenDeserializes() throws Exception {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    LdapUserDetailsImpl expectedAuthentication = (LdapUserDetailsImpl) mapper.mapUserFromContext(createUserContext(), "ghengis", AuthorityUtils.NO_AUTHORITIES);
    LdapUserDetailsImpl authentication = this.mapper.readValue(USER_JSON, LdapUserDetailsImpl.class);
    assertThat(authentication.getAuthorities()).containsExactlyElementsOf(expectedAuthentication.getAuthorities());
    assertThat(authentication.getDn()).isEqualTo(expectedAuthentication.getDn());
    assertThat(authentication.getUsername()).isEqualTo(expectedAuthentication.getUsername());
    assertThat(authentication.getPassword()).isEqualTo(expectedAuthentication.getPassword());
    assertThat(authentication.getGraceLoginsRemaining()).isEqualTo(expectedAuthentication.getGraceLoginsRemaining());
    assertThat(authentication.getTimeBeforeExpiration()).isEqualTo(expectedAuthentication.getTimeBeforeExpiration());
    assertThat(authentication.isAccountNonExpired()).isEqualTo(expectedAuthentication.isAccountNonExpired());
    assertThat(authentication.isAccountNonLocked()).isEqualTo(expectedAuthentication.isAccountNonLocked());
    assertThat(authentication.isEnabled()).isEqualTo(expectedAuthentication.isEnabled());
    assertThat(authentication.isCredentialsNonExpired()).isEqualTo(expectedAuthentication.isCredentialsNonExpired());
}
Also used : LdapUserDetailsImpl(org.springframework.security.ldap.userdetails.LdapUserDetailsImpl) LdapUserDetailsMapper(org.springframework.security.ldap.userdetails.LdapUserDetailsMapper) Test(org.junit.jupiter.api.Test)

Example 5 with LdapUserDetailsMapper

use of org.springframework.security.ldap.userdetails.LdapUserDetailsMapper in project spring-security by spring-projects.

the class LdapUserDetailsImplMixinTests method serializeWhenEraseCredentialInvokedThenUserPasswordIsNull.

@Test
public void serializeWhenEraseCredentialInvokedThenUserPasswordIsNull() throws JsonProcessingException, JSONException {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    LdapUserDetailsImpl p = (LdapUserDetailsImpl) mapper.mapUserFromContext(createUserContext(), "ghengis", AuthorityUtils.NO_AUTHORITIES);
    p.eraseCredentials();
    String actualJson = this.mapper.writeValueAsString(p);
    JSONAssert.assertEquals(USER_JSON.replaceAll("\"" + USER_PASSWORD + "\"", "null"), actualJson, true);
}
Also used : LdapUserDetailsImpl(org.springframework.security.ldap.userdetails.LdapUserDetailsImpl) LdapUserDetailsMapper(org.springframework.security.ldap.userdetails.LdapUserDetailsMapper) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)5 LdapUserDetailsMapper (org.springframework.security.ldap.userdetails.LdapUserDetailsMapper)5 LdapUserDetailsImpl (org.springframework.security.ldap.userdetails.LdapUserDetailsImpl)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 Authentication (org.springframework.security.core.Authentication)1