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

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

You can now check for new mail while logged in (when you open the mail bay)

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