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());
}
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()));
}
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();
}
}
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();
}
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);
}
Aggregations