Search in sources :

Example 1 with AuthenticationRequiredException

use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.

the class AuthenticationRequiredHandlerImplTest method testSendError.

@Test
public void testSendError() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    handler.handle(context, new AuthenticationRequiredException(""));
    verify(requestCache).saveRequest(request, response);
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertTrue(response.isCommitted());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) AuthenticationRequiredException(org.craftercms.security.exception.AuthenticationRequiredException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with AuthenticationRequiredException

use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.

the class AuthenticationRequiredHandlerImplTest method testRedirectToLoginFormUrl.

@Test
public void testRedirectToLoginFormUrl() throws Exception {
    handler.setLoginFormUrl(LOGIN_FORM_URL);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    handler.handle(context, new AuthenticationRequiredException(""));
    verify(requestCache).saveRequest(request, response);
    assertEquals(LOGIN_FORM_URL, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) AuthenticationRequiredException(org.craftercms.security.exception.AuthenticationRequiredException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with AuthenticationRequiredException

use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.

the class SecurityExceptionProcessor method handleAccessDeniedException.

/**
 * Handles the specified {@link AccessDeniedException}, by calling the {@link AccessDeniedHandler}.
 */
protected void handleAccessDeniedException(RequestContext context, AccessDeniedException e) throws SecurityProviderException, IOException {
    Authentication auth = SecurityUtils.getAuthentication(context.getRequest());
    // If user is anonymous, authentication is required
    if (auth == null) {
        try {
            // Throw ex just to initialize stack trace
            throw new AuthenticationRequiredException("Authentication required to access the resource", e);
        } catch (AuthenticationRequiredException ae) {
            logger.debug("Authentication is required", ae);
            authenticationRequiredHandler.handle(context, ae);
        }
    } else {
        logger.debug("Access denied to user '" + auth.getProfile().getUsername() + "'", e);
        accessDeniedHandler.handle(context, e);
    }
}
Also used : Authentication(org.craftercms.security.authentication.Authentication) AuthenticationRequiredException(org.craftercms.security.exception.AuthenticationRequiredException)

Example 4 with AuthenticationRequiredException

use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.

the class RestAuthenticationRequiredHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/profile.json");
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    handler.handle(context, new AuthenticationRequiredException(ERROR_MESSAGE));
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertEquals(EXPECTED_RESPONSE_CONTENT, response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) AuthenticationRequiredException(org.craftercms.security.exception.AuthenticationRequiredException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

AuthenticationRequiredException (org.craftercms.security.exception.AuthenticationRequiredException)4 RequestContext (org.craftercms.commons.http.RequestContext)3 Test (org.junit.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 Authentication (org.craftercms.security.authentication.Authentication)1