Ignore:
Timestamp:
01/29/20 13:20:24 (4 years ago)
Author:
bnemhaus
Message:

Revised implementation of authenticated Expeditee mail. Motivated by bugs relating to messages not being marked as read and incorrect counting of new messages for users, the Expeditee mail system has been rewritten. The new code not only does not exhibit the previous bugs but is also better engineered. Whilst the MailBay is static (which is in line with the MessageBay), the Mail class is no longer static and must be initialised for each user as they log in.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/auth/Actions.java

    r1494 r1504  
    99import java.nio.file.StandardCopyOption;
    1010import java.security.InvalidKeyException;
    11 import java.security.KeyFactory;
    1211import java.security.KeyStoreException;
    1312import java.security.NoSuchAlgorithmException;
    14 import java.security.PrivateKey;
    1513import java.security.PublicKey;
    1614import java.security.SecureRandom;
    1715import java.security.cert.CertificateException;
    1816import java.security.spec.InvalidKeySpecException;
    19 import java.security.spec.PKCS8EncodedKeySpec;
    2017import java.sql.SQLException;
    2118import java.text.ParseException;
    2219import java.util.Base64;
    2320import java.util.Collection;
     21import java.util.Date;
    2422import java.util.HashMap;
    2523import java.util.List;
     
    5755import org.expeditee.items.Text;
    5856import org.expeditee.settings.UserSettings;
    59 import org.expeditee.settings.identity.secrets.KeyList;
    60 import org.expeditee.stats.Formatter;
    6157
    6258public class Actions implements CryptographyConstants {
    63        
    64         //      Start Debug Actions
    65         public static void SendTestMessage(String colleagueName) throws InvalidKeySpecException, NoSuchAlgorithmException, FileNotFoundException, KeyStoreException, CertificateException, ClassNotFoundException, IOException, SQLException {
    66                 String time = org.expeditee.stats.Formatter.getDateTime();
     59
     60        // Start Debug Actions
     61        public static void SendTestMessage(String recipient) {
     62                String time = Mail.FORMAT.format(new Date());
    6763                String sender = UserSettings.UserName.get();
    6864                String topic = "Test Message";
     
    7066                Map<String, String> options = new HashMap<String, String>();
    7167                options.put("Neat", "Beep");
    72                 MailEntry mail = new MailEntry(time, sender, colleagueName, topic, message, options);
    73                 Mail.sendMail(mail, colleagueName);
     68                Mail mailClient = MailBay.getMailClient();
     69                Mail.MailEntry mail = mailClient.new MailEntry(time, sender, recipient, topic, message, options);
     70                mailClient.sendMail(mail, recipient);
    7471                MessageBay.displayMessage("Test message sent.");
    7572        }
    76        
    77         public static void SendTestMessageHemi(String param) {
    78                 String time = Formatter.getDateTime();
    79                 String sender = UserSettings.UserName.get();
    80                 String recipient = param.split(" ")[0];
    81                 String message = param.split(" ")[1];
    82                 Map<String, String> options = new HashMap<String, String>();
    83                 options.put("Accept", "beep");
    84                 options.put("Reject", "beep");
    85                 MailEntry mail = new MailEntry(time, sender, recipient, "Have a key", message, options);
    86                 Mail.sendMail(mail, recipient);
    87                 MessageBay.displayMessage("Test message sent.");
    88         }
    89        
    90         public static void SendTestOneOffMessage(String colleagueName) {
    91                 String time = Formatter.getDateTime();
     73               
     74        public static void CheckMailTest() {
     75                List<Mail.MailEntry> mail = MailBay.getMailClient().checkMail();
     76                for (Mail.MailEntry entry: mail) {
     77                        MessageBay.displayMessage(entry.toString());
     78                }
     79                MessageBay.displayMessage(mail.size() + " new mail since last run.");
     80        }
     81       
     82        public static void SendTestOneOffMessage(String recipient) {
     83                String time = Mail.FORMAT.format(new Date());
    9284                String sender = UserSettings.UserName.get();
    9385                String topic = "Test Message";
     
    9587                Map<String, String> options = new HashMap<String, String>();
    9688                options.put("Neat", "Beep");
    97                 MailEntry mail = new MailEntry(time, sender, colleagueName, topic, message, options);
     89               
    9890                Random rand = new SecureRandom();
    99                 byte[] key = new byte[16];
    100                 rand.nextBytes(key);
    101                 System.out.println(Base64.getEncoder().encodeToString(key));
    102                 Mail.sendOneOffMail(mail, colleagueName, key);
     91                byte[] keyBytes = new byte[16];
     92                rand.nextBytes(keyBytes);
     93                SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
     94                System.out.println(Base64.getEncoder().encodeToString(keyBytes));
     95               
     96                Mail mailClient = MailBay.getMailClient();
     97                Mail.MailEntry mail = mailClient.new MailEntry(time, sender, recipient, topic, message, options);
     98                mailClient.sendOneOffMail(mail, recipient, key);
    10399                MessageBay.displayMessage("Test message sent.");
    104100        }
     
    114110       
    115111        //      Start Misc Auth Actions
     112        public static void CheckForNewMail() {
     113                MailBay.checkMail();
     114        }
     115       
    116116        /**
    117117         * Action ran by user to read a message using a single use distributed Symmetric key
     
    124124                SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
    125125                List<String> data = actionItem.getData();
    126                 Mail.decryptOneOffSecureMessage(key, data);
     126                MailBay.decryptOneOffSecureMessage(key, data);
    127127                StandardGestureActions.Refresh();
    128128        }
     
    146146        public static void ToggleBay() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, SQLException, IOException, ParseException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    147147                if (!AuthenticatorBrowser.isAuthenticated()) return;
    148                 if (!DisplayController.isMailMode()) {
    149                         MailBay.ensureLink();
    150                         Mail.clear();
    151                         String keyEncoded = KeyList.PrivateKey.get().getData().get(0);
    152                         byte[] keyBytes = Base64.getDecoder().decode(keyEncoded);
    153                         PrivateKey key = KeyFactory.getInstance(AsymmetricAlgorithm).generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
    154                         Mail.checkMail(key);
    155                 }
     148                //if (!DisplayController.isMailMode()) {
     149                //      MailBayV2.checkMail();
     150                //}
    156151                DisplayController.ToggleMailMode();
    157152        }
     
    558553        @SuppressWarnings("unused")
    559554        private static boolean submitTrustedUsersPasswordRecovery(Map<AuthenticationTag, String> userData) throws InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException {
    560                 String colleagueOne = userData.get(AuthenticationTag.TrustedUserOne);
    561                 String colleagueTwo = userData.get(AuthenticationTag.TrustedUserTwo);
    562                 PublicKey colleagueOneKey = AuthenticatorBrowser.getInstance().getPublicKey(colleagueOne);
    563                 PublicKey colleagueTwoKey = AuthenticatorBrowser.getInstance().getPublicKey(colleagueTwo);
     555                String trustedUserOne = userData.get(AuthenticationTag.TrustedUserOne);
     556                String trustedUserTwo = userData.get(AuthenticationTag.TrustedUserTwo);
     557                PublicKey colleagueOneKey = AuthenticatorBrowser.getInstance().getPublicKey(trustedUserOne);
     558                PublicKey colleagueTwoKey = AuthenticatorBrowser.getInstance().getPublicKey(trustedUserTwo);
    564559                if (colleagueOneKey == null) {
    565                         MessageBay.errorMessage("Unable to get public key for colleague: " + colleagueOne);
     560                        MessageBay.errorMessage("Unable to get public key for colleague: " + trustedUserOne);
    566561                        return false;
    567562                } else if (colleagueTwoKey == null) {
    568                         MessageBay.errorMessage("Unable to get public key for colleague: " + colleagueTwo);
     563                        MessageBay.errorMessage("Unable to get public key for colleague: " + trustedUserTwo);
    569564                        return false;
    570565                } else {
    571                         String time = org.expeditee.stats.Formatter.getDateTime();
     566                        String time = Mail.FORMAT.format(new Date());
    572567                        String sender = userData.get(AuthenticationTag.Username);
    573568                        String topic = "You have received a request for cooperation from your colleague " + sender;
     
    576571                        arguments.put("I agree to assist " + sender + " if they loose access to their account.", "AuthConfirmPasswordColleagueRelationship " + sender);
    577572                        arguments.put("I wish to excuse myself from this responsibility.", "AuthDenyPasswordColleagueRelationship " + sender);
    578                         MailEntry mail = new MailEntry(time, sender, colleagueOne, topic, message, arguments);
    579                         Mail.sendMail(mail, colleagueOne);
    580                         mail = new MailEntry(time, sender, colleagueTwo, topic, message, arguments);
    581                         Mail.sendMail(mail, colleagueTwo);
     573                        Mail outbox = MailBay.getMailClient();
     574                        MailEntry mail = outbox.new MailEntry(time, sender, trustedUserOne, topic, message, arguments);
     575                        outbox.sendMail(mail, trustedUserOne);
     576                        mail = outbox.new MailEntry(time, sender, trustedUserTwo, topic, message, arguments);
     577                        outbox.sendMail(mail, trustedUserTwo);
    582578                        AuthenticatorBrowser.getInstance().markRequestedColleagues(UserSettings.UserName.get());
    583579                        return true;
Note: See TracChangeset for help on using the changeset viewer.