1
|
package eu.dnetlib.openaire.usermanagement;
|
2
|
|
3
|
import com.unboundid.ldap.sdk.LDAPException;
|
4
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
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
|
logger.debug("I am in do post for " + formEmail);
|
50
|
|
51
|
if (formEmail == null) {
|
52
|
request.getSession().setAttribute("message", "Error reading email.");
|
53
|
response.sendRedirect("./forgotPassword.jsp");
|
54
|
}
|
55
|
|
56
|
try {
|
57
|
|
58
|
String username = ldapActions.getUsername(formEmail);
|
59
|
|
60
|
if (username == null) {
|
61
|
request.getSession().setAttribute("message", "User does not exist.");
|
62
|
response.sendRedirect("./forgotPassword.jsp");
|
63
|
|
64
|
} else {
|
65
|
UUID verificationCode = UUID.randomUUID();
|
66
|
Date creationDate = new Date();
|
67
|
|
68
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
69
|
|
70
|
//logger.info("verificationCode = " + verificationCode);
|
71
|
|
72
|
if (!verificationActions.verificationEntryExists(username)) {
|
73
|
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
|
74
|
|
75
|
} else {
|
76
|
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
|
77
|
}
|
78
|
|
79
|
String verificationCodeMsg = "Hello,\n" +
|
80
|
"\n" +
|
81
|
"A request has been made to reset your OpenAIRE account password. To reset your\n" +
|
82
|
"password, you will need to submit this verification code in order to verify that the\n" +
|
83
|
"request was legitimate.\n" +
|
84
|
"\n" +
|
85
|
"The verification code is" + verificationCode.toString() + "\n Thank you";
|
86
|
|
87
|
String verificationCodeSubject = "Your OpenAIRE password reset request";
|
88
|
|
89
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
|
90
|
|
91
|
}
|
92
|
|
93
|
|
94
|
} catch (LDAPException ldape) {
|
95
|
//TODO create error page
|
96
|
request.getSession().setAttribute("message", "Error sending email.");
|
97
|
response.sendRedirect("./forgotPassword.jsp");
|
98
|
}
|
99
|
|
100
|
response.setContentType("text/html");
|
101
|
response.sendRedirect("./verify.jsp");
|
102
|
}
|
103
|
|
104
|
}
|