Search in sources :

Example 11 with BadCredentialsException

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

the class OpenIDAuthenticationProviderTests method testAuthenticateFailure.

/*
	 * Test method for
	 * 'org.springframework.security.authentication.openid.OpenIDAuthenticationProvider.
	 * authenticate(Authentication)'
	 */
@Test
public void testAuthenticateFailure() {
    OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
    provider.setAuthenticationUserDetailsService(new UserDetailsByNameServiceWrapper<OpenIDAuthenticationToken>(new MockUserDetailsService()));
    Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.FAILURE, USERNAME, "", null);
    assertThat(preAuth.isAuthenticated()).isFalse();
    try {
        provider.authenticate(preAuth);
        fail("Should throw an AuthenticationException");
    } catch (BadCredentialsException expected) {
        assertThat("Log in failed - identity could not be verified").isEqualTo(expected.getMessage());
    }
}
Also used : Authentication(org.springframework.security.core.Authentication) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 12 with BadCredentialsException

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

the class OAuth2AuthenticationEntryPointTests method testCommenceWithHtmlAndJsonAccept.

@Test
public void testCommenceWithHtmlAndJsonAccept() throws Exception {
    request.addHeader("Accept", String.format("%s,%s", MediaType.TEXT_HTML_VALUE, MediaType.APPLICATION_JSON));
    entryPoint.commence(request, response, new BadCredentialsException("Bad"));
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertEquals(null, response.getErrorMessage());
}
Also used : BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 13 with BadCredentialsException

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

the class OAuth2AuthenticationEntryPointTests method testCommenceWithEmptyAccept.

@Test
public void testCommenceWithEmptyAccept() throws Exception {
    entryPoint.commence(request, response, new BadCredentialsException("Bad"));
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertEquals("{\"error\":\"unauthorized\",\"error_description\":\"Bad\"}", response.getContentAsString());
    assertTrue(MediaType.APPLICATION_JSON.isCompatibleWith(MediaType.valueOf(response.getContentType())));
    assertEquals(null, response.getErrorMessage());
}
Also used : BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 14 with BadCredentialsException

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

the class OAuth2AuthenticationEntryPointTests method testCommenceWithHtmlAccept.

@Test
public void testCommenceWithHtmlAccept() throws Exception {
    request.addHeader("Accept", MediaType.TEXT_HTML_VALUE);
    entryPoint.commence(request, response, new BadCredentialsException("Bad"));
    // TODO: maybe use forward / redirect for HTML content?
    assertEquals(HttpServletResponse.SC_NOT_ACCEPTABLE, response.getStatus());
    assertEquals("", response.getContentAsString());
    assertEquals(null, response.getErrorMessage());
}
Also used : BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Example 15 with BadCredentialsException

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

the class OAuth2AuthenticationEntryPointTests method testCommenceWithXml.

@Test
public void testCommenceWithXml() throws Exception {
    request.addHeader("Accept", MediaType.APPLICATION_XML_VALUE);
    entryPoint.commence(request, response, new BadCredentialsException("Bad"));
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertEquals("<oauth><error_description>Bad</error_description><error>unauthorized</error></oauth>", response.getContentAsString());
    assertEquals(MediaType.APPLICATION_XML_VALUE, response.getContentType());
    assertEquals(null, response.getErrorMessage());
}
Also used : BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.Test)

Aggregations

BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)170 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)63 Authentication (org.springframework.security.core.Authentication)57 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)27 AuthenticationException (org.springframework.security.core.AuthenticationException)23 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)20 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)20 UserDetails (org.springframework.security.core.userdetails.UserDetails)20 GrantedAuthority (org.springframework.security.core.GrantedAuthority)15 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)14 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)13 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 FilterChain (jakarta.servlet.FilterChain)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)10 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)9 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)7