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