pEpObjCAdapter/PEPPassphraseCache.m
author Dirk Zimmermann <dz@pep.security>
Fri, 26 Jun 2020 16:54:34 +0200
branchIOSAD-172
changeset 1472 5127b205711e
parent 1470 f6c4020bf409
child 1473 066dcbeeb6a0
permissions -rw-r--r--
IOSAD-172 Default timer interval
     1 //
     2 //  PEPPassphraseCache.m
     3 //  pEpObjCAdapter
     4 //
     5 //  Created by Dirk Zimmermann on 25.06.20.
     6 //  Copyright © 2020 p≡p. All rights reserved.
     7 //
     8 
     9 #import "PEPPassphraseCache.h"
    10 
    11 #import "PEPPassphraseCacheInternal.h"
    12 
    13 #import "PEPPassphraseCacheEntry.h"
    14 
    15 static NSUInteger s_maxNumberOfPassphrases = 20;
    16 static NSTimeInterval s_defaultTimeoutInSeconds = 10 * 60;
    17 static NSTimeInterval s_defaultRemoveStalePassphrasesTimeout = 60;
    18 
    19 @interface PEPPassphraseCache ()
    20 
    21 @property (nonatomic) dispatch_queue_t queue;
    22 @property (nonatomic) NSMutableArray<PEPPassphraseCacheEntry *> *mutablePassphrases;
    23 
    24 @end
    25 
    26 @implementation PEPPassphraseCache
    27 
    28 /// Internal constructor (for now).
    29 - (instancetype)initWithTimeout:(NSTimeInterval)timeout
    30 {
    31     self = [super init];
    32     if (self) {
    33         _timeout = timeout;
    34         _queue = dispatch_queue_create("PEPPassphraseCache Queue", DISPATCH_QUEUE_SERIAL);
    35         _mutablePassphrases = [NSMutableArray arrayWithCapacity:s_maxNumberOfPassphrases];
    36     }
    37     return self;
    38 }
    39 
    40 /// Public constructor with default values.
    41 - (instancetype)init
    42 {
    43     return [self initWithTimeout:s_defaultTimeoutInSeconds];
    44 }
    45 
    46 - (void)addPassphrase:(NSString *)passphrase
    47 {
    48     PEPPassphraseCacheEntry *entry = [[PEPPassphraseCacheEntry alloc]
    49                                       initWithPassphrase:passphrase];
    50     dispatch_sync(self.queue, ^{
    51         [self.mutablePassphrases addObject:entry];
    52         if (self.mutablePassphrases.count > s_maxNumberOfPassphrases) {
    53             [self.mutablePassphrases removeObjectAtIndex:0];
    54         }
    55     });
    56 }
    57 
    58 - (NSArray *)passphrases
    59 {
    60     NSMutableArray *resultingPassphrases = [NSMutableArray
    61                                             arrayWithCapacity:s_maxNumberOfPassphrases + 1];
    62     [resultingPassphrases addObject:@""];
    63     dispatch_sync(self.queue, ^{
    64         for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
    65             [resultingPassphrases addObject:entry.passphrase];
    66         }
    67     });
    68     return [NSArray arrayWithArray:resultingPassphrases];
    69 }
    70 
    71 /// Remove password entries that have timed out.
    72 - (void)removeStaleEntries
    73 {
    74     NSDate *now = [NSDate date];
    75     NSDate *minimum = [now dateByAddingTimeInterval:-s_defaultTimeoutInSeconds];
    76     NSTimeInterval minimumTimeInterval = [minimum timeIntervalSinceReferenceDate];
    77     dispatch_sync(self.queue, ^{
    78         NSMutableArray *resultingPassphrases = [NSMutableArray
    79                                                 arrayWithCapacity:s_maxNumberOfPassphrases];
    80 
    81         for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
    82             if ([entry.dateAdded timeIntervalSinceReferenceDate] >= minimumTimeInterval) {
    83                 [resultingPassphrases addObject:entry];
    84             }
    85         }
    86 
    87         [self.mutablePassphrases removeAllObjects];
    88         [self.mutablePassphrases addObjectsFromArray:resultingPassphrases];
    89     });
    90 }
    91 
    92 @end