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