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