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.mail.MessagingException;
|
12
|
import javax.servlet.RequestDispatcher;
|
13
|
import javax.servlet.ServletConfig;
|
14
|
import javax.servlet.ServletException;
|
15
|
import javax.servlet.http.HttpServlet;
|
16
|
import javax.servlet.http.HttpServletRequest;
|
17
|
import javax.servlet.http.HttpServletResponse;
|
18
|
import java.io.IOException;
|
19
|
import java.sql.Timestamp;
|
20
|
import java.util.Date;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
/**
|
24
|
* Created by kiatrop on 28/9/2017.
|
25
|
*/
|
26
|
|
27
|
public class ForgotPasswordServlet extends HttpServlet {
|
28
|
|
29
|
public void init(ServletConfig config) throws ServletException {
|
30
|
super.init(config);
|
31
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
32
|
config.getServletContext());
|
33
|
}
|
34
|
|
35
|
@Autowired
|
36
|
private LDAPActions ldapActions;
|
37
|
|
38
|
@Autowired
|
39
|
private VerificationActions verificationActions;
|
40
|
|
41
|
@Autowired
|
42
|
private EmailSender emailSender;
|
43
|
|
44
|
private Logger logger = Logger.getLogger(ForgotPasswordServlet.class);
|
45
|
|
46
|
|
47
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
48
|
|
49
|
String formEmail = request.getParameter("email");
|
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
|
if (username == null) {
|
60
|
request.getSession().setAttribute("message", "User does not exist.");
|
61
|
response.sendRedirect("./forgotPassword.jsp");
|
62
|
|
63
|
} else {
|
64
|
UUID verificationCode = UUID.randomUUID();
|
65
|
Date creationDate = new Date();
|
66
|
|
67
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
68
|
|
69
|
if (!verificationActions.verificationEntryExists(username)) {
|
70
|
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
|
71
|
|
72
|
} else {
|
73
|
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
|
74
|
}
|
75
|
|
76
|
String verificationCodeMsg = "Hello,\n" +
|
77
|
"\n" +
|
78
|
"A request has been made to reset your OpenAIRE account password. To reset your\n" +
|
79
|
"password, you will need to submit this verification code in order to verify that the\n" +
|
80
|
"request was legitimate.\n" +
|
81
|
"\n" +
|
82
|
"The verification code is " + verificationCode.toString() + "\n Thank you";
|
83
|
|
84
|
String verificationCodeSubject = "Your OpenAIRE password reset request";
|
85
|
|
86
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
|
87
|
}
|
88
|
|
89
|
} catch (LDAPException ldape) {
|
90
|
logger.error("LDAP error", ldape);
|
91
|
response.sendRedirect("./error.jsp");
|
92
|
|
93
|
} catch (MessagingException e) {
|
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
|
}
|