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