Search in sources :

Example 6 with RequestContext

use of org.craftercms.commons.http.RequestContext in project profile by craftercms.

the class SecurityExceptionProcessorTest method testAuthenticationRequired.

@Test
public void testAuthenticationRequired() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    doThrow(AuthenticationRequiredException.class).when(chain).processRequest(context);
    processor.processRequest(context, chain);
    verify(chain).processRequest(context);
    verify(authenticationRequiredHandler).handle(eq(context), any(AuthenticationRequiredException.class));
}
Also used : RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) 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 7 with RequestContext

use of org.craftercms.commons.http.RequestContext in project profile by craftercms.

the class ProfileControllerTest method setCurrentRequestContext.

private void setCurrentRequestContext() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestContext.setCurrent(context);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 8 with RequestContext

use of org.craftercms.commons.http.RequestContext in project profile by craftercms.

the class TenantControllerTest method setCurrentRequestContext.

private void setCurrentRequestContext() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestContext.setCurrent(context);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 9 with RequestContext

use of org.craftercms.commons.http.RequestContext in project profile by craftercms.

the class RequestSecurityFilter method doFilterInternal.

/**
 * Passes the request through the chain of {@link RequestSecurityProcessor}s.
 *
 * @param request
 * @param response
 * @param chain
 * @throws IOException
 * @throws ServletException
 */
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    RequestContext context = RequestContext.getCurrent();
    if (context == null) {
        context = createRequestContext(request, response);
    }
    List<RequestSecurityProcessor> finalSecurityProcessors = new ArrayList<>(securityProcessors);
    finalSecurityProcessors.add(getLastProcessorInChain(chain));
    Iterator<RequestSecurityProcessor> processorIter = finalSecurityProcessors.iterator();
    RequestSecurityProcessorChain processorChain = new RequestSecurityProcessorChainImpl(processorIter);
    try {
        processorChain.processRequest(context);
    } catch (IOException | ServletException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new ServletException(e.getMessage(), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) RequestSecurityProcessor(org.craftercms.security.processors.RequestSecurityProcessor) ArrayList(java.util.ArrayList) RequestContext(org.craftercms.commons.http.RequestContext) IOException(java.io.IOException) RequestSecurityProcessorChainImpl(org.craftercms.security.processors.impl.RequestSecurityProcessorChainImpl) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 10 with RequestContext

use of org.craftercms.commons.http.RequestContext 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)

Aggregations

RequestContext (org.craftercms.commons.http.RequestContext)47 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)40 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)40 Test (org.junit.Test)37 RequestSecurityProcessorChain (org.craftercms.security.processors.RequestSecurityProcessorChain)17 Authentication (org.craftercms.security.authentication.Authentication)12 DefaultAuthentication (org.craftercms.security.authentication.impl.DefaultAuthentication)11 Cookie (javax.servlet.http.Cookie)9 Profile (org.craftercms.profile.api.Profile)9 ObjectId (org.bson.types.ObjectId)7 AuthenticationRequiredException (org.craftercms.security.exception.AuthenticationRequiredException)5 AccessDeniedException (org.craftercms.security.exception.AccessDeniedException)4 Date (java.util.Date)3 BadCredentialsException (org.craftercms.security.exception.BadCredentialsException)3 RequestSecurityProcessor (org.craftercms.security.processors.RequestSecurityProcessor)3 HashMap (java.util.HashMap)2 HttpSession (javax.servlet.http.HttpSession)2 AuthenticationException (org.craftercms.security.exception.AuthenticationException)2 ExecutionInput (graphql.ExecutionInput)1 ExecutionInput.newExecutionInput (graphql.ExecutionInput.newExecutionInput)1