- Timestamp:
- 08/15/19 12:57:28 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.