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

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

Fixed bug with frame data that reared its head because of just implemented ability to encrypt frames with custom labels.

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