1 |
52050
|
sofia.balt
|
package eu.dnetlib.openaire.usermanagement;
|
2 |
|
|
|
3 |
|
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
4 |
|
|
import eu.dnetlib.openaire.user.utils.LDAPActions;
|
5 |
|
|
import eu.dnetlib.openaire.user.utils.VerificationActions;
|
6 |
|
|
import eu.dnetlib.openaire.user.utils.VerifyRecaptcha;
|
7 |
|
|
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
|
8 |
|
|
import org.apache.log4j.Logger;
|
9 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
10 |
|
|
import org.springframework.beans.factory.annotation.Value;
|
11 |
|
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
12 |
|
|
|
13 |
|
|
import javax.mail.MessagingException;
|
14 |
|
|
import javax.servlet.ServletConfig;
|
15 |
|
|
import javax.servlet.ServletException;
|
16 |
|
|
import javax.servlet.http.HttpServlet;
|
17 |
|
|
import javax.servlet.http.HttpServletRequest;
|
18 |
|
|
import javax.servlet.http.HttpServletResponse;
|
19 |
|
|
import javax.servlet.http.HttpSession;
|
20 |
|
|
import java.io.IOException;
|
21 |
|
|
import java.sql.Timestamp;
|
22 |
|
|
import java.util.Date;
|
23 |
|
|
import java.util.UUID;
|
24 |
|
|
|
25 |
|
|
/**
|
26 |
|
|
* Created by sofia on 14/5/2018.
|
27 |
|
|
*/
|
28 |
|
|
public class RequestActivationCodeServlet extends HttpServlet {
|
29 |
|
|
|
30 |
|
|
@Autowired
|
31 |
|
|
private VerificationActions verificationActions;
|
32 |
|
|
|
33 |
|
|
@Autowired
|
34 |
|
|
private LDAPActions ldapActions;
|
35 |
|
|
|
36 |
|
|
@Autowired
|
37 |
|
|
private EmailSender emailSender;
|
38 |
|
|
|
39 |
|
|
@Value("${oidc.home}")
|
40 |
|
|
private String oidcHomeUrl;
|
41 |
|
|
|
42 |
|
|
@Value("${google.recaptcha.secret}")
|
43 |
|
|
private String secret;
|
44 |
|
|
|
45 |
|
|
@Value("${google.recaptcha.key}")
|
46 |
|
|
private String sitekey;
|
47 |
|
|
|
48 |
|
|
private static final Logger logger = Logger.getLogger(RequestActivationCodeServlet.class);
|
49 |
|
|
|
50 |
|
|
public void init(ServletConfig config) throws ServletException {
|
51 |
|
|
super.init(config);
|
52 |
|
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
53 |
|
|
config.getServletContext());
|
54 |
|
|
config.getServletContext().setAttribute("sitekey", sitekey);
|
55 |
|
|
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
@Override
|
59 |
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
60 |
|
|
String formUsername = request.getParameter("username").trim();
|
61 |
|
|
|
62 |
|
|
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
|
63 |
|
|
|
64 |
|
|
HttpSession session = request.getSession();
|
65 |
|
|
session.setAttribute("homeUrl", oidcHomeUrl);
|
66 |
|
|
|
67 |
|
|
if (formUsername == null) {
|
68 |
|
|
request.getSession().setAttribute("message", "Error reading username.");
|
69 |
|
|
response.sendRedirect("./requestActivationCode.jsp");
|
70 |
|
|
|
71 |
|
|
} else if (formUsername.isEmpty()) {
|
72 |
|
|
request.getSession().setAttribute("message", "Please enter your username.");
|
73 |
|
|
response.sendRedirect("./requestActivationCode.jsp");
|
74 |
|
|
|
75 |
|
|
} else if (!VerifyRecaptcha.verify(gRecaptchaResponse, secret)) {
|
76 |
|
|
request.getSession().setAttribute("reCAPTCHA_message", "You missed the reCAPTCHA validation!");
|
77 |
|
|
response.sendRedirect("./requestActivationCode.jsp");
|
78 |
|
|
|
79 |
|
|
} else {
|
80 |
|
|
|
81 |
|
|
try {
|
82 |
|
|
if (ldapActions.isZombieUsersUsername(formUsername)) {
|
83 |
|
|
logger.info("User " + formUsername + " is zombie user!");
|
84 |
|
|
|
85 |
|
|
UUID verificationCode = UUID.randomUUID();
|
86 |
|
|
Date creationDate = new Date();
|
87 |
|
|
String vCode = verificationCode.toString();
|
88 |
|
|
|
89 |
|
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
90 |
|
|
|
91 |
|
|
if (!verificationActions.verificationEntryExists(formUsername)) {
|
92 |
|
|
verificationActions.addVerificationEntry(formUsername, vCode, timestamp);
|
93 |
|
|
|
94 |
|
|
} else {
|
95 |
|
|
verificationActions.updateVerificationEntry(formUsername, vCode, timestamp);
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
String resultPath = UrlConstructor.getRedirectUrl(request, "activate.jsp");
|
99 |
|
|
String resultPathWithVCode = UrlConstructor.getVerificationLink(resultPath, vCode);
|
100 |
|
|
|
101 |
|
|
String verificationCodeMsg = "<p>Hello " + formUsername + ",</p>" +
|
102 |
|
|
"<p> A request has been made to get a new activation code to verify your email and activate your OpenAIRE account. To activate your " +
|
103 |
|
|
"account, you will need to submit your username and this activation code in order to verify that the " +
|
104 |
|
|
"request was legitimate.</p>" +
|
105 |
|
|
"<p>" +
|
106 |
|
|
"The activation code is " + vCode +
|
107 |
|
|
"</p>" +
|
108 |
|
|
"Click the URL below and proceed with activating your password." +
|
109 |
|
|
"<p><a href=" + resultPathWithVCode + ">" + resultPathWithVCode + "</a></p>" +
|
110 |
|
|
"<p>The activation code is valid for 24 hours.</p>" +
|
111 |
|
|
"<p>Thank you,</p>" +
|
112 |
|
|
"<p>OpenAIRE technical team</p>";
|
113 |
|
|
|
114 |
|
|
String verificationCodeSubject = "Request a new activation code for your OpenAIRE account";
|
115 |
|
|
|
116 |
|
|
String email = ldapActions.getZombieUsersEmail(formUsername);
|
117 |
|
|
|
118 |
|
|
if (email != null && !email.isEmpty()) {
|
119 |
|
|
emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
|
120 |
|
|
logger.info("Sending activation code to user: " + formUsername);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
response.sendRedirect("./activate.jsp");
|
124 |
|
|
|
125 |
|
|
} else if (ldapActions.usernameExists(formUsername)) {
|
126 |
|
|
logger.info("User " + formUsername + " has already activated his account.");
|
127 |
|
|
request.getSession().setAttribute("message", "Your account is already activated.");
|
128 |
|
|
response.sendRedirect("./requestActivationCode.jsp");
|
129 |
|
|
|
130 |
|
|
} else {
|
131 |
|
|
logger.info("No user with username: " + formUsername);
|
132 |
|
|
request.getSession().setAttribute("message", "There is no user registered with that username.");
|
133 |
|
|
response.sendRedirect("./requestActivationCode.jsp");
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
} catch (MessagingException e) {
|
137 |
|
|
logger.error("Error in sending email", e);
|
138 |
|
|
request.getSession().setAttribute("message", "Error sending email");
|
139 |
|
|
response.sendRedirect("./requestActivationCode.jsp");
|
140 |
|
|
} catch (Exception ldape) {
|
141 |
|
|
logger.error("Could not find zombie user with username " + formUsername, ldape);
|
142 |
|
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
public String getOidcHomeUrl() {
|
148 |
|
|
return oidcHomeUrl;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
public void setOidcHomeUrl(String oidcHomeUrl) {
|
152 |
|
|
this.oidcHomeUrl = oidcHomeUrl;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
}
|