Project

General

Profile

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.VerificationActions;
7
import eu.dnetlib.openaire.user.utils.VerifyRecaptcha;
8
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
9
import org.apache.commons.validator.routines.EmailValidator;
10
import org.apache.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Value;
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 java.io.IOException;
22
import java.sql.Timestamp;
23
import java.util.Date;
24
import java.util.UUID;
25
/**
26
 * Created by kiatrop on 28/9/2017.
27
 */
28

    
29
public class ForgotPasswordServlet extends HttpServlet {
30

    
31
    @Autowired
32
    private LDAPActions ldapActions;
33

    
34
    @Autowired
35
    private VerificationActions verificationActions;
36

    
37
    @Autowired
38
    private EmailSender emailSender;
39

    
40
    @Value("${google.recaptcha.secret}")
41
    private String secret;
42

    
43
    @Value("${google.recaptcha.key}")
44
    private String sitekey;
45

    
46
    private Logger logger = Logger.getLogger(ForgotPasswordServlet.class);
47

    
48
    public void init(ServletConfig config) throws ServletException {
49
        super.init(config);
50
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
51
                config.getServletContext());
52
        config.getServletContext().setAttribute("sitekey", sitekey);
53

    
54
    }
55

    
56
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
57

    
58
        String formEmail = request.getParameter("email").trim();
59
        String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
60

    
61

    
62
        if (formEmail == null) {
63
            request.getSession().setAttribute("message", "Error reading email.");
64
            response.sendRedirect("./forgotPassword.jsp");
65
        } else if (formEmail.isEmpty()) {
66
            request.getSession().setAttribute("message", "Please enter your email.");
67
            response.sendRedirect("./forgotPassword.jsp");
68
        } else if (!EmailValidator.getInstance().isValid(formEmail)) {
69
            request.getSession().setAttribute("message", "Please enter a valid email.");
70
            response.sendRedirect("./forgotPassword.jsp");
71
        } else if (!VerifyRecaptcha.verify(gRecaptchaResponse, secret)) {
72
            request.getSession().setAttribute("message", "You missed the reCAPTCHA validation!");
73
            response.sendRedirect("./forgotPassword.jsp");
74
        } else {
75
            try {
76
                String username = ldapActions.getUsername(formEmail);
77
                if (username == null || username.isEmpty()) {
78
                    request.getSession().setAttribute("message", "There is no user registered with that email.");
79
                    response.sendRedirect("./forgotPassword.jsp");
80
                } else {
81
                    UUID verificationCode = UUID.randomUUID();
82
                    Date creationDate = new Date();
83
                    String vCode = verificationCode.toString();
84

    
85
                    Timestamp timestamp = new Timestamp(creationDate.getTime());
86

    
87
                    if (!verificationActions.verificationEntryExists(username)) {
88
                        verificationActions.addVerificationEntry(username, vCode, timestamp);
89

    
90
                    } else {
91
                        verificationActions.updateVerificationEntry(username, vCode, timestamp);
92
                    }
93

    
94
                    String resultPath = UrlConstructor.getRedirectUrl(request, "verify.jsp");
95
                    String resultPathWithVCode = UrlConstructor.getVerificationLink(resultPath, vCode);
96

    
97
                    String verificationCodeMsg = "<p>Hello,</p>" +
98
                            "<p> A request has been made to reset your OpenAIRE account password. To reset your " +
99
                            "password, you will need to submit this verification code in order to verify that the " +
100
                            "request was legitimate.</p>" +
101
                            "<p> The verification code is " + vCode + "</p>" +
102
                            "Click the URL below and proceed with verification." +
103
                            "<p><a href=" + resultPathWithVCode + ">" + resultPathWithVCode + "</a></p>" +
104
                            "<p>The verification code is valid for 24 hours.</p>" +
105
                            "<p>Thank you,</p>" +
106
                            "<p>OpenAIRE technical team</p>";
107

    
108
                    String verificationCodeSubject = "Your OpenAIRE password reset request";
109

    
110
                    emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
111

    
112
                    response.setContentType("text/html");
113
                    response.sendRedirect("./verify.jsp");
114
                }
115

    
116
            } catch (LDAPException ldape) {
117
                logger.error("LDAP error", ldape);
118
                response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
119
            } catch (MessagingException e) {
120
                logger.error("Error in sending email", e);
121
                request.getSession().setAttribute("message", "Error sending email.");
122
                response.sendRedirect("./forgotPassword.jsp");
123
            }
124
        }
125
    }
126
}
(3-3/18)