1 |
49196
|
katerina.i
|
package eu.dnetlib.openaire.usermanagement;
|
2 |
|
|
|
3 |
|
|
import com.unboundid.ldap.sdk.LDAPException;
|
4 |
49360
|
sofia.balt
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
5 |
49196
|
katerina.i
|
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 |
49412
|
sofia.balt
|
import javax.mail.MessagingException;
|
12 |
49406
|
sofia.balt
|
import javax.servlet.RequestDispatcher;
|
13 |
49196
|
katerina.i
|
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 |
49349
|
sofia.balt
|
import java.sql.Timestamp;
|
20 |
49196
|
katerina.i
|
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 |
49349
|
sofia.balt
|
@Autowired
|
42 |
|
|
private EmailSender emailSender;
|
43 |
49196
|
katerina.i
|
|
44 |
|
|
private Logger logger = Logger.getLogger(ForgotPasswordServlet.class);
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
48 |
|
|
|
49 |
49197
|
katerina.i
|
String formEmail = request.getParameter("email");
|
50 |
49196
|
katerina.i
|
|
51 |
|
|
if (formEmail == null) {
|
52 |
|
|
request.getSession().setAttribute("message", "Error reading email.");
|
53 |
|
|
response.sendRedirect("./forgotPassword.jsp");
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
try {
|
57 |
49349
|
sofia.balt
|
|
58 |
49199
|
katerina.i
|
String username = ldapActions.getUsername(formEmail);
|
59 |
49657
|
sofia.balt
|
if (username == null || username.isEmpty()) {
|
60 |
49196
|
katerina.i
|
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 |
49349
|
sofia.balt
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
68 |
49196
|
katerina.i
|
|
69 |
49349
|
sofia.balt
|
if (!verificationActions.verificationEntryExists(username)) {
|
70 |
|
|
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
|
71 |
|
|
|
72 |
49196
|
katerina.i
|
} else {
|
73 |
49349
|
sofia.balt
|
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
|
74 |
49196
|
katerina.i
|
}
|
75 |
|
|
|
76 |
49349
|
sofia.balt
|
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 |
49406
|
sofia.balt
|
"The verification code is " + verificationCode.toString() + "\n Thank you";
|
83 |
49196
|
katerina.i
|
|
84 |
49360
|
sofia.balt
|
String verificationCodeSubject = "Your OpenAIRE password reset request";
|
85 |
49349
|
sofia.balt
|
|
86 |
49360
|
sofia.balt
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
|
87 |
49657
|
sofia.balt
|
|
88 |
|
|
response.setContentType("text/html");
|
89 |
|
|
response.sendRedirect("./verify.jsp");
|
90 |
49196
|
katerina.i
|
}
|
91 |
|
|
|
92 |
|
|
} catch (LDAPException ldape) {
|
93 |
49415
|
sofia.balt
|
logger.error("LDAP error", ldape);
|
94 |
49412
|
sofia.balt
|
response.sendRedirect("./error.jsp");
|
95 |
|
|
|
96 |
|
|
} catch (MessagingException e) {
|
97 |
49196
|
katerina.i
|
request.getSession().setAttribute("message", "Error sending email.");
|
98 |
|
|
response.sendRedirect("./forgotPassword.jsp");
|
99 |
|
|
}
|
100 |
49683
|
sofia.balt
|
|
101 |
49196
|
katerina.i
|
}
|
102 |
|
|
|
103 |
|
|
}
|