/** * Check whether the given token code is valid. This method will also clear the token code if the token has expired. * * @param token * Token Code * @param maxTokenAgeMins * maximum allowed token age in minutes * @return */ default boolean isResetTokenValid(String token, int maxTokenAgeMins) { if (token == null) { return false; } long maxTokenAge = 1000 * 60 * maxTokenAgeMins; long tokenAge = System.currentTimeMillis() - getResetTokenIssueTimestamp(); boolean isExpired = tokenAge > maxTokenAge; boolean isTokenMatch = token.equals(getResetToken()); if (isTokenMatch && isExpired) { invalidateResetToken(); return false; } return isTokenMatch && !isExpired; }