Search in sources :

Example 6 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class UserServiceBeanDefinitionParserTests method namePasswordAndAuthoritiesSupportPlaceholders.

@Test
public void namePasswordAndAuthoritiesSupportPlaceholders() {
    System.setProperty("principal.name", "joe");
    System.setProperty("principal.pass", "joespassword");
    System.setProperty("principal.authorities", "ROLE_A,ROLE_B");
    setContext("<b:bean class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'/>" + "<user-service id='service'>" + "    <user name='${principal.name}' password='${principal.pass}' authorities='${principal.authorities}'/>" + "</user-service>");
    UserDetailsService userService = (UserDetailsService) appContext.getBean("service");
    UserDetails joe = userService.loadUserByUsername("joe");
    assertThat(joe.getPassword()).isEqualTo("joespassword");
    assertThat(joe.getAuthorities()).hasSize(2);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Example 7 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class UserServiceBeanDefinitionParserTests method disabledAndEmbeddedFlagsAreSupported.

@Test
public void disabledAndEmbeddedFlagsAreSupported() {
    setContext("<user-service id='service'>" + "    <user name='joe' password='joespassword' authorities='ROLE_A' locked='true'/>" + "    <user name='Bob' password='bobspassword' authorities='ROLE_A' disabled='true'/>" + "</user-service>");
    UserDetailsService userService = (UserDetailsService) appContext.getBean("service");
    UserDetails joe = userService.loadUserByUsername("joe");
    assertThat(joe.isAccountNonLocked()).isFalse();
    // Check case-sensitive lookup SEC-1432
    UserDetails bob = userService.loadUserByUsername("Bob");
    assertThat(bob.isEnabled()).isFalse();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Example 8 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class OpenIDAuthenticationProvider method authenticate.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.springframework.security.authentication.AuthenticationProvider#authenticate
	 * (org.springframework.security.Authentication)
	 */
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        return null;
    }
    if (authentication instanceof OpenIDAuthenticationToken) {
        OpenIDAuthenticationToken response = (OpenIDAuthenticationToken) authentication;
        OpenIDAuthenticationStatus status = response.getStatus();
        // handle the various possibilities
        if (status == OpenIDAuthenticationStatus.SUCCESS) {
            // Lookup user details
            UserDetails userDetails = this.userDetailsService.loadUserDetails(response);
            return createSuccessfulAuthentication(userDetails, response);
        } else if (status == OpenIDAuthenticationStatus.CANCELLED) {
            throw new AuthenticationCancelledException("Log in cancelled");
        } else if (status == OpenIDAuthenticationStatus.ERROR) {
            throw new AuthenticationServiceException("Error message from server: " + response.getMessage());
        } else if (status == OpenIDAuthenticationStatus.FAILURE) {
            throw new BadCredentialsException("Log in failed - identity could not be verified");
        } else if (status == OpenIDAuthenticationStatus.SETUP_NEEDED) {
            throw new AuthenticationServiceException("The server responded setup was needed, which shouldn't happen");
        } else {
            throw new AuthenticationServiceException("Unrecognized return value " + status.toString());
        }
    }
    return null;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException)

Example 9 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class UserDetailsServiceLdapAuthoritiesPopulatorTests method delegationToUserDetailsServiceReturnsCorrectRoles.

@Test
public void delegationToUserDetailsServiceReturnsCorrectRoles() throws Exception {
    UserDetailsService uds = mock(UserDetailsService.class);
    UserDetails user = mock(UserDetails.class);
    when(uds.loadUserByUsername("joe")).thenReturn(user);
    List authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
    when(user.getAuthorities()).thenReturn(authorities);
    UserDetailsServiceLdapAuthoritiesPopulator populator = new UserDetailsServiceLdapAuthoritiesPopulator(uds);
    Collection<? extends GrantedAuthority> auths = populator.getGrantedAuthorities(new DirContextAdapter(), "joe");
    assertThat(auths).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(auths).contains("ROLE_USER")).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsServiceLdapAuthoritiesPopulator(org.springframework.security.ldap.authentication.UserDetailsServiceLdapAuthoritiesPopulator) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) List(java.util.List) Test(org.junit.Test)

Example 10 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails 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.Test)

Aggregations

UserDetails (org.springframework.security.core.userdetails.UserDetails)97 Test (org.junit.Test)37 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)32 Authentication (org.springframework.security.core.Authentication)30 GrantedAuthority (org.springframework.security.core.GrantedAuthority)16 User (org.springframework.security.core.userdetails.User)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)9 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)6 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)3