[1409] | 1 | package org.expeditee.encryption.items.surrogates;
|
---|
[1401] | 2 |
|
---|
[1413] | 3 | import java.util.ArrayList;
|
---|
[1415] | 4 | import java.util.Arrays;
|
---|
[1401] | 5 | import java.util.Base64;
|
---|
[1413] | 6 | import java.util.List;
|
---|
[1415] | 7 | import java.util.function.Predicate;
|
---|
[1413] | 8 | import java.util.stream.Collectors;
|
---|
[1415] | 9 | import java.util.stream.Stream;
|
---|
[1401] | 10 |
|
---|
[1415] | 11 | import org.expeditee.gui.ItemsList;
|
---|
[1413] | 12 | import org.expeditee.items.Item;
|
---|
[1401] | 13 | import org.expeditee.items.Text;
|
---|
[1415] | 14 | import org.expeditee.settings.identity.secrets.KeyList;
|
---|
[1401] | 15 |
|
---|
| 16 | public class Label {
|
---|
| 17 |
|
---|
[1415] | 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;
|
---|
| 26 | break;
|
---|
| 27 | }
|
---|
[1413] | 28 | }
|
---|
| 29 |
|
---|
[1415] | 30 | if (labelText == null) {
|
---|
| 31 | return unableToFindLabelResult(label);
|
---|
[1401] | 32 | } else {
|
---|
[1415] | 33 | List<String> data = labelText.getData();
|
---|
| 34 | if (data == null || data.isEmpty()) {
|
---|
| 35 | return unableToFindLabelResult(label);
|
---|
[1401] | 36 | }
|
---|
[1415] | 37 | String dataEntry = data.get(0);
|
---|
| 38 |
|
---|
| 39 | if (dataEntry.contains("{")) {
|
---|
| 40 | return partialKeyResult(label, dataEntry);
|
---|
| 41 | } else {
|
---|
| 42 | return fullKeyResult(label, dataEntry);
|
---|
| 43 | }
|
---|
[1401] | 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[1413] | 47 | public static void progressSurrogateMode() {
|
---|
[1415] | 48 | surrogateStep += 1;
|
---|
[1413] | 49 | }
|
---|
[1415] | 50 |
|
---|
[1413] | 51 | public static void resetSurrogateMode() {
|
---|
[1415] | 52 | surrogateStep = -1;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public static List<String> getAccessibleLabelsNames(ItemsList itemsList) {
|
---|
| 56 | Text[] userLabels = KeyList.UserLabels.get();
|
---|
| 57 | Stream<String> accessibleUserWideLabels = Arrays.asList(userLabels).stream().map(label -> label.getText());
|
---|
[1413] | 58 |
|
---|
[1415] | 59 | Predicate<Item> isLabelWithoutEncryptionLabel = item -> item.getEncryptionLabel() == null || item.getEncryptionLabel().isEmpty();
|
---|
| 60 | itemsList.removeIf(isLabelWithoutEncryptionLabel);
|
---|
| 61 | Stream<String> accessibleFrameLocalLabels = itemsList.underlying().stream().map(item -> item.getEncryptionLabel()).distinct();
|
---|
| 62 |
|
---|
| 63 | List<String> accessibleLabelsUnion = accessibleUserWideLabels.collect(Collectors.toList());
|
---|
| 64 | accessibleLabelsUnion.retainAll(accessibleFrameLocalLabels.collect(Collectors.toList()));
|
---|
| 65 |
|
---|
| 66 | if (surrogateStep > -1 && surrogateStep < accessibleLabelsUnion.size()) {
|
---|
| 67 | String surrogateModeFilter = accessibleLabelsUnion.get(surrogateStep);
|
---|
| 68 | List<String> single = new ArrayList<String>();
|
---|
| 69 | single.add(surrogateModeFilter);
|
---|
| 70 | return single;
|
---|
| 71 | } else {
|
---|
| 72 | resetSurrogateMode();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return accessibleLabelsUnion;
|
---|
[1413] | 76 | }
|
---|
[1415] | 77 |
|
---|
| 78 | private static LabelResult partialKeyResult(String label, String dataEntry) {
|
---|
| 79 | dataEntry = dataEntry.substring(dataEntry.indexOf('}') + 1).trim();
|
---|
| 80 | LabelResult res = LabelResult.SuccessResolveLabelToPartialKey;
|
---|
| 81 | res.name = label;
|
---|
| 82 | return res;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private static LabelResult unableToFindLabelResult(String label) {
|
---|
| 86 | LabelResult res = LabelResult.ErrorUnableToFindLabel;
|
---|
| 87 | res.name = label;
|
---|
| 88 | return res;
|
---|
| 89 | }
|
---|
[1413] | 90 |
|
---|
[1415] | 91 | private static LabelResult fullKeyResult(String label, String dataEntry) {
|
---|
| 92 | try {
|
---|
| 93 | LabelResult res = LabelResult.SuccessResolveLabelToKey;
|
---|
| 94 | res.key = Base64.getDecoder().decode(dataEntry);
|
---|
| 95 | res.name = label;
|
---|
| 96 | return res;
|
---|
| 97 | } catch (IllegalArgumentException e) {
|
---|
| 98 | return LabelResult.ErrorUnableToFindLabel;
|
---|
[1413] | 99 | }
|
---|
| 100 | }
|
---|
[1415] | 101 |
|
---|
| 102 | //private static Map<LabelResult, LabelAccess> labelCache = new LinkedHashMap<LabelResult, LabelAccess>();
|
---|
| 103 | //private static int surrogateStep = -1;
|
---|
| 104 | //private static final Predicate<? super LabelResult> isAccessible = a -> labelCache.get(a).allowed;
|
---|
[1413] | 105 |
|
---|
[1415] | 106 | // public static LabelResult resolveKey(String label) {
|
---|
| 107 | // Stream<LabelResult> stream = labelCache.keySet().stream();
|
---|
| 108 | // stream = stream.filter(res -> res.name.equals(label));
|
---|
| 109 | // if (stream.count() == 0) {
|
---|
| 110 | // return resolveKeyWorker(label);
|
---|
| 111 | // } else {
|
---|
| 112 | // LabelResult labelResult = stream.collect(Collectors.toList()).get(0);
|
---|
| 113 | // LabelAccess accessible = labelCache.get(labelResult);
|
---|
| 114 | // if (accessible.allowed) { return labelResult; }
|
---|
| 115 | // else { return LabelResult.ErrorForbiddenLabel; }
|
---|
[1413] | 116 | // }
|
---|
[1415] | 117 | // }
|
---|
| 118 | // private static LabelResult resolveKeyWorker(String label) {
|
---|
| 119 | // Frame secretsFrame = getSecretsFrame();
|
---|
| 120 | // if (secretsFrame == null) {
|
---|
| 121 | // return LabelResult.ErrorUnableToFindSecretsFrame;
|
---|
| 122 | // }
|
---|
| 123 | //
|
---|
| 124 | // String data = getDataFromLabel(secretsFrame, label);
|
---|
| 125 | // if (data == null) {
|
---|
| 126 | // return LabelResult.ErrorUnableToFindLabel;
|
---|
| 127 | // }
|
---|
| 128 | //
|
---|
| 129 | // if (data.contains("{")) {
|
---|
| 130 | // data = data.substring(data.indexOf('}') + 1).trim();
|
---|
| 131 | // LabelResult res = LabelResult.SuccessResolveLabelToPartialKey;
|
---|
| 132 | // res.key = Base64.getDecoder().decode(data);
|
---|
| 133 | // return res;
|
---|
| 134 | // } else {
|
---|
| 135 | // try {
|
---|
| 136 | // LabelResult res = LabelResult.SuccessResolveLabelToKey;
|
---|
| 137 | // res.key = Base64.getDecoder().decode(data);
|
---|
| 138 | // res.name = label;
|
---|
| 139 | // labelCache.put(res, new LabelAccess(true));
|
---|
| 140 | // return res;
|
---|
| 141 | // } catch (IllegalArgumentException e) {
|
---|
| 142 | // return LabelResult.ErrorUnableToFindLabel;
|
---|
| 143 | // }
|
---|
| 144 | // }
|
---|
[1413] | 145 | // }
|
---|
[1415] | 146 | //
|
---|
| 147 | // private static String getDataFromLabel(Frame frame, String label) {
|
---|
| 148 | // Collection<Text> labels = frame.getTextItems();
|
---|
| 149 | // labels.removeIf(lbl -> !lbl.getText().equals(label));
|
---|
| 150 | // labels.removeIf(lbl -> lbl.getData() == null || lbl.getData().size() == 0);
|
---|
| 151 | // if (labels.isEmpty()) { return null; }
|
---|
| 152 | // Text labelItem = labels.iterator().next();
|
---|
| 153 | // List<String> data = labelItem.getData();
|
---|
| 154 | // if (data.isEmpty()) { return null; }
|
---|
| 155 | // return data.get(0);
|
---|
| 156 | // }
|
---|
| 157 | //
|
---|
| 158 | // private static Frame getSecretsFrame() {
|
---|
| 159 | // String credentialsFrameName = UserSettings.ProfileName.get() +
|
---|
| 160 | // AuthenticatorBrowser.CREDENTIALS_FRAME;
|
---|
| 161 | // Frame credentialsFrame = FrameIO.LoadFrame(credentialsFrameName);
|
---|
| 162 | // Collection<Text> textItems = credentialsFrame.getTextItems();
|
---|
| 163 | // textItems.removeIf(t -> !t.getText().equals("Secrets"));
|
---|
| 164 | // textItems.removeIf(t -> !t.hasLink());
|
---|
| 165 | // if (textItems.isEmpty()) {
|
---|
| 166 | // return null;
|
---|
| 167 | // }
|
---|
| 168 | // Text linkToSecretsFrame = textItems.iterator().next();
|
---|
| 169 | // if (!linkToSecretsFrame.hasLink()) {
|
---|
| 170 | // return null;
|
---|
| 171 | // }
|
---|
| 172 | // Frame secretsFrame = FrameIO.LoadFrame(linkToSecretsFrame.getParent().getFramesetName() + linkToSecretsFrame.getLink());
|
---|
| 173 | // return secretsFrame;
|
---|
| 174 | // }
|
---|
| 175 | //
|
---|
| 176 | // private static class LabelAccess {
|
---|
| 177 | // public final boolean allowedDefault;
|
---|
| 178 | // public boolean allowed;
|
---|
| 179 | //
|
---|
| 180 | // public LabelAccess(boolean value) {
|
---|
| 181 | // allowedDefault = value;
|
---|
| 182 | // allowed = allowedDefault;
|
---|
| 183 | // }
|
---|
| 184 | // }
|
---|
[1413] | 185 |
|
---|
[1401] | 186 | public enum LabelResult {
|
---|
| 187 | SuccessResolveLabelToKey,
|
---|
| 188 | SuccessResolveLabelToPartialKey,
|
---|
| 189 | ErrorUnableToFindSecretsFrame,
|
---|
[1415] | 190 | ErrorUnableToFindLabel,
|
---|
| 191 | ErrorForbiddenLabel;
|
---|
[1401] | 192 |
|
---|
| 193 | public byte[] key;
|
---|
[1413] | 194 | public String name;
|
---|
[1401] | 195 |
|
---|
| 196 | @Override
|
---|
| 197 | public String toString() {
|
---|
| 198 | switch (this) {
|
---|
| 199 | case SuccessResolveLabelToKey:
|
---|
| 200 | return "Resolved label to key: " + Base64.getEncoder().encodeToString(key);
|
---|
| 201 | case SuccessResolveLabelToPartialKey:
|
---|
| 202 | return "Resolved label to slice of a key: " + Base64.getEncoder().encodeToString(key);
|
---|
| 203 | case ErrorUnableToFindSecretsFrame:
|
---|
| 204 | return "Unable to find your Secrets Frame.";
|
---|
| 205 | case ErrorUnableToFindLabel:
|
---|
[1415] | 206 | return "Unable to resolve label to encrypt/decrypt frame. Label: " + name;
|
---|
| 207 | case ErrorForbiddenLabel:
|
---|
| 208 | return "Whilst you have the key for label " + name + " ; you are not allowed to use it.";
|
---|
[1401] | 209 | }
|
---|
| 210 |
|
---|
| 211 | String message = "Was the list of possible enum results updated without nessasary changes to the toString() function?";
|
---|
| 212 | throw new IllegalArgumentException(message);
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|