IOSAD-172 Sort passphrases on changes IOSAD-172
authorDirk Zimmermann <dz@pep.security>
Mon, 29 Jun 2020 16:39:43 +0200
branchIOSAD-172
changeset 1541c6f4eab1ed95
parent 1540 61a8e9a60a4c
child 1542 c68371dfc71e
IOSAD-172 Sort passphrases on changes
pEpObjCAdapter/PEPPassphraseCache.m
     1.1 --- a/pEpObjCAdapter/PEPPassphraseCache.m	Mon Jun 29 16:39:43 2020 +0200
     1.2 +++ b/pEpObjCAdapter/PEPPassphraseCache.m	Mon Jun 29 16:39:43 2020 +0200
     1.3 @@ -87,6 +87,8 @@
     1.4          if (self.mutablePassphraseEntries.count > s_maxNumberOfPassphrases) {
     1.5              [self.mutablePassphraseEntries removeObjectAtIndex:0];
     1.6          }
     1.7 +
     1.8 +        [self sortPassphrases];
     1.9      });
    1.10  }
    1.11  
    1.12 @@ -131,11 +133,17 @@
    1.13      }
    1.14  
    1.15      dispatch_sync(self.queue, ^{
    1.16 +        BOOL foundAtLeastOnce = NO;
    1.17          for (PEPPassphraseCacheEntry *entry in self.mutablePassphraseEntries) {
    1.18              if ([entry.passphrase isEqualToString:passphrase]) {
    1.19 +                foundAtLeastOnce = YES;
    1.20                  entry.dateAdded = [NSDate date];
    1.21              }
    1.22          }
    1.23 +
    1.24 +        if (foundAtLeastOnce) {
    1.25 +            [self sortPassphrases];
    1.26 +        }
    1.27      });
    1.28  }
    1.29  
    1.30 @@ -152,6 +160,16 @@
    1.31      return NO;
    1.32  }
    1.33  
    1.34 +/// Sort the stored passphrases, last (successfully) used or added first.
    1.35 +/// Assumes being called from the internal queue.
    1.36 +- (void)sortPassphrases
    1.37 +{
    1.38 +    NSArray *sorted = [self sortedArrayByDateNewestFirst:self.mutablePassphraseEntries];
    1.39 +    [self.mutablePassphraseEntries
    1.40 +     replaceObjectsInRange:NSMakeRange(0, [self.mutablePassphraseEntries count])
    1.41 +     withObjectsFromArray:sorted];
    1.42 +}
    1.43 +
    1.44  - (NSArray<PEPPassphraseCacheEntry *> *)sortedArrayByDateNewestFirst:(NSArray<PEPPassphraseCacheEntry *> *)array
    1.45  {
    1.46      return [array sortedArrayUsingComparator:^NSComparisonResult(PEPPassphraseCacheEntry *entry1,