- Timestamp:
- 08/15/19 12:57:28 (5 years ago)
- Location:
- trunk/src/org/expeditee
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/encryption/io/EncryptedExpReader.java
r1415 r1431 28 28 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 29 29 import org.expeditee.encryption.items.surrogates.Label; 30 //import org.expeditee.encryption.items.surrogates.Label; 30 31 import org.expeditee.encryption.items.surrogates.Label.LabelResult; 32 import org.expeditee.encryption.items.surrogates.Label.LabelInfo; 31 33 import org.expeditee.gui.Frame; 32 34 import org.expeditee.gui.FrameIO; … … 222 224 // Attempt to decrypt the line if necessary. 223 225 if (isEncryptedLine) { 224 Label Resultres = Label.getLabel(item.getEncryptionLabel());225 if (res == LabelResult.SuccessResolveLabelToKey) {226 LabelInfo res = Label.getLabel(item.getEncryptionLabel()); 227 if (res.is(LabelResult.SuccessResolveLabelToKey)) { 226 228 EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave); 227 229 item.setEncryptionDetailForTag(tag, reencryptOnSave); … … 370 372 371 373 private SecretKeySpec resolveLabel(String label) { 372 Label Resultres = Label.getLabel(label);373 if (res == LabelResult.SuccessResolveLabelToKey) {374 LabelInfo res = Label.getLabel(label); 375 if (res.is(LabelResult.SuccessResolveLabelToKey)) { 374 376 byte[] keyBytes = res.key; 375 377 return new SecretKeySpec(keyBytes, SymmetricAlgorithm); -
trunk/src/org/expeditee/encryption/io/EncryptedExpWriter.java
r1430 r1431 24 24 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 25 25 import org.expeditee.encryption.items.surrogates.EncryptionDetail.Type; 26 import org.expeditee.encryption.items.surrogates.Label.LabelResult; 26 27 import org.expeditee.encryption.items.surrogates.Label; 27 import org.expeditee.encryption.items.surrogates.Label.Label Result;28 import org.expeditee.encryption.items.surrogates.Label.LabelInfo; 28 29 import org.expeditee.gui.Frame; 29 30 import org.expeditee.gui.Frame.BodyType; … … 64 65 } 65 66 } else { 66 Label Resultres = Label.getLabel(label);67 if (res == LabelResult.SuccessResolveLabelToKey) {67 LabelInfo res = Label.getLabel(label); 68 if (res.is(LabelResult.SuccessResolveLabelToKey)) { 68 69 byte[] keyBytes = res.key; 69 70 key = new SecretKeySpec(keyBytes, SymmetricAlgorithm); 70 } else if (res == LabelResult.ErrorUnableToFindLabel) {71 } else if (res.is(LabelResult.ErrorUnableToFindLabel)) { 71 72 MessageBay.errorMessage(res.toString() + encryptionLabel); 72 73 key = null; … … 195 196 BinaryOperator<Boolean> trueExists = (a, b) -> a || b; 196 197 boolean surrogatesInherit = surrogateItems.stream().map(isTagInherited).collect(Collectors.reducing(trueExists)).orElseGet(() -> false); 197 boolean userHasKey = Label.getLabel(toWrite.getEncryptionLabel()) == LabelResult.SuccessResolveLabelToKey;198 boolean userHasKey = Label.getLabel(toWrite.getEncryptionLabel()).is(LabelResult.SuccessResolveLabelToKey); 198 199 199 200 // If we have no surrogates that inherit this property from us, and we have the label required to encrypt it, then we should encrypt it. … … 213 214 Method toRun = tags.get(tag); 214 215 Class<?> declarer = toRun.getDeclaringClass(); 215 Label Resultres = Label.getLabel(toWrite.getEncryptionLabel());216 if (declarer.isAssignableFrom(toWrite.getClass()) && res == LabelResult.SuccessResolveLabelToKey) {216 LabelInfo res = Label.getLabel(toWrite.getEncryptionLabel()); 217 if (declarer.isAssignableFrom(toWrite.getClass()) && res.is(LabelResult.SuccessResolveLabelToKey)) { 217 218 try { 218 219 Object o = toRun.invoke(toWrite, new Object[] {}); -
trunk/src/org/expeditee/encryption/items/surrogates/Label.java
r1430 r1431 4 4 import java.util.Arrays; 5 5 import java.util.Base64; 6 import java.util.Collection; 6 7 import java.util.List; 7 8 import java.util.function.Predicate; … … 10 11 11 12 import org.expeditee.gui.ItemsList; 13 import org.expeditee.gui.MessageBay; 12 14 import org.expeditee.items.Item; 13 15 import org.expeditee.items.Text; … … 15 17 16 18 public class Label { 17 18 private static int surrogateStep = -1; 19 20 public static LabelResult getLabel(String label) { 21 Text[] userLabels = KeyList.UserLabels.get(); 22 Text labelText = null; 23 for (Text userLabel : userLabels) { 24 if (userLabel.getText().equals(label)) { 25 labelText = userLabel; 19 private static List<String> userLabels = new ArrayList<String>(); 20 private static List<LabelInfo> labelsInfo = new ArrayList<LabelInfo>(); 21 private static int surrogateStep = 0; 22 private static boolean isInSurrogateMode = false; 23 private static boolean isUnprivileged = false; 24 25 public static LabelInfo getLabel(String labelName) { 26 // Maintain the local cache of user labels 27 ensureUserLabels(); 28 29 int indexOfLabel = userLabels.indexOf(labelName); 30 if (indexOfLabel == -1) { 31 //A label by the specified name does not exist. 32 return unableToFindLabelResult(labelName); 33 } else { 34 //A label by the specified name is in the local cache and can be used. 35 LabelInfo labelResult = labelsInfo.get(indexOfLabel); 36 return labelResult; 37 } 38 } 39 40 public static List<String> progressSurrogateMode(ItemsList context) { 41 // Maintain the local cache of user labels 42 ensureUserLabels(); 43 44 List<String> ret; 45 46 List<String> localLabels = getLabelsFromContext(context); 47 if (userLabels.isEmpty() || localLabels.isEmpty()) { 48 // If either the labels in the context are empty or the user 49 // has no labels then we are not able to enter surrogate mode. 50 return userLabels; 51 } 52 53 ArrayList<String> intersection = new ArrayList<String>(userLabels); 54 intersection.retainAll(localLabels); 55 if (!isInSurrogateMode) { 56 isInSurrogateMode = true; 57 // Priming surrogate step to become zero in the loop below. 58 surrogateStep = -1; 59 } 60 61 // We are already in surrogate mode, progress it. 62 int index = surrogateStep + 1; 63 boolean found = false; 64 while (index < userLabels.size()) { 65 String surrogateMode = userLabels.get(index); 66 if (intersection.contains(surrogateMode)) { 67 found = true; 26 68 break; 27 } 28 } 29 30 if (labelText == null) { 31 return unableToFindLabelResult(label); 69 } 70 index++; 71 } 72 73 if (found) { 74 isUnprivileged = false; 75 surrogateStep = index; 76 String chosenSurrogateMode = userLabels.get(surrogateStep); 77 ret = Arrays.asList(chosenSurrogateMode); 78 } else { 79 if (surrogateStep >= userLabels.size()) { 80 surrogateStep = 0; 81 isInSurrogateMode = false; 82 isUnprivileged = false; 83 ret = intersection; 84 } else { 85 ret = new ArrayList<String>(); 86 surrogateStep = index; 87 isUnprivileged = true; 88 } 89 } 90 91 return ret; 92 } 93 94 public static void resetSurrogateMode() { 95 // Maintain the local cache of user labels 96 ensureUserLabels(); 97 98 surrogateStep = 0; 99 isInSurrogateMode = false; 100 } 101 102 public static List<String> getAccessibleLabelsNames(ItemsList itemsList) { 103 // Maintain the local cache of user labels 104 ensureUserLabels(); 105 106 if (isInSurrogateMode) { 107 if (isUnprivileged) { 108 return new ArrayList<String>(); 109 } else { 110 return Arrays.asList(userLabels.get(surrogateStep)); 111 } 32 112 } else { 33 List<String> data = labelText.getData(); 34 if (data == null || data.isEmpty()) { 35 return unableToFindLabelResult(label); 36 } 113 return getLabelsFromContext(itemsList); 114 } 115 } 116 117 private static void ensureUserLabels() { 118 List<String> userSettingsLabelNames = KeyList.getLabelsNameOnly(); 119 boolean hasNoKeys = userLabels.isEmpty() && userSettingsLabelNames.isEmpty(); 120 boolean hasNotChanged = userLabels.equals(userSettingsLabelNames) || hasNoKeys; 121 122 if (hasNotChanged) { return; } 123 124 // Deal with the fact that the position of our current surrogate mode 125 // might have moved in the users list of labels. 126 if (userLabels.size() > surrogateStep) { 127 String surrogateMode = userLabels.get(surrogateStep); 128 int newIndexOfSurrogateMode = userSettingsLabelNames.indexOf(surrogateMode); 129 if (newIndexOfSurrogateMode >= 0) { 130 surrogateStep = newIndexOfSurrogateMode; 131 } else { 132 surrogateStep = 0; 133 isInSurrogateMode = false; 134 MessageBay.displayMessage("You have lost access to the encryption label " 135 + surrogateMode + ". Dropping out of surrogate mode."); 136 } 137 } 138 139 // (Re)Initialise the userLabels list and labelResultsCache. 140 userLabels.clear(); 141 labelsInfo.clear(); 142 Text[] userSettings = KeyList.UserLabels.get(); 143 for (Text userSetting: userSettings) { 144 String labelName = userSetting.getName(); 145 List<String> data = userSetting.getData(); 146 if (data == null || data.isEmpty()) { 147 continue; 148 } 149 150 // Extract the data and check if we have a full or partial key. 37 151 String dataEntry = data.get(0); 38 39 152 if (dataEntry.contains("{")) { 40 return partialKeyResult(label, dataEntry); 41 } else { 42 return fullKeyResult(label, dataEntry); 43 } 44 } 45 } 46 47 public static void progressSurrogateMode() { 48 surrogateStep += 1; 49 } 50 51 public static void resetSurrogateMode() { 52 surrogateStep = -1; 53 } 54 55 public static List<String> getAccessibleLabelsNames(ItemsList itemsList) { 56 // TODO make copy of itemsList in here and then adjust callers so they do not need to make copy. 57 // Alternatively, filter instead of removeIf 58 Text[] userLabels = KeyList.UserLabels.get(); 59 Stream<String> accessibleUserWideLabels = Arrays.asList(userLabels).stream().map(label -> label.getText()); 60 61 Predicate<Item> isItemWithoutEncryptionLabel = item -> item.getEncryptionLabel() == null || item.getEncryptionLabel().isEmpty(); 62 itemsList.removeIf(isItemWithoutEncryptionLabel); 63 Stream<String> accessibleFrameLocalLabels = itemsList.underlying().stream().map(item -> item.getEncryptionLabel()).distinct(); 64 65 List<String> accessibleLabelsUnion = accessibleUserWideLabels.collect(Collectors.toList()); 66 accessibleLabelsUnion.retainAll(accessibleFrameLocalLabels.collect(Collectors.toList())); 67 68 if (surrogateStep > -1 && surrogateStep < accessibleLabelsUnion.size()) { 69 String surrogateModeFilter = accessibleLabelsUnion.get(surrogateStep); 70 List<String> single = new ArrayList<String>(); 71 single.add(surrogateModeFilter); 72 return single; 73 } else { 74 resetSurrogateMode(); 75 } 76 77 return accessibleLabelsUnion; 78 } 79 80 private static LabelResult partialKeyResult(String label, String dataEntry) { 81 dataEntry = dataEntry.substring(dataEntry.indexOf('}') + 1).trim(); 82 LabelResult res = LabelResult.SuccessResolveLabelToPartialKey; 83 res.name = label; 84 return res; 85 } 86 87 private static LabelResult unableToFindLabelResult(String label) { 88 LabelResult res = LabelResult.ErrorUnableToFindLabel; 89 res.name = label; 90 return res; 91 } 92 93 private static LabelResult fullKeyResult(String label, String dataEntry) { 94 try { 95 LabelResult res = LabelResult.SuccessResolveLabelToKey; 96 res.key = Base64.getDecoder().decode(dataEntry); 97 res.name = label; 98 return res; 99 } catch (IllegalArgumentException e) { 100 return LabelResult.ErrorUnableToFindLabel; 101 } 102 } 103 104 //private static Map<LabelResult, LabelAccess> labelCache = new LinkedHashMap<LabelResult, LabelAccess>(); 105 //private static int surrogateStep = -1; 106 //private static final Predicate<? super LabelResult> isAccessible = a -> labelCache.get(a).allowed; 107 108 // public static LabelResult resolveKey(String label) { 109 // Stream<LabelResult> stream = labelCache.keySet().stream(); 110 // stream = stream.filter(res -> res.name.equals(label)); 111 // if (stream.count() == 0) { 112 // return resolveKeyWorker(label); 113 // } else { 114 // LabelResult labelResult = stream.collect(Collectors.toList()).get(0); 115 // LabelAccess accessible = labelCache.get(labelResult); 116 // if (accessible.allowed) { return labelResult; } 117 // else { return LabelResult.ErrorForbiddenLabel; } 118 // } 119 // } 120 // private static LabelResult resolveKeyWorker(String label) { 121 // Frame secretsFrame = getSecretsFrame(); 122 // if (secretsFrame == null) { 123 // return LabelResult.ErrorUnableToFindSecretsFrame; 124 // } 125 // 126 // String data = getDataFromLabel(secretsFrame, label); 127 // if (data == null) { 128 // return LabelResult.ErrorUnableToFindLabel; 129 // } 130 // 131 // if (data.contains("{")) { 132 // data = data.substring(data.indexOf('}') + 1).trim(); 133 // LabelResult res = LabelResult.SuccessResolveLabelToPartialKey; 134 // res.key = Base64.getDecoder().decode(data); 135 // return res; 136 // } else { 137 // try { 138 // LabelResult res = LabelResult.SuccessResolveLabelToKey; 139 // res.key = Base64.getDecoder().decode(data); 140 // res.name = label; 141 // labelCache.put(res, new LabelAccess(true)); 142 // return res; 143 // } catch (IllegalArgumentException e) { 144 // return LabelResult.ErrorUnableToFindLabel; 145 // } 146 // } 147 // } 148 // 149 // private static String getDataFromLabel(Frame frame, String label) { 150 // Collection<Text> labels = frame.getTextItems(); 151 // labels.removeIf(lbl -> !lbl.getText().equals(label)); 152 // labels.removeIf(lbl -> lbl.getData() == null || lbl.getData().size() == 0); 153 // if (labels.isEmpty()) { return null; } 154 // Text labelItem = labels.iterator().next(); 155 // List<String> data = labelItem.getData(); 156 // if (data.isEmpty()) { return null; } 157 // return data.get(0); 158 // } 159 // 160 // private static Frame getSecretsFrame() { 161 // String credentialsFrameName = UserSettings.ProfileName.get() + 162 // AuthenticatorBrowser.CREDENTIALS_FRAME; 163 // Frame credentialsFrame = FrameIO.LoadFrame(credentialsFrameName); 164 // Collection<Text> textItems = credentialsFrame.getTextItems(); 165 // textItems.removeIf(t -> !t.getText().equals("Secrets")); 166 // textItems.removeIf(t -> !t.hasLink()); 167 // if (textItems.isEmpty()) { 168 // return null; 169 // } 170 // Text linkToSecretsFrame = textItems.iterator().next(); 171 // if (!linkToSecretsFrame.hasLink()) { 172 // return null; 173 // } 174 // Frame secretsFrame = FrameIO.LoadFrame(linkToSecretsFrame.getParent().getFramesetName() + linkToSecretsFrame.getLink()); 175 // return secretsFrame; 176 // } 177 // 178 // private static class LabelAccess { 179 // public final boolean allowedDefault; 180 // public boolean allowed; 181 // 182 // public LabelAccess(boolean value) { 183 // allowedDefault = value; 184 // allowed = allowedDefault; 185 // } 186 // } 187 188 public enum LabelResult { 189 SuccessResolveLabelToKey, 190 SuccessResolveLabelToPartialKey, 191 ErrorUnableToFindSecretsFrame, 192 ErrorUnableToFindLabel, 193 ErrorForbiddenLabel; 153 userLabels.add(labelName); 154 LabelInfo labelInfo = new LabelInfo(LabelResult.SuccessResolveLabelToPartialKey, labelName, null); 155 labelsInfo.add(labelInfo); 156 } else { 157 userLabels.add(labelName); 158 byte[] key = Base64.getDecoder().decode(dataEntry); 159 LabelInfo labelInfo = new LabelInfo(LabelResult.SuccessResolveLabelToKey, labelName, key); 160 labelsInfo.add(labelInfo); 161 } 162 } 163 } 164 165 private static List<String> getLabelsFromContext(ItemsList context) { 166 Predicate<Item> hasEncryptionLabel = item -> item.getEncryptionLabel() != null && !item.getEncryptionLabel().isEmpty(); 167 Collection<Item> local = new ItemsList(context).underlying(); 168 Stream<Item> itemsWithLabel = local.stream().filter(hasEncryptionLabel); 169 Stream<String> labels = itemsWithLabel.map(item -> item.getEncryptionLabel()).distinct(); 170 return labels.collect(Collectors.toList()); 171 } 172 173 private static LabelInfo unableToFindLabelResult(String label) { 174 LabelInfo info = new LabelInfo(LabelResult.ErrorUnableToFindLabel, label, null); 175 return info; 176 } 177 178 public static class LabelInfo { 194 179 195 180 public byte[] key; 196 181 public String name; 197 198 @Override 182 public LabelResult type; 183 184 public LabelInfo(LabelResult type, String name, byte[] key) { 185 this.type = type; 186 this.name = name; 187 this.key = key; 188 } 189 190 public boolean is(LabelResult type) { 191 return this.type == type; 192 } 193 199 194 public String toString() { 200 switch (this) {195 switch(type) { 201 196 case SuccessResolveLabelToKey: 202 197 return "Resolved label to key: " + Base64.getEncoder().encodeToString(key); … … 215 210 } 216 211 } 217 } 212 213 public enum LabelResult { 214 SuccessResolveLabelToKey, 215 SuccessResolveLabelToPartialKey, 216 ErrorUnableToFindSecretsFrame, 217 ErrorUnableToFindLabel, 218 ErrorForbiddenLabel; 219 } 220 } -
trunk/src/org/expeditee/gui/DisplayController.java
r1430 r1431 1234 1234 } 1235 1235 1236 Label.progressSurrogateMode();1237 1236 Frame currentFrame = DisplayController.getCurrentFrame(); 1238 1237 ItemsList primaryBody = currentFrame.getPrimaryBody(); 1239 1238 ItemsList primaryBodyCopy = new ItemsList(primaryBody); 1240 List<String> accessibleLabelsNames = Label.getAccessibleLabelsNames(primaryBodyCopy); 1241 StringBuilder sb = new StringBuilder("Surrogate Mode currently accepts labels: "); 1242 for (String acceessibleLabel: accessibleLabelsNames) { 1243 sb.append(acceessibleLabel + ", "); 1244 } 1245 String message = sb.substring(0, sb.length() - 2); 1246 MessageBay.displayMessage(message); 1239 List<String> accessibleLabelsNames = Label.progressSurrogateMode(primaryBodyCopy); 1240 if (accessibleLabelsNames.isEmpty()) { 1241 MessageBay.displayMessage("Surrogate Mode currently accepts no labels. (Unprivileged view)"); 1242 } else { 1243 StringBuilder sb = new StringBuilder("Surrogate Mode currently accepts labels: "); 1244 for (String acceessibleLabel: accessibleLabelsNames) { 1245 sb.append(acceessibleLabel + ", "); 1246 } 1247 String message = sb.substring(0, sb.length() - 2); 1248 MessageBay.displayMessage(message); 1249 } 1247 1250 1248 1251 getCurrentFrame().parse(); -
trunk/src/org/expeditee/items/Item.java
r1430 r1431 50 50 import org.expeditee.encryption.items.surrogates.EncryptionDetail; 51 51 import org.expeditee.encryption.items.surrogates.EncryptionDetail.Type; 52 import org.expeditee.encryption.items.surrogates.Label.LabelInfo; 53 import org.expeditee.encryption.items.surrogates.Label.LabelResult; 52 54 import org.expeditee.encryption.items.surrogates.Label; 53 import org.expeditee.encryption.items.surrogates.Label.LabelResult;54 55 import org.expeditee.gio.EcosystemManager; 55 56 import org.expeditee.gio.GraphicsManager; … … 4053 4054 return; 4054 4055 } 4055 Label ResultlabelResult = Label.getLabel(label);4056 if ( labelResult != LabelResult.SuccessResolveLabelToKey) {4056 LabelInfo labelResult = Label.getLabel(label); 4057 if (!labelResult.is(LabelResult.SuccessResolveLabelToKey)) { 4057 4058 MessageBay.displayMessage(labelResult.toString()); 4058 4059 this._encryptionLabel = null; … … 4232 4233 } 4233 4234 4234 return Label.getLabel(label) == LabelResult.SuccessResolveLabelToKey;4235 return Label.getLabel(label).is(LabelResult.SuccessResolveLabelToKey); 4235 4236 } 4236 4237 -
trunk/src/org/expeditee/items/Picture.java
r1428 r1431 39 39 import org.expeditee.encryption.core.EncryptedImage; 40 40 import org.expeditee.encryption.items.surrogates.Label; 41 import org.expeditee.encryption.items.surrogates.Label.LabelInfo; 41 42 import org.expeditee.encryption.items.surrogates.Label.LabelResult; 42 43 import org.expeditee.gio.EcosystemManager; … … 786 787 _image = Image.getImage(_path); 787 788 } else { 788 Label Resultresult = Label.getLabel(encryptionLabel);789 if (result == LabelResult.SuccessResolveLabelToKey) {789 LabelInfo result = Label.getLabel(encryptionLabel); 790 if (result.is(LabelResult.SuccessResolveLabelToKey)) { 790 791 _image = EncryptedImage.getImage(_path, result.key); 791 792 } else { -
trunk/src/org/expeditee/items/XRayable.java
r1427 r1431 25 25 import org.expeditee.core.Colour; 26 26 import org.expeditee.core.Point; 27 import org.expeditee.encryption.core.EncryptedImage;28 27 import org.expeditee.encryption.items.surrogates.Label; 29 28 import org.expeditee.gui.DisplayController; -
trunk/src/org/expeditee/settings/identity/secrets/KeyList.java
r1415 r1431 1 1 package org.expeditee.settings.identity.secrets; 2 2 3 import java.util.Arrays; 3 4 import java.util.List; 4 5 import java.util.function.Predicate; 6 import java.util.stream.Collectors; 7 import java.util.stream.Stream; 5 8 6 9 import org.expeditee.gui.Frame; … … 57 60 } 58 61 }; 62 63 public static List<String> getLabelsNameOnly() { 64 Text[] allLabels = UserLabels.get(); 65 Stream<String> allLabelsNames = Arrays.asList(allLabels).stream().map(label -> label.getText()); 66 return allLabelsNames.collect(Collectors.toList()); 67 } 68 69 public static Text getByName(String labelName) { 70 Text[] allLabels = UserLabels.get(); 71 for (Text label: allLabels) { 72 if (label.getText().equals(labelName)) { 73 return label; 74 } 75 } 76 77 return null; 78 } 59 79 }
Note:
See TracChangeset
for help on using the changeset viewer.