Search in sources :

Example 71 with UsernamePasswordAuthenticationToken

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

the class DaoAuthenticationProviderTests method testDetectsNullBeingReturnedFromAuthenticationDao.

@Test
public void testDetectsNullBeingReturnedFromAuthenticationDao() {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(new MockAuthenticationDaoReturnsNull());
    try {
        provider.authenticate(token);
        fail("Should have thrown AuthenticationServiceException");
    } catch (AuthenticationServiceException expected) {
        assertThat("UserDetailsService returned null, which is an interface contract violation").isEqualTo(expected.getMessage());
    }
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) Test(org.junit.Test)

Example 72 with UsernamePasswordAuthenticationToken

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

the class DaoAuthenticationProviderTests method testAuthenticateFailsWithEmptyUsername.

@Test
public void testAuthenticateFailsWithEmptyUsername() {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(null, "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)

Example 73 with UsernamePasswordAuthenticationToken

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

the class DaoAuthenticationProviderTests method testAuthenticateFailsWhenAuthenticationDaoHasBackendFailure.

@Test
public void testAuthenticateFailsWhenAuthenticationDaoHasBackendFailure() {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(new MockAuthenticationDaoSimulateBackendError());
    provider.setUserCache(new MockUserCache());
    try {
        provider.authenticate(token);
        fail("Should have thrown InternalAuthenticationServiceException");
    } catch (InternalAuthenticationServiceException expected) {
    }
}
Also used : InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 74 with UsernamePasswordAuthenticationToken

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

the class RemoteAuthenticationProviderTests method testExceptionsGetPassedBackToCaller.

// ~ Methods
// ========================================================================================================
@Test
public void testExceptionsGetPassedBackToCaller() {
    RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
    provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(false));
    try {
        provider.authenticate(new UsernamePasswordAuthenticationToken("rod", "password"));
        fail("Should have thrown RemoteAuthenticationException");
    } catch (RemoteAuthenticationException expected) {
    }
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 75 with UsernamePasswordAuthenticationToken

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

the class RemoteAuthenticationProviderTests method testNullCredentialsDoesNotCauseNullPointerException.

@Test
public void testNullCredentialsDoesNotCauseNullPointerException() {
    RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
    provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(false));
    try {
        provider.authenticate(new UsernamePasswordAuthenticationToken("rod", null));
        fail("Expected Exception");
    } catch (RemoteAuthenticationException success) {
    }
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) 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