Search in sources :

Example 1 with MotechJsonMessage

use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.

the class MotechLoginErrorHandlerTest method shouldReturnJSON.

@Test
public void shouldReturnJSON() throws ServletException, IOException {
    AuthenticationException exception = new BadCredentialsException("Wrong Password");
    exception.setAuthentication(authentication);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addHeader("x-requested-with", "XMLHttpRequest");
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    motechLoginErrorHandler.onAuthenticationFailure(mockRequest, mockResponse, exception);
    MotechJsonMessage messageObject = new MotechJsonMessage("security.wrongPassword");
    assertEquals(messageObject.toJson(), mockResponse.getContentAsString());
}
Also used : MotechJsonMessage(org.motechproject.commons.api.json.MotechJsonMessage) AuthenticationException(org.springframework.security.core.AuthenticationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with MotechJsonMessage

use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.

the class MotechLoginUrlAuthenticationEntryPoint method commence.

/**
 * Performs the redirect (or forward) to the login form URL,
 * unless the request is an Ajax request, where a 401 response will be returned.
 */
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
    if (HttpRequestEnvironment.isAjax(request)) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setHeader("Content-Type", "application/json");
        MotechJsonMessage message = new MotechJsonMessage("ERROR");
        response.getWriter().write(message.toJson());
    } else {
        super.commence(request, response, authException);
    }
}
Also used : MotechJsonMessage(org.motechproject.commons.api.json.MotechJsonMessage)

Example 3 with MotechJsonMessage

use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.

the class MotechLoginErrorJSONHandler method onAuthenticationFailure.

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    if (HttpRequestEnvironment.isAjax(request)) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setHeader("Content-Type", "application/json");
        String message = getMessageForException(exception);
        MotechJsonMessage messageObject = new MotechJsonMessage(message);
        response.getWriter().write(messageObject.toJson());
    } else {
        super.onAuthenticationFailure(request, response, exception);
    }
}
Also used : MotechJsonMessage(org.motechproject.commons.api.json.MotechJsonMessage)

Example 4 with MotechJsonMessage

use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.

the class MotechLoginSuccessJSONHandler method onAuthenticationSuccess.

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
    super.onAuthenticationSuccess(request, response, authentication);
    if (HttpRequestEnvironment.isAjax(request)) {
        response.setHeader("Content-Type", "application/json");
        MotechJsonMessage message = new MotechJsonMessage("SUCCESS");
        response.getWriter().write(message.toJson());
    }
}
Also used : MotechJsonMessage(org.motechproject.commons.api.json.MotechJsonMessage)

Example 5 with MotechJsonMessage

use of org.motechproject.commons.api.json.MotechJsonMessage in project motech by motech.

the class MotechLoginSuccessHandlerTest method shouldReturnJSON.

@Test
public void shouldReturnJSON() throws ServletException, IOException {
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addHeader("x-requested-with", "XMLHttpRequest");
    MotechUser user = new MotechUser();
    user.setUserName("testUser");
    user.setFailureLoginCounter(3);
    when(authentication.getName()).thenReturn("testUser");
    when(motechUsersDao.findByUserName("testUser")).thenReturn(user);
    motechLoginSuccessHandler.onAuthenticationSuccess(mockRequest, mockResponse, authentication);
    MotechJsonMessage message = new MotechJsonMessage("SUCCESS");
    assertEquals(message.toJson(), mockResponse.getContentAsString());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechJsonMessage(org.motechproject.commons.api.json.MotechJsonMessage) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MotechJsonMessage (org.motechproject.commons.api.json.MotechJsonMessage)5 Test (org.junit.Test)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 MotechUser (org.motechproject.security.domain.MotechUser)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1