use of org.motechproject.security.exception.InvalidTokenException in project motech by motech.
the class PasswordRecoveryServiceImpl method resetPassword.
@Override
@Transactional
public void resetPassword(String token, String password, String passwordConfirmation) throws InvalidTokenException {
if (!password.equals(passwordConfirmation)) {
throw new IllegalArgumentException("Password and confirmation do not match");
}
PasswordRecovery recovery = findForToken(token);
if (!validateRecovery(recovery)) {
throw new InvalidTokenException();
}
MotechUser user = motechUsersDao.findByUserName(recovery.getUsername());
if (user == null) {
throw new InvalidTokenException("This user has been deleted");
}
String encodedPassword = passwordEncoder.encodePassword(password);
user.setPassword(encodedPassword);
motechUsersDao.update(user);
passwordRecoveriesDataService.delete(recovery);
}
use of org.motechproject.security.exception.InvalidTokenException in project motech by motech.
the class ResetControllerTest method testResetInvalidToken.
@Test
public void testResetInvalidToken() throws Exception {
ResetViewData expected = getResetViewData(true, true, asList("server.reset.invalidToken"), getResetForm(TOKEN, PASSWORD, PASSWORD));
doThrow(new InvalidTokenException()).when(recoveryService).resetPassword(TOKEN, PASSWORD, PASSWORD);
controller.perform(post("/forgotreset").locale(Locale.ENGLISH).body(new ObjectMapper().writeValueAsBytes(getResetForm(TOKEN, PASSWORD, PASSWORD))).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(new ObjectMapper().writeValueAsString(expected)));
verify(recoveryService).resetPassword(TOKEN, PASSWORD, PASSWORD);
}
use of org.motechproject.security.exception.InvalidTokenException in project motech by motech.
the class ResetController method reset.
@RequestMapping(value = "/forgotreset", method = RequestMethod.POST)
@ResponseBody
public ResetViewData reset(@RequestBody ResetForm form, final HttpServletRequest request) {
ResetViewData viewData = new ResetViewData();
viewData.setResetForm(form);
viewData.setPageLang(cookieLocaleResolver.resolveLocale(request));
viewData.setInvalidToken(false);
List<String> errors = resetFormValidator.validate(form);
if (!errors.isEmpty()) {
viewData.setResetSucceed(false);
viewData.setErrors(errors);
return viewData;
} else {
try {
recoveryService.resetPassword(form.getToken(), form.getPassword(), form.getPasswordConfirmation());
} catch (InvalidTokenException e) {
LOGGER.debug("Reset with invalid token attempted", e);
errors.add("server.reset.invalidToken");
viewData.setInvalidToken(true);
} catch (RuntimeException e) {
LOGGER.error("Error while reseting passsword", e);
errors.add("server.reset.error");
}
viewData.setResetSucceed(true);
viewData.setErrors(errors);
}
return viewData;
}
Aggregations