1 | package org.expeditee.auth.account;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Base64;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import javax.crypto.SecretKey;
|
---|
10 |
|
---|
11 | import org.expeditee.auth.Actions;
|
---|
12 | import org.expeditee.auth.AuthenticatorBrowser;
|
---|
13 | import org.expeditee.auth.mail.gui.MailBay;
|
---|
14 | import org.expeditee.auth.tags.AuthenticationTag;
|
---|
15 | import org.expeditee.encryption.CryptographyConstants;
|
---|
16 | import org.expeditee.gui.Browser;
|
---|
17 | import org.expeditee.gui.DisplayController;
|
---|
18 | import org.expeditee.gui.Frame;
|
---|
19 | import org.expeditee.gui.FrameIO;
|
---|
20 | import org.expeditee.gui.FrameUtils;
|
---|
21 | import org.expeditee.gui.MessageBay;
|
---|
22 | import org.expeditee.gui.management.ResourceManager;
|
---|
23 | import org.expeditee.items.Item;
|
---|
24 | import org.expeditee.items.Text;
|
---|
25 | import org.expeditee.settings.Settings;
|
---|
26 | import org.expeditee.settings.UserSettings;
|
---|
27 | import org.expeditee.settings.identity.secrets.KeyList;
|
---|
28 |
|
---|
29 | public class Authenticate implements CryptographyConstants {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Given a username and password, potentially login.
|
---|
33 | * @param userdata
|
---|
34 | * @return AuthenticationResult.SuccessLogin if login works, AuthenticationResult.ErrorLoginNobody or AuthenticationResult.ErrorLoginUsernamePasswordCombo otherwise.
|
---|
35 | */
|
---|
36 | public static AuthenticationResult login(Map<AuthenticationTag, String> userdata) {
|
---|
37 | String username = userdata.get(AuthenticationTag.Username);
|
---|
38 | String password = userdata.get(AuthenticationTag.Password);
|
---|
39 |
|
---|
40 | if (username.equals(AuthenticatorBrowser.USER_NOBODY)) {
|
---|
41 | return AuthenticationResult.ErrorLoginNobody;
|
---|
42 | }
|
---|
43 |
|
---|
44 | SecretKey personalKey = null;
|
---|
45 | try {
|
---|
46 | personalKey = AuthenticatorBrowser.getInstance().getSecretKey(username, password);
|
---|
47 | } catch (Exception e) {
|
---|
48 | return AuthenticationResult.ErrorLoginUsernamePasswordCombo;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (personalKey == null) {
|
---|
52 | return AuthenticationResult.ErrorLoginUsernamePasswordCombo;
|
---|
53 | }
|
---|
54 |
|
---|
55 | System.setProperty("user.name", username);
|
---|
56 | UserSettings.UserName.set(username);
|
---|
57 | if (!username.equals(AuthenticatorBrowser.ADMINACCOUNT)) {
|
---|
58 | // Set the personal key to bootstrap the encrypted frame loading.
|
---|
59 | Text personalKeyText = KeyList.PersonalKey.generateText();
|
---|
60 | personalKeyText.setData(Base64.getEncoder().encodeToString(personalKey.getEncoded()));
|
---|
61 | KeyList.PersonalKey.setSetting(personalKeyText);
|
---|
62 |
|
---|
63 | // Load in and cache the profile frame using the personal key fetched from keystore.
|
---|
64 | FrameIO.ClearCache();
|
---|
65 | Frame oneFrame = FrameIO.LoadProfile(username);
|
---|
66 | for (int i = 1; i <= FrameIO.getLastNumber(oneFrame.getFramesetName()); i++) {
|
---|
67 | Frame f = FrameIO.LoadFrame(oneFrame.getFramesetName() + i);
|
---|
68 | if (f != null) {
|
---|
69 | List<String> data = f.getData();
|
---|
70 | if(data != null && data.contains("MultiuserCredentials")) {
|
---|
71 | AuthenticatorBrowser.CREDENTIALS_FRAME = f.getNumber();
|
---|
72 | } else if (data != null && data.contains("PasswordColleagues")) {
|
---|
73 | AuthenticatorBrowser.PASSWORD_RECOVERY_FRAME = f.getNumber();
|
---|
74 | } else if (data != null && data.contains("SecretsFrame")) {
|
---|
75 | AuthenticatorBrowser.SECRETS_FRAME = f.getNumber();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // Update were we get our frames.
|
---|
81 | UserSettings.setupDefaultFolders();
|
---|
82 | MessageBay.clear();
|
---|
83 | MessageBay.updateFramesetLocation();
|
---|
84 | MailBay.reconnectToUser(UserSettings.UserName.get());
|
---|
85 |
|
---|
86 | // Parse the users profile to refresh settings.
|
---|
87 | //Text settingsLink = new Text("settings");
|
---|
88 | //settingsLink.setLink(oneFrame.getFramesetName() + "2");
|
---|
89 | //Settings.parseSettings(settingsLink);
|
---|
90 | FrameUtils.ParseProfile(oneFrame);
|
---|
91 | ResourceManager.invalidateAllResourceDirectories();
|
---|
92 |
|
---|
93 | // At this point we at least login, but maybe with problems.
|
---|
94 | AuthenticationResult res = AuthenticationResult.SuccessLogin;
|
---|
95 |
|
---|
96 | // Check mail for new user.
|
---|
97 | MailBay.checkMail();
|
---|
98 |
|
---|
99 | Collection<Item> usernameFields = Actions.getByData(FrameIO.LoadFrame("multiuser1"), "txtUsername");
|
---|
100 | usernameFields.forEach(usernameField -> usernameField.setText(username));
|
---|
101 |
|
---|
102 | Frame requestedFrame = FrameIO.LoadFrame(Browser.getStartFrame());
|
---|
103 | Frame homeFrame = FrameIO.LoadFrame("home1");
|
---|
104 | Frame choice = requestedFrame != null ? requestedFrame : homeFrame != null ? homeFrame : oneFrame;
|
---|
105 | DisplayController.setCurrentFrame(choice, true);
|
---|
106 | }
|
---|
107 |
|
---|
108 | return AuthenticationResult.SuccessLogin;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Logs out the current authenticated user.
|
---|
113 | * @return AuthenticationResult.SuccessLogout to signal the logout has occured.
|
---|
114 | */
|
---|
115 | public static AuthenticationResult logout() {
|
---|
116 | // Set user to nobody.
|
---|
117 | System.setProperty("user.name", AuthenticatorBrowser.USER_NOBODY);
|
---|
118 | UserSettings.UserName.set(AuthenticatorBrowser.USER_NOBODY);
|
---|
119 |
|
---|
120 | // Update were we get our frames.
|
---|
121 | UserSettings.setupDefaultFolders();
|
---|
122 | MessageBay.updateFramesetLocation();
|
---|
123 |
|
---|
124 | // Reset all of the settings.
|
---|
125 | Settings.resetAllSettings();
|
---|
126 |
|
---|
127 | // Display login frame
|
---|
128 | Frame auth1 = FrameIO.LoadFrame("authentication1");
|
---|
129 | DisplayController.setCurrentFrame(auth1, true);
|
---|
130 |
|
---|
131 | return AuthenticationResult.SuccessLogout;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public enum AuthenticationResult {
|
---|
135 |
|
---|
136 | SuccessLogin, SuccessLogout, ErrorLoginNobody, ErrorLoginUsernamePasswordCombo;
|
---|
137 |
|
---|
138 | private List<String> additionalInfo = new ArrayList<String>();
|
---|
139 |
|
---|
140 | public String toString() {
|
---|
141 | switch (this) {
|
---|
142 | case SuccessLogin:
|
---|
143 | StringBuilder sb = new StringBuilder();
|
---|
144 | sb.append("Logged in as: " + UserSettings.UserName.get());
|
---|
145 | if (additionalInfo.isEmpty()) {
|
---|
146 | return sb.toString();
|
---|
147 | } else {
|
---|
148 | String nl = System.getProperty("line.separator");
|
---|
149 | sb.append("However: " + nl);
|
---|
150 | for (String info: additionalInfo) {
|
---|
151 | sb.append(info + nl);
|
---|
152 | }
|
---|
153 | return sb.toString();
|
---|
154 | }
|
---|
155 | case SuccessLogout:
|
---|
156 | return "You are now logged out of Expeditee.";
|
---|
157 | case ErrorLoginNobody:
|
---|
158 | return "You cannot log into Expeditee as the user \'nobody\'";
|
---|
159 | case ErrorLoginUsernamePasswordCombo:
|
---|
160 | return "The username + password combination was incorrect.";
|
---|
161 | }
|
---|
162 |
|
---|
163 | String message = "Was the list of possible enum results updated without nessasary changes to the toString() function?";
|
---|
164 | throw new IllegalArgumentException(message);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|