1 | package org.expeditee.auth.account;
|
---|
2 |
|
---|
3 | import java.io.FileNotFoundException;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.security.KeyStoreException;
|
---|
6 | import java.security.NoSuchAlgorithmException;
|
---|
7 | import java.security.cert.CertificateException;
|
---|
8 | import java.sql.SQLException;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Properties;
|
---|
11 |
|
---|
12 | import javax.crypto.SecretKey;
|
---|
13 | import javax.mail.Message;
|
---|
14 | import javax.mail.MessagingException;
|
---|
15 | import javax.mail.PasswordAuthentication;
|
---|
16 | import javax.mail.Session;
|
---|
17 | import javax.mail.Transport;
|
---|
18 | import javax.mail.internet.InternetAddress;
|
---|
19 | import javax.mail.internet.MimeMessage;
|
---|
20 |
|
---|
21 | import org.expeditee.auth.AuthenticatorBrowser;
|
---|
22 | import org.expeditee.auth.tags.AuthenticationTag;
|
---|
23 | import org.expeditee.gui.DisplayController;
|
---|
24 | import org.expeditee.gui.FrameIO;
|
---|
25 | import org.expeditee.gui.MessageBay;
|
---|
26 |
|
---|
27 | public class Password {
|
---|
28 | /*
|
---|
29 | * Changes the recorded password for a user in the key store.
|
---|
30 | */
|
---|
31 | public static void changePassword(Map<AuthenticationTag, String> userdata) throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, IOException, ClassNotFoundException, SQLException {
|
---|
32 | String username = userdata.get(AuthenticationTag.Username);
|
---|
33 | String password = userdata.get(AuthenticationTag.Password);
|
---|
34 | String newpassword = userdata.get(AuthenticationTag.NewPassword);
|
---|
35 |
|
---|
36 | final SecretKey key = AuthenticatorBrowser.getInstance().getSecretKey(username, password);
|
---|
37 | if (key == null) {
|
---|
38 | MessageBay.errorMessage("The username + existing password combination was incorrect.");
|
---|
39 | } else {
|
---|
40 | AuthenticatorBrowser.getInstance().putKey(username, newpassword, key);
|
---|
41 | MessageBay.displayMessage("Password changed successfully.");
|
---|
42 | DisplayController.setCurrentFrame(FrameIO.LoadFrame("multiuser1"), true);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public static void generateAndDeliverIntergalacticNumber(Map<AuthenticationTag, String> userData) {
|
---|
47 | String username = userData.get(AuthenticationTag.Username);
|
---|
48 | String email = userData.get(AuthenticationTag.Email);
|
---|
49 | try {
|
---|
50 | // Generate message text.
|
---|
51 | String intergalacticNumber = AuthenticatorBrowser.getInstance().newIntergalacticNumber(username, email);
|
---|
52 | String nl = System.getProperty("line.separator");
|
---|
53 | StringBuilder sb = new StringBuilder();
|
---|
54 | sb.append("You are receiving this email because someone is attempting to reset your Expeditee password." + nl);
|
---|
55 | sb.append("If you did not make this request then no action is required." + nl);
|
---|
56 | sb.append("If it was you who made this request, the following string of characters is your intergalactic number: " + intergalacticNumber + nl);
|
---|
57 |
|
---|
58 | // Establish properties for email.
|
---|
59 | Properties properties = System.getProperties();
|
---|
60 | properties.setProperty("mail.transport.protocol", "smtp");
|
---|
61 | properties.setProperty("mail.smtp.host", "smtp.gmail.com");
|
---|
62 | properties.setProperty("mail.smtp.port", "465");
|
---|
63 | properties.setProperty("mail.smtp.starttls.enable", "true");
|
---|
64 | properties.setProperty("mail.smtp.auth", "true");
|
---|
65 | properties.setProperty("mail.smtp.debug", "true");
|
---|
66 | properties.setProperty("mail.smtp.auth", "true");
|
---|
67 | properties.setProperty("mail.smtp.socketFactory.port", "465");
|
---|
68 | properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
---|
69 | properties.setProperty("mail.smtp.socketFactory.fallback", "false");
|
---|
70 |
|
---|
71 | Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
|
---|
72 | @Override
|
---|
73 | protected PasswordAuthentication getPasswordAuthentication() {
|
---|
74 | return new PasswordAuthentication("noreply.expeditee", "intergalacticnumber");
|
---|
75 | };
|
---|
76 | });
|
---|
77 |
|
---|
78 | // construct email message
|
---|
79 | final MimeMessage message = new MimeMessage(session);
|
---|
80 | message.setFrom(new InternetAddress("[email protected]"));
|
---|
81 | message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
|
---|
82 | message.setSubject("Expeditee Password Recovery");
|
---|
83 | message.setText(sb.toString());
|
---|
84 |
|
---|
85 | // send email message
|
---|
86 | Transport.send(message);
|
---|
87 | } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | ClassNotFoundException
|
---|
88 | | IOException | SQLException | MessagingException e) {
|
---|
89 | e.printStackTrace();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|