pEpObjCAdapter/PEPPassphraseCache.m
author Dirk Zimmermann <dz@pep.security>
Fri, 26 Jun 2020 16:54:34 +0200
branchIOSAD-172
changeset 1477 5cb57fd85d00
parent 1476 388741294076
child 1478 17e48ad64829
permissions -rw-r--r--
IOSAD-172 We are already running on our queue
dz@1449
     1
//
dz@1449
     2
//  PEPPassphraseCache.m
dz@1449
     3
//  pEpObjCAdapter
dz@1449
     4
//
dz@1449
     5
//  Created by Dirk Zimmermann on 25.06.20.
dz@1449
     6
//  Copyright © 2020 p≡p. All rights reserved.
dz@1449
     7
//
dz@1449
     8
dz@1449
     9
#import "PEPPassphraseCache.h"
dz@1449
    10
dz@1469
    11
#import "PEPPassphraseCacheInternal.h"
dz@1469
    12
dz@1467
    13
#import "PEPPassphraseCacheEntry.h"
dz@1467
    14
dz@1460
    15
static NSUInteger s_maxNumberOfPassphrases = 20;
dz@1468
    16
static NSTimeInterval s_defaultTimeoutInSeconds = 10 * 60;
dz@1474
    17
static NSTimeInterval s_defaultCheckExpiryInterval = 60;
dz@1457
    18
dz@1454
    19
@interface PEPPassphraseCache ()
dz@1454
    20
dz@1456
    21
@property (nonatomic) dispatch_queue_t queue;
dz@1467
    22
@property (nonatomic) NSMutableArray<PEPPassphraseCacheEntry *> *mutablePassphrases;
dz@1476
    23
@property (nonatomic) dispatch_source_t timer;
dz@1456
    24
dz@1454
    25
@end
dz@1454
    26
dz@1449
    27
@implementation PEPPassphraseCache
dz@1449
    28
dz@1449
    29
/// Internal constructor (for now).
dz@1474
    30
- (instancetype)initWithPassphraseTimeout:(NSTimeInterval)timeout
dz@1474
    31
                      checkExpiryInterval:(NSTimeInterval)checkExpiryInterval
dz@1449
    32
{
dz@1449
    33
    self = [super init];
dz@1449
    34
    if (self) {
dz@1449
    35
        _timeout = timeout;
dz@1456
    36
        _queue = dispatch_queue_create("PEPPassphraseCache Queue", DISPATCH_QUEUE_SERIAL);
dz@1460
    37
        _mutablePassphrases = [NSMutableArray arrayWithCapacity:s_maxNumberOfPassphrases];
dz@1475
    38
dz@1475
    39
        // we have a strong reference to the timer, but the timer doesn't have one to us
dz@1475
    40
        typeof(self) __weak weakSelf = self;
dz@1476
    41
dz@1476
    42
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
dz@1476
    43
        dispatch_source_set_timer(_timer,
dz@1476
    44
                                  DISPATCH_TIME_NOW,
dz@1476
    45
                                  checkExpiryInterval * NSEC_PER_SEC,
dz@1476
    46
                                  checkExpiryInterval / 10 * NSEC_PER_SEC);
dz@1476
    47
        dispatch_source_set_event_handler(_timer, ^{
dz@1475
    48
            [weakSelf removeStaleEntries];
dz@1476
    49
        });
dz@1476
    50
        dispatch_resume(_timer);
dz@1449
    51
    }
dz@1449
    52
    return self;
dz@1449
    53
}
dz@1449
    54
dz@1449
    55
/// Public constructor with default values.
dz@1449
    56
- (instancetype)init
dz@1449
    57
{
dz@1474
    58
    return [self initWithPassphraseTimeout:s_defaultTimeoutInSeconds
dz@1474
    59
                       checkExpiryInterval:s_defaultCheckExpiryInterval];
dz@1449
    60
}
dz@1449
    61
dz@1449
    62
- (void)addPassphrase:(NSString *)passphrase
dz@1449
    63
{
dz@1467
    64
    PEPPassphraseCacheEntry *entry = [[PEPPassphraseCacheEntry alloc]
dz@1467
    65
                                      initWithPassphrase:passphrase];
dz@1459
    66
    dispatch_sync(self.queue, ^{
dz@1467
    67
        [self.mutablePassphrases addObject:entry];
dz@1460
    68
        if (self.mutablePassphrases.count > s_maxNumberOfPassphrases) {
dz@1459
    69
            [self.mutablePassphrases removeObjectAtIndex:0];
dz@1459
    70
        }
dz@1459
    71
    });
dz@1449
    72
}
dz@1449
    73
dz@1449
    74
- (NSArray *)passphrases
dz@1449
    75
{
dz@1457
    76
    NSMutableArray *resultingPassphrases = [NSMutableArray
dz@1460
    77
                                            arrayWithCapacity:s_maxNumberOfPassphrases + 1];
dz@1457
    78
    [resultingPassphrases addObject:@""];
dz@1457
    79
    dispatch_sync(self.queue, ^{
dz@1467
    80
        for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
dz@1467
    81
            [resultingPassphrases addObject:entry.passphrase];
dz@1457
    82
        }
dz@1457
    83
    });
dz@1457
    84
    return [NSArray arrayWithArray:resultingPassphrases];
dz@1449
    85
}
dz@1449
    86
dz@1468
    87
/// Remove password entries that have timed out.
dz@1477
    88
/// - Note: Assumes it gets called on `queue`.
dz@1468
    89
- (void)removeStaleEntries
dz@1468
    90
{
dz@1468
    91
    NSDate *now = [NSDate date];
dz@1468
    92
    NSDate *minimum = [now dateByAddingTimeInterval:-s_defaultTimeoutInSeconds];
dz@1468
    93
    NSTimeInterval minimumTimeInterval = [minimum timeIntervalSinceReferenceDate];
dz@1477
    94
    NSMutableArray *resultingPassphrases = [NSMutableArray
dz@1477
    95
                                            arrayWithCapacity:s_maxNumberOfPassphrases];
dz@1468
    96
dz@1477
    97
    for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
dz@1477
    98
        if ([entry.dateAdded timeIntervalSinceReferenceDate] >= minimumTimeInterval) {
dz@1477
    99
            [resultingPassphrases addObject:entry];
dz@1468
   100
        }
dz@1477
   101
    }
dz@1468
   102
dz@1477
   103
    [self.mutablePassphrases removeAllObjects];
dz@1477
   104
    [self.mutablePassphrases addObjectsFromArray:resultingPassphrases];
dz@1468
   105
}
dz@1468
   106
dz@1449
   107
@end