Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. // guava cache field
  2. private Cache<String, User> resetCache = CacheBuilder.newBuilder()
  3. .expireAfterWrite(15, TimeUnit.MINUTES) // token will expire after 15 minutes
  4. .build();
  5.  
  6. // password reset request
  7. public Pair<String, String> resetPassword(String email) {
  8. User user = userRepository.findByEmail(email);
  9.  
  10. if (isNull(user)) {
  11. throw new IllegalArgumentException(String.format("Email address '%s' not found!", email));
  12. }
  13.  
  14. String token = encryptionService.generateToken();
  15. resetCache.put(token, user);
  16.  
  17. return Pair.of(user.getUsername(), token);
  18. }
  19.  
  20. // actual password reset
  21. public void resetPasswordWithToken(String token, String newPassword) {
  22. // look for user in cache
  23. User user = resetCache.getIfPresent(token);
  24.  
  25. // if not found, the token is invalid
  26. if (isNull(user)) {
  27. throw new IllegalArgumentException("Invalid Email reset token!");
  28. }
  29.  
  30. user.setPassword(encryptionService.encode(newPassword));
  31. userRepository.save(user);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement