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.VerifyRecaptcha;
|
7
|
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
|
8
|
import org.apache.commons.validator.routines.EmailValidator;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.beans.factory.annotation.Value;
|
12
|
import org.springframework.stereotype.Component;
|
13
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
14
|
|
15
|
import javax.mail.MessagingException;
|
16
|
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
|
import javax.servlet.http.HttpSession;
|
22
|
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
|
@Autowired
|
34
|
private EmailSender emailSender;
|
35
|
|
36
|
@Value("${oidc.home}")
|
37
|
private String oidcHomeUrl;
|
38
|
|
39
|
@Value("${google.recaptcha.secret}")
|
40
|
private String secret;
|
41
|
|
42
|
@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
|
@Override
|
56
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
57
|
String formEmail = request.getParameter("email").trim();
|
58
|
|
59
|
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
|
60
|
|
61
|
HttpSession session = request.getSession();
|
62
|
session.setAttribute("homeUrl", oidcHomeUrl);
|
63
|
|
64
|
if (formEmail == null) {
|
65
|
request.getSession().setAttribute("message", "Error reading email.");
|
66
|
response.sendRedirect("./remindUsername.jsp");
|
67
|
|
68
|
} else if (formEmail.isEmpty()) {
|
69
|
request.getSession().setAttribute("message", "Please enter your email.");
|
70
|
response.sendRedirect("./remindUsername.jsp");
|
71
|
|
72
|
} else if (!EmailValidator.getInstance().isValid(formEmail)) {
|
73
|
request.getSession().setAttribute("message", "Please enter a valid email.");
|
74
|
response.sendRedirect("./remindUsername.jsp");
|
75
|
|
76
|
} else if (!VerifyRecaptcha.verify(gRecaptchaResponse, secret)) {
|
77
|
request.getSession().setAttribute("message", "You missed the reCAPTCHA validation!");
|
78
|
response.sendRedirect("./remindUsername.jsp");
|
79
|
|
80
|
} else {
|
81
|
|
82
|
try {
|
83
|
String username = ldapActions.getUsername(formEmail);
|
84
|
if (username != null && !username.isEmpty()) {
|
85
|
logger.info("Remind username to: "+username);
|
86
|
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
|
"<p> Thank you, </p>" +
|
90
|
"<p> OpenAIRE technical team</p>";
|
91
|
|
92
|
String verificationCodeSubject = "Your OpenAIRE username";
|
93
|
|
94
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
|
95
|
|
96
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "emailSuccess.jsp"));
|
97
|
|
98
|
} else {
|
99
|
request.getSession().setAttribute("message", "There is no user registered with that email.");
|
100
|
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
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "remindUsername.jsp"));
|
111
|
}
|
112
|
}
|
113
|
}
|
114
|
|
115
|
public String getOidcHomeUrl() {
|
116
|
return oidcHomeUrl;
|
117
|
}
|
118
|
|
119
|
public void setOidcHomeUrl(String oidcHomeUrl) {
|
120
|
this.oidcHomeUrl = oidcHomeUrl;
|
121
|
}
|
122
|
|
123
|
}
|
124
|
|
125
|
|