Ignore:
Timestamp:
07/24/19 11:23:45 (5 years ago)
Author:
bln4
Message:

Renamed Frame.getItems() to Frame.getSortedItems() to better represent its functionality.

-> org.apollo.ApolloGestureActions
-> org.apollo.ApolloSystem
-> org.expeditee.actions.Actions
-> org.expeditee.actions.Debug
-> org.expeditee.actions.ExploratorySearchActions
-> org.expeditee.actions.JfxBrowserActions
-> org.expeditee.actions.Misc
-> org.expeditee.actions.Navigation
-> org.expeditee.actions.ScriptBase
-> org.expeditee.actions.Simple
-> org.expeditee.agents.ComputeTree
-> org.expeditee.agents.CopyTree
-> org.expeditee.agents.DisplayComet
-> org.expeditee.agents.DisplayTree
-> org.expeditee.agents.DisplayTreeLeaves
-> org.expeditee.agents.GraphFramesetLinks
-> org.expeditee.agents.TreeProcessor
-> org.expeditee.gio.gesture.StandardGestureActions
-> org.expeditee.gui.DisplayController
-> org.expeditee.gui.FrameCreator
-> org.expeditee.gui.FrameIO
-> org.expeditee.io.DefaultTreeWriter
-> org.expeditee.io.JavaWriter
-> org.expeditee.io.PDF2Writer
-> org.expeditee.io.TXTWriter
-> org.expeditee.io.WebParser
-> org.expeditee.io.flowlayout.XGroupItem
-> org.expeditee.items.Dot
-> org.expeditee.items.Item
-> org.expeditee.items.ItemUtils
-> org.expeditee.network.FrameShare
-> org.expeditee.stats.TreeStats


Created ItemsList class to wrap ArrayList<Item>. Frames now use this new class to store its body list (used for display) as well as its primaryBody and surrogateBody.

-> org.expeditee.agents.Format
-> org.expeditee.agents.HFormat
-> org.expeditee.gio.gesture.StandardGestureActions
-> org.expeditee.gui.Frame
-> org.expeditee.gui.FrameUtils


Refactorted Frame.setResort(bool) to Frame.invalidateSorted() to better function how it is intended to with a more accurate name.

-> org.expeditee.agents.Sort


When writing out .exp files and getting attributes to respond to LEFT + RIGHT click, boolean items are by default true. This has always been the case. An ammendment to this is that defaults can now be established.
Also added 'EnterClick' functionality. If cursored over a item with this property and you press enter, it acts as if you have clicked on it instead.

-> org.expeditee.assets.resources-public.framesets.authentication.1.exp to 6.exp
-> org.expeditee.gio.gesture.StandardGestureActions
-> org.expeditee.gio.input.KBMInputEvent
-> org.expeditee.gio.javafx.JavaFXConversions
-> org.expeditee.gio.swing.SwingConversions
-> org.expeditee.gui.AttributeUtils
-> org.expeditee.io.Conversion
-> org.expeditee.io.DefaultFrameWriter
-> org.expeditee.items.Item


Fixed a bug caused by calling Math.abs on Integer.MIN_VALUE returning unexpected result. Due to zero being a thing, you cannot represent Math.abs(Integer.MIN_VALUE) in a Integer object. The solution is to use Integer.MIN_VALUE + 1 instead of Integer.MIN_VALUE.

-> org.expeditee.core.bounds.CombinationBounds
-> org.expeditee.io.flowlayout.DimensionExtent


Recoded the contains function in EllipticalBounds so that intersection tests containing circles work correctly.

-> org.expeditee.core.bounds.EllipticalBounds


Added toString() to PolygonBounds to allow for useful printing during debugging.

-> org.expeditee.core.bounds.PolygonBounds

Implemented Surrogate Mode!

-> org.expeditee.encryption.io.EncryptedExpReader
-> org.expeditee.encryption.io.EncryptedExpWriter
-> org.expeditee.encryption.items.surrogates.EncryptionDetail
-> org.expeditee.encryption.items.surrogates.Label
-> org.expeditee.gui.FrameUtils
-> org.expeditee.gui.ItemsList
-> org.expeditee.items.Item
-> org.expeditee.items.Text


???? Use Integer.MAX_VALUE cast to a float instead of Float.MAX_VALUE. This fixed some bug which I cannot remember.

-> org.expeditee.gio.TextLayoutManager
-> org.expeditee.gio.swing.SwingTextLayoutManager


Improved solution for dealing with the F10 key taking focus away from Expeditee due to it being a assessibility key.

-> org.expeditee.gio.swing.SwingInputManager


Renamed variable visibleItems in FrameGraphics.paintFrame to itemsToPaintCanditates to better represent functional intent.

-> org.expeditee.gui.FrameGraphics


Improved checking for if personal resources exist before recreating them

-> org.expeditee.gui.FrameIO


Repeated messages to message bay now have a visual feedback instead of just a beep. This visual feedback is in the form of a count of the amount of times it has repeated.

-> org.expeditee.gui.MessageBay


Updated comment on the Vector class to explain what vectors are.

-> org.expeditee.gui.Vector


Added constants to represent all of the property keys in DefaultFrameReader and DefaultFrameWriter.

-> org.expeditee.io.DefaultFrameReader
-> org.expeditee.io.DefaultFrameWriter


Updated the KeyList setting to be more heirarcial with how users will store their Secrets.

-> org.expeditee.settings.identity.secrets.KeyList

Location:
trunk/src/org/expeditee/encryption
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/encryption/io/EncryptedExpReader.java

    r1414 r1415  
    2424import javax.crypto.spec.SecretKeySpec;
    2525
     26import org.expeditee.core.Point;
    2627import org.expeditee.encryption.CryptographyConstants;
    2728import org.expeditee.encryption.items.surrogates.EncryptionDetail;
     
    3334import org.expeditee.io.DefaultFrameWriter;
    3435import org.expeditee.io.ExpReader;
     36import org.expeditee.items.Constraint;
    3537import org.expeditee.items.Item;
    3638import org.expeditee.items.Text;
     
    4446        private SecretKey personalKey;
    4547        private boolean accessDenied = false;
     48        private boolean _readingSurrogates;
     49        private static final Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
    4650
    4751        public static boolean isEncryptedExpediteeFile(final String path) throws IOException {
    48                 final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
    49                 final String firstLine = in.readLine();
     52                BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
     53                String firstLine = in.readLine();
    5054                in.close();
     55                if (firstLine == null) return false;
    5156                return firstLine.startsWith(ENCRYPTED_EXP_FLAG);
    5257        }
     
    8792                if (accessDenied) { return null; }
    8893               
     94                _readingSurrogates = false;
    8995                _reader = reader;
    9096                String next = "";
     
    106112                        next = readTheConstraints();
    107113                       
     114                        if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) {
     115                                // Read the surrogates
     116                                _readingSurrogates = true;
     117                                next = readTheItems(newFrame, delayedActions);
     118                                _readingSurrogates = false;
     119                        }
     120                       
    108121                        for(DelayedAction action: delayedActions) {
    109122                                action.exec();
     
    124137        }
    125138       
    126 //      @Override
    127 //      protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException {
    128 //              String next = null;
    129 //              Item currentItem = null;
    130 //             
    131 //              Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
    132 //              while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
    133 //                      if (!isValidLine(next)) {
    134 //                              continue;
    135 //                      }
    136 //                     
    137 //                      String tag = getTagEnc(next);
    138 //                      if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {
    139 //                              currentItem = newItem(next);
    140 //                              _linePoints.put(currentItem.getID(), currentItem);
    141 //                              newFrame.addItem(currentItem);
    142 //                              currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
    143 //                      } else if (next.startsWith("SurrogateFor")) {
    144 //                              int parentID = Integer.parseInt(next.split(" ")[1]);
    145 //                              newFrame.getItemWithID(parentID).addToSurrogates(currentItem);
    146 //                              newFrame.addToSurrogates(currentItem);
    147 //                      } else if (currentItem != null && actionShouldBeDelayed(tag.charAt(0))) {
    148 //                              delayedActions.add(new DelayedAction(currentItem, next));
    149 //                      } else if (currentItem != null) {
    150 //                              processBodyLine(currentItem, next);
    151 //                      } else {
    152 //                              System.err.println("Error while reading in frame (ExpReader): Found body line but no current item to apply it to.");
    153 //                      }
    154 //              }
    155 //                             
    156 //              if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) {
    157 //                      next = readTheItems(newFrame, delayedActions);
    158 //              }
    159 //             
    160 //              return next;
    161 //      }
    162        
    163139        protected String readTheItems(Frame newFrame, List<DelayedAction> delayedActions) throws IOException {
    164                 // Read the primary item.
    165                 BiConsumer<Item, String> addToBody = (item, line) -> newFrame.addItem(item);
    166                 String next = readLineAfterLine(false, addToBody, delayedActions);
    167                
    168                 // Read the surrogate items.
    169                 if (next.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION)) {
    170                         BiConsumer<Item, String> addToSurrogates = (item, line) -> {
    171                                 int parentID = Integer.parseInt(line.split(" ")[1]);
    172                                 newFrame.getItemWithID(parentID).addToSurrogates(item);
    173                                 newFrame.addToSurrogates(item);
    174                         };
    175                         next = readLineAfterLine(true, addToSurrogates, delayedActions);
     140                BiConsumer<Item, String> primaryAdd = (item, line) -> newFrame.addItem(item);
     141                BiConsumer<Item, String> surrogateAdd = (item, line) -> {
     142                        int parentID = Integer.parseInt(line.split(" ")[1]);
     143                        Item parent = newFrame.getItemWithID(parentID);
     144                        newFrame.addToSurrogatesOnLoad(item, parent);
     145                };
     146               
     147                if (_readingSurrogates) {
     148                        return readLineAfterLine(true, surrogateAdd, delayedActions);
     149                } else {
     150                        return readLineAfterLine(false, primaryAdd, delayedActions);
     151                }
     152        }
     153       
     154        @Override
     155        protected String readTheConstraints() throws IOException, Exception {
     156                String next = null;
     157                while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
     158                        if (isValidLine(next)) {
     159                                Point idtype = separateValues(next.substring(2));
     160                                // The next line must be the endpoints
     161                                if (!_reader.ready()) {
     162                                        throw new Exception("Unexpected end of file");
     163                                }
     164                                next = _reader.readLine();
     165                                Point startend = separateValues(next.substring(2));
     166
     167                                Item a = _linePoints.get(startend.getX());
     168                                Item b = _linePoints.get(startend.getY());
     169
     170                                new Constraint(a, b, idtype.getX(), idtype.getY());
     171                        }
    176172                }
    177173                return next;
     
    182178                Item currentItem = null;
    183179               
    184                 Predicate<String> endOfSection = s -> s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
    185180                while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
    186181                        if (!isValidLine(next)) {
     
    195190                                        storeResult.accept(currentItem, next);
    196191                                }
    197                                 currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
     192                                EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
     193                                currentItem.setEncryptionDetailForTag(tag + "", unencryptedOnSave);
    198194                        } else if (next.startsWith("SurrogateFor")) {
    199195                                if (isSurrogate) {
     
    212208        }
    213209       
    214 //      private String readLineAfterLine(Consumer<Item> storeResult, List<DelayedAction> delayedActions) throws IOException {
    215 //              String next = null;
    216 //              Item currentItem = null;
    217 //             
    218 //              Predicate<String> endOfSection = s -> {
    219 //                      return s.equals(EncryptedExpWriter.TERMINATOR + "") || s.equals(EncryptedExpWriter.TERMINATOR_WITH_CONTINUATION);
    220 //              };
    221 //              while (_reader.ready() && !endOfSection.test(next = _reader.readLine())) {
    222 //                      if (!isValidLine(next)) {
    223 //                              continue;
    224 //                      }
    225 //                     
    226 //                      String tag = getTagEnc(next);
    227 //                      if (next.startsWith(DefaultFrameWriter.TYPE_AND_ID_STR + " ")) {
    228 //                              currentItem = newItem(next);
    229 //                              _linePoints.put(currentItem.getID(), currentItem);
    230 //                              currentItem.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
    231 //                              storeResult.accept(currentItem);
    232 //                      } else if (next.startsWith("SurrogateFor")) {
    233 //                              int parentID = Integer.parseInt(next.split(" ")[1]);
    234 //                              newFrame.getItemWithID(parentID).addToSurrogates(currentItem);
    235 //                              new Delayed
    236 //                      }
    237 //              }
    238 //             
    239 //              return next;
    240 //      }
    241        
    242210        @Override
    243211        protected void processBodyLine(Item item, String line) {
     
    245213                String tag = getTagEnc(line);
    246214                String value = getValue(line);
    247                
    248                 if (item.isSurrogate() && item.isTagInherited(tag)) {
     215                boolean isEncryptedLine = isEncryptedLine(line);
     216               
     217                if (item.isSurrogate() && isEncryptedLine) {
     218                        // Surrogates should never have encrypted body lines.
    249219                        return;
    250220                }
    251221               
    252222                // Attempt to decrypt the line if necessary.
    253                 if (isEncryptedLine(line)) {
    254                         LabelResult res = Label.resolveKey(item.getEncryptionLabel());
     223                if (isEncryptedLine) {
     224                        LabelResult res = Label.getLabel(item.getEncryptionLabel());
    255225                        if (res == LabelResult.SuccessResolveLabelToKey) {
    256                                 item.setEncryptionDetailForTag(tag, EncryptionDetail.ReencryptOnSave);
     226                                EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave);
     227                                item.setEncryptionDetailForTag(tag, reencryptOnSave);
    257228                                SecretKey key = new SecretKeySpec(res.key, SymmetricAlgorithm);
    258229                                byte[] decryptedBytes = DecryptSymmetric(Base64.getDecoder().decode(value), key);
    259230                                value = new String(decryptedBytes);                             
    260231                        } else {
    261                                 EncryptionDetail encryptionDetail = EncryptionDetail.UseUndecipheredValueOnSave.setUndecipheredValue(getValue(line));
    262                                 item.setEncryptionDetailForTag(tag, encryptionDetail);
     232                                EncryptionDetail undecipheredValueOnSave = new EncryptionDetail(EncryptionDetail.Type.UseUndecipheredValueOnSave);
     233                                undecipheredValueOnSave.setUndecipheredValue(getValue(line));
     234                                item.setEncryptionDetailForTag(tag, undecipheredValueOnSave);
    263235                                return;
    264236                        }
    265237                } else {
    266                         item.setEncryptionDetailForTag(tag, EncryptionDetail.UnencryptedOnSave);
     238                        EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
     239                        item.setEncryptionDetailForTag(tag, unencryptedOnSave);
     240                        if (item.isSurrogate()) {
     241                                item.setTagNotInherited(tag);
     242                        }
    267243                }
    268244               
     
    394370               
    395371                private SecretKeySpec resolveLabel(String label) {
    396                         LabelResult res = Label.resolveKey(label);
     372                        LabelResult res = Label.getLabel(label);
    397373                        if (res == LabelResult.SuccessResolveLabelToKey) {
    398374                                byte[] keyBytes = res.key;
  • trunk/src/org/expeditee/encryption/io/EncryptedExpWriter.java

    r1414 r1415  
    2323import org.expeditee.encryption.CryptographyConstants;
    2424import org.expeditee.encryption.items.surrogates.EncryptionDetail;
     25import org.expeditee.encryption.items.surrogates.EncryptionDetail.Type;
    2526import org.expeditee.encryption.items.surrogates.Label;
    2627import org.expeditee.encryption.items.surrogates.Label.LabelResult;
    2728import org.expeditee.gui.Frame;
     29import org.expeditee.gui.Frame.BodyType;
    2830import org.expeditee.gui.MessageBay;
    2931import org.expeditee.io.Conversion;
     
    6264                        }
    6365                } else {
    64                         LabelResult res = Label.resolveKey(label);
     66                        LabelResult res = Label.getLabel(label);
    6567                        if (res == LabelResult.SuccessResolveLabelToKey) {
    6668                                byte[] keyBytes = res.key;
     
    9496               
    9597                // write item
    96                 writeItemData(frame);
     98                writeItemData(frame, true);
    9799                writeTerminator();
    98                
    99100                // write lines and constraints
    100101                writeLineData();
    101102                writeTerminator();
    102103                writeConstraintData();
    103                 writeTerminator();
    104                
     104               
     105                // write surrogate items
     106                if (frame.hasSurrogates()) {
     107                        writeSurrogateTerminator();
     108                        writeItemData(frame, false);
     109                        writeTerminator();
     110                } else {
     111                        writeTerminator();
     112                }
     113                               
    105114                writeLine(SessionStats.getFrameEventList(frame));
    106115        }
     
    126135       
    127136        protected void writeClass(Item toWrite) throws IOException {
     137                if (toWrite instanceof Text && toWrite.getText().equals("Content for Label Two")) {
     138                        System.err.println("EncryptedExpWriter::writeClass::breakpoint");
     139                } else if (toWrite instanceof Text && toWrite.getText().equals("Encrypted") && toWrite.getPrimary().getText().equals("Content for Label One")) {
     140                        System.err.println("EncryptedExpWriter::writeClass::breakpoint");
     141                }
     142               
    128143                LinkedHashMap<Character,Method> itemTags = new LinkedHashMap<Character, Method>(getItemTags());
    129144                LinkedHashMap<String,Method> itemTagsExt = new LinkedHashMap<String, Method>(getItemTagsExt());
     
    149164                }
    150165                EncryptionDetail encryptionDetail = toWrite.getEncryptionDetailForTag(tag + "");
    151                                
    152                 switch(encryptionDetail) {
     166                Type encryptionDetailType = encryptionDetail.getEncryptionDetailType();
     167                               
     168                switch(encryptionDetailType) {
    153169                case UnencryptedOnSave:
    154170                        writeTagUnencryptedOnSave(toWrite, param, tags, tag);
     
    166182        }
    167183       
    168         private void writeItemData(Frame frame) throws IOException {
     184        private void writeItemData(Frame frame, boolean primaryItems) throws IOException {
    169185                // write each item in the frame
    170                 for (Item i : frame.getItemsToSave()) {
     186                BodyType bodyType = primaryItems ? BodyType.PrimaryBody : BodyType.SurrogateBody;
     187                for (Item i : frame.getItemsToSave(bodyType)) {
    171188                        assert (!(i instanceof Line));
    172189                        writeItem(i);
     
    176193                        assert (!(i instanceof Line));
    177194                        writeItem(i);
    178                 }
    179                
    180                 // write out the surrogates
    181                 List<Item> surrogates = frame.getSurrogateItemsToSave();
    182                 if (!surrogates.isEmpty()) {
    183                         writeSurrogateTerminator();
    184                         for (Item i: surrogates) {
    185                                 assert (!(i instanceof Line));
    186                                 writeItem(i);
    187                         }
    188195                }
    189196        }
     
    194201                BinaryOperator<Boolean> trueExists = (a, b) -> a || b;
    195202                boolean surrogatesInherit = surrogateItems.stream().map(isTagInherited).collect(Collectors.reducing(trueExists)).get();
    196                 if (surrogatesInherit &&
    197                                 Label.resolveKey(toWrite.getEncryptionLabel()) == LabelResult.SuccessResolveLabelToKey) {
    198                         toWrite.setEncryptionDetailForTag(tag + "", EncryptionDetail.UnencryptedOnSave);
     203                boolean userHasKey = Label.getLabel(toWrite.getEncryptionLabel()) == LabelResult.SuccessResolveLabelToKey;
     204               
     205                if (!surrogatesInherit && userHasKey) {
     206                        EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave);
     207                        toWrite.setEncryptionDetailForTag(tag + "", reencryptOnSave);
     208                        writeTagReencryptOnSave(toWrite, tags, tag);
     209                } else {
     210                        EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
     211                        toWrite.setEncryptionDetailForTag(tag + "", unencryptedOnSave);
    199212                        writeTagUnencryptedOnSave(toWrite, new Object[] {}, tags, tag);
    200                 } else {
    201                         toWrite.setEncryptionDetailForTag(tag + "", EncryptionDetail.ReencryptOnSave);
    202                         writeTagReencryptOnSave(toWrite, tags, tag);
    203213                }
    204214        }
     
    207217                Method toRun = tags.get(tag);
    208218                Class<?> declarer = toRun.getDeclaringClass();
    209                 LabelResult res = Label.resolveKey(toWrite.getEncryptionLabel());
     219                LabelResult res = Label.getLabel(toWrite.getEncryptionLabel());
    210220                if (declarer.isAssignableFrom(toWrite.getClass()) && res == LabelResult.SuccessResolveLabelToKey) {
    211221                        try {
  • trunk/src/org/expeditee/encryption/items/surrogates/EncryptionDetail.java

    r1413 r1415  
    11package org.expeditee.encryption.items.surrogates;
    22
    3 public enum EncryptionDetail {
    4         // If the property needs to be unencrypted due to inheritance then do not try to encrypt it.
    5         UnencryptedOnSave,
    6        
    7         // If the property is not inherited by any surrogates then we can try to encrypt it. 
    8         // If we do not have key then default to UnencryptedOnSave.
    9         ReencryptOnSave,
    10        
    11         // If the property is no longer inherited by at least one surrogate then this value is present. 
    12         // If it turns out there is no longer any surrogates who inherit the value then this value can be replaced with ReencryptOnSave.
    13         // Otherwise, this value must be replaced with UnencryptedOnSave.
    14         InheritanceCheckOnSave,
    15        
    16         // If we could not decrypt the value on load then EncryptionDetail.getUndecipheredValue() can be used to get the value for saving.
    17         UseUndecipheredValueOnSave;
    18        
    19         private String undecipheredValue = null;
     3public class EncryptionDetail {
     4        private Type type;
     5        private String undecipheredValue;
     6
     7        public EncryptionDetail(Type type) {
     8                this.type = type;               
     9        }
    2010       
    2111        public EncryptionDetail setUndecipheredValue(String undecipheredValue) {
     
    2515       
    2616        public String getUndecipheredValue() {
    27                 return this.undecipheredValue;
     17                return undecipheredValue;
     18        }
     19       
     20        public Type getEncryptionDetailType() {
     21                return type;
     22        }
     23       
     24        @Override
     25        public EncryptionDetail clone() {
     26                EncryptionDetail copy = new EncryptionDetail(this.type);
     27                copy.setUndecipheredValue(this.undecipheredValue);
     28                return copy;
     29        }
     30       
     31        public enum Type {
     32                // If the property needs to be unencrypted due to inheritance then do not try to encrypt it.
     33                UnencryptedOnSave,
     34               
     35                // If the property is not inherited by any surrogates then we can try to encrypt it. 
     36                // If we do not have key then default to UnencryptedOnSave.
     37                ReencryptOnSave,
     38               
     39                // If the property is no longer inherited by at least one surrogate then this value is present. 
     40                // If it turns out there is no longer any surrogates who inherit the value then this value can be replaced with ReencryptOnSave.
     41                // Otherwise, this value must be replaced with UnencryptedOnSave.
     42                InheritanceCheckOnSave,
     43               
     44                // If we could not decrypt the value on load then EncryptionDetail.getUndecipheredValue() can be used to get the value for saving.
     45                UseUndecipheredValueOnSave;
    2846        }
    2947}
  • trunk/src/org/expeditee/encryption/items/surrogates/Label.java

    r1413 r1415  
    22
    33import java.util.ArrayList;
     4import java.util.Arrays;
    45import java.util.Base64;
    5 import java.util.Collection;
    66import java.util.List;
    7 import java.util.Set;
     7import java.util.function.Predicate;
    88import 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;
     9import java.util.stream.Stream;
     10
     11import org.expeditee.gui.ItemsList;
    1512import org.expeditee.items.Item;
    1613import org.expeditee.items.Text;
    17 import org.expeditee.settings.UserSettings;
     14import org.expeditee.settings.identity.secrets.KeyList;
    1815
    1916public class Label {
    2017       
    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)) {
     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                Text[] userLabels = KeyList.UserLabels.get();
     57                Stream<String> accessibleUserWideLabels = Arrays.asList(userLabels).stream().map(label -> label.getText());
     58               
     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;
     76        }
     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        }
     90       
     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) {
    2798                        return LabelResult.ErrorUnableToFindLabel;
    2899                }
    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 //                     
     100        }
     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;
     105       
     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; }
     116//              }
     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//                      }
    105144//              }
    106145//      }
     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//      }
    107185       
    108186        public enum LabelResult {
     
    110188                SuccessResolveLabelToPartialKey,
    111189                ErrorUnableToFindSecretsFrame,
    112                 ErrorUnableToFindLabel;
     190                ErrorUnableToFindLabel,
     191                ErrorForbiddenLabel;
    113192               
    114193                public byte[] key;
     
    125204                                return "Unable to find your Secrets Frame.";
    126205                        case ErrorUnableToFindLabel:
    127                                 return "Unable to resolve label to encrypt/decrypt frame.  Label: ";
     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.";
    128209                        }
    129210                       
Note: See TracChangeset for help on using the changeset viewer.