1 | package org.expeditee.encryption.items.surrogates;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Base64;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Set;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 |
|
---|
10 | import org.expeditee.auth.AuthenticatorBrowser;
|
---|
11 | import org.expeditee.gui.DisplayController;
|
---|
12 | import org.expeditee.gui.Frame;
|
---|
13 | import org.expeditee.gui.FrameIO;
|
---|
14 | import org.expeditee.gui.MessageBay;
|
---|
15 | import org.expeditee.items.Item;
|
---|
16 | import org.expeditee.items.Text;
|
---|
17 | import org.expeditee.settings.UserSettings;
|
---|
18 |
|
---|
19 | public class Label {
|
---|
20 |
|
---|
21 | private static List<LabelResult> labelCache = new ArrayList<LabelResult>();
|
---|
22 | private static List<String> forbiddenLabels = new ArrayList<String>();
|
---|
23 |
|
---|
24 | public static LabelResult resolveKey(String label) {
|
---|
25 |
|
---|
26 | if (forbiddenLabels.contains(label)) {
|
---|
27 | return LabelResult.ErrorUnableToFindLabel;
|
---|
28 | }
|
---|
29 |
|
---|
30 | LabelResult fromCache = getFromCache(label);
|
---|
31 | if (fromCache != null) {
|
---|
32 | return fromCache;
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | String credentialsFrameName = UserSettings.ProfileName.get() +
|
---|
37 | AuthenticatorBrowser.CREDENTIALS_FRAME;
|
---|
38 | Frame credentialsFrame = FrameIO.LoadFrame(credentialsFrameName);
|
---|
39 | Collection<Text> textItems = credentialsFrame.getTextItems();
|
---|
40 | textItems.removeIf(t -> !t.getText().equals("Secrets"));
|
---|
41 | textItems.removeIf(t -> !t.hasLink());
|
---|
42 | if (textItems.isEmpty()) {
|
---|
43 | return LabelResult.ErrorUnableToFindSecretsFrame;
|
---|
44 | }
|
---|
45 | Text linkToSecretsFrame = textItems.iterator().next();
|
---|
46 | if (!linkToSecretsFrame.hasLink()) {
|
---|
47 | return LabelResult.ErrorUnableToFindSecretsFrame;
|
---|
48 | }
|
---|
49 | Frame secretsFrame = FrameIO.LoadFrame(linkToSecretsFrame.getParent().getFramesetName() + linkToSecretsFrame.getLink());
|
---|
50 | Collection<Text> labels = secretsFrame.getTextItems();
|
---|
51 | labels.removeIf(lbl -> !lbl.getText().equals(label));
|
---|
52 | labels.removeIf(lbl -> lbl.getData() == null || lbl.getData().size() == 0);
|
---|
53 | if (labels.isEmpty()) {
|
---|
54 | return LabelResult.ErrorUnableToFindLabel;
|
---|
55 | }
|
---|
56 | Text labelItem = labels.iterator().next();
|
---|
57 | String data = labelItem.getData().get(0);
|
---|
58 | if (data.contains("{")) {
|
---|
59 | data = data.substring(data.indexOf('}') + 1).trim();
|
---|
60 | LabelResult res = LabelResult.SuccessResolveLabelToPartialKey;
|
---|
61 | res.key = Base64.getDecoder().decode(data);
|
---|
62 | return res;
|
---|
63 | } else {
|
---|
64 | try {
|
---|
65 | LabelResult res = LabelResult.SuccessResolveLabelToKey;
|
---|
66 | res.key = Base64.getDecoder().decode(data);
|
---|
67 | res.name = label;
|
---|
68 | labelCache.add(res);
|
---|
69 | return res;
|
---|
70 | } catch (IllegalArgumentException e) {
|
---|
71 | return LabelResult.ErrorUnableToFindLabel;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static void progressSurrogateMode() {
|
---|
77 | Collection<Item> allItems = DisplayController.getCurrentFrame().getAllItems();
|
---|
78 | allItems.removeIf(item -> item.getEncryptionLabel() == null);
|
---|
79 | MessageBay.displayMessage("Encryption labels found on items on current frame: ");
|
---|
80 | Set<String> allLabels = allItems.stream().map(i -> i.getEncryptionLabel()).collect(Collectors.toSet());
|
---|
81 | StringBuilder sb = new StringBuilder();
|
---|
82 | for (String label: allLabels) {
|
---|
83 | sb.append(label + ",");
|
---|
84 | }
|
---|
85 | MessageBay.displayMessage(sb.deleteCharAt(sb.length() - 1).toString());
|
---|
86 | }
|
---|
87 |
|
---|
88 | public static void resetSurrogateMode() {
|
---|
89 | // TODO Auto-generated method stub
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | private static LabelResult getFromCache(String label) {
|
---|
94 | List<LabelResult> cacheCanditates = labelCache.stream().filter(labelResult -> labelResult.name.equals(label)).collect(Collectors.toList());
|
---|
95 | if (!cacheCanditates.isEmpty()) {
|
---|
96 | return cacheCanditates.get(0);
|
---|
97 | } else {
|
---|
98 | return null;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | // private static List<List<String>> permutation(List<String> strings, List<List<String>> acc) {
|
---|
103 | // for (int i = 0; i < strings.size(); i++) {
|
---|
104 | //
|
---|
105 | // }
|
---|
106 | // }
|
---|
107 |
|
---|
108 | public enum LabelResult {
|
---|
109 | SuccessResolveLabelToKey,
|
---|
110 | SuccessResolveLabelToPartialKey,
|
---|
111 | ErrorUnableToFindSecretsFrame,
|
---|
112 | ErrorUnableToFindLabel;
|
---|
113 |
|
---|
114 | public byte[] key;
|
---|
115 | public String name;
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public String toString() {
|
---|
119 | switch (this) {
|
---|
120 | case SuccessResolveLabelToKey:
|
---|
121 | return "Resolved label to key: " + Base64.getEncoder().encodeToString(key);
|
---|
122 | case SuccessResolveLabelToPartialKey:
|
---|
123 | return "Resolved label to slice of a key: " + Base64.getEncoder().encodeToString(key);
|
---|
124 | case ErrorUnableToFindSecretsFrame:
|
---|
125 | return "Unable to find your Secrets Frame.";
|
---|
126 | case ErrorUnableToFindLabel:
|
---|
127 | return "Unable to resolve label to encrypt/decrypt frame. Label: ";
|
---|
128 | }
|
---|
129 |
|
---|
130 | String message = "Was the list of possible enum results updated without nessasary changes to the toString() function?";
|
---|
131 | throw new IllegalArgumentException(message);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|