1 | package org.expeditee.auth;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.IOException;
|
---|
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;
|
---|
11 | import java.security.KeyStoreException;
|
---|
12 | import java.security.NoSuchAlgorithmException;
|
---|
13 | import java.security.PublicKey;
|
---|
14 | import java.security.SecureRandom;
|
---|
15 | import java.security.cert.CertificateException;
|
---|
16 | import java.security.spec.InvalidKeySpecException;
|
---|
17 | import java.sql.SQLException;
|
---|
18 | import java.text.ParseException;
|
---|
19 | import java.util.Base64;
|
---|
20 | import java.util.Collection;
|
---|
21 | import java.util.Date;
|
---|
22 | import java.util.HashMap;
|
---|
23 | import java.util.List;
|
---|
24 | import java.util.Map;
|
---|
25 | import java.util.Optional;
|
---|
26 | import java.util.Random;
|
---|
27 | import java.util.Scanner;
|
---|
28 | import java.util.stream.Collectors;
|
---|
29 |
|
---|
30 | import javax.crypto.BadPaddingException;
|
---|
31 | import javax.crypto.IllegalBlockSizeException;
|
---|
32 | import javax.crypto.NoSuchPaddingException;
|
---|
33 | import javax.crypto.SecretKey;
|
---|
34 | import javax.crypto.spec.SecretKeySpec;
|
---|
35 |
|
---|
36 | import org.expeditee.agents.ExistingFramesetException;
|
---|
37 | import org.expeditee.agents.InvalidFramesetNameException;
|
---|
38 | import org.expeditee.auth.account.Authenticate;
|
---|
39 | import org.expeditee.auth.account.Authenticate.AuthenticationResult;
|
---|
40 | import org.expeditee.auth.account.Contacts;
|
---|
41 | import org.expeditee.auth.account.Create;
|
---|
42 | import org.expeditee.auth.account.Create.CreateResult;
|
---|
43 | import org.expeditee.auth.account.Password;
|
---|
44 | import org.expeditee.auth.mail.Mail;
|
---|
45 | import org.expeditee.auth.mail.Mail.MailEntry;
|
---|
46 | import org.expeditee.auth.mail.gui.MailBay;
|
---|
47 | import org.expeditee.auth.tags.AuthenticationTag;
|
---|
48 | import org.expeditee.encryption.CryptographyConstants;
|
---|
49 | import org.expeditee.gio.gesture.StandardGestureActions;
|
---|
50 | import org.expeditee.gui.Browser;
|
---|
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 {
|
---|
60 |
|
---|
61 | // Start Debug Actions
|
---|
62 | public static void SendTestMessage(String recipient) {
|
---|
63 | String time = Mail.FORMAT.format(new Date());
|
---|
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");
|
---|
69 | Mail mailClient = MailBay.getMailClient();
|
---|
70 | Mail.MailEntry mail = mailClient.new MailEntry(time, sender, recipient, topic, message, options);
|
---|
71 | mailClient.sendMail(mail, recipient);
|
---|
72 | MessageBay.displayMessage("Test message sent.");
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static void SendTestOneOffMessage(String recipient) {
|
---|
76 | String time = Mail.FORMAT.format(new Date());
|
---|
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");
|
---|
82 |
|
---|
83 | Random rand = new SecureRandom();
|
---|
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);
|
---|
92 | MessageBay.displayMessage("Test message sent.");
|
---|
93 | }
|
---|
94 |
|
---|
95 | private static String userbackup = "authadmin";
|
---|
96 | public static void ToggleAuth() {
|
---|
97 | String backup = UserSettings.UserName.get();
|
---|
98 | System.setProperty("user.name", userbackup);
|
---|
99 | Browser.setExpediteeUserName(userbackup);
|
---|
100 | UserSettings.UserName.set(userbackup);
|
---|
101 | userbackup = backup;
|
---|
102 | }
|
---|
103 | // End Debug Actions
|
---|
104 |
|
---|
105 | // Start Misc Auth Actions
|
---|
106 | public static void CheckForNewMail() {
|
---|
107 | MailBay.checkMail();
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
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();
|
---|
120 | MailBay.decryptOneOffSecureMessage(key, data);
|
---|
121 | StandardGestureActions.Refresh();
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Display Expeditee Mail
|
---|
126 | * @throws IOException
|
---|
127 | * @throws SQLException
|
---|
128 | * @throws ClassNotFoundException
|
---|
129 | * @throws CertificateException
|
---|
130 | * @throws NoSuchAlgorithmException
|
---|
131 | * @throws FileNotFoundException
|
---|
132 | * @throws KeyStoreException
|
---|
133 | * @throws ParseException
|
---|
134 | * @throws InvalidKeySpecException
|
---|
135 | * @throws BadPaddingException
|
---|
136 | * @throws IllegalBlockSizeException
|
---|
137 | * @throws NoSuchPaddingException
|
---|
138 | * @throws InvalidKeyException
|
---|
139 | */
|
---|
140 | public static void ToggleBay() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, SQLException, IOException, ParseException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
|
---|
141 | if (!AuthenticatorBrowser.isAuthenticated()) return;
|
---|
142 | //if (!DisplayController.isMailMode()) {
|
---|
143 | // MailBayV2.checkMail();
|
---|
144 | //}
|
---|
145 | DisplayController.ToggleMailMode();
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Action used to navigate the authorised user back to their desktop.
|
---|
150 | */
|
---|
151 | public static void AuthGoToDesktop() {
|
---|
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 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
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) {
|
---|
165 | DisplayController.setCurrentFrame(FrameIO.LoadFrame("multiuser1"), true);
|
---|
166 | } else {
|
---|
167 | MessageBay.displayMessage("Please Login to proceed to account managment.");
|
---|
168 | DisplayController.setCurrentFrame(FrameIO.LoadFrame("authentication1"), true);
|
---|
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 |
|
---|
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) {
|
---|
198 | return Contacts.distributeContactDetails(username);
|
---|
199 | }
|
---|
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) {
|
---|
204 | return Contacts.addContactDetails(username);
|
---|
205 | }
|
---|
206 | // End Making Contacts Actions
|
---|
207 |
|
---|
208 | // Start Regain Account Access Actions
|
---|
209 | /**
|
---|
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);
|
---|
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 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
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 | */
|
---|
250 | public static void AuthSubmitTrustedUsersPasswordRecovery(String colleagueOne, String colleagueTwo) {
|
---|
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();
|
---|
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))) {
|
---|
282 | MessageBay.errorMessage("The passwords you have provided do not match.");
|
---|
283 | } else {
|
---|
284 | Password.regainAccountAccess(userData);
|
---|
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() {
|
---|
294 | Text displayMessage = MessageBay.displayMessage("Action processing....");
|
---|
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);
|
---|
304 | MessageBay.displayMessage("An identity number has been sent to the email "
|
---|
305 | + "associated with your account. Enter it below to proceed.");
|
---|
306 | }
|
---|
307 | displayMessage.setText(displayMessage.getText() + "..Done");
|
---|
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() {
|
---|
316 | Text displayMessage = MessageBay.displayMessage("Action processing....");
|
---|
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()) {
|
---|
322 | Password.confirmIntergalacticNumberAndAlertTrustedUsers(userdata.get());
|
---|
323 | }
|
---|
324 | displayMessage.setText(displayMessage.getText() + "..Done");
|
---|
325 | }
|
---|
326 | // End Regain Account Access Actions
|
---|
327 |
|
---|
328 | // Start Create Account Actions
|
---|
329 | /**
|
---|
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.
|
---|
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
|
---|
347 | * @throws ParseException
|
---|
348 | * @throws Exception
|
---|
349 | */
|
---|
350 | public static void AuthCreateAccount() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, ClassNotFoundException, InvalidFramesetNameException, ExistingFramesetException, IOException, SQLException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ParseException {
|
---|
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 |
|
---|
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))) {
|
---|
365 | MessageBay.errorMessage("A Expeditee profile with this username already exists, please choose another.");
|
---|
366 | return;
|
---|
367 | }
|
---|
368 |
|
---|
369 | // The chosen username must be a valid frameset name.
|
---|
370 | if (!FrameIO.isValidFramesetName(userData.get(AuthenticationTag.Username))) {
|
---|
371 | MessageBay.errorMessage("The provided username must begin and end with a letter and contain only letters and numbers inbetween, please choose another.");
|
---|
372 | return;
|
---|
373 | }
|
---|
374 |
|
---|
375 | // The passwords provided must match
|
---|
376 | if (userData.get(AuthenticationTag.Password).compareTo(userData.get(AuthenticationTag.PasswordAgain)) != 0) {
|
---|
377 | MessageBay.errorMessage("The provided passwords do not match, please fix this and try again.");
|
---|
378 | return;
|
---|
379 | }
|
---|
380 |
|
---|
381 | // The emails provided must match
|
---|
382 | if (userData.get(AuthenticationTag.Email).compareTo(userData.get(AuthenticationTag.EmailAgain)) != 0) {
|
---|
383 | MessageBay.errorMessage("The provided emails do not match, please fix this and try again.");
|
---|
384 | return;
|
---|
385 | }
|
---|
386 |
|
---|
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 | }
|
---|
394 | } else {
|
---|
395 | MessageBay.errorMessage("Please fill out all the supplied text boxes.");
|
---|
396 | }
|
---|
397 | }
|
---|
398 | // End Create Account Actions
|
---|
399 |
|
---|
400 | // Start Account Login Actions
|
---|
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 | */
|
---|
407 | public static void AuthLogin() {
|
---|
408 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
409 | Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.Username, AuthenticationTag.Password);
|
---|
410 | if (userdata.isPresent()) {
|
---|
411 | AuthenticationResult result = Authenticate.login(userdata.get());
|
---|
412 | if (result == AuthenticationResult.SuccessLogin) {
|
---|
413 | MessageBay.displayMessage(result.toString());
|
---|
414 | AuthenticatorBrowser.Authenticated = true;
|
---|
415 | } else {
|
---|
416 | MessageBay.errorMessage(result.toString());
|
---|
417 | }
|
---|
418 | } else {
|
---|
419 | MessageBay.errorMessage("Please fill out all the supplied text boxes.");
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Action used by the user to log out of their account.
|
---|
425 | */
|
---|
426 | public static void AuthLogout() {
|
---|
427 | MessageBay.displayMessage(Authenticate.logout().toString());
|
---|
428 | }
|
---|
429 | // End Account Login Actions
|
---|
430 |
|
---|
431 | // Start Change Access Actions
|
---|
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 |
|
---|
448 | if (!AuthenticatorBrowser.Authenticated) {
|
---|
449 | MessageBay.errorMessage("You must be logged in to perform this action.");
|
---|
450 | } else {
|
---|
451 | final Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, false, AuthenticationTag.Password, AuthenticationTag.NewPassword, AuthenticationTag.NewPasswordAgain);
|
---|
452 | if (userdata.isPresent()) {
|
---|
453 | final Map<AuthenticationTag, String> userData = userdata.get();
|
---|
454 | if (userData.get(AuthenticationTag.NewPassword).compareTo(userData.get(AuthenticationTag.NewPasswordAgain)) != 0) {
|
---|
455 | MessageBay.errorMessage("The provided passwords do not match, please fix this and try again.");
|
---|
456 | } else {
|
---|
457 | userData.put(AuthenticationTag.Username, UserSettings.UserName.get());
|
---|
458 | Password.changePassword(userData);
|
---|
459 | }
|
---|
460 | } else {
|
---|
461 | MessageBay.errorMessage("Please fill out all the supplied text boxes.");
|
---|
462 | }
|
---|
463 | }
|
---|
464 | }
|
---|
465 | // End Change Access Actions
|
---|
466 |
|
---|
467 | // Start Private Helper Functions.
|
---|
468 | /**
|
---|
469 | * Gets the public email address associated with the specified username.
|
---|
470 | * @param username
|
---|
471 | * @return
|
---|
472 | */
|
---|
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);
|
---|
491 | Collection<Text> textItems = credentialsFrame.getTextItems();
|
---|
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 | }
|
---|
502 | // End Private Helper Functions.
|
---|
503 |
|
---|
504 | // Start Future Functionality
|
---|
505 | public static void AuthShareFrameset() throws IOException {
|
---|
506 | Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
|
---|
507 |
|
---|
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) {
|
---|
516 | MessageBay.errorMessage("Insufficient information provided to complete this action.");
|
---|
517 | return;
|
---|
518 | }
|
---|
519 |
|
---|
520 | shareFrameset(toShare);
|
---|
521 | }
|
---|
522 | }
|
---|
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.
|
---|
527 | */
|
---|
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;
|
---|
535 | }
|
---|
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.");
|
---|
542 | }
|
---|
543 |
|
---|
544 | /*
|
---|
545 | * Function to submit a request to specified contacts to be the current users pw colleagues.
|
---|
546 | */
|
---|
547 | @SuppressWarnings("unused")
|
---|
548 | private static boolean submitTrustedUsersPasswordRecovery(Map<AuthenticationTag, String> userData) throws InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException {
|
---|
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);
|
---|
553 | if (colleagueOneKey == null) {
|
---|
554 | MessageBay.errorMessage("Unable to get public key for colleague: " + trustedUserOne);
|
---|
555 | return false;
|
---|
556 | } else if (colleagueTwoKey == null) {
|
---|
557 | MessageBay.errorMessage("Unable to get public key for colleague: " + trustedUserTwo);
|
---|
558 | return false;
|
---|
559 | } else {
|
---|
560 | String time = Mail.FORMAT.format(new Date());
|
---|
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.";
|
---|
564 | Map<String, String> arguments = new HashMap<String, String>();
|
---|
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);
|
---|
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);
|
---|
572 | AuthenticatorBrowser.getInstance().markRequestedColleagues(UserSettings.UserName.get());
|
---|
573 | return true;
|
---|
574 | }
|
---|
575 | }
|
---|
576 | // End Future Functionality
|
---|
577 | }
|
---|