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