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());
}
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"));
}
Aggregations