dnet-openaire-users/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServlet.java

301 lines
14 KiB
Java
Raw Normal View History

2017-10-20 14:31:39 +02:00
package eu.dnetlib.openaire.usermanagement;
2018-04-12 12:12:32 +02:00
import com.unboundid.ldap.sdk.LDAPException;
2017-10-25 16:15:07 +02:00
import eu.dnetlib.openaire.user.utils.EmailSender;
import org.apache.commons.validator.routines.EmailValidator;
2017-10-20 14:31:39 +02:00
import eu.dnetlib.openaire.user.utils.LDAPActions;
import eu.dnetlib.openaire.user.utils.VerificationActions;
2018-04-12 12:12:32 +02:00
import eu.dnetlib.openaire.user.utils.VerifyRecaptcha;
2018-03-09 17:11:28 +01:00
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
2017-10-20 14:31:39 +02:00
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
2018-04-12 12:12:32 +02:00
import org.springframework.beans.factory.annotation.Value;
2017-10-20 14:31:39 +02:00
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
2018-04-12 12:12:32 +02:00
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
2017-10-20 14:31:39 +02:00
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
2017-10-25 16:15:07 +02:00
import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;
2017-10-20 14:31:39 +02:00
/**
* Created by sofia on 20/10/2017.
*/
public class RegisterServlet extends HttpServlet {
@Autowired
private VerificationActions verificationActions;
2017-10-25 16:15:07 +02:00
@Autowired
private EmailSender emailSender;
2017-10-20 14:31:39 +02:00
@Autowired
private LDAPActions ldapActions;
2018-04-12 12:12:32 +02:00
@Value("${google.recaptcha.secret}")
private String secret;
@Value("${google.recaptcha.key}")
private String sitekey;
2017-10-20 14:31:39 +02:00
2018-04-12 12:12:32 +02:00
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
config.getServletContext().setAttribute("sitekey", sitekey);
}
private static Logger logger = Logger.getLogger(RegisterServlet.class);
@Override
2017-10-20 14:31:39 +02:00
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
2017-11-07 11:56:43 +01:00
String firstName = request.getParameter("first_name").trim();
String lastName = request.getParameter("last_name").trim();
String organization = request.getParameter("organization").trim();
String username = request.getParameter("username").trim();
String email =request.getParameter("email").trim();
String confirmEmail = request.getParameter("email_conf").trim();
2017-11-01 11:58:15 +01:00
String password = request.getParameter("password");
String confirmPassword = request.getParameter("password_conf");
2018-04-12 12:12:32 +02:00
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
boolean isRecaptchaVerified = VerifyRecaptcha.verify(gRecaptchaResponse, secret);
2018-04-12 12:12:32 +02:00
//System.out.println("RESPONSE " + gRecaptchaResponse);
2017-10-20 14:31:39 +02:00
if (organization == null){
logger.info("organization is null");
}
2018-04-12 12:12:32 +02:00
if (firstName != null && lastName != null && username != null && email!= null &&
email.equals(confirmEmail) && password!= null && password.equals(confirmPassword) &&
EmailValidator.getInstance().isValid(email) && isValidPassword(password) && isRecaptchaVerified) {
2017-10-20 14:31:39 +02:00
try {
2017-10-25 16:15:07 +02:00
if (username.matches("^[a-zA-Z0-9][a-zA-Z0-9\\.\\_\\-]{4,150}") && !ldapActions.usernameExists(username) && !ldapActions.emailExists(email)
&& !ldapActions.isZombieUsersEmail(email) && !ldapActions.isZombieUsersUsername(username) && EmailValidator.getInstance().isValid(email)) {
ldapActions.createZombieUser(username, email, firstName, lastName, organization, password);
2017-11-07 11:56:43 +01:00
logger.info("Zombie user successfully created");
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
UUID verificationCode = UUID.randomUUID();
Date creationDate = new Date();
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
Timestamp timestamp = new Timestamp(creationDate.getTime());
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
if (!verificationActions.verificationEntryExists(username)) {
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
} else {
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
}
2017-10-25 16:15:07 +02:00
2018-03-09 17:11:28 +01:00
String resultPath = UrlConstructor.getRedirectUrl(request, "activate.jsp");
String verificationCodeMsg = "<p>Hello " + username + ",</p>" +
"<p> A request has been made to verify your email and activate your OpenAIRE account. To activate your " +
"account, you will need to submit your username and this activation code in order to verify that the" +
"request was legitimate.</p>" +
"<p>" +
"The activation code is " + verificationCode.toString() +
"</p>" +
"Click the URL below and proceed with activating your password." +
"<p><a href=" + resultPath + ">" + resultPath + "</a></p>" +
"<p>Thank you</p>";
2017-10-25 16:15:07 +02:00
2017-10-26 15:42:42 +02:00
String verificationCodeSubject = "Activate your OpenAIRE account";
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
response.sendRedirect("./activate.jsp");
2017-10-30 15:34:06 +01:00
2017-10-26 15:10:30 +02:00
} else {
validateUsername(request, username);
if (ldapActions.usernameExists(username) || ldapActions.isZombieUsersUsername(username)) {
2017-10-26 15:10:30 +02:00
request.getSession().setAttribute("username_message", "Username already exists! Choose another one.");
logger.info("Username already exists");
}
if (!EmailValidator.getInstance().isValid(email)) {
request.getSession().setAttribute("email_message", "Please enter a valid email.");
logger.info("Invalid email.");
}
if (ldapActions.emailExists(email)) {
request.getSession().setAttribute("email_message", "There is another user with this email.");
logger.info("There is another user with this email");
}
if (ldapActions.isZombieUsersEmail(email)) {
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>.");
logger.info("There is another user with this email");
}
2017-10-30 15:34:06 +01:00
request.getSession().setAttribute("first_name", firstName);
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_first_name_error_display", "display:none");
2017-10-30 15:34:06 +01:00
request.getSession().setAttribute("last_name", lastName);
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_last_name_error_display", "display:none");
2017-10-30 15:34:06 +01:00
request.getSession().setAttribute("organization", organization);
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("email", email);
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_email_error_display", "display:none" );
2017-10-30 15:34:06 +01:00
request.getSession().setAttribute("email_conf", confirmEmail);
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_email_conf_error_display", "display:none");
request.getSession().setAttribute("msg_email_validation_error_display", "display:none");
request.getSession().setAttribute("msg_password_error_display", "display:none" );
request.getSession().setAttribute("msg_pass_conf_error_display", "display:none" );
request.getSession().setAttribute("msg_invalid_password_display", "display:none");
request.getSession().setAttribute("recaptcha_error_display", "display:none");
2017-10-30 15:34:06 +01:00
2017-10-26 15:10:30 +02:00
response.sendRedirect("./register.jsp");
}
2017-10-25 16:15:07 +02:00
2018-04-12 12:12:32 +02:00
} catch (MessagingException e) {
logger.error("Error in sending email", e);
request.getSession().setAttribute("message","Error sending email");
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "./register.jsp"));
2018-04-12 12:12:32 +02:00
//response.sendRedirect("./error.jsp");
//TODO better handling of these exceprions
}catch (Exception e) {
2017-10-20 14:31:39 +02:00
logger.error("LDAP error in creating user", e);
2018-03-09 17:11:28 +01:00
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
//response.sendRedirect("./error.jsp");
2017-10-20 14:31:39 +02:00
}
2018-04-12 12:12:32 +02:00
} else {
request.getSession().setAttribute("first_name", firstName);
request.getSession().setAttribute("last_name", lastName);
request.getSession().setAttribute("organization", organization);
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("email", email);
request.getSession().setAttribute("email_conf", confirmEmail);
if (firstName == null || firstName.isEmpty()) {
logger.info("No first name");
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_first_name_error_display", "display:block" );
}
if (lastName == null || lastName.isEmpty()) {
logger.info("No last name");
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_last_name_error_display", "display:block" );
}
if (username == null || username.isEmpty()) {
request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
logger.info("No username");
2018-04-12 12:12:32 +02:00
} else {
validateUsername(request, username);
}
if (password == null || password.isEmpty()) {
logger.info("No valid password");
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_password_error_display", "display:block" );
}
if(!EmailValidator.getInstance().isValid(email)) {
logger.info("No valid e-mail");
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_email_validation_error_display", "display:block");
}
if (!email.equals(confirmEmail)) {
logger.info("No matching e-mails");
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_email_conf_error_display", "display:block" );
}
if(!isValidPassword(password)) {
logger.info("No valid password");
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("msg_invalid_password_display", "display:block");
}
if (!password.equals(confirmPassword)){
logger.info("No matching passwords");
request.getSession().setAttribute("msg_pass_conf_error_display", "display:block" );
}
if (!isRecaptchaVerified) {
logger.info("No valid recaptcha");
2018-04-12 12:12:32 +02:00
request.getSession().setAttribute("recaptcha_error_display", "display:block" );
}
response.sendRedirect("./register.jsp");
2017-10-20 14:31:39 +02:00
}
2018-04-12 12:12:32 +02:00
2017-10-20 14:31:39 +02:00
printWriter.close();
}
2018-04-12 12:12:32 +02:00
private void validateUsername(HttpServletRequest request, String username) {
if(!username.matches("^[a-zA-Z0-9][a-zA-Z0-9\\.\\_\\-]{4,150}")) {
2018-04-12 12:12:32 +02:00
logger.info("No valid username");
if (username.length() < 5) {
request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
logger.info("Minimum username length 5 characters.");
}
2018-04-12 12:12:32 +02:00
if (username.length() > 150) {
request.getSession().setAttribute("username_message", "Maximum username length 150 characters.");
logger.info("Maximum username length 150 characters.");
}
if (!username.matches("^[a-zA-Z0-9\\.\\_\\-]")) {
request.getSession().setAttribute("username_allowed_chars_message", "You can use letters, numbers, underscores, hyphens and periods.");
logger.info("Only letters, numbers, underscores, hyphens and periods.");
}
if (!username.matches("^[a-zA-Z0-9].*")) {
request.getSession().setAttribute("username_first_char_message", "The username must start with letter or digit.");
logger.info("The username must start with letter or digit.");
}
}
2018-04-12 12:12:32 +02:00
}
public static boolean isValidPassword(String password) {
/*
^ # start-of-string
(?=.*[0-9]) # a digit must occur at least once
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
(?=.*[@#$%^&+=]) # a special character must occur at least once. This has been removed.
# Please add if special character is needed.
2018-04-12 12:12:32 +02:00
(?=\S+$) # no whitespace allowed in the entire string
.{8,} # anything, at least eight places though
$ # end-of-string
*/
if (password.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+$).{6,}$")) {
2018-04-12 12:12:32 +02:00
logger.info("Valid password!");
return true;
}
logger.info("Not valid password!");
return false;
}
2017-10-20 14:31:39 +02:00
}