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