source: trunk/src/org/expeditee/auth/account/Authenticate.java@ 1454

Last change on this file since 1454 was 1454, checked in by bnemhaus, 5 years ago

Keep how the system remembers username in lockstep with current username in settings.

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