Search in sources :

Example 1 with ResetPasswordResult

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);
    }
}
Also used : Salt(edu.stanford.bmir.protege.web.shared.auth.Salt) UserDetails(edu.stanford.bmir.protege.web.shared.user.UserDetails) UserId(edu.stanford.bmir.protege.web.shared.user.UserId) SaltedPasswordDigest(edu.stanford.bmir.protege.web.shared.auth.SaltedPasswordDigest) ResetPasswordResult(edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult) Nonnull(javax.annotation.Nonnull)

Example 2 with ResetPasswordResult

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));
}
Also used : ResetPasswordResult(edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult) Test(org.junit.Test)

Example 3 with ResetPasswordResult

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));
}
Also used : ResetPasswordResult(edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult) Test(org.junit.Test)

Example 4 with ResetPasswordResult

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));
}
Also used : ResetPasswordResult(edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult) Test(org.junit.Test)

Example 5 with ResetPasswordResult

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));
}
Also used : Salt(edu.stanford.bmir.protege.web.shared.auth.Salt) UserId(edu.stanford.bmir.protege.web.shared.user.UserId) SaltedPasswordDigest(edu.stanford.bmir.protege.web.shared.auth.SaltedPasswordDigest) ResetPasswordResult(edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult) Test(org.junit.Test)

Aggregations

ResetPasswordResult (edu.stanford.bmir.protege.web.shared.chgpwd.ResetPasswordResult)6 Test (org.junit.Test)5 Salt (edu.stanford.bmir.protege.web.shared.auth.Salt)2 SaltedPasswordDigest (edu.stanford.bmir.protege.web.shared.auth.SaltedPasswordDigest)2 UserId (edu.stanford.bmir.protege.web.shared.user.UserId)2 UserDetails (edu.stanford.bmir.protege.web.shared.user.UserDetails)1 Nonnull (javax.annotation.Nonnull)1