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