use of site.model.ResetPasswordToken in project jprime by bgjug.
the class ResetPasswordService method createNewToken.
public String createNewToken(User user) {
String tokenId = getNewTokenId();
ResetPasswordToken resetPassToken = new ResetPasswordToken(user, tokenId);
String tokenShaHex = Sha512DigestUtils.shaHex(resetPassToken.getTokenId());
resetPassToken.setTokenId(tokenShaHex);
resetPassRepository.save(resetPassToken);
return tokenId;
}
use of site.model.ResetPasswordToken in project jprime by bgjug.
the class ResetPasswordService method setTokenToUsed.
public void setTokenToUsed(String tokenId) {
String tokenShaHex = Sha512DigestUtils.shaHex(tokenId);
ResetPasswordToken resetPasswordToken = resetPassRepository.findByTokenId(tokenShaHex);
User owner = resetPasswordToken.getOwner();
List<ResetPasswordToken> tokens = resetPassRepository.findAllByOwner(owner);
for (ResetPasswordToken token : tokens) {
token.setUsed(true);
}
resetPassRepository.saveAll(tokens);
}
use of site.model.ResetPasswordToken in project jprime by bgjug.
the class ResetPasswordService method checkTokenValidity.
/**
* @param tokenId
* @return User owning the token if tokenId is valid, return NULL if tokenId
* is not valid
*/
public User checkTokenValidity(String tokenId) {
String tokenShaHex = Sha512DigestUtils.shaHex(tokenId);
ResetPasswordToken resetPasswordToken = resetPassRepository.findByTokenId(tokenShaHex);
if (resetPasswordToken == null) {
logger.debug("ResetPasswordToken id=" + tokenId + " , ShaHex: " + tokenShaHex + " NOT found. This could be an attacker brute forcing the token!");
return null;
}
User owner = resetPasswordToken.getOwner();
if (resetPasswordToken.isUsed()) {
logger.debug("ResetPassworToken for user: " + owner + " with Id=" + tokenId + ", ShaHex: " + tokenShaHex + " is aleady used.");
return null;
}
DateTime createdDate = resetPasswordToken.getCreatedDate();
DateTime deadline = createdDate.plusHours(TOKEN_DURATION_IN_HOURS);
if (deadline.isBeforeNow()) {
logger.debug("ResetPassworToken for user: " + owner + " with Id=" + tokenId + ", ShaHex: " + tokenShaHex + " is expired.");
return null;
}
return owner;
}
Aggregations