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