source: trunk/src/org/expeditee/auth/Actions.java@ 1227

Last change on this file since 1227 was 1227, checked in by bln4, 5 years ago

org.expeditee.auth.EncryptedExpReader ->
org.expeditee.auth.EncryptedExpWriter ->
Actions ->
AttributeUtils ->
Frame ->
FrameIO ->
UserSettings ->

Changed how reading and writing encrypted files worked. A Frame attribute is now consulted to determine what to use as key for encryption. The 'profile' attribute setting is used to signal that the users personal aes key is used. Further enhancement will mean that other labels will be able to be used.


Actions ->

MailMode action now consults the database to reaquire the mail.

File size: 28.1 KB
Line 
1package org.expeditee.auth;
2
3import java.io.FileNotFoundException;
4import java.io.IOException;
5import java.security.KeyFactory;
6import java.security.KeyPair;
7import java.security.KeyPairGenerator;
8import java.security.KeyStoreException;
9import java.security.NoSuchAlgorithmException;
10import java.security.PrivateKey;
11import java.security.PublicKey;
12import java.security.UnrecoverableEntryException;
13import java.security.cert.CertificateException;
14import java.security.spec.InvalidKeySpecException;
15import java.security.spec.PKCS8EncodedKeySpec;
16import java.sql.SQLException;
17import java.util.Base64;
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22import java.util.Optional;
23import java.util.Random;
24
25import javax.crypto.SecretKey;
26import javax.crypto.spec.SecretKeySpec;
27
28import org.expeditee.auth.Mail.MailEntry;
29import org.expeditee.auth.gui.MailBay;
30import org.expeditee.core.Colour;
31import org.expeditee.gui.DisplayController;
32import org.expeditee.gui.Frame;
33import org.expeditee.gui.FrameIO;
34import org.expeditee.gui.MessageBay;
35import org.expeditee.items.Item;
36import org.expeditee.items.Text;
37import org.expeditee.settings.UserSettings;
38import org.ngikm.cryptography.CryptographyConstants;
39
40public class Actions implements CryptographyConstants {
41
42 /**
43 * Display Expeditee Mail
44 * @throws IOException
45 * @throws SQLException
46 * @throws ClassNotFoundException
47 * @throws CertificateException
48 * @throws NoSuchAlgorithmException
49 * @throws FileNotFoundException
50 * @throws KeyStoreException
51 */
52 public static void MailMode() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, SQLException, IOException {
53 if (!DisplayController.isMailMode()) {
54 Mail.clear();
55 Authenticator.getInstance().loadMailDatabase();
56 }
57 DisplayController.ToggleMailMode();
58 }
59
60 /**
61 * Action used to navigate the authorised user back to their desktop.
62 */
63 public static void AuthGoToDesktop() {
64 DisplayController.setCurrentFrame(FrameIO.LoadFrame(UserSettings.HomeFrame.get()), true);
65 }
66
67 /**
68 * Log out of current user and navigate to authentication1
69 */
70 public static void AuthLogout() {
71 UserSettings.Authenticated.set(false);
72 setUser(System.getProperty("user.name"));
73 DisplayController.setCurrentFrame(FrameIO.LoadFrame("authentication1"), true);
74 }
75
76 /**
77 * Action used to created a new user account.
78 * Attempts to use content from text items on frame, will default to java properties if they cannot be found.
79 * Will fail if it cannot find content from text items on frame and all required java properties are not present.
80 * @throws Exception
81 */
82 public static void AuthCreateAccount() throws Exception {
83 final Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
84 final Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, AuthenticationTag.Username, AuthenticationTag.Password, AuthenticationTag.PasswordAgain);
85 if (userdata.isPresent()) {
86 final Map<AuthenticationTag, String> userData = userdata.get();
87 final String password = userData.get(AuthenticationTag.Password);
88 final String passwordAgain = userData.get(AuthenticationTag.PasswordAgain);
89 if (password.compareTo(passwordAgain) == 0) {
90 createAccount(userData);
91 } else {
92 MessageBay.errorMessage("The passwords provided do not match.");
93 }
94 } else {
95 MessageBay.errorMessage("Please enter a username and password ( + repeated password) to create an account.");
96 }
97 }
98
99 /**
100 * Action used to start authentication as a specified user.
101 * Attempts to use content from text items on frame, will default to java properties if they cannot be found.
102 * Will fail if it cannot find content from text items on frame and all required java properties are not present.
103 * @throws Exception
104 */
105 public static void AuthLogin() throws Exception {
106 final Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
107 final Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, AuthenticationTag.Username, AuthenticationTag.Password);
108 if (userdata.isPresent()) {
109 login(userdata.get());
110 }
111 else {
112 MessageBay.errorMessage("Please enter a username and password to log in.");
113 }
114 }
115
116 /**
117 * Action used to change the currently authenticated users password.
118 * Attempts to use content from text items on frame, will default to java properties if they cannot be found.
119 * Will fail if it cannot find content from text items on frame and all required java properties are not present.
120 * Will fail if no user is currently logged in.
121 * @throws IOException
122 * @throws CertificateException
123 * @throws FileNotFoundException
124 * @throws KeyStoreException
125 * @throws NoSuchAlgorithmException
126 * @throws SQLException
127 * @throws ClassNotFoundException
128 */
129 public static void AuthChangePassword() throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, IOException, ClassNotFoundException, SQLException {
130 final Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
131
132 if (!UserSettings.Authenticated.get()) {
133 MessageBay.errorMessage("You must be logged in to change your password.");
134 } else {
135 final Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, AuthenticationTag.Password, AuthenticationTag.NewPassword, AuthenticationTag.NewPasswordAgain);
136 if (userdata.isPresent()) {
137 final Map<AuthenticationTag, String> userData = userdata.get();
138 if (userData.get(AuthenticationTag.NewPassword).compareTo(userData.get(AuthenticationTag.NewPasswordAgain)) != 0) {
139 MessageBay.errorMessage("The passwords provided do not match.");
140 } else {
141 userData.put(AuthenticationTag.Username, UserSettings.UserName.get());
142 changePassword(userData);
143 }
144 } else {
145 MessageBay.errorMessage("Insufficient information provided to complete this action.");
146 }
147 }
148 }
149
150 /**
151 * Part of the process to secure an authorised account, the AuthAssociateEmail action associates a supplied email
152 * with the currently logged in user.
153 */
154 public static void AuthAssociateEmail() {
155 Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
156
157 if (!UserSettings.Authenticated.get()) {
158 MessageBay.errorMessage("You must be logged in to perform this action.");
159 } else {
160 Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, AuthenticationTag.Email);
161 if (userdata.isPresent()) {
162 Map<AuthenticationTag, String> userData = userdata.get();
163 userData.put(AuthenticationTag.Username, UserSettings.UserName.get());
164 String emailRegex = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$";
165 if (!userData.get(AuthenticationTag.Email).toUpperCase().matches(emailRegex)) {
166 MessageBay.errorMessage("Invalid email address supplied.");
167 } else {
168 try {
169 associateEmail(userData);
170 DisplayController.setCurrentFrame(FrameIO.LoadFrame("authentication5"), true);
171 } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | ClassNotFoundException | IOException | SQLException e) {
172 MessageBay.errorMessage("An unknown error has occured. Details sent to standard error.");
173 e.printStackTrace();
174 }
175 }
176 } else {
177 MessageBay.errorMessage("Insufficient information provided to complete this action.");
178 }
179 }
180 }
181
182 /**
183 * Navigation action for progressing the process of recruiting colleagues to assist in password recovery.
184 * Hides certain content that AuthSubmitPWCollegues goes onto show if it does not fail.
185 */
186 public static void AuthGotoColleagueSubmissionFrame() {
187 Frame destination = FrameIO.LoadFrame("authentication7");
188 DisplayController.setCurrentFrame(destination, true);
189 Collection<Item> toHide = getByData(destination, "ShowOnProgress");
190 for (Item i: toHide) {
191 i.setVisible(false);
192 }
193 }
194
195 /**
196 * Action used to start the process of formalising the password recovery process.
197 * @throws SQLException
198 * @throws IOException
199 * @throws ClassNotFoundException
200 * @throws CertificateException
201 * @throws NoSuchAlgorithmException
202 * @throws FileNotFoundException
203 * @throws KeyStoreException
204 * @throws InvalidKeySpecException
205 */
206 public static void AuthSubmitPWColleagues() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, IOException, SQLException, InvalidKeySpecException {
207 final Frame currentFrame = DisplayController.getCurrentFrame();
208 Collection<Text> textItems = currentFrame.getTextItems();
209
210 if (!UserSettings.Authenticated.get()) {
211 MessageBay.errorMessage("You must be logged in to perform this action.");
212 } else {
213 Optional<Map<AuthenticationTag, String>> userdata = AuthenticationTag.fetchUserData(textItems, AuthenticationTag.ColleagueOne, AuthenticationTag.ColleagueTwo);
214 if (userdata.isPresent()) {
215 Map<AuthenticationTag, String> userData = userdata.get();
216 userData.put(AuthenticationTag.Username, UserSettings.UserName.get());
217 boolean succsess = submitPWColleagues(userData);
218 if (succsess) {
219 Collection<Item> toShow = getByData(currentFrame, "ShowOnProgress");
220 for (Item i: toShow) {
221 i.setVisible(true);
222 }
223 currentFrame.change();
224 }
225 FrameIO.SaveFrame(currentFrame);
226 DisplayController.requestRefresh(false);
227 } else {
228 MessageBay.errorMessage("Insufficient information provided to complete this action.");
229 }
230 }
231 }
232
233 public static void AuthSetupPasswordRecovery() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, IOException, SQLException, UnrecoverableEntryException {
234 if (!UserSettings.Authenticated.get()) {
235 MessageBay.errorMessage("You must be logged in to perform this action.");
236 } else if (!Authenticator.getInstance().hasRegisteredEmail(UserSettings.UserName.get())) {
237 Frame registerEmailFrame = FrameIO.LoadFrame("authentication4");
238 DisplayController.setCurrentFrame(registerEmailFrame, true);
239 } else if (!Authenticator.getInstance().hasRequestedColleagues(UserSettings.UserName.get()) && Authenticator.getInstance().getColleagues(UserSettings.UserName.get()) == null) {
240 Frame submitColleaguesFrame = FrameIO.LoadFrame("authentication5");
241 DisplayController.setCurrentFrame(submitColleaguesFrame, true);
242 } else if (Authenticator.getInstance().hasRequestedColleagues(UserSettings.UserName.get()) && Authenticator.getInstance().getColleagues(UserSettings.UserName.get()) == null) {
243 MessageBay.displayMessage("You have already nominated two colleagues to assist you in the process of password recovery and are awaiting their response."
244 + " You will be alerted on Expeditee startup when they have both responded.");
245 } else if (Authenticator.getInstance().getColleagues(UserSettings.UserName.get()) != null) {
246 MessageBay.displayMessage("You have completed the Password Recovery Setup process, there is nothing more to do here.");
247 }
248 }
249
250 public static void AuthConfirmPasswordColleagueRelationship(String colleagueName) {
251
252 }
253
254 public static void AuthDenyPasswordColleagueRelationship(String colleagueName) throws InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException {
255 denyPasswordColleagueRelationship(colleagueName);
256 }
257
258 public static void AuthClearPWColleaguesNominated() {
259
260 }
261
262 private static void denyPasswordColleagueRelationship(String colleagueName) throws InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, ClassNotFoundException, IOException, SQLException {
263 String sender = UserSettings.UserName.get();
264 String message = "You have received a reply from " + sender + " reguarding your request for assistance.";
265 String message2 = "Unfortunately " + sender + " has indicated that they are unable to help you with your potential password recovery.";
266 Map<String, String> options = new HashMap<String, String>();
267 options.put("Clear Preview Colleague Nominations", "AuthClearPWColleaguesNominated");
268 MailEntry mail = new MailEntry(sender, colleagueName, message, message2, options);
269 Mail.sendMail(mail, Authenticator.getInstance().getPublicKey(colleagueName));
270 }
271
272 private static boolean submitPWColleagues(Map<AuthenticationTag, String> userData) throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException, CertificateException, ClassNotFoundException, IOException, SQLException, InvalidKeySpecException {
273 // Confirm both colleagues exist
274 String colleagueOne = userData.get(AuthenticationTag.ColleagueOne);
275 String colleagueTwo = userData.get(AuthenticationTag.ColleagueTwo);
276 PublicKey colleagueOneKey = null;
277 PublicKey colleagueTwoKey = null;
278 if (!FrameIO.canAccessFrameset(colleagueOne) && FrameIO.canAccessFrameset(colleagueTwo)) {
279 MessageBay.errorMessage("At least one of the colleagues you specified does not have a Expeditee profile.");
280 return false;
281 } else if ((colleagueOneKey = Authenticator.getInstance().getPublicKey(colleagueOne)) == null ||
282 (colleagueTwoKey = Authenticator.getInstance().getPublicKey(colleagueTwo)) == null) {
283 MessageBay.errorMessage("At least one of the colleagues you specified is not set up to be authorised.");
284 return false;
285 } else {
286 String sender = UserSettings.UserName.get();
287 String message = "You have received a request for cooperation from your colleague " + sender;
288 String message2 = "\"I would like to receive your help if I happen to forget my password.\"";
289 Map<String, String> arguments = new HashMap<String, String>();
290 arguments.put("I Agree To Assist " + sender + " If They Forget Their Password" , "AuthConfirmPasswordColleagueRelationship " + sender);
291 arguments.put("I Wish To Excuse Myself From This Responsibility", "AuthDenyPasswordColleagueRelationship " + sender);
292 MailEntry mail = new MailEntry(sender, colleagueOne, message, message2, arguments);
293 Mail.sendMail(mail, colleagueOneKey);
294 mail = new MailEntry(sender, colleagueTwo, message, message2, arguments);
295 Mail.sendMail(mail, colleagueTwoKey);
296 Authenticator.getInstance().markRequestedColleagues(UserSettings.UserName.get());
297 return true;
298 }
299 }
300
301 /*
302 * Function used to authenticate as a specified user (via function arguments).
303 */
304 private static void login(final Map<AuthenticationTag, String> userdata) throws Exception {
305 final String username = userdata.get(AuthenticationTag.Username);
306 final String password = userdata.get(AuthenticationTag.Password);
307
308 final SecretKey personalKey = Authenticator.getInstance().getSecretKey(username, password);
309 if (personalKey == null) {
310 // Incorrect username and password
311 MessageBay.errorMessage("The username + password combination was incorrect.");
312 } else {
313 // Proceed with login process.
314 org.expeditee.settings.auth.secrets.KeyList.PersonalKey.setSetting(new Text(Base64.getEncoder().encodeToString(personalKey.getEncoded())));
315 setUser(username);
316 MessageBay.displayMessage("Logged in as: " + UserSettings.UserName.get());
317
318 // Set Public Key and private key in settings datastructure
319 String profileName = UserSettings.ProfileName.get();
320 int lastNumber = FrameIO.getLastNumber(profileName);
321 for (int i = 1; i <= lastNumber; i++) {
322 Frame frame = FrameIO.LoadFrame(profileName + i);
323 if (frame == null) {
324 continue;
325 }
326 Collection<Item> items = frame.getAllItems();
327 boolean foundPublic = false;
328 boolean foundPrivate = false;
329 for (Item item: items) {
330 if (item.getText().compareTo("PublicKey") == 0) {
331 org.expeditee.settings.auth.KeyList.PublicKey.set((Text) item);
332 foundPublic = true;
333 } else if (item.getText().compareTo("PrivateKey") == 0) {
334 org.expeditee.settings.auth.secrets.KeyList.PrivateKey.set((Text) item);
335 foundPrivate = true;
336 }
337 }
338 if (foundPublic && foundPrivate) { break; }
339 }
340
341 // Check mail
342 MailBay.clear();
343 Authenticator.getInstance().loadMailDatabase();
344 Text keyItem = org.expeditee.settings.auth.secrets.KeyList.PrivateKey.get();
345 if (keyItem.getData() != null) {
346 String keyEncoded = keyItem.getData().get(0);
347 byte[] keyBytes = Base64.getDecoder().decode(keyEncoded);
348 PrivateKey key = KeyFactory.getInstance(AsymmetricAlgorithm).generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
349 List<MailEntry> mailForLoggingInUser = Mail.getEntries(UserSettings.UserName.get(), key);
350 for (MailEntry mail: mailForLoggingInUser) {
351 MailBay.addMessage(mail.message, mail.message2, mail.options);
352 }
353 }
354
355 UserSettings.Authenticated.set(true);
356 // navigate to logged in profile
357 DisplayController.setCurrentFrame(loadProfile(username), false);
358 }
359 }
360
361 /*
362 * Creates an account from specified user data.
363 */
364 private static void createAccount(final Map<AuthenticationTag, String> userdata) throws Exception {
365 String username = userdata.get(AuthenticationTag.Username);
366 String password = userdata.get(AuthenticationTag.Password);
367 if (FrameIO.LoadProfile(username) != null) {
368 MessageBay.errorMessage("A Expeditee profile with this username already exists, please choose another.");
369 } else {
370 // generate the newly created user a profile frame using the specialized default frame as a blueprint
371 String defaultBackup = UserSettings.DEFAULT_PROFILE_NAME;
372 UserSettings.DEFAULT_PROFILE_NAME = "multiusermodedefault";
373 Frame profile = FrameIO.CreateNewProfile(username);
374 UserSettings.DEFAULT_PROFILE_NAME = defaultBackup;
375
376 // perform necessary changes to generated frame: remove default title, set background color, set custom title
377 profile.getTitleItem().delete();
378 profile.setBackgroundColor(new Colour(1, 1, 0.39f));
379 getByData(profile, "txtUsername").stream().forEach(i -> i.setText(username));
380 getByText(profile, "“If Aphroditē be the godess of love").stream().forEach(i -> i.setWidth(300));
381 getByText(profile, "Sleek like Aphroditē, Wise like Athēnâ.").stream().forEach(i -> i.setWidth(300));
382
383 // update ownership
384 int lastNumber = FrameIO.getLastNumber(profile.getFramesetName());
385 for (int i = 1; i <= lastNumber; i++) {
386 Frame frame = FrameIO.LoadFrame(profile.getFramesetName() + i);
387 frame.setOwner(username);
388 frame.setEncryptionLabel("Profile");
389 Collection<Item> items = frame.getAllItems();
390 for (Item item: items) {
391 String newText = item.getText().replace("authadmin", username);
392 item.setText(newText);
393 }
394 }
395
396 // generate and store personal, public and private keys
397 String backupUser = UserSettings.UserName.get();
398 setUser(username);
399 UserSettings.Authenticated.set(true);
400
401 // personal key
402 Random rand = new Random();
403 byte[] keyBytes = new byte[16];
404 rand.nextBytes(keyBytes);
405 SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
406 Authenticator.getInstance().putKey(username, password, key);
407 org.expeditee.settings.auth.secrets.KeyList.PersonalKey.setSetting(new Text(Base64.getEncoder().encodeToString(key.getEncoded())));
408
409 // public+private key pair
410 KeyPairGenerator keyGen = KeyPairGenerator.getInstance(AsymmetricAlgorithm);
411 keyGen.initialize(1024);
412 KeyPair keyPair = keyGen.generateKeyPair();
413
414 org.expeditee.settings.auth.secrets.KeyList.PrivateKey.setSetting(new Text(Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded())));
415 org.expeditee.settings.auth.KeyList.PublicKey.setSetting(new Text(Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded())));
416
417 // ensure save of keys
418 lastNumber = FrameIO.getLastNumber(profile.getFramesetName());
419 for (int i = 1; i <= lastNumber; i++) {
420 Frame frame = FrameIO.LoadFrame(profile.getFramesetName() + i);
421 frame.setChanged(true);
422 FrameIO.SaveFrame(frame);
423 }
424 setUser(backupUser);
425 UserSettings.Authenticated.set(false);
426
427 // proceed to log into new account.
428 login(userdata);
429 }
430 }
431
432 /*
433 * Changes the recorded password for a user in the key store.
434 */
435 private static void changePassword(final Map<AuthenticationTag, String> userdata) throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, CertificateException, IOException, ClassNotFoundException, SQLException {
436 final String username = userdata.get(AuthenticationTag.Username);
437 final String password = userdata.get(AuthenticationTag.Password);
438 final String newpassword = userdata.get(AuthenticationTag.NewPassword);
439
440 final SecretKey key = Authenticator.getInstance().getSecretKey(username, password);
441 if (key == null) {
442 MessageBay.errorMessage("The username + existing password combination was incorrect.");
443 } else {
444 Authenticator.getInstance().putKey(username, newpassword, key);
445 MessageBay.displayMessage("Password changed successfully.");
446 }
447 }
448
449 private static void associateEmail(Map<AuthenticationTag, String> userdata) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, ClassNotFoundException, IOException, SQLException {
450 Authenticator.getInstance().putEmail(
451 userdata.get(AuthenticationTag.Username),
452 userdata.get(AuthenticationTag.Email));
453 }
454
455 private static void setUser(final String username) {
456 UserSettings.UserName.set(username);
457 UserSettings.ProfileName.set(username);
458 UserSettings.HomeFrame.set(username + "1");
459 UserSettings.UserName.setDefault(username);
460 UserSettings.ProfileName.setDefault(username);
461 UserSettings.HomeFrame.setDefault(username + "1");
462 }
463
464 private static Frame loadProfile(final String username) throws Exception {
465 final Frame frameOne = FrameIO.LoadProfile(username);
466 if (frameOne != null) {
467 return frameOne;
468 } else {
469 final Frame profile = FrameIO.CreateNewProfile(username);
470 for (int i = 0; i < FrameIO.getLastNumber(profile.getFramesetName()); i++) {
471 final Frame frame = FrameIO.LoadFrame(profile.getFramesetName() + i);
472 final Collection<Text> textItems = frame.getTextItems();
473 for (final Text t: textItems) {
474 if (t.getText().startsWith("User name:")) {
475 t.setText("User name: " + UserSettings.UserName.get());
476 } else if (t.getText().startsWith("Profile name:")) {
477 t.setText("Profile name: " + UserSettings.ProfileName.get());
478 }
479 }
480 FrameIO.ForceSaveFrame(frame);
481 }
482 return profile;
483 }
484 }
485
486// public static void AuthRecoverPasswordStage1() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException {
487// final Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
488// final Optional<Text> txtUsername = textItems.stream().filter(t -> t.getData() != null && t.getData().contains("txtUsername")).findFirst();
489// final Optional<Text> txtRecoveryEmail = textItems.stream().filter(t -> t.getData() != null && t.getData().contains("txtRecoveryEmail")).findFirst();
490// final String username = txtUsername.isPresent() ? txtUsername.get().getText() : System.getProperty("user.name");
491// final String email = txtRecoveryEmail.isPresent() ? txtRecoveryEmail.get().getText() : System.getProperty("user.recoveryemail");
492// if (areAllPresent(new String[] { username, email })) {
493// // generate intergalactic number
494// final String intergalacticNumber = Authenticator.getInstance().newIntergalacticNumber(username, email);
495//
496// // establish properties
497// final String from = "[email protected]";
498// final Properties properties = System.getProperties();
499//
500// properties.setProperty("mail.transport.protocol", "smtp");
501// properties.setProperty("mail.smtp.host", "smtp.gmail.com");
502// properties.setProperty("mail.smtp.port", "465");
503// properties.setProperty("mail.smtp.starttls.enable", "true");
504// properties.setProperty("mail.smtp.auth", "true");
505// properties.setProperty("mail.smtp.debug", "true");
506// properties.setProperty("mail.smtp.auth", "true");
507// properties.setProperty("mail.smtp.socketFactory.port", "465");
508// properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
509// properties.setProperty("mail.smtp.socketFactory.fallback", "false");
510//
511// final Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
512// @Override
513// protected PasswordAuthentication getPasswordAuthentication() {
514// return new PasswordAuthentication("noreply.expeditee", "intergalacticnumber");
515// };
516// });
517//
518// try {
519// // construct email message
520// final MimeMessage message = new MimeMessage(session);
521// message.setFrom(new InternetAddress(from));
522// message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
523// message.setSubject("Expeditee Password Recovery");
524// message.setText(intergalacticNumber);
525//
526// // send email message
527// Transport.send(message);
528// } catch (final MessagingException e) {
529// e.printStackTrace();
530// }
531//
532// FrameUtils.DisplayFrame("4", false, true);
533// } else {
534// MessageBay.errorMessage("To recover a lost password please fill in all the provided fields.");
535// }
536// }
537//
538// public static void AuthRecoverPasswordStage2() throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException {
539// final Collection<Text> textItems = DisplayController.getCurrentFrame().getTextItems();
540// final Optional<Text> txtUsername = textItems.stream().filter(t -> t.getData() != null && t.getData().contains("txtUsername")).findFirst();
541// final Optional<Text> txtRecoveryEmail = textItems.stream().filter(t -> t.getData() != null && t.getData().contains("txtRecoveryEmail")).findFirst();
542// final Optional<Text> txtIntergalacticNumber = textItems.stream().filter(t -> t.getData() != null && t.getData().contains("txtIntergalacticNumber")).findFirst();
543// final String username = txtUsername.isPresent() ? txtUsername.get().getText() : System.getProperty("user.name");
544// final String email = txtRecoveryEmail.isPresent() ? txtRecoveryEmail.get().getText() : System.getProperty("user.recoveryemail");
545// final String intergalacticNumber = txtIntergalacticNumber.isPresent() ? txtIntergalacticNumber.get().getText() : System.getProperty("user.intergalacticnumber");
546// if (areAllPresent(new String[] { username, email, intergalacticNumber })) {
547// // confirm intergalactic number matches what is stored
548// final boolean intergalaticNumberMatches = Authenticator.getInstance().confirmIntergalaticNumber(username, email, intergalacticNumber);
549// if (intergalaticNumberMatches) {
550// final Frame changePasswordFrame = FrameIO.LoadFrame("5");
551// final Optional<Text> actionNewPassword = changePasswordFrame.getTextItems().stream().filter(t -> t.getAction().contains("AuthNewPassword")).findFirst();
552// if (actionNewPassword.isPresent()) {
553// actionNewPassword.get().setData(intergalacticNumber);
554// }
555// FrameUtils.DisplayFrame("5", false, true);
556// }
557// } else {
558// MessageBay.errorMessage("To recover a lost password please fill in all the provided fields.");
559// }
560// }
561
562 public static void TickBox(final Text item) {
563 if (item.getBackgroundColor() != Colour.RED) {
564 item.setBackgroundColor(Colour.RED);
565 } else {
566 item.setBackgroundColor(Colour.GREEN);
567 }
568 }
569
570 /*
571 * Gets all items on a specified frame that contain the specified text.
572 */
573 private static Collection<Item> getByText(final Frame frame, final String text) {
574 final Collection<Item> allItems = frame.getAllItems();
575 allItems.removeIf(i -> i.getText() == null || !i.getText().contains(text));
576 return allItems;
577 }
578
579 /*
580 * Gets all items on a specified frame that contain the specified data.
581 */
582 private static Collection<Item> getByData(final Frame frame, final String data) {
583 final Collection<Item> allItems = frame.getAllItems();
584 allItems.removeIf(i -> i.getData() == null || !i.hasData(data));
585 return allItems;
586 }
587}
Note: See TracBrowser for help on using the repository browser.