[1202] | 1 | package org.expeditee.auth;
|
---|
| 2 |
|
---|
[1243] | 3 | import java.io.File;
|
---|
[1202] | 4 | import java.io.FileNotFoundException;
|
---|
| 5 | import java.io.IOException;
|
---|
[1243] | 6 | import java.nio.file.Files;
|
---|
| 7 | import java.nio.file.Path;
|
---|
| 8 | import java.nio.file.Paths;
|
---|
| 9 | import java.nio.file.StandardCopyOption;
|
---|
| 10 | import java.security.InvalidKeyException;
|
---|
[1202] | 11 | import java.security.KeyStoreException;
|
---|
| 12 | import java.security.NoSuchAlgorithmException;
|
---|
| 13 | import java.security.PublicKey;
|
---|
[1362] | 14 | import java.security.SecureRandom;
|
---|
[1202] | 15 | import java.security.cert.CertificateException;
|
---|
| 16 | import java.security.spec.InvalidKeySpecException;
|
---|
| 17 | import java.sql.SQLException;
|
---|
[1277] | 18 | import java.text.ParseException;
|
---|
[1202] | 19 | import java.util.Base64;
|
---|
| 20 | import java.util.Collection;
|
---|
[1504] | 21 | import java.util.Date;
|
---|
[1202] | 22 | import java.util.HashMap;
|
---|
| 23 | import java.util.List;
|
---|
| 24 | import java.util.Map;
|
---|
| 25 | import java.util.Optional;
|
---|
[1362] | 26 | import java.util.Random;
|
---|
[1340] | 27 | import java.util.Scanner;
|
---|
[1243] | 28 | import java.util.stream.Collectors;
|
---|
[1202] | 29 |
|
---|
[1243] | 30 | import javax.crypto.BadPaddingException;
|
---|
| 31 | import javax.crypto.IllegalBlockSizeException;
|
---|
| 32 | import javax.crypto.NoSuchPaddingException;
|
---|
[1362] | 33 | import javax.crypto.SecretKey;
|
---|
| 34 | import javax.crypto.spec.SecretKeySpec;
|
---|
[1202] | 35 |
|
---|
[1243] | 36 | import org.expeditee.agents.ExistingFramesetException;
|
---|
| 37 | import org.expeditee.agents.InvalidFramesetNameException;
|
---|
[1303] | 38 | import org.expeditee.auth.account.Authenticate;
|
---|
| 39 | import org.expeditee.auth.account.Authenticate.AuthenticationResult;
|
---|
[1387] | 40 | import org.expeditee.auth.account.Contacts;
|
---|
[1334] | 41 | import org.expeditee.auth.account.Create;
|
---|
[1357] | 42 | import org.expeditee.auth.account.Create.CreateResult;
|
---|
[1335] | 43 | import org.expeditee.auth.account.Password;
|
---|
[1363] | 44 | import org.expeditee.auth.mail.Mail;
|
---|
| 45 | import org.expeditee.auth.mail.Mail.MailEntry;
|
---|
| 46 | import org.expeditee.auth.mail.gui.MailBay;
|
---|
[1243] | 47 | import org.expeditee.auth.tags.AuthenticationTag;
|
---|
[1389] | 48 | import org.expeditee.encryption.CryptographyConstants;
|
---|
[1362] | 49 | import org.expeditee.gio.gesture.StandardGestureActions;
|
---|
[1540] | 50 | import org.expeditee.gui.Browser;
|
---|
[1202] | 51 | import org.expeditee.gui.DisplayController;
|
---|
| 52 | import org.expeditee.gui.Frame;
|
---|
| 53 | import org.expeditee.gui.FrameIO;
|
---|
| 54 | import org.expeditee.gui.MessageBay;
|
---|
| 55 | import org.expeditee.items.Item;
|
---|
| 56 | import org.expeditee.items.Text;
|
---|
| 57 | import org.expeditee.settings.UserSettings;
|
---|
| 58 |
|
---|
| 59 | public class Actions implements CryptographyConstants {
|
---|
[1504] | 60 |
|
---|
| 61 | // Start Debug Actions
|
---|
| 62 | public static void SendTestMessage(String recipient) {
|
---|
| 63 | String time = Mail.FORMAT.format(new Date());
|
---|
[1243] | 64 | String sender = UserSettings.UserName.get();
|
---|
| 65 | String topic = "Test Message";
|
---|
| 66 | String message = "This is a test message.";
|
---|
| 67 | Map<String, String> options = new HashMap<String, String>();
|
---|
| 68 | options.put("Neat", "Beep");
|
---|
[1504] | 69 | Mail mailClient = MailBay.getMailClient();
|
---|
| 70 | Mail.MailEntry mail = mailClient.new MailEntry(time, sender, recipient, topic, message, options);
|
---|
| 71 | mailClient.sendMail(mail, recipient);
|
---|
[1313] | 72 | MessageBay.displayMessage("Test message sent.");
|
---|
[1243] | 73 | }
|
---|
[1363] | 74 |
|
---|
[1504] | 75 | public static void SendTestOneOffMessage(String recipient) {
|
---|
| 76 | String time = Mail.FORMAT.format(new Date());
|
---|
[1362] | 77 | String sender = UserSettings.UserName.get();
|
---|
| 78 | String topic = "Test Message";
|
---|
| 79 | String message = "This is a test message.";
|
---|
| 80 | Map<String, String> options = new HashMap<String, String>();
|
---|
| 81 | options.put("Neat", "Beep");
|
---|
[1504] | 82 |
|
---|
[1362] | 83 | Random rand = new SecureRandom();
|
---|
[1504] | 84 | byte[] keyBytes = new byte[16];
|
---|
| 85 | rand.nextBytes(keyBytes);
|
---|
| 86 | SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
| 87 | System.out.println(Base64.getEncoder().encodeToString(keyBytes));
|
---|
| 88 |
|
---|
| 89 | Mail mailClient = MailBay.getMailClient();
|
---|
| 90 | Mail.MailEntry mail = mailClient.new MailEntry(time, sender, recipient, topic, message, options);
|
---|
| 91 | mailClient.sendOneOffMail(mail, recipient, key);
|
---|
[1494] | 92 | MessageBay.displayMessage("Test message sent.");
|
---|
[1362] | 93 | }
|
---|
[1310] | 94 |
|
---|
[1270] | 95 | private static String userbackup = "authadmin";
|
---|
| 96 | public static void ToggleAuth() {
|
---|
| 97 | String backup = UserSettings.UserName.get();
|
---|
[1452] | 98 | System.setProperty("user.name", userbackup);
|
---|
[1540] | 99 | Browser.setExpediteeUserName(userbackup);
|
---|
[1270] | 100 | UserSettings.UserName.set(userbackup);
|
---|
| 101 | userbackup = backup;
|
---|
| 102 | }
|
---|
[1363] | 103 | // End Debug Actions
|
---|
[1202] | 104 |
|
---|
[1363] | 105 | // Start Misc Auth Actions
|
---|
[1504] | 106 | public static void CheckForNewMail() {
|
---|
| 107 | MailBay.checkMail();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[1202] | 110 | /**
|
---|
[1363] | 111 | * Action ran by user to read a message using a single use distributed Symmetric key
|
---|
| 112 | * @param cursor The content on the cursor should be a text item whose content is the
|
---|
| 113 | * Symmetric key to use, represented as a Base64 encoded string.
|
---|
| 114 | * @param actionItem The action item will contain the encrypted message in its data.
|
---|
| 115 | */
|
---|
| 116 | public static void AuthOneOffSecureMessage(Text cursor, Text actionItem) {
|
---|
| 117 | byte[] keyBytes = Base64.getDecoder().decode(cursor.getText());
|
---|
| 118 | SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
| 119 | List<String> data = actionItem.getData();
|
---|
[1504] | 120 | MailBay.decryptOneOffSecureMessage(key, data);
|
---|
[1363] | 121 | StandardGestureActions.Refresh();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
[1202] | 125 | * Display Expeditee Mail
|
---|
[1227] | 126 | * @throws IOException
|
---|
| 127 | * @throws SQLException
|
---|
| 128 | * @throws ClassNotFoundException
|
---|
| 129 | * @throws CertificateException
|
---|
| 130 | * @throws NoSuchAlgorithmException
|
---|
| 131 | * @throws FileNotFoundException
|
---|
| 132 | * @throws KeyStoreException
|
---|
[1277] | 133 | * @throws ParseException
|
---|
[1320] | 134 | * @throws InvalidKeySpecException
|
---|
| 135 | * @throws BadPaddingException
|
---|
| 136 | * @throws IllegalBlockSizeException
|
---|
| 137 | * @throws NoSuchPaddingException
|
---|
| 138 | * @throws InvalidKeyException
|
---|
[1202] | 139 | */
|
---|
[1320] | 140 | public static void ToggleBay() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, SQLException, IOException, ParseException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
|
---|
[1316] | 141 | if (!AuthenticatorBrowser.isAuthenticated()) return;
|
---|
[1504] | 142 | //if (!DisplayController.isMailMode()) {
|
---|
| 143 | // MailBayV2.checkMail();
|
---|
| 144 | //}
|
---|
[1202] | 145 | DisplayController.ToggleMailMode();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Action used to navigate the authorised user back to their desktop.
|
---|
| 150 | */
|
---|
| 151 | public static void AuthGoToDesktop() {
|
---|
[1365] | 152 | if (AuthenticatorBrowser.Authenticated) {
|
---|
| 153 | DisplayController.setCurrentFrame(FrameIO.LoadFrame(UserSettings.HomeFrame.get()), true);
|
---|
| 154 | } else {
|
---|
| 155 | MessageBay.displayMessage("Please Login to proceed to your home frame.");
|
---|
| 156 | DisplayController.setCurrentFrame(FrameIO.LoadFrame("authentication1"), true);
|
---|
| 157 | }
|
---|
[1202] | 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
[1363] | 161 | * Action used to navigate to multiuser1 (multiuser abilities) if authenticated and authentication1 (login) is not so.
|
---|
| 162 | */
|
---|
| 163 | public static void AuthGotoAccountManagement() {
|
---|
| 164 | if (AuthenticatorBrowser.Authenticated) {
|
---|
[1365] | 165 | DisplayController.setCurrentFrame(FrameIO.LoadFrame("multiuser1"), true);
|
---|
[1363] | 166 | } else {
|
---|
[1365] | 167 | MessageBay.displayMessage("Please Login to proceed to account managment.");
|
---|
| 168 | DisplayController.setCurrentFrame(FrameIO.LoadFrame("authentication1"), true);
|
---|
[1363] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Gets all items on a specified frame that contain the specified data.
|
---|
| 174 | */
|
---|
| 175 | public static Collection<Item> getByData(Frame frame, String data) {
|
---|
| 176 | Collection<Item> allItems = frame.getAllItems();
|
---|
| 177 | allItems.removeIf(i -> i.getData() == null || !i.hasData(data));
|
---|
| 178 | return allItems;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * Gets all items on a specified frame that contains the specified content.
|
---|
| 183 | */
|
---|
| 184 | public static Collection<Item> getByContent(Frame frame, String content) {
|
---|
| 185 | Collection<Item> allItems = frame.getAllItems();
|
---|
| 186 | allItems.removeIf(i -> i.getText().compareTo(content) != 0);
|
---|
| 187 | return allItems;
|
---|
| 188 | }
|
---|
| 189 | // End Misc Auth Actions
|
---|
| 190 |
|
---|
[1384] | 191 | // Start Making Contacts Actions
|
---|
| 192 | public static void AuthDistributeContactDetails() {
|
---|
| 193 | MessageBay.displayMessage(
|
---|
| 194 | "To receive directions on distributing your contact details to someone, attach their username to your cursor and run this action again."
|
---|
| 195 | );
|
---|
| 196 | }
|
---|
| 197 | public static String AuthDistributeContactDetails(String username) {
|
---|
[1387] | 198 | return Contacts.distributeContactDetails(username);
|
---|
[1384] | 199 | }
|
---|
[1385] | 200 | public static void AuthAddContactDetails() {
|
---|
| 201 | MessageBay.displayMessage("If a user has sent their contact details to you, running this action again with their username attached to your cursor will display instructions to adding those details to your contacts directory.");
|
---|
| 202 | }
|
---|
| 203 | public static String AuthAddContactDetails(String username) {
|
---|
[1387] | 204 | return Contacts.addContactDetails(username);
|
---|
[1385] | 205 | }
|
---|
[1384] | 206 | // End Making Contacts Actions
|
---|
| 207 |
|
---|
[1368] | 208 | // Start Regain Account Access Actions
|
---|
[1363] | 209 | /**
|
---|
[1368] | 210 | * Action used to start the process of formalising the password recovery process.
|
---|
| 211 | * @throws SQLException
|
---|
| 212 | * @throws IOException
|
---|
| 213 | * @throws ClassNotFoundException
|
---|
| 214 | * @throws CertificateException
|
---|
| 215 | * @throws NoSuchAlgorithmException
|
---|
| 216 | * @throws FileNotFoundException
|
---|
| 217 | * @throws KeyStoreException
|
---|
| 218 | * @throws InvalidKeySpecException
|
---|
| 219 | */
|
---|
| 220 | public static void AuthSubmitTrustedUsersPasswordRecovery() throws InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException {
|
---|
| 221 | Frame currentFrame = DisplayController.getCurrentFrame();
|
---|
| 222 | Collection<Text> textItems = currentFrame.getTextItems();
|
---|
| 223 |
|
---|
| 224 | if (!AuthenticatorBrowser.Authenticated) {
|
---|
| 225 | MessageBay.errorMessage("You must be logged in to perform this action.");
|
---|
| 226 | return;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.TrustedUserOne, AuthenticationTag.TrustedUserTwo);
|
---|
| 230 | if (userdata.isPresent()) {
|
---|
| 231 | Map<AuthenticationTag, String> userData = userdata.get();
|
---|
| 232 | String colleagueOne = userData.get(AuthenticationTag.TrustedUserOne);
|
---|
| 233 | String colleagueTwo = userData.get(AuthenticationTag.TrustedUserTwo);
|
---|
[1492] | 234 | String username = UserSettings.UserName.get().toLowerCase();
|
---|
| 235 | if (colleagueOne.toLowerCase().equals(username) || colleagueTwo.toLowerCase().equals(username)) {
|
---|
| 236 | MessageBay.displayMessage("You cannot nominate yourself as one of your trusted colleagues.");
|
---|
| 237 | } else {
|
---|
| 238 | AuthSubmitTrustedUsersPasswordRecovery(colleagueOne, colleagueTwo);
|
---|
| 239 | }
|
---|
[1368] | 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /**
|
---|
[1363] | 244 | * Action ran by user to specify who their password colleagues are. These are the
|
---|
| 245 | * individuals who will be consulted if and when the user needs to regain access
|
---|
| 246 | * to their account.
|
---|
| 247 | * @param colleagueOne
|
---|
| 248 | * @param colleagueTwo
|
---|
| 249 | */
|
---|
[1373] | 250 | public static void AuthSubmitTrustedUsersPasswordRecovery(String colleagueOne, String colleagueTwo) {
|
---|
[1363] | 251 | Password.setPWColleagues(colleagueOne, colleagueTwo);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | /**
|
---|
| 255 | * Action ran by user to oblige with a request from colleague who has nominated the
|
---|
| 256 | * user as a pw colleague. Will email (not Expeditee mail) the colleague the password
|
---|
| 257 | * share that the user has stored on their secrets frame.
|
---|
| 258 | * @param colleagueName
|
---|
| 259 | */
|
---|
| 260 | public static void AuthEmailPasswordShare(String colleagueName) {
|
---|
| 261 | Password.emailPasswordShare(colleagueName);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /**
|
---|
| 265 | * Action ran by user to regain access to their account by providing:
|
---|
| 266 | * their username
|
---|
| 267 | * two password shares obtained from pw colleagues
|
---|
| 268 | * their desired new password
|
---|
| 269 | */
|
---|
| 270 | public static void AuthRegainAccountAccess() {
|
---|
| 271 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
| 272 | Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, false,
|
---|
| 273 | AuthenticationTag.Username, AuthenticationTag.NewPassword, AuthenticationTag.NewPasswordAgain,
|
---|
| 274 | AuthenticationTag.PasswordSliceOne, AuthenticationTag.PasswordSliceTwo);
|
---|
| 275 | if (userdata.isPresent()) {
|
---|
| 276 | // Confirm new requested passwords match
|
---|
| 277 | Map<AuthenticationTag, String> userData = userdata.get();
|
---|
[1476] | 278 | String username = userData.get(AuthenticationTag.Username).trim();
|
---|
| 279 | if (username.length() == 0) {
|
---|
| 280 | MessageBay.errorMessage("Please fill out the username box.");
|
---|
| 281 | } else if (!userData.get(AuthenticationTag.NewPassword).equals(userData.get(AuthenticationTag.NewPasswordAgain))) {
|
---|
[1367] | 282 | MessageBay.errorMessage("The passwords you have provided do not match.");
|
---|
[1476] | 283 | } else {
|
---|
| 284 | Password.regainAccountAccess(userData);
|
---|
[1363] | 285 | }
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | * Actions used to generate and deliver an intergalactic number to a users public email
|
---|
| 291 | * address after they have began the password recovery process.
|
---|
| 292 | */
|
---|
| 293 | public static void AuthDistributeIntergalacticNumber() {
|
---|
[1484] | 294 | Text displayMessage = MessageBay.displayMessage("Action processing....");
|
---|
[1363] | 295 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
| 296 | Optional<Map<AuthenticationTag, String>> userdata =
|
---|
| 297 | AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.Username);
|
---|
| 298 | if (userdata.isPresent()) {
|
---|
| 299 | Map<AuthenticationTag, String> userData = userdata.get();
|
---|
| 300 | String username = userData.get(AuthenticationTag.Username);
|
---|
| 301 | String email = getEmailFromUsername(username);
|
---|
| 302 | userData.put(AuthenticationTag.Email, email);
|
---|
| 303 | Password.generateAndDeliverIntergalacticNumber(userData);
|
---|
[1482] | 304 | MessageBay.displayMessage("An identity number has been sent to the email "
|
---|
[1363] | 305 | + "associated with your account. Enter it below to proceed.");
|
---|
| 306 | }
|
---|
[1484] | 307 | displayMessage.setText(displayMessage.getText() + "..Done");
|
---|
[1363] | 308 | }
|
---|
| 309 |
|
---|
| 310 | /**
|
---|
| 311 | * Action used by user to submit their intergalactic number along with their username
|
---|
| 312 | * in order to confirm that they own the public email address registered to their account.
|
---|
| 313 | * This is part of the process of recoverying access to an account.
|
---|
| 314 | */
|
---|
| 315 | public static void AuthSubmitIntergalacticNumber() {
|
---|
[1484] | 316 | Text displayMessage = MessageBay.displayMessage("Action processing....");
|
---|
[1363] | 317 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
| 318 | Optional<Map<AuthenticationTag, String>> userdata =
|
---|
| 319 | AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.Username,
|
---|
| 320 | AuthenticationTag.IntergalacticNumber);
|
---|
| 321 | if (userdata.isPresent()) {
|
---|
[1368] | 322 | Password.confirmIntergalacticNumberAndAlertTrustedUsers(userdata.get());
|
---|
[1363] | 323 | }
|
---|
[1484] | 324 | displayMessage.setText(displayMessage.getText() + "..Done");
|
---|
[1363] | 325 | }
|
---|
| 326 | // End Regain Account Access Actions
|
---|
| 327 |
|
---|
| 328 | // Start Create Account Actions
|
---|
| 329 | /**
|
---|
[1202] | 330 | * Action used to created a new user account.
|
---|
| 331 | * Attempts to use content from text items on frame, will default to java properties if they cannot be found.
|
---|
| 332 | * Will fail if it cannot find content from text items on frame and all required java properties are not present.
|
---|
[1243] | 333 | * @throws SQLException
|
---|
| 334 | * @throws IOException
|
---|
| 335 | * @throws ExistingFramesetException
|
---|
| 336 | * @throws InvalidFramesetNameException
|
---|
| 337 | * @throws ClassNotFoundException
|
---|
| 338 | * @throws FileNotFoundException
|
---|
| 339 | * @throws CertificateException
|
---|
| 340 | * @throws NoSuchAlgorithmException
|
---|
| 341 | * @throws KeyStoreException
|
---|
| 342 | * @throws BadPaddingException
|
---|
| 343 | * @throws IllegalBlockSizeException
|
---|
| 344 | * @throws NoSuchPaddingException
|
---|
| 345 | * @throws InvalidKeySpecException
|
---|
| 346 | * @throws InvalidKeyException
|
---|
[1277] | 347 | * @throws ParseException
|
---|
[1202] | 348 | * @throws Exception
|
---|
| 349 | */
|
---|
[1277] | 350 | public static void AuthCreateAccount() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, ClassNotFoundException, InvalidFramesetNameException, ExistingFramesetException, IOException, SQLException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ParseException {
|
---|
[1243] | 351 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
| 352 | Optional<Map<AuthenticationTag, String>> userdata =
|
---|
| 353 | AuthenticationTag.fetchUserData(textItems, false,
|
---|
| 354 | AuthenticationTag.Username,
|
---|
| 355 | AuthenticationTag.Password,
|
---|
| 356 | AuthenticationTag.PasswordAgain,
|
---|
| 357 | AuthenticationTag.Email,
|
---|
| 358 | AuthenticationTag.EmailAgain);
|
---|
| 359 |
|
---|
| 360 | if (userdata.isPresent()) {
|
---|
| 361 | Map<AuthenticationTag, String> userData = userdata.get();
|
---|
| 362 |
|
---|
[1316] | 363 | // A profile already existing with 'username' means an account cannot be created with that username.
|
---|
| 364 | if (FrameIO.getProfilesList().contains(userData.get(AuthenticationTag.Username))) {
|
---|
[1334] | 365 | MessageBay.errorMessage("A Expeditee profile with this username already exists, please choose another.");
|
---|
[1243] | 366 | return;
|
---|
[1202] | 367 | }
|
---|
[1243] | 368 |
|
---|
| 369 | // The chosen username must be a valid frameset name.
|
---|
| 370 | if (!FrameIO.isValidFramesetName(userData.get(AuthenticationTag.Username))) {
|
---|
[1334] | 371 | MessageBay.errorMessage("The provided username must begin and end with a letter and contain only letters and numbers inbetween, please choose another.");
|
---|
[1243] | 372 | return;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | // The passwords provided must match
|
---|
| 376 | if (userData.get(AuthenticationTag.Password).compareTo(userData.get(AuthenticationTag.PasswordAgain)) != 0) {
|
---|
[1334] | 377 | MessageBay.errorMessage("The provided passwords do not match, please fix this and try again.");
|
---|
[1243] | 378 | return;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | // The emails provided must match
|
---|
| 382 | if (userData.get(AuthenticationTag.Email).compareTo(userData.get(AuthenticationTag.EmailAgain)) != 0) {
|
---|
[1334] | 383 | MessageBay.errorMessage("The provided emails do not match, please fix this and try again.");
|
---|
[1243] | 384 | return;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[1357] | 387 | CreateResult result = Create.createAccount(userData);
|
---|
| 388 | if (result == CreateResult.SuccessCreateAccount) {
|
---|
| 389 | Authenticate.login(userData);
|
---|
| 390 | AuthenticatorBrowser.Authenticated = true;
|
---|
| 391 | } else {
|
---|
| 392 | MessageBay.errorMessage(result.toString());
|
---|
| 393 | }
|
---|
[1202] | 394 | } else {
|
---|
[1334] | 395 | MessageBay.errorMessage("Please fill out all the supplied text boxes.");
|
---|
[1202] | 396 | }
|
---|
| 397 | }
|
---|
[1363] | 398 | // End Create Account Actions
|
---|
[1202] | 399 |
|
---|
[1363] | 400 | // Start Account Login Actions
|
---|
[1202] | 401 | /**
|
---|
| 402 | * Action used to start authentication as a specified user.
|
---|
| 403 | * Attempts to use content from text items on frame, will default to java properties if they cannot be found.
|
---|
| 404 | * Will fail if it cannot find content from text items on frame and all required java properties are not present.
|
---|
| 405 | * @throws Exception
|
---|
| 406 | */
|
---|
[1303] | 407 | public static void AuthLogin() {
|
---|
[1357] | 408 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
| 409 | Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.Username, AuthenticationTag.Password);
|
---|
[1303] | 410 | if (userdata.isPresent()) {
|
---|
| 411 | AuthenticationResult result = Authenticate.login(userdata.get());
|
---|
| 412 | if (result == AuthenticationResult.SuccessLogin) {
|
---|
| 413 | MessageBay.displayMessage(result.toString());
|
---|
[1357] | 414 | AuthenticatorBrowser.Authenticated = true;
|
---|
[1303] | 415 | } else {
|
---|
| 416 | MessageBay.errorMessage(result.toString());
|
---|
| 417 | }
|
---|
[1277] | 418 | } else {
|
---|
[1334] | 419 | MessageBay.errorMessage("Please fill out all the supplied text boxes.");
|
---|
[1202] | 420 | }
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[1363] | 423 | /**
|
---|
| 424 | * Action used by the user to log out of their account.
|
---|
| 425 | */
|
---|
[1303] | 426 | public static void AuthLogout() {
|
---|
| 427 | MessageBay.displayMessage(Authenticate.logout().toString());
|
---|
| 428 | }
|
---|
[1363] | 429 | // End Account Login Actions
|
---|
[1303] | 430 |
|
---|
[1363] | 431 | // Start Change Access Actions
|
---|
[1202] | 432 | /**
|
---|
| 433 | * Action used to change the currently authenticated users password.
|
---|
| 434 | * Attempts to use content from text items on frame, will default to java properties if they cannot be found.
|
---|
| 435 | * Will fail if it cannot find content from text items on frame and all required java properties are not present.
|
---|
| 436 | * Will fail if no user is currently logged in.
|
---|
| 437 | * @throws IOException
|
---|
| 438 | * @throws CertificateException
|
---|
| 439 | * @throws FileNotFoundException
|
---|
| 440 | * @throws KeyStoreException
|
---|
| 441 | * @throws NoSuchAlgorithmException
|
---|
| 442 | * @throws SQLException
|
---|
| 443 | * @throws ClassNotFoundException
|
---|
| 444 | */
|
---|
| 445 | public static void AuthChangePassword() throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, IOException, ClassNotFoundException, SQLException {
|
---|
| 446 | final Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
| 447 |
|
---|
[1283] | 448 | if (!AuthenticatorBrowser.Authenticated) {
|
---|
[1334] | 449 | MessageBay.errorMessage("You must be logged in to perform this action.");
|
---|
[1202] | 450 | } else {
|
---|
[1231] | 451 | final Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.Password, AuthenticationTag.NewPassword, AuthenticationTag.NewPasswordAgain);
|
---|
[1202] | 452 | if (userdata.isPresent()) {
|
---|
| 453 | final Map<AuthenticationTag, String> userData = userdata.get();
|
---|
| 454 | if (userData.get(AuthenticationTag.NewPassword).compareTo(userData.get(AuthenticationTag.NewPasswordAgain)) != 0) {
|
---|
[1334] | 455 | MessageBay.errorMessage("The provided passwords do not match, please fix this and try again.");
|
---|
[1202] | 456 | } else {
|
---|
| 457 | userData.put(AuthenticationTag.Username, UserSettings.UserName.get());
|
---|
[1335] | 458 | Password.changePassword(userData);
|
---|
[1202] | 459 | }
|
---|
| 460 | } else {
|
---|
[1334] | 461 | MessageBay.errorMessage("Please fill out all the supplied text boxes.");
|
---|
[1202] | 462 | }
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
[1363] | 465 | // End Change Access Actions
|
---|
[1202] | 466 |
|
---|
[1363] | 467 | // Start Private Helper Functions.
|
---|
| 468 | /**
|
---|
| 469 | * Gets the public email address associated with the specified username.
|
---|
| 470 | * @param username
|
---|
| 471 | * @return
|
---|
| 472 | */
|
---|
[1348] | 473 | private static String getEmailFromUsername(String username) {
|
---|
| 474 | Path credentialsDirPath = Paths.get(FrameIO.PROFILE_PATH).resolve(username).resolve(username + "-credentials");
|
---|
| 475 | Path credentialsFilePath = credentialsDirPath.resolve("credentials.inf");
|
---|
| 476 | String fileName = null;
|
---|
| 477 | if (credentialsFilePath.toFile().exists()) {
|
---|
| 478 | try (Scanner in = new Scanner(credentialsFilePath)) {
|
---|
| 479 | fileName = in.nextLine();
|
---|
| 480 | } catch (IOException e) {
|
---|
| 481 | MessageBay.errorMessage("Unable to locate public email for specified user, are they registered on this computer?");
|
---|
| 482 | return null;
|
---|
| 483 | }
|
---|
| 484 | } else {
|
---|
| 485 | MessageBay.errorMessage("Unable to locate public email for specified user, are they registered on this computer?");
|
---|
| 486 | return null;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | int number = Integer.parseInt(fileName.replace(".exp", ""));
|
---|
| 490 | Frame credentialsFrame = FrameIO.LoadFrame(username + number, FrameIO.PROFILE_PATH);
|
---|
[1357] | 491 | Collection<Text> textItems = credentialsFrame.getTextItems();
|
---|
[1348] | 492 | textItems.removeIf(text -> !text.getText().startsWith("Email: "));
|
---|
| 493 | if (textItems.isEmpty()) {
|
---|
| 494 | MessageBay.errorMessage("Unable to locate public email for specified user, are they registered on this computer?");
|
---|
| 495 | return null;
|
---|
| 496 | } else {
|
---|
| 497 | Text emailText = textItems.iterator().next();
|
---|
| 498 | String email = emailText.getText().replace("Email: ", "");
|
---|
| 499 | return email;
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
[1363] | 502 | // End Private Helper Functions.
|
---|
[1348] | 503 |
|
---|
[1363] | 504 | // Start Future Functionality
|
---|
[1243] | 505 | public static void AuthShareFrameset() throws IOException {
|
---|
[1202] | 506 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
| 507 |
|
---|
[1243] | 508 | Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.Frameset);
|
---|
| 509 | if (userdata.isPresent()) {
|
---|
| 510 | Map<AuthenticationTag, String> userData = userdata.get();
|
---|
| 511 | FrameIO.SuspendCache();
|
---|
| 512 | Frame toShare = FrameIO.LoadFrame(userData.get(AuthenticationTag.Frameset) + 1);
|
---|
| 513 | FrameIO.ResumeCache();
|
---|
| 514 |
|
---|
| 515 | if (toShare == null) {
|
---|
[1334] | 516 | MessageBay.errorMessage("Insufficient information provided to complete this action.");
|
---|
[1243] | 517 | return;
|
---|
[1202] | 518 | }
|
---|
[1243] | 519 |
|
---|
| 520 | shareFrameset(toShare);
|
---|
[1202] | 521 | }
|
---|
| 522 | }
|
---|
[1363] | 523 |
|
---|
| 524 | /*
|
---|
| 525 | * Function to share a specified frameset.
|
---|
| 526 | * Currently, this moves the frameset to the 'Shared By Me' directory and then relies on the user to use Google Drive functionality to share it appropriately.
|
---|
[1202] | 527 | */
|
---|
[1363] | 528 | private static void shareFrameset(Frame toShare) throws IOException {
|
---|
| 529 | File destinationDir = new File(FrameIO.SHARED_FRAMESETS_PATH + File.separator + toShare.getFramesetName());
|
---|
| 530 | File sourceDir = new File(toShare.getFramesetPath());
|
---|
| 531 |
|
---|
| 532 | if (destinationDir.exists()) {
|
---|
| 533 | MessageBay.errorMessage("A frameset by this name already exists.");
|
---|
| 534 | return;
|
---|
[1202] | 535 | }
|
---|
[1363] | 536 |
|
---|
| 537 | destinationDir.mkdir();
|
---|
| 538 | List<Path> files = Files.walk(sourceDir.toPath()).collect(Collectors.toList());
|
---|
| 539 | Files.move(files.get(0), destinationDir.toPath(), StandardCopyOption.ATOMIC_MOVE);
|
---|
| 540 |
|
---|
| 541 | MessageBay.displayMessage("The frameset " + toShare.getFramesetName() + " has been moved to " + destinationDir + ". Google Drive functionality can now be used to share it with colleagues.");
|
---|
[1202] | 542 | }
|
---|
| 543 |
|
---|
[1243] | 544 | /*
|
---|
[1363] | 545 | * Function to submit a request to specified contacts to be the current users pw colleagues.
|
---|
[1243] | 546 | */
|
---|
[1384] | 547 | @SuppressWarnings("unused")
|
---|
[1368] | 548 | private static boolean submitTrustedUsersPasswordRecovery(Map<AuthenticationTag, String> userData) throws InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException {
|
---|
[1504] | 549 | String trustedUserOne = userData.get(AuthenticationTag.TrustedUserOne);
|
---|
| 550 | String trustedUserTwo = userData.get(AuthenticationTag.TrustedUserTwo);
|
---|
| 551 | PublicKey colleagueOneKey = AuthenticatorBrowser.getInstance().getPublicKey(trustedUserOne);
|
---|
| 552 | PublicKey colleagueTwoKey = AuthenticatorBrowser.getInstance().getPublicKey(trustedUserTwo);
|
---|
[1243] | 553 | if (colleagueOneKey == null) {
|
---|
[1504] | 554 | MessageBay.errorMessage("Unable to get public key for colleague: " + trustedUserOne);
|
---|
[1202] | 555 | return false;
|
---|
[1243] | 556 | } else if (colleagueTwoKey == null) {
|
---|
[1504] | 557 | MessageBay.errorMessage("Unable to get public key for colleague: " + trustedUserTwo);
|
---|
[1202] | 558 | return false;
|
---|
| 559 | } else {
|
---|
[1504] | 560 | String time = Mail.FORMAT.format(new Date());
|
---|
[1243] | 561 | String sender = userData.get(AuthenticationTag.Username);
|
---|
| 562 | String topic = "You have received a request for cooperation from your colleague " + sender;
|
---|
| 563 | String message = "Should " + sender + " forget their password, they would like your help recoverying it.";
|
---|
[1202] | 564 | Map<String, String> arguments = new HashMap<String, String>();
|
---|
[1243] | 565 | arguments.put("I agree to assist " + sender + " if they loose access to their account.", "AuthConfirmPasswordColleagueRelationship " + sender);
|
---|
| 566 | arguments.put("I wish to excuse myself from this responsibility.", "AuthDenyPasswordColleagueRelationship " + sender);
|
---|
[1504] | 567 | Mail outbox = MailBay.getMailClient();
|
---|
| 568 | MailEntry mail = outbox.new MailEntry(time, sender, trustedUserOne, topic, message, arguments);
|
---|
| 569 | outbox.sendMail(mail, trustedUserOne);
|
---|
| 570 | mail = outbox.new MailEntry(time, sender, trustedUserTwo, topic, message, arguments);
|
---|
| 571 | outbox.sendMail(mail, trustedUserTwo);
|
---|
[1283] | 572 | AuthenticatorBrowser.getInstance().markRequestedColleagues(UserSettings.UserName.get());
|
---|
[1202] | 573 | return true;
|
---|
| 574 | }
|
---|
| 575 | }
|
---|
[1363] | 576 | // End Future Functionality
|
---|
[1202] | 577 | }
|
---|