1 |
49590
|
sofia.balt
|
package eu.dnetlib.openaire.usermanagement;
|
2 |
|
|
|
3 |
51770
|
katerina.i
|
import com.unboundid.ldap.sdk.LDAPException;
|
4 |
49683
|
sofia.balt
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
5 |
51986
|
sofia.balt
|
import eu.dnetlib.openaire.user.utils.InputValidator;
|
6 |
51695
|
sofia.balt
|
import org.apache.commons.validator.routines.EmailValidator;
|
7 |
49590
|
sofia.balt
|
import eu.dnetlib.openaire.user.utils.LDAPActions;
|
8 |
|
|
import eu.dnetlib.openaire.user.utils.VerificationActions;
|
9 |
51770
|
katerina.i
|
import eu.dnetlib.openaire.user.utils.VerifyRecaptcha;
|
10 |
51139
|
sofia.balt
|
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
|
11 |
49590
|
sofia.balt
|
import org.apache.log4j.Logger;
|
12 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
13 |
51770
|
katerina.i
|
import org.springframework.beans.factory.annotation.Value;
|
14 |
49590
|
sofia.balt
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
15 |
|
|
|
16 |
51770
|
katerina.i
|
import javax.mail.MessagingException;
|
17 |
|
|
import javax.mail.internet.AddressException;
|
18 |
|
|
import javax.mail.internet.InternetAddress;
|
19 |
49590
|
sofia.balt
|
import javax.servlet.ServletConfig;
|
20 |
|
|
import javax.servlet.ServletException;
|
21 |
|
|
import javax.servlet.http.HttpServlet;
|
22 |
|
|
import javax.servlet.http.HttpServletRequest;
|
23 |
|
|
import javax.servlet.http.HttpServletResponse;
|
24 |
|
|
import java.io.IOException;
|
25 |
|
|
import java.io.PrintWriter;
|
26 |
49683
|
sofia.balt
|
import java.sql.Timestamp;
|
27 |
|
|
import java.util.Date;
|
28 |
|
|
import java.util.UUID;
|
29 |
49590
|
sofia.balt
|
|
30 |
|
|
/**
|
31 |
|
|
* Created by sofia on 20/10/2017.
|
32 |
|
|
*/
|
33 |
|
|
public class RegisterServlet extends HttpServlet {
|
34 |
|
|
|
35 |
|
|
@Autowired
|
36 |
|
|
private VerificationActions verificationActions;
|
37 |
|
|
|
38 |
|
|
@Autowired
|
39 |
49683
|
sofia.balt
|
private EmailSender emailSender;
|
40 |
|
|
|
41 |
|
|
@Autowired
|
42 |
49590
|
sofia.balt
|
private LDAPActions ldapActions;
|
43 |
|
|
|
44 |
51770
|
katerina.i
|
@Value("${google.recaptcha.secret}")
|
45 |
|
|
private String secret;
|
46 |
49590
|
sofia.balt
|
|
47 |
51770
|
katerina.i
|
@Value("${google.recaptcha.key}")
|
48 |
|
|
private String sitekey;
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
public void init(ServletConfig config) throws ServletException {
|
52 |
|
|
super.init(config);
|
53 |
|
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
54 |
|
|
config.getServletContext());
|
55 |
|
|
config.getServletContext().setAttribute("sitekey", sitekey);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
private static Logger logger = Logger.getLogger(RegisterServlet.class);
|
59 |
|
|
|
60 |
|
|
@Override
|
61 |
49590
|
sofia.balt
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
62 |
|
|
response.setContentType("text/html");
|
63 |
|
|
PrintWriter printWriter = response.getWriter();
|
64 |
|
|
|
65 |
49833
|
sofia.balt
|
String firstName = request.getParameter("first_name").trim();
|
66 |
|
|
String lastName = request.getParameter("last_name").trim();
|
67 |
|
|
String organization = request.getParameter("organization").trim();
|
68 |
|
|
String username = request.getParameter("username").trim();
|
69 |
|
|
String email =request.getParameter("email").trim();
|
70 |
|
|
String confirmEmail = request.getParameter("email_conf").trim();
|
71 |
49769
|
sofia.balt
|
String password = request.getParameter("password");
|
72 |
|
|
String confirmPassword = request.getParameter("password_conf");
|
73 |
51772
|
katerina.i
|
|
74 |
51770
|
katerina.i
|
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
|
75 |
49590
|
sofia.balt
|
|
76 |
51772
|
katerina.i
|
boolean isRecaptchaVerified = VerifyRecaptcha.verify(gRecaptchaResponse, secret);
|
77 |
51770
|
katerina.i
|
//System.out.println("RESPONSE " + gRecaptchaResponse);
|
78 |
|
|
|
79 |
49590
|
sofia.balt
|
if (organization == null){
|
80 |
|
|
logger.info("organization is null");
|
81 |
|
|
}
|
82 |
49744
|
sofia.balt
|
|
83 |
51770
|
katerina.i
|
if (firstName != null && lastName != null && username != null && email!= null &&
|
84 |
|
|
email.equals(confirmEmail) && password!= null && password.equals(confirmPassword) &&
|
85 |
51986
|
sofia.balt
|
EmailValidator.getInstance().isValid(email) && InputValidator.isValidPassword(password) && isRecaptchaVerified) {
|
86 |
|
|
|
87 |
49590
|
sofia.balt
|
try {
|
88 |
51986
|
sofia.balt
|
if (InputValidator.isValidUsername(username) && !ldapActions.usernameExists(username) && !ldapActions.emailExists(email)
|
89 |
51695
|
sofia.balt
|
&& !ldapActions.isZombieUsersEmail(email) && !ldapActions.isZombieUsersUsername(username) && EmailValidator.getInstance().isValid(email)) {
|
90 |
49741
|
sofia.balt
|
|
91 |
49826
|
sofia.balt
|
ldapActions.createZombieUser(username, email, firstName, lastName, organization, password);
|
92 |
49833
|
sofia.balt
|
logger.info("Zombie user successfully created");
|
93 |
49683
|
sofia.balt
|
|
94 |
49698
|
sofia.balt
|
UUID verificationCode = UUID.randomUUID();
|
95 |
|
|
Date creationDate = new Date();
|
96 |
51775
|
sofia.balt
|
String vCode = verificationCode.toString();
|
97 |
49683
|
sofia.balt
|
|
98 |
49698
|
sofia.balt
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
99 |
49683
|
sofia.balt
|
|
100 |
49698
|
sofia.balt
|
if (!verificationActions.verificationEntryExists(username)) {
|
101 |
51775
|
sofia.balt
|
verificationActions.addVerificationEntry(username, vCode, timestamp);
|
102 |
49683
|
sofia.balt
|
|
103 |
49698
|
sofia.balt
|
} else {
|
104 |
51775
|
sofia.balt
|
verificationActions.updateVerificationEntry(username, vCode, timestamp);
|
105 |
49698
|
sofia.balt
|
}
|
106 |
49683
|
sofia.balt
|
|
107 |
51139
|
sofia.balt
|
String resultPath = UrlConstructor.getRedirectUrl(request, "activate.jsp");
|
108 |
51775
|
sofia.balt
|
String resultPathWithVCode = UrlConstructor.getVerificationLink(resultPath, vCode);
|
109 |
49683
|
sofia.balt
|
|
110 |
49835
|
sofia.balt
|
String verificationCodeMsg = "<p>Hello " + username + ",</p>" +
|
111 |
49836
|
sofia.balt
|
"<p> A request has been made to verify your email and activate your OpenAIRE account. To activate your " +
|
112 |
52050
|
sofia.balt
|
"account, you will need to submit your username and this activation code in order to verify that the " +
|
113 |
49835
|
sofia.balt
|
"request was legitimate.</p>" +
|
114 |
|
|
"<p>" +
|
115 |
51775
|
sofia.balt
|
"The activation code is " + vCode +
|
116 |
49835
|
sofia.balt
|
"</p>" +
|
117 |
51774
|
sofia.balt
|
"Click the URL below and proceed with activating your password." +
|
118 |
51775
|
sofia.balt
|
"<p><a href=" + resultPathWithVCode + ">" + resultPathWithVCode + "</a></p>" +
|
119 |
52050
|
sofia.balt
|
"<p>The activation code is valid for 24 hours.</p>" +
|
120 |
|
|
"<p>Thank you,</p>" +
|
121 |
|
|
"<p>OpenAIRE technical team</p>";
|
122 |
49835
|
sofia.balt
|
|
123 |
49700
|
sofia.balt
|
String verificationCodeSubject = "Activate your OpenAIRE account";
|
124 |
49683
|
sofia.balt
|
|
125 |
49698
|
sofia.balt
|
emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
|
126 |
49683
|
sofia.balt
|
|
127 |
49747
|
sofia.balt
|
response.sendRedirect("./activate.jsp");
|
128 |
49683
|
sofia.balt
|
|
129 |
49698
|
sofia.balt
|
} else {
|
130 |
49683
|
sofia.balt
|
|
131 |
51772
|
katerina.i
|
validateUsername(request, username);
|
132 |
49744
|
sofia.balt
|
|
133 |
49846
|
sofia.balt
|
if (ldapActions.usernameExists(username) || ldapActions.isZombieUsersUsername(username)) {
|
134 |
49698
|
sofia.balt
|
request.getSession().setAttribute("username_message", "Username already exists! Choose another one.");
|
135 |
|
|
logger.info("Username already exists");
|
136 |
|
|
}
|
137 |
|
|
|
138 |
51772
|
katerina.i
|
if (!EmailValidator.getInstance().isValid(email)) {
|
139 |
|
|
request.getSession().setAttribute("email_message", "Please enter a valid email.");
|
140 |
|
|
logger.info("Invalid email.");
|
141 |
|
|
}
|
142 |
|
|
|
143 |
49698
|
sofia.balt
|
if (ldapActions.emailExists(email)) {
|
144 |
|
|
request.getSession().setAttribute("email_message", "There is another user with this email.");
|
145 |
|
|
logger.info("There is another user with this email");
|
146 |
|
|
}
|
147 |
|
|
|
148 |
49846
|
sofia.balt
|
if (ldapActions.isZombieUsersEmail(email)) {
|
149 |
|
|
request.getSession().setAttribute("email_message", "You have already registered with this email address! Please check your email to activate your account or contact OpenAIRE <a href=\"https://www.openaire.eu/support/helpdesk\">helpdesk</a>.");
|
150 |
|
|
logger.info("There is another user with this email");
|
151 |
|
|
}
|
152 |
|
|
|
153 |
49747
|
sofia.balt
|
request.getSession().setAttribute("first_name", firstName);
|
154 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_first_name_error_display", "display:none");
|
155 |
|
|
|
156 |
49747
|
sofia.balt
|
request.getSession().setAttribute("last_name", lastName);
|
157 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_last_name_error_display", "display:none");
|
158 |
|
|
|
159 |
49747
|
sofia.balt
|
request.getSession().setAttribute("organization", organization);
|
160 |
|
|
request.getSession().setAttribute("username", username);
|
161 |
|
|
request.getSession().setAttribute("email", email);
|
162 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_email_error_display", "display:none" );
|
163 |
|
|
|
164 |
49747
|
sofia.balt
|
request.getSession().setAttribute("email_conf", confirmEmail);
|
165 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_email_conf_error_display", "display:none");
|
166 |
|
|
request.getSession().setAttribute("msg_email_validation_error_display", "display:none");
|
167 |
49747
|
sofia.balt
|
|
168 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_password_error_display", "display:none" );
|
169 |
|
|
request.getSession().setAttribute("msg_pass_conf_error_display", "display:none" );
|
170 |
|
|
request.getSession().setAttribute("msg_invalid_password_display", "display:none");
|
171 |
|
|
|
172 |
|
|
request.getSession().setAttribute("recaptcha_error_display", "display:none");
|
173 |
|
|
|
174 |
49698
|
sofia.balt
|
response.sendRedirect("./register.jsp");
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
|
178 |
51770
|
katerina.i
|
} catch (MessagingException e) {
|
179 |
|
|
logger.error("Error in sending email", e);
|
180 |
|
|
request.getSession().setAttribute("message","Error sending email");
|
181 |
51986
|
sofia.balt
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, ".register.jsp"));
|
182 |
51770
|
katerina.i
|
|
183 |
|
|
//TODO better handling of these exceprions
|
184 |
51986
|
sofia.balt
|
} catch (Exception e) {
|
185 |
49590
|
sofia.balt
|
logger.error("LDAP error in creating user", e);
|
186 |
51139
|
sofia.balt
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
|
187 |
49590
|
sofia.balt
|
}
|
188 |
51770
|
katerina.i
|
|
189 |
|
|
} else {
|
190 |
|
|
|
191 |
|
|
request.getSession().setAttribute("first_name", firstName);
|
192 |
|
|
request.getSession().setAttribute("last_name", lastName);
|
193 |
|
|
request.getSession().setAttribute("organization", organization);
|
194 |
|
|
request.getSession().setAttribute("username", username);
|
195 |
|
|
request.getSession().setAttribute("email", email);
|
196 |
|
|
request.getSession().setAttribute("email_conf", confirmEmail);
|
197 |
|
|
|
198 |
51986
|
sofia.balt
|
if (!InputValidator.isFilled(firstName)) {
|
199 |
51772
|
katerina.i
|
logger.info("No first name");
|
200 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_first_name_error_display", "display:block" );
|
201 |
|
|
}
|
202 |
|
|
|
203 |
51986
|
sofia.balt
|
if (!InputValidator.isFilled(lastName)) {
|
204 |
51772
|
katerina.i
|
logger.info("No last name");
|
205 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_last_name_error_display", "display:block" );
|
206 |
|
|
}
|
207 |
|
|
|
208 |
51986
|
sofia.balt
|
if (!InputValidator.isFilled(username)) {
|
209 |
51770
|
katerina.i
|
request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
|
210 |
51772
|
katerina.i
|
logger.info("No username");
|
211 |
51770
|
katerina.i
|
|
212 |
|
|
} else {
|
213 |
|
|
validateUsername(request, username);
|
214 |
|
|
}
|
215 |
|
|
|
216 |
51986
|
sofia.balt
|
if (!InputValidator.isFilled(password)) {
|
217 |
51772
|
katerina.i
|
logger.info("No valid password");
|
218 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_password_error_display", "display:block" );
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
if(!EmailValidator.getInstance().isValid(email)) {
|
222 |
51772
|
katerina.i
|
logger.info("No valid e-mail");
|
223 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_email_validation_error_display", "display:block");
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
if (!email.equals(confirmEmail)) {
|
227 |
51772
|
katerina.i
|
logger.info("No matching e-mails");
|
228 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_email_conf_error_display", "display:block" );
|
229 |
|
|
}
|
230 |
|
|
|
231 |
51986
|
sofia.balt
|
if(!InputValidator.isValidPassword(password)) {
|
232 |
51772
|
katerina.i
|
logger.info("No valid password");
|
233 |
|
|
request.getSession().setAttribute("msg_invalid_password_display", "display:block");
|
234 |
|
|
}
|
235 |
|
|
|
236 |
51770
|
katerina.i
|
if (!password.equals(confirmPassword)){
|
237 |
51772
|
katerina.i
|
logger.info("No matching passwords");
|
238 |
51770
|
katerina.i
|
request.getSession().setAttribute("msg_pass_conf_error_display", "display:block" );
|
239 |
|
|
}
|
240 |
|
|
|
241 |
51772
|
katerina.i
|
if (!isRecaptchaVerified) {
|
242 |
|
|
logger.info("No valid recaptcha");
|
243 |
51770
|
katerina.i
|
request.getSession().setAttribute("recaptcha_error_display", "display:block" );
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
response.sendRedirect("./register.jsp");
|
247 |
|
|
|
248 |
49590
|
sofia.balt
|
}
|
249 |
51770
|
katerina.i
|
|
250 |
49590
|
sofia.balt
|
printWriter.close();
|
251 |
|
|
|
252 |
|
|
}
|
253 |
51770
|
katerina.i
|
|
254 |
|
|
private void validateUsername(HttpServletRequest request, String username) {
|
255 |
|
|
|
256 |
51986
|
sofia.balt
|
if(!InputValidator.isValidUsername(username)) {
|
257 |
|
|
logger.info("No valid username");
|
258 |
51770
|
katerina.i
|
|
259 |
51986
|
sofia.balt
|
if (InputValidator.containsLessCharsThan(5,username)) {
|
260 |
51772
|
katerina.i
|
request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
|
261 |
|
|
logger.info("Minimum username length 5 characters.");
|
262 |
|
|
}
|
263 |
51770
|
katerina.i
|
|
264 |
51986
|
sofia.balt
|
if (InputValidator.containsMoreCharsThan(150,username)) {
|
265 |
51772
|
katerina.i
|
request.getSession().setAttribute("username_message", "Maximum username length 150 characters.");
|
266 |
|
|
logger.info("Maximum username length 150 characters.");
|
267 |
|
|
}
|
268 |
|
|
|
269 |
51986
|
sofia.balt
|
if (!InputValidator.containsOnlyAllowedChars(username)) {
|
270 |
51772
|
katerina.i
|
request.getSession().setAttribute("username_allowed_chars_message", "You can use letters, numbers, underscores, hyphens and periods.");
|
271 |
|
|
logger.info("Only letters, numbers, underscores, hyphens and periods.");
|
272 |
|
|
}
|
273 |
|
|
|
274 |
51986
|
sofia.balt
|
if (!InputValidator.startsWithLetterOrDigit(username)) {
|
275 |
51772
|
katerina.i
|
request.getSession().setAttribute("username_first_char_message", "The username must start with letter or digit.");
|
276 |
|
|
logger.info("The username must start with letter or digit.");
|
277 |
|
|
}
|
278 |
|
|
}
|
279 |
51770
|
katerina.i
|
}
|
280 |
49590
|
sofia.balt
|
}
|