Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // guava cache field
- private Cache<String, User> resetCache = CacheBuilder.newBuilder()
- .expireAfterWrite(15, TimeUnit.MINUTES) // token will expire after 15 minutes
- .build();
- // password reset request
- public Pair<String, String> resetPassword(String email) {
- User user = userRepository.findByEmail(email);
- if (isNull(user)) {
- throw new IllegalArgumentException(String.format("Email address '%s' not found!", email));
- }
- String token = encryptionService.generateToken();
- resetCache.put(token, user);
- return Pair.of(user.getUsername(), token);
- }
- // actual password reset
- public void resetPasswordWithToken(String token, String newPassword) {
- // look for user in cache
- User user = resetCache.getIfPresent(token);
- // if not found, the token is invalid
- if (isNull(user)) {
- throw new IllegalArgumentException("Invalid Email reset token!");
- }
- user.setPassword(encryptionService.encode(newPassword));
- userRepository.save(user);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement