Search in sources :

Example 41 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project camel by apache.

the class SpringSecurityAuthorizationPolicyTest method createAuthenticationToken.

private Authentication createAuthenticationToken(String username, String password, String... roles) {
    Authentication authToken;
    if (roles != null && roles.length > 0) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(roles.length);
        for (String role : roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
        authToken = new UsernamePasswordAuthenticationToken(username, password, authorities);
    } else {
        authToken = new UsernamePasswordAuthenticationToken(username, password);
    }
    return authToken;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 42 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class RunAsManagerImplTests method testReturnsAdditionalGrantedAuthorities.

@Test
public void testReturnsAdditionalGrantedAuthorities() throws Exception {
    UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test", "Password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
    RunAsManagerImpl runAs = new RunAsManagerImpl();
    runAs.setKey("my_password");
    Authentication result = runAs.buildRunAs(inputToken, new Object(), SecurityConfig.createList("RUN_AS_SOMETHING"));
    if (!(result instanceof RunAsUserToken)) {
        fail("Should have returned a RunAsUserToken");
    }
    assertThat(result.getPrincipal()).isEqualTo(inputToken.getPrincipal());
    assertThat(result.getCredentials()).isEqualTo(inputToken.getCredentials());
    Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities());
    assertThat(authorities.contains("ROLE_RUN_AS_SOMETHING")).isTrue();
    assertThat(authorities.contains("ROLE_ONE")).isTrue();
    assertThat(authorities.contains("ROLE_TWO")).isTrue();
    RunAsUserToken resultCast = (RunAsUserToken) result;
    assertThat(resultCast.getKeyHash()).isEqualTo("my_password".hashCode());
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 43 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class RunAsManagerImplTests method testRespectsRolePrefix.

@Test
public void testRespectsRolePrefix() throws Exception {
    UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test", "Password", AuthorityUtils.createAuthorityList("ONE", "TWO"));
    RunAsManagerImpl runAs = new RunAsManagerImpl();
    runAs.setKey("my_password");
    runAs.setRolePrefix("FOOBAR_");
    Authentication result = runAs.buildRunAs(inputToken, new Object(), SecurityConfig.createList("RUN_AS_SOMETHING"));
    assertThat(result instanceof RunAsUserToken).withFailMessage("Should have returned a RunAsUserToken").isTrue();
    assertThat(result.getPrincipal()).isEqualTo(inputToken.getPrincipal());
    assertThat(result.getCredentials()).isEqualTo(inputToken.getCredentials());
    Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities());
    assertThat(authorities.contains("FOOBAR_RUN_AS_SOMETHING")).isTrue();
    assertThat(authorities.contains("ONE")).isTrue();
    assertThat(authorities.contains("TWO")).isTrue();
    RunAsUserToken resultCast = (RunAsUserToken) result;
    assertThat(resultCast.getKeyHash()).isEqualTo("my_password".hashCode());
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 44 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class DaoAuthenticationProviderTests method testAuthenticateFailsIfAccountExpired.

@Test
public void testAuthenticateFailsIfAccountExpired() {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("peter", "opal");
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(new MockAuthenticationDaoUserPeterAccountExpired());
    provider.setUserCache(new MockUserCache());
    try {
        provider.authenticate(token);
        fail("Should have thrown AccountExpiredException");
    } catch (AccountExpiredException expected) {
    }
}
Also used : AccountExpiredException(org.springframework.security.authentication.AccountExpiredException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 45 with UsernamePasswordAuthenticationToken

use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class DaoAuthenticationProviderTests method testAuthenticateFailsForIncorrectPasswordCase.

// ~ Methods
// ========================================================================================================
@Test
public void testAuthenticateFailsForIncorrectPasswordCase() {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "KOala");
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(new MockAuthenticationDaoUserrod());
    provider.setUserCache(new MockUserCache());
    try {
        provider.authenticate(token);
        fail("Should have thrown BadCredentialsException");
    } catch (BadCredentialsException expected) {
    }
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Aggregations

UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)309 Test (org.junit.Test)156 Authentication (org.springframework.security.core.Authentication)114 GrantedAuthority (org.springframework.security.core.GrantedAuthority)37 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)34 UserDetails (org.springframework.security.core.userdetails.UserDetails)33 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)27 SecurityContext (org.springframework.security.core.context.SecurityContext)21 AuthenticationException (org.springframework.security.core.AuthenticationException)20 User (org.springframework.security.core.userdetails.User)17 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)15 ArrayList (java.util.ArrayList)14 OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)13 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)13 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)12 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)11 Before (org.junit.Before)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8