/
PasswordService.java
104 lines (81 loc) · 2.94 KB
/
PasswordService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.nonononoki.alovoa.service;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.nonononoki.alovoa.entity.User;
import com.nonononoki.alovoa.entity.user.UserPasswordToken;
import com.nonononoki.alovoa.model.AlovoaException;
import com.nonononoki.alovoa.model.PasswordChangeDto;
import com.nonononoki.alovoa.model.PasswordResetDto;
import com.nonononoki.alovoa.repo.UserPasswordTokenRepository;
import com.nonononoki.alovoa.repo.UserRepository;
@Service
public class PasswordService {
@Autowired
private UserPasswordTokenRepository userPasswordTokenRepo;
@Autowired
private UserRepository userRepo;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private CaptchaService captchaService;
@Autowired
private AuthService authService;
@Autowired
private MailService mailService;
@Value("${app.password-token.length}")
private int tokenLength;
public UserPasswordToken resetPasword(PasswordResetDto dto)
throws AlovoaException, NoSuchAlgorithmException, MessagingException, IOException {
User u = authService.getCurrentUser();
if (u == null) {
if (!captchaService.isValid(dto.getCaptchaId(), dto.getCaptchaText())) {
throw new AlovoaException("captcha_invalid");
}
u = userRepo.findByEmail(dto.getEmail().toLowerCase());
if (u == null) {
throw new DisabledException("user_not_found");
}
if (u.isDisabled()) {
throw new DisabledException("user_disabled");
}
}
UserPasswordToken token = new UserPasswordToken();
token.setContent(RandomStringUtils.randomAlphanumeric(tokenLength));
token.setDate(new Date());
token.setUser(u);
u.setPasswordToken(token);
u = userRepo.saveAndFlush(u);
mailService.sendPasswordResetMail(u);
return u.getPasswordToken();
}
public void changePasword(PasswordChangeDto dto, HttpSession session) throws AlovoaException {
UserPasswordToken token = userPasswordTokenRepo.findByContent(dto.getToken());
if (token == null) {
throw new AlovoaException("token_not_found");
}
if (!token.getContent().equals(dto.getToken())) {
throw new AlovoaException("token_wrong_content");
}
User user = token.getUser();
if (!user.getEmail().equals(dto.getEmail().toLowerCase())) {
throw new AlovoaException("wrong_email");
}
user.setPassword(passwordEncoder.encode(dto.getPassword()));
user.setPasswordToken(null);
if (!user.isConfirmed()) {
user.setConfirmed(true);
user.setRegisterToken(null);
}
userRepo.saveAndFlush(user);
session.invalidate();
}
}