Search in sources :

Example 1 with PasswordResetTokenEntity

use of py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity in project FP-PSP-SERVER by FundacionParaguaya.

the class PasswordResetTokenServiceImpl method validatePasswordResetToken.

@Override
public void validatePasswordResetToken(String token, Long userId, String password, String repeatPassword) {
    checkArgument(userId > 0, i18n.translate("argument.nonNegative", userId));
    UserEntity userEntity = userRepository.findOne(userId);
    if (userEntity == null) {
        throw new UnknownResourceException(i18n.translate("user.notExist", userId));
    }
    PasswordResetTokenEntity passwordResetTokenEntity = passwordTokenRepository.findByToken(token);
    if (passwordResetTokenEntity == null || passwordResetTokenEntity.getUser().getId().longValue() != userId.longValue()) {
        throw new CustomParameterizedException(i18n.translate("email.invalidToken", token));
    }
    Calendar cal = Calendar.getInstance();
    if ((passwordResetTokenEntity.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
        throw new CustomParameterizedException(i18n.translate("email.expiredToken", token));
    }
    if (!password.equals(repeatPassword)) {
        throw new CustomParameterizedException("Password does not match the confirm password");
    }
    userEntity.setPass(encryptPassword(repeatPassword));
    userRepository.save(userEntity);
}
Also used : PasswordResetTokenEntity(py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) Calendar(java.util.Calendar) UnknownResourceException(py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)

Example 2 with PasswordResetTokenEntity

use of py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity in project FP-PSP-SERVER by FundacionParaguaya.

the class PasswordResetTokenServiceImpl method createPasswordResetTokenForUser.

@Override
public void createPasswordResetTokenForUser(UserEntity user, String token) {
    PasswordResetTokenEntity myToken = new PasswordResetTokenEntity(token, user);
    LocalDateTime expirationLocalDate = LocalDateTime.now().plusMinutes(PasswordResetTokenEntity.getExpiration());
    Instant instant = expirationLocalDate.toInstant(ZoneOffset.UTC);
    Date expirationDate = Date.from(instant);
    myToken.setExpiryDate(expirationDate);
    passwordTokenRepository.save(myToken);
}
Also used : LocalDateTime(java.time.LocalDateTime) PasswordResetTokenEntity(py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity) Instant(java.time.Instant) Date(java.util.Date)

Aggregations

PasswordResetTokenEntity (py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity)2 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)1 UnknownResourceException (py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)1 UserEntity (py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)1