use of org.springframework.security.core.AuthenticationException in project motech by motech.
the class MotechLoginErrorHandlerTest method shouldBlockUser.
@Test
public void shouldBlockUser() throws ServletException, IOException {
AuthenticationException exception = new BadCredentialsException("Wrong Password");
exception.setAuthentication(authentication);
MotechUser user = createUser(UserStatus.ACTIVE, 3);
when(authentication.getName()).thenReturn("testUser");
when(motechUsersDao.findByUserName("testUser")).thenReturn(user);
when(settingService.getFailureLoginLimit()).thenReturn(3);
motechLoginErrorHandler.onAuthenticationFailure(request, response, exception);
verify(response).sendRedirect(LOGIN_BLOCKED);
verify(motechUsersDao).update(userCaptor.capture());
MotechUser capturedUser = userCaptor.getValue();
assertEquals((Integer) 0, capturedUser.getFailureLoginCounter());
assertEquals(UserStatus.BLOCKED, capturedUser.getUserStatus());
}
use of org.springframework.security.core.AuthenticationException 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());
}
use of org.springframework.security.core.AuthenticationException in project irida by phac-nml.
the class CredentialsExpiredAuthenticationFailureHandlerTest method testOnAuthenticationFailure.
@Test
public void testOnAuthenticationFailure() throws IOException, ServletException {
String username = "tom";
User user = new User();
PasswordReset reset = new PasswordReset(user);
String expectedRedirect = "/password_reset/" + reset.getId() + "?expired=true";
AuthenticationException exception = new CredentialsExpiredException("Credentials expired");
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(request.getParameter("username")).thenReturn(username);
when(request.getContextPath()).thenReturn("");
when(userService.getUserByUsername(username)).thenReturn(user);
when(resetService.create(any(PasswordReset.class))).thenReturn(reset);
handler.onAuthenticationFailure(request, response, exception);
verify(request).getParameter("username");
verify(userService).getUserByUsername(username);
verify(resetService).create(any(PasswordReset.class));
ArgumentCaptor<String> redirectCaptor = ArgumentCaptor.forClass(String.class);
verify(response).sendRedirect(redirectCaptor.capture());
String redirect = redirectCaptor.getValue();
assertEquals(expectedRedirect, redirect);
}
use of org.springframework.security.core.AuthenticationException in project irida by phac-nml.
the class CredentialsExpiredAuthenticationFailureHandlerTest method testOnAuthenticationFailureWithOtherException.
@Test
public void testOnAuthenticationFailureWithOtherException() throws IOException, ServletException {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
AuthenticationException exception = new DisabledException("disabled");
handler.onAuthenticationFailure(request, response, exception);
verifyZeroInteractions(userService);
verifyZeroInteractions(resetService);
}
use of org.springframework.security.core.AuthenticationException in project dubion by valsamiq.
the class CustomSignInAdapter method signIn.
@Override
public String signIn(String userId, Connection<?> connection, NativeWebRequest request) {
try {
UserDetails user = userDetailsService.loadUserByUsername(userId);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
String jwt = tokenProvider.createToken(authenticationToken, false);
ServletWebRequest servletWebRequest = (ServletWebRequest) request;
servletWebRequest.getResponse().addCookie(getSocialAuthenticationCookie(jwt));
} catch (AuthenticationException ae) {
log.error("Social authentication error");
log.trace("Authentication exception trace: {}", ae);
}
return jHipsterProperties.getSocial().getRedirectAfterSignIn();
}
Aggregations