/src/java/ForgotPassword.java

https://github.com/aaronmgross/Heinz-Disaster-Response-Mobile-Application · Java · 132 lines · 105 code · 22 blank · 5 comment · 6 complexity · af61a53c9f0e2c75fc8929e31d7491a9 MD5 · raw file

  1. import com.mysql.jdbc.Connection;
  2. import java.io.IOException;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import DataDAO.*;
  12. import java.util.Properties;
  13. import java.util.Random;
  14. import javax.mail.*;
  15. import javax.mail.internet.*;
  16. import javax.servlet.RequestDispatcher;
  17. /**
  18. *
  19. * @author Yao
  20. */
  21. public class ForgotPassword extends HttpServlet {
  22. private static Connection con;
  23. private String user = null;
  24. private String pw_con = null;
  25. /*Get the database user and password from config file*/
  26. @Override
  27. public void init() throws ServletException {
  28. user = getInitParameter("dbUser");
  29. pw_con = getInitParameter("dbPassword");
  30. }
  31. @Override
  32. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  33. throws ServletException, IOException {
  34. String email = request.getParameter("email");
  35. User u = null;
  36. try {
  37. Class.forName("com.mysql.jdbc.Driver");
  38. } catch (ClassNotFoundException e) {
  39. throw new AssertionError(e);
  40. }
  41. String connectionStr = "jdbc:mysql://localhost/DisasterAssessment";
  42. try {
  43. con = (Connection) DriverManager.getConnection(connectionStr, user, pw_con);
  44. u = User.lookup(email, con);
  45. } catch (SQLException ex) {
  46. Logger.getLogger(ForgotPassword.class.getName()).log(Level.SEVERE, null, ex);
  47. }
  48. String message;
  49. if (u != null) {
  50. String newPassword = genNewPassword();
  51. u.setPassword(newPassword);
  52. try {
  53. u.update(con, u.getUserId());
  54. } catch (SQLException ex) {
  55. System.out.println("failed to update the new password.");
  56. }
  57. sendEmail(email, newPassword);
  58. message = "Your new password has been sent to your email.";
  59. } else {
  60. message = "Your email doesn't exsit.";
  61. }
  62. request.setAttribute("RegisterMessage", message);
  63. RequestDispatcher red = getServletContext().getRequestDispatcher("/index.jsp");
  64. red.forward(request, response);
  65. }
  66. private void sendEmail(String toAddress, String newPassword) {
  67. Properties props = new Properties();
  68. props.put("mail.smtp.host", "smtp.gmail.com");
  69. props.put("mail.smtp.socketFactory.port", "465");
  70. props.put("mail.smtp.socketFactory.class",
  71. "javax.net.ssl.SSLSocketFactory");
  72. props.put("mail.smtp.auth", "true");
  73. props.put("mail.smtp.port", "465");
  74. Session session = Session.getInstance(props,
  75. new javax.mail.Authenticator() {
  76. protected PasswordAuthentication getPasswordAuthentication() {
  77. return new PasswordAuthentication("disasterassessment", "disasterassessment");
  78. }
  79. });
  80. try {
  81. Message message = new MimeMessage(session);
  82. message.setFrom(new InternetAddress("account_update@redcross.org"));
  83. message.setRecipients(Message.RecipientType.TO,
  84. InternetAddress.parse(toAddress));
  85. message.setSubject("Disaster Assessment App Password Assistance");
  86. message.setText("We received a request to reset the password associated with this e-mail address"
  87. + "\n\n Your new password is: " + newPassword
  88. + "\n\n Please log in using the new password and reset it as soon as possible. ");
  89. Transport.send(message);
  90. System.out.println("Done");
  91. } catch (MessagingException e) {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. private String genNewPassword() {
  96. char[] symbols = new char[36];
  97. char[] buf = new char[8];
  98. Random random = new Random();
  99. for (int idx = 0; idx < 10; ++idx) {
  100. symbols[idx] = (char) ('0' + idx);
  101. }
  102. for (int idx = 10; idx < 36; ++idx) {
  103. symbols[idx] = (char) ('a' + idx - 10);
  104. }
  105. for (int idx = 0; idx < buf.length; ++idx) {
  106. buf[idx] = symbols[random.nextInt(symbols.length)];
  107. }
  108. return new String(buf);
  109. }
  110. }