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 |
|
|
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 |
49349
|
sofia.balt
|
import java.sql.Timestamp;
|
18 |
49196
|
katerina.i
|
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 |
49349
|
sofia.balt
|
@Autowired
|
40 |
|
|
private EmailSender emailSender;
|
41 |
49196
|
katerina.i
|
|
42 |
|
|
private Logger logger = Logger.getLogger(ForgotPasswordServlet.class);
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
46 |
|
|
|
47 |
49197
|
katerina.i
|
String formEmail = request.getParameter("email");
|
48 |
49196
|
katerina.i
|
|
49 |
49360
|
sofia.balt
|
logger.debug("I am in do post for " + formEmail);
|
50 |
|
|
|
51 |
49196
|
katerina.i
|
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 |
49196
|
katerina.i
|
|
60 |
49199
|
katerina.i
|
if (username == null) {
|
61 |
49196
|
katerina.i
|
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 |
49349
|
sofia.balt
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
69 |
49196
|
katerina.i
|
|
70 |
49349
|
sofia.balt
|
//logger.info("verificationCode = " + verificationCode);
|
71 |
|
|
|
72 |
|
|
if (!verificationActions.verificationEntryExists(username)) {
|
73 |
|
|
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
|
74 |
|
|
|
75 |
49196
|
katerina.i
|
} else {
|
76 |
49349
|
sofia.balt
|
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
|
77 |
49196
|
katerina.i
|
}
|
78 |
|
|
|
79 |
49349
|
sofia.balt
|
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 |
49196
|
katerina.i
|
|
87 |
49360
|
sofia.balt
|
String verificationCodeSubject = "Your OpenAIRE password reset request";
|
88 |
49349
|
sofia.balt
|
|
89 |
49360
|
sofia.balt
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
|
90 |
49349
|
sofia.balt
|
|
91 |
49196
|
katerina.i
|
}
|
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 |
|
|
}
|