1 |
49196
|
katerina.i
|
package eu.dnetlib.openaire.usermanagement;
|
2 |
|
|
|
3 |
|
|
import com.unboundid.ldap.sdk.LDAPException;
|
4 |
49406
|
sofia.balt
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
5 |
49196
|
katerina.i
|
import eu.dnetlib.openaire.user.utils.LDAPActions;
|
6 |
51770
|
katerina.i
|
import eu.dnetlib.openaire.user.utils.VerifyRecaptcha;
|
7 |
51139
|
sofia.balt
|
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
|
8 |
51770
|
katerina.i
|
import org.apache.commons.validator.routines.EmailValidator;
|
9 |
49196
|
katerina.i
|
import org.apache.log4j.Logger;
|
10 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
11 |
51202
|
sofia.balt
|
import org.springframework.beans.factory.annotation.Value;
|
12 |
49196
|
katerina.i
|
import org.springframework.stereotype.Component;
|
13 |
|
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
14 |
|
|
|
15 |
49412
|
sofia.balt
|
import javax.mail.MessagingException;
|
16 |
49196
|
katerina.i
|
import javax.servlet.ServletConfig;
|
17 |
|
|
import javax.servlet.ServletException;
|
18 |
|
|
import javax.servlet.http.HttpServlet;
|
19 |
|
|
import javax.servlet.http.HttpServletRequest;
|
20 |
|
|
import javax.servlet.http.HttpServletResponse;
|
21 |
51202
|
sofia.balt
|
import javax.servlet.http.HttpSession;
|
22 |
49196
|
katerina.i
|
import java.io.IOException;
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* Created by kiatrop on 2/10/2017.
|
26 |
|
|
*/
|
27 |
|
|
@Component
|
28 |
|
|
public class RemindUsernameServlet extends HttpServlet {
|
29 |
|
|
|
30 |
|
|
@Autowired
|
31 |
|
|
private LDAPActions ldapActions;
|
32 |
|
|
|
33 |
49406
|
sofia.balt
|
@Autowired
|
34 |
|
|
private EmailSender emailSender;
|
35 |
49196
|
katerina.i
|
|
36 |
51202
|
sofia.balt
|
@Value("${oidc.home}")
|
37 |
|
|
private String oidcHomeUrl;
|
38 |
|
|
|
39 |
51770
|
katerina.i
|
@Value("${google.recaptcha.secret}")
|
40 |
|
|
private String secret;
|
41 |
49196
|
katerina.i
|
|
42 |
51770
|
katerina.i
|
@Value("${google.recaptcha.key}")
|
43 |
|
|
private String sitekey;
|
44 |
|
|
|
45 |
|
|
private static final Logger logger = Logger.getLogger(RemindUsernameServlet.class);
|
46 |
|
|
|
47 |
|
|
public void init(ServletConfig config) throws ServletException {
|
48 |
|
|
super.init(config);
|
49 |
|
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
50 |
|
|
config.getServletContext());
|
51 |
|
|
config.getServletContext().setAttribute("sitekey", sitekey);
|
52 |
|
|
|
53 |
|
|
}
|
54 |
|
|
|
55 |
49196
|
katerina.i
|
@Override
|
56 |
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
57 |
49833
|
sofia.balt
|
String formEmail = request.getParameter("email").trim();
|
58 |
49196
|
katerina.i
|
|
59 |
51770
|
katerina.i
|
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
|
60 |
|
|
|
61 |
51202
|
sofia.balt
|
HttpSession session = request.getSession();
|
62 |
|
|
session.setAttribute("homeUrl", oidcHomeUrl);
|
63 |
|
|
|
64 |
51770
|
katerina.i
|
if (formEmail == null) {
|
65 |
49196
|
katerina.i
|
request.getSession().setAttribute("message", "Error reading email.");
|
66 |
|
|
response.sendRedirect("./remindUsername.jsp");
|
67 |
|
|
|
68 |
51770
|
katerina.i
|
} else if (formEmail.isEmpty()) {
|
69 |
|
|
request.getSession().setAttribute("message", "Please enter your email.");
|
70 |
|
|
response.sendRedirect("./remindUsername.jsp");
|
71 |
49196
|
katerina.i
|
|
72 |
51770
|
katerina.i
|
} else if (!EmailValidator.getInstance().isValid(formEmail)) {
|
73 |
|
|
request.getSession().setAttribute("message", "Please enter a valid email.");
|
74 |
|
|
response.sendRedirect("./remindUsername.jsp");
|
75 |
49406
|
sofia.balt
|
|
76 |
51770
|
katerina.i
|
} else if (!VerifyRecaptcha.verify(gRecaptchaResponse, secret)) {
|
77 |
|
|
request.getSession().setAttribute("message", "You missed the reCAPTCHA validation!");
|
78 |
|
|
response.sendRedirect("./remindUsername.jsp");
|
79 |
49406
|
sofia.balt
|
|
80 |
51770
|
katerina.i
|
} else {
|
81 |
49406
|
sofia.balt
|
|
82 |
51770
|
katerina.i
|
try {
|
83 |
|
|
String username = ldapActions.getUsername(formEmail);
|
84 |
|
|
if (username != null && !username.isEmpty()) {
|
85 |
51986
|
sofia.balt
|
logger.info("Remind username to: "+username);
|
86 |
51770
|
katerina.i
|
String verificationCodeMsg = "<p>Hello,</p>" +
|
87 |
|
|
"<p> A username reminder has been requested for your OpenAIRE account.</p>" +
|
88 |
|
|
"<p> Your username is " + username + ".</p>" +
|
89 |
52050
|
sofia.balt
|
"<p> Thank you, </p>" +
|
90 |
|
|
"<p> OpenAIRE technical team</p>";
|
91 |
49196
|
katerina.i
|
|
92 |
51770
|
katerina.i
|
String verificationCodeSubject = "Your OpenAIRE username";
|
93 |
49412
|
sofia.balt
|
|
94 |
51770
|
katerina.i
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
|
95 |
|
|
|
96 |
|
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "emailSuccess.jsp"));
|
97 |
|
|
|
98 |
|
|
} else {
|
99 |
51986
|
sofia.balt
|
request.getSession().setAttribute("message", "There is no user registered with that email.");
|
100 |
51770
|
katerina.i
|
response.sendRedirect("./remindUsername.jsp");
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
} catch (LDAPException ldape) {
|
104 |
|
|
logger.error("Could not find user with email " + formEmail, ldape);
|
105 |
|
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
|
106 |
|
|
|
107 |
|
|
} catch (MessagingException e) {
|
108 |
|
|
logger.error("Error in sending email", e);
|
109 |
|
|
request.getSession().setAttribute("message", "Error sending email");
|
110 |
51986
|
sofia.balt
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "remindUsername.jsp"));
|
111 |
51770
|
katerina.i
|
}
|
112 |
49196
|
katerina.i
|
}
|
113 |
|
|
}
|
114 |
|
|
|
115 |
51202
|
sofia.balt
|
public String getOidcHomeUrl() {
|
116 |
|
|
return oidcHomeUrl;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
public void setOidcHomeUrl(String oidcHomeUrl) {
|
120 |
|
|
this.oidcHomeUrl = oidcHomeUrl;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
49196
|
katerina.i
|
}
|
124 |
51770
|
katerina.i
|
|