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