use of edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult in project webprotege by protegeproject.
the class ResetPasswordActionHandler method execute.
@Nonnull
@Override
public ResetPasswordResult execute(@Nonnull ResetPasswordAction action, @Nonnull ExecutionContext executionContext) {
final String emailAddress = action.getResetPasswordData().getEmailAddress();
try {
Optional<UserId> userId = userDetailsManager.getUserByUserIdOrEmail(emailAddress);
if (!userId.isPresent()) {
return new ResetPasswordResult(INVALID_EMAIL_ADDRESS);
}
Optional<UserDetails> userDetails = userDetailsManager.getUserDetails(userId.get());
if (!userDetails.isPresent()) {
return new ResetPasswordResult(INVALID_EMAIL_ADDRESS);
}
if (!userDetails.get().getEmailAddress().isPresent()) {
return new ResetPasswordResult(INVALID_EMAIL_ADDRESS);
}
if (!userDetails.get().getEmailAddress().get().equalsIgnoreCase(emailAddress)) {
return new ResetPasswordResult(INVALID_EMAIL_ADDRESS);
}
String pwd = IdUtil.getBase62UUID();
Salt salt = saltProvider.get();
SaltedPasswordDigest saltedPasswordDigest = passwordDigestAlgorithm.getDigestOfSaltedPassword(pwd, salt);
authenticationManager.setDigestedPassword(userId.get(), saltedPasswordDigest, salt);
mailer.sendEmail(userId.get(), emailAddress, pwd, ex -> {
throw new RuntimeException(ex);
});
logger.info("The password for {} has been reset. " + "An email has been sent to {} that contains the new password.", userId.get().getUserName(), emailAddress);
return new ResetPasswordResult(SUCCESS);
} catch (Exception e) {
logger.error("Could not reset the user password " + "associated with the email " + "address {}. The following " + "error occurred: {}.", emailAddress, e.getMessage(), e);
return new ResetPasswordResult(INTERNAL_ERROR);
}
}
use of edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult in project webprotege by protegeproject.
the class ResetPasswordActionHandler_TestCase method shouldReturnInvalidEmailAddressIfCannotFindAnyUser.
@Test
public void shouldReturnInvalidEmailAddressIfCannotFindAnyUser() {
when(userDetailsManager.getUserByUserIdOrEmail(any(String.class))).thenReturn(Optional.empty());
ResetPasswordResult result = handler.execute(action, context);
assertThat(result.getResultCode(), is(ResetPasswordResultCode.INVALID_EMAIL_ADDRESS));
}
use of edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult in project webprotege by protegeproject.
the class ResetPasswordActionHandler_TestCase method shouldReturnInvalidEmailAddressIfUserEmailAddressDoesEqualSuppliedEmailAddress.
@Test
public void shouldReturnInvalidEmailAddressIfUserEmailAddressDoesEqualSuppliedEmailAddress() {
when(userDetailsManager.getUserByUserIdOrEmail(any(String.class))).thenReturn(Optional.of(userId));
when(userDetailsManager.getUserDetails(userId)).thenReturn(Optional.of(userDetails));
when(userDetails.getEmailAddress()).thenReturn(Optional.of("other.address"));
ResetPasswordResult result = handler.execute(action, context);
assertThat(result.getResultCode(), is(ResetPasswordResultCode.INVALID_EMAIL_ADDRESS));
}
use of edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult in project webprotege by protegeproject.
the class ResetPasswordActionHandler_TestCase method shouldReturnInvalidEmailAddressIfUserEmailAddressDoesNotExist.
@Test
public void shouldReturnInvalidEmailAddressIfUserEmailAddressDoesNotExist() {
when(userDetailsManager.getUserByUserIdOrEmail(any(String.class))).thenReturn(Optional.of(userId));
when(userDetailsManager.getUserDetails(userId)).thenReturn(Optional.of(userDetails));
when(userDetails.getEmailAddress()).thenReturn(Optional.empty());
ResetPasswordResult result = handler.execute(action, context);
assertThat(result.getResultCode(), is(ResetPasswordResultCode.INVALID_EMAIL_ADDRESS));
}
use of edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult in project webprotege by protegeproject.
the class ResetPasswordActionHandler_TestCase method shouldReturnErrorOnException.
@Test
public void shouldReturnErrorOnException() {
when(userDetailsManager.getUserByUserIdOrEmail(any(String.class))).thenReturn(Optional.of(userId));
when(userDetailsManager.getUserDetails(userId)).thenReturn(Optional.of(userDetails));
when(userDetails.getEmailAddress()).thenReturn(Optional.of(EMAIL_ADDRESS));
doThrow(new RuntimeException()).when(authenticationManager).setDigestedPassword(any(UserId.class), any(SaltedPasswordDigest.class), any(Salt.class));
ResetPasswordResult result = handler.execute(action, context);
assertThat(result.getResultCode(), is(ResetPasswordResultCode.INTERNAL_ERROR));
}
Aggregations