1
|
package eu.dnetlib.openaire.usermanagement;
|
2
|
|
3
|
import com.unboundid.ldap.sdk.LDAPException;
|
4
|
import eu.dnetlib.openaire.user.utils.EmailActions;
|
5
|
import eu.dnetlib.openaire.user.utils.LDAPActions;
|
6
|
import eu.dnetlib.openaire.user.utils.VerificationActions;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
10
|
|
11
|
import javax.servlet.ServletConfig;
|
12
|
import javax.servlet.ServletException;
|
13
|
import javax.servlet.http.HttpServlet;
|
14
|
import javax.servlet.http.HttpServletRequest;
|
15
|
import javax.servlet.http.HttpServletResponse;
|
16
|
import java.io.IOException;
|
17
|
import java.sql.Timestamp;
|
18
|
import java.util.Date;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
/**
|
22
|
* Created by kiatrop on 28/9/2017.
|
23
|
*/
|
24
|
|
25
|
public class ForgotPasswordServlet extends HttpServlet {
|
26
|
|
27
|
public void init(ServletConfig config) throws ServletException {
|
28
|
super.init(config);
|
29
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
30
|
config.getServletContext());
|
31
|
}
|
32
|
|
33
|
@Autowired
|
34
|
private LDAPActions ldapActions;
|
35
|
|
36
|
@Autowired
|
37
|
private VerificationActions verificationActions;
|
38
|
|
39
|
@Autowired
|
40
|
private EmailSender emailSender;
|
41
|
|
42
|
private Logger logger = Logger.getLogger(ForgotPasswordServlet.class);
|
43
|
|
44
|
|
45
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
46
|
|
47
|
String formEmail = request.getParameter("email");
|
48
|
|
49
|
if (formEmail == null) {
|
50
|
request.getSession().setAttribute("message", "Error reading email.");
|
51
|
response.sendRedirect("./forgotPassword.jsp");
|
52
|
}
|
53
|
|
54
|
try {
|
55
|
|
56
|
String username = ldapActions.getUsername(formEmail);
|
57
|
|
58
|
if (username == null) {
|
59
|
request.getSession().setAttribute("message", "User does not exist.");
|
60
|
response.sendRedirect("./forgotPassword.jsp");
|
61
|
|
62
|
} else {
|
63
|
UUID verificationCode = UUID.randomUUID();
|
64
|
Date creationDate = new Date();
|
65
|
|
66
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
67
|
|
68
|
//logger.info("verificationCode = " + verificationCode);
|
69
|
|
70
|
if (!verificationActions.verificationEntryExists(username)) {
|
71
|
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
|
72
|
|
73
|
} else {
|
74
|
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
|
75
|
}
|
76
|
|
77
|
String verificationCodeMsg = "Hello,\n" +
|
78
|
"\n" +
|
79
|
"A request has been made to reset your OpenAIRE account password. To reset your\n" +
|
80
|
"password, you will need to submit this verification code in order to verify that the\n" +
|
81
|
"request was legitimate.\n" +
|
82
|
"\n" +
|
83
|
"The verification code is" + verificationCode.toString() + "\n Thank you";
|
84
|
|
85
|
verificationCodeSubject = "Your OpenAIRE password reset request";
|
86
|
|
87
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg)
|
88
|
|
89
|
}
|
90
|
|
91
|
|
92
|
} catch (LDAPException ldape) {
|
93
|
//TODO create error page
|
94
|
request.getSession().setAttribute("message", "Error sending email.");
|
95
|
response.sendRedirect("./forgotPassword.jsp");
|
96
|
}
|
97
|
|
98
|
response.setContentType("text/html");
|
99
|
response.sendRedirect("./verify.jsp");
|
100
|
}
|
101
|
|
102
|
}
|