Search in sources :

Example 1 with ForgottenPasswordEntity

use of uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity in project pay-adminusers by alphagov.

the class ForgottenPasswordDaoIT method shouldNotFindForgottenPasswordByCode_ifExpired.

@Test
public void shouldNotFindForgottenPasswordByCode_ifExpired() {
    String forgottenPasswordCode = randomUuid();
    String username = randomUuid();
    String email = username + "@example.com";
    User user = userDbFixture(databaseHelper).withUsername(username).withEmail(email).insertUser();
    String userExternalId = user.getExternalId();
    UserEntity userEntity = userDao.findByExternalId(userExternalId).get();
    ZonedDateTime expired = ZonedDateTime.now().minusMinutes(91);
    ForgottenPassword forgottenPassword = forgottenPassword(randomInt(), forgottenPasswordCode, expired, userExternalId);
    databaseHelper.add(forgottenPassword, userEntity.getId());
    Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode());
    assertFalse(forgottenPasswordEntityOptional.isPresent());
}
Also used : User(uk.gov.pay.adminusers.model.User) ZonedDateTime(java.time.ZonedDateTime) ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) ForgottenPassword(uk.gov.pay.adminusers.model.ForgottenPassword) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 2 with ForgottenPasswordEntity

use of uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity in project pay-adminusers by alphagov.

the class ForgottenPasswordDaoIT method shouldFindForgottenPasswordByCode_ifNotExpired.

@Test
public void shouldFindForgottenPasswordByCode_ifNotExpired() {
    String forgottenPasswordCode = random(10);
    String username = randomUuid();
    String email = username + "@example.com";
    User user = userDbFixture(databaseHelper).withUsername(username).withEmail(email).insertUser();
    String userExternalId = user.getExternalId();
    UserEntity userEntity = userDao.findByExternalId(userExternalId).get();
    ZonedDateTime notExpired = ZonedDateTime.now().minusMinutes(89);
    ForgottenPassword forgottenPassword = forgottenPassword(randomInt(), forgottenPasswordCode, notExpired, userExternalId);
    databaseHelper.add(forgottenPassword, userEntity.getId());
    Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode());
    assertTrue(forgottenPasswordEntityOptional.isPresent());
    ForgottenPasswordEntity forgottenPasswordEntity = forgottenPasswordEntityOptional.get();
    assertThat(forgottenPasswordEntity.getCode(), is(forgottenPassword.getCode()));
    assertThat(forgottenPasswordEntity.getDate(), within(1, MINUTES, forgottenPassword.getDate()));
}
Also used : User(uk.gov.pay.adminusers.model.User) ZonedDateTime(java.time.ZonedDateTime) ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) ForgottenPassword(uk.gov.pay.adminusers.model.ForgottenPassword) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 3 with ForgottenPasswordEntity

use of uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity in project pay-adminusers by alphagov.

the class ForgottenPasswordServices method create.

public void create(String username) {
    Optional<UserEntity> userOptional = userDao.findByUsername(username);
    if (userOptional.isPresent()) {
        UserEntity userEntity = userOptional.get();
        ForgottenPasswordEntity forgottenPasswordEntity = new ForgottenPasswordEntity(randomUuid(), ZonedDateTime.now(), userEntity);
        forgottenPasswordDao.persist(forgottenPasswordEntity);
        String forgottenPasswordUrl = fromUri(selfserviceBaseUrl).path(SELFSERVICE_FORGOTTEN_PASSWORD_PATH).path(forgottenPasswordEntity.getCode()).build().toString();
        try {
            String notificationId = notificationService.sendForgottenPasswordEmail(userEntity.getEmail(), forgottenPasswordUrl);
            LOGGER.info("sent forgot password email successfully user [{}], notification id [{}]", userEntity.getExternalId(), notificationId);
        } catch (Exception e) {
            LOGGER.error(format("error sending forgotten password email for user [%s]", userEntity.getExternalId()), e);
        }
    } else {
        LOGGER.warn("Attempted forgotten password for non existent user {}", username);
        throw AdminUsersExceptions.notFoundException();
    }
}
Also used : ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity)

Example 4 with ForgottenPasswordEntity

use of uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity in project pay-adminusers by alphagov.

the class ForgottenPasswordDao method findNonExpiredByCode.

public Optional<ForgottenPasswordEntity> findNonExpiredByCode(String code) {
    String query = "SELECT fp FROM ForgottenPasswordEntity fp " + "WHERE fp.code = :code AND fp.date >= :expiry";
    ZonedDateTime expiryDateTime = ZonedDateTime.now(ZoneId.of("UTC")).minusMinutes(forgottenPasswordExpiryMinutes);
    return entityManager.get().createQuery(query, ForgottenPasswordEntity.class).setParameter("code", code).setParameter("expiry", expiryDateTime).getResultList().stream().findFirst();
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity)

Example 5 with ForgottenPasswordEntity

use of uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity in project pay-adminusers by alphagov.

the class ResetPasswordServiceTest method shouldUpdatePasswordAsEncrypted_whenCodeIsValid.

@Test
public void shouldUpdatePasswordAsEncrypted_whenCodeIsValid() {
    String code = "forgottenPasswordCode";
    String plainPassword = "myNewPlainPassword";
    String hashedPassword = "hashedPassword";
    int userId = 666;
    ArgumentCaptor<UserEntity> argumentCaptor = ArgumentCaptor.forClass(UserEntity.class);
    UserEntity user = new UserEntity();
    user.setId(userId);
    user.setLoginCounter(2);
    user.setPassword("whatever");
    ForgottenPasswordEntity forgottenPasswordEntity = new ForgottenPasswordEntity(code, ZonedDateTime.now(), user);
    when(mockForgottenPasswordDao.findNonExpiredByCode(code)).thenReturn(Optional.of(forgottenPasswordEntity));
    when(mockPasswordHasher.hash(plainPassword)).thenReturn(hashedPassword);
    Optional<Integer> userIdOptional = resetPasswordService.updatePassword(code, plainPassword);
    assertThat(userIdOptional.isPresent(), is(true));
    assertThat(userIdOptional.get(), is(userId));
    verify(mockUserDao).merge(argumentCaptor.capture());
    UserEntity updatedUser = argumentCaptor.getValue();
    assertThat(updatedUser.getLoginCounter(), is(0));
    assertThat(updatedUser.getPassword(), is(hashedPassword));
    verify(mockForgottenPasswordDao).remove(forgottenPasswordEntity);
}
Also used : ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Aggregations

ForgottenPasswordEntity (uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity)10 Test (org.junit.jupiter.api.Test)8 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)8 ZonedDateTime (java.time.ZonedDateTime)5 ForgottenPassword (uk.gov.pay.adminusers.model.ForgottenPassword)5 User (uk.gov.pay.adminusers.model.User)4 Timestamp (java.sql.Timestamp)1 Map (java.util.Map)1