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
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@1472
    17
static NSTimeInterval s_defaultRemoveStalePassphrasesTimeout = 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@1456
    23
dz@1454
    24
@end
dz@1454
    25
dz@1449
    26
@implementation PEPPassphraseCache
dz@1449
    27
dz@1449
    28
/// Internal constructor (for now).
dz@1470
    29
- (instancetype)initWithTimeout:(NSTimeInterval)timeout
dz@1449
    30
{
dz@1449
    31
    self = [super init];
dz@1449
    32
    if (self) {
dz@1449
    33
        _timeout = timeout;
dz@1456
    34
        _queue = dispatch_queue_create("PEPPassphraseCache Queue", DISPATCH_QUEUE_SERIAL);
dz@1460
    35
        _mutablePassphrases = [NSMutableArray arrayWithCapacity:s_maxNumberOfPassphrases];
dz@1449
    36
    }
dz@1449
    37
    return self;
dz@1449
    38
}
dz@1449
    39
dz@1449
    40
/// Public constructor with default values.
dz@1449
    41
- (instancetype)init
dz@1449
    42
{
dz@1469
    43
    return [self initWithTimeout:s_defaultTimeoutInSeconds];
dz@1449
    44
}
dz@1449
    45
dz@1449
    46
- (void)addPassphrase:(NSString *)passphrase
dz@1449
    47
{
dz@1467
    48
    PEPPassphraseCacheEntry *entry = [[PEPPassphraseCacheEntry alloc]
dz@1467
    49
                                      initWithPassphrase:passphrase];
dz@1459
    50
    dispatch_sync(self.queue, ^{
dz@1467
    51
        [self.mutablePassphrases addObject:entry];
dz@1460
    52
        if (self.mutablePassphrases.count > s_maxNumberOfPassphrases) {
dz@1459
    53
            [self.mutablePassphrases removeObjectAtIndex:0];
dz@1459
    54
        }
dz@1459
    55
    });
dz@1449
    56
}
dz@1449
    57
dz@1449
    58
- (NSArray *)passphrases
dz@1449
    59
{
dz@1457
    60
    NSMutableArray *resultingPassphrases = [NSMutableArray
dz@1460
    61
                                            arrayWithCapacity:s_maxNumberOfPassphrases + 1];
dz@1457
    62
    [resultingPassphrases addObject:@""];
dz@1457
    63
    dispatch_sync(self.queue, ^{
dz@1467
    64
        for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
dz@1467
    65
            [resultingPassphrases addObject:entry.passphrase];
dz@1457
    66
        }
dz@1457
    67
    });
dz@1457
    68
    return [NSArray arrayWithArray:resultingPassphrases];
dz@1449
    69
}
dz@1449
    70
dz@1468
    71
/// Remove password entries that have timed out.
dz@1468
    72
- (void)removeStaleEntries
dz@1468
    73
{
dz@1468
    74
    NSDate *now = [NSDate date];
dz@1468
    75
    NSDate *minimum = [now dateByAddingTimeInterval:-s_defaultTimeoutInSeconds];
dz@1468
    76
    NSTimeInterval minimumTimeInterval = [minimum timeIntervalSinceReferenceDate];
dz@1468
    77
    dispatch_sync(self.queue, ^{
dz@1468
    78
        NSMutableArray *resultingPassphrases = [NSMutableArray
dz@1468
    79
                                                arrayWithCapacity:s_maxNumberOfPassphrases];
dz@1468
    80
dz@1468
    81
        for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
dz@1468
    82
            if ([entry.dateAdded timeIntervalSinceReferenceDate] >= minimumTimeInterval) {
dz@1468
    83
                [resultingPassphrases addObject:entry];
dz@1468
    84
            }
dz@1468
    85
        }
dz@1468
    86
dz@1468
    87
        [self.mutablePassphrases removeAllObjects];
dz@1468
    88
        [self.mutablePassphrases addObjectsFromArray:resultingPassphrases];
dz@1468
    89
    });
dz@1468
    90
}
dz@1468
    91
dz@1449
    92
@end