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