1 | package org.expeditee.encryption.items.surrogates;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Base64;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.function.Predicate;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 | import java.util.stream.Stream;
|
---|
10 |
|
---|
11 | import org.expeditee.gui.ItemsList;
|
---|
12 | import org.expeditee.items.Item;
|
---|
13 | import org.expeditee.items.Text;
|
---|
14 | import org.expeditee.settings.identity.secrets.KeyList;
|
---|
15 |
|
---|
16 | 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;
|
---|
26 | break;
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | if (labelText == null) {
|
---|
31 | return unableToFindLabelResult(label);
|
---|
32 | } else {
|
---|
33 | List<String> data = labelText.getData();
|
---|
34 | if (data == null || data.isEmpty()) {
|
---|
35 | return unableToFindLabelResult(label);
|
---|
36 | }
|
---|
37 | String dataEntry = data.get(0);
|
---|
38 |
|
---|
39 | 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;
|
---|
194 |
|
---|
195 | public byte[] key;
|
---|
196 | public String name;
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public String toString() {
|
---|
200 | switch (this) {
|
---|
201 | case SuccessResolveLabelToKey:
|
---|
202 | return "Resolved label to key: " + Base64.getEncoder().encodeToString(key);
|
---|
203 | case SuccessResolveLabelToPartialKey:
|
---|
204 | return "Resolved label to slice of a key: " + Base64.getEncoder().encodeToString(key);
|
---|
205 | case ErrorUnableToFindSecretsFrame:
|
---|
206 | return "Unable to find your Secrets Frame.";
|
---|
207 | case ErrorUnableToFindLabel:
|
---|
208 | return "Unable to resolve label to encrypt/decrypt frame. Label: " + name;
|
---|
209 | case ErrorForbiddenLabel:
|
---|
210 | return "Whilst you have the key for label " + name + " ; you are not allowed to use it.";
|
---|
211 | }
|
---|
212 |
|
---|
213 | String message = "Was the list of possible enum results updated without nessasary changes to the toString() function?";
|
---|
214 | throw new IllegalArgumentException(message);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|