IOSAD-172 Can remove stale password entries IOSAD-172
authorDirk Zimmermann <dz@pep.security>
Fri, 26 Jun 2020 16:54:34 +0200
branchIOSAD-172
changeset 1468488cc5514ac7
parent 1467 34c00ddf4c4c
child 1469 857787b1a954
IOSAD-172 Can remove stale password entries
pEpObjCAdapter/PEPPassphraseCache.m
     1.1 --- a/pEpObjCAdapter/PEPPassphraseCache.m	Fri Jun 26 16:54:34 2020 +0200
     1.2 +++ b/pEpObjCAdapter/PEPPassphraseCache.m	Fri Jun 26 16:54:34 2020 +0200
     1.3 @@ -11,7 +11,7 @@
     1.4  #import "PEPPassphraseCacheEntry.h"
     1.5  
     1.6  static NSUInteger s_maxNumberOfPassphrases = 20;
     1.7 -static NSUInteger s_defaultTimeoutInSeconds = 10 * 60;
     1.8 +static NSTimeInterval s_defaultTimeoutInSeconds = 10 * 60;
     1.9  
    1.10  @interface PEPPassphraseCache ()
    1.11  
    1.12 @@ -65,4 +65,25 @@
    1.13      return [NSArray arrayWithArray:resultingPassphrases];
    1.14  }
    1.15  
    1.16 +/// Remove password entries that have timed out.
    1.17 +- (void)removeStaleEntries
    1.18 +{
    1.19 +    NSDate *now = [NSDate date];
    1.20 +    NSDate *minimum = [now dateByAddingTimeInterval:-s_defaultTimeoutInSeconds];
    1.21 +    NSTimeInterval minimumTimeInterval = [minimum timeIntervalSinceReferenceDate];
    1.22 +    dispatch_sync(self.queue, ^{
    1.23 +        NSMutableArray *resultingPassphrases = [NSMutableArray
    1.24 +                                                arrayWithCapacity:s_maxNumberOfPassphrases];
    1.25 +
    1.26 +        for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
    1.27 +            if ([entry.dateAdded timeIntervalSinceReferenceDate] >= minimumTimeInterval) {
    1.28 +                [resultingPassphrases addObject:entry];
    1.29 +            }
    1.30 +        }
    1.31 +
    1.32 +        [self.mutablePassphrases removeAllObjects];
    1.33 +        [self.mutablePassphrases addObjectsFromArray:resultingPassphrases];
    1.34 +    });
    1.35 +}
    1.36 +
    1.37  @end