Search in sources :

Example 1 with UserDetailsContextMapper

use of org.springframework.security.ldap.userdetails.UserDetailsContextMapper in project pentaho-platform by pentaho.

the class DefaultLdapAuthenticationProviderTest method testAuthenticate.

@Test
public void testAuthenticate() throws Exception {
    ldapAuthProvider = new DefaultLdapAuthenticationProvider(authenticator, authoritiesPopulator, roleMapper, "admin");
    when(usernamePasswordAuthenticationToken.getName()).thenReturn("admin");
    when(usernamePasswordAuthenticationToken.getCredentials()).thenReturn("p@$$w0rd");
    DirContextOperations dirContextOps = mock(DirContextOperations.class);
    when(authenticator.authenticate(usernamePasswordAuthenticationToken)).thenReturn(dirContextOps);
    Collection grantedAuthorities = Arrays.asList(new GrantedAuthority[] { new SimpleGrantedAuthority("admin") });
    when(authoritiesPopulator.getGrantedAuthorities(dirContextOps, "admin")).thenReturn(grantedAuthorities);
    UserDetailsContextMapper contextMapper = mock(UserDetailsContextMapper.class);
    ldapAuthProvider.setUserDetailsContextMapper(contextMapper);
    UserDetails userDetails = mock(UserDetails.class);
    when(userDetails.getAuthorities()).thenReturn(grantedAuthorities);
    when(contextMapper.mapUserFromContext(any(DirContextOperations.class), anyString(), any(grantedAuthorities.getClass()))).thenReturn(userDetails);
    when(roleMapper.toPentahoRole(anyString())).thenReturn("admin");
    Authentication result = ldapAuthProvider.authenticate(usernamePasswordAuthenticationToken);
    assertNotNull(result);
    assertEquals("p@$$w0rd", result.getCredentials().toString());
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) DirContextOperations(org.springframework.ldap.core.DirContextOperations) UserDetailsContextMapper(org.springframework.security.ldap.userdetails.UserDetailsContextMapper) Authentication(org.springframework.security.core.Authentication) Collection(java.util.Collection) Test(org.junit.Test)

Example 2 with UserDetailsContextMapper

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

the class LdapBindAuthenticationManagerFactoryITests method authenticationManagerFactoryWhenCustomUserDetailsContextMapperThenUsed.

@Test
public void authenticationManagerFactoryWhenCustomUserDetailsContextMapperThenUsed() throws Exception {
    CustomUserDetailsContextMapperConfig.CONTEXT_MAPPER = new UserDetailsContextMapper() {

        @Override
        public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
            return User.withUsername("other").password("password").roles("USER").build();
        }

        @Override
        public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
        }
    };
    this.spring.register(CustomUserDetailsContextMapperConfig.class).autowire();
    this.mockMvc.perform(formLogin().user("bob").password("bobspassword")).andExpect(authenticated().withUsername("other"));
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsContextMapper(org.springframework.security.ldap.userdetails.UserDetailsContextMapper) DirContextOperations(org.springframework.ldap.core.DirContextOperations) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 UserDetailsContextMapper (org.springframework.security.ldap.userdetails.UserDetailsContextMapper)2 Collection (java.util.Collection)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)1 Authentication (org.springframework.security.core.Authentication)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1