pEpObjCAdapter/PEPPassphraseCache.m
author Dirk Zimmermann <dz@pep.security>
Fri, 26 Jun 2020 16:54:34 +0200
branchIOSAD-172
changeset 1467 34c00ddf4c4c
parent 1461 c7790f7d9185
child 1468 488cc5514ac7
permissions -rw-r--r--
IOSAD-172 Internally use passphrase entries
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@1467
    11
#import "PEPPassphraseCacheEntry.h"
dz@1467
    12
dz@1460
    13
static NSUInteger s_maxNumberOfPassphrases = 20;
dz@1461
    14
static NSUInteger s_defaultTimeoutInSeconds = 10 * 60;
dz@1457
    15
dz@1454
    16
@interface PEPPassphraseCache ()
dz@1454
    17
dz@1456
    18
@property (nonatomic) dispatch_queue_t queue;
dz@1467
    19
@property (nonatomic) NSMutableArray<PEPPassphraseCacheEntry *> *mutablePassphrases;
dz@1456
    20
dz@1454
    21
@end
dz@1454
    22
dz@1449
    23
@implementation PEPPassphraseCache
dz@1449
    24
dz@1449
    25
/// Internal constructor (for now).
dz@1449
    26
- (instancetype)initTimeout:(NSUInteger)timeout
dz@1449
    27
{
dz@1449
    28
    self = [super init];
dz@1449
    29
    if (self) {
dz@1449
    30
        _timeout = timeout;
dz@1456
    31
        _queue = dispatch_queue_create("PEPPassphraseCache Queue", DISPATCH_QUEUE_SERIAL);
dz@1460
    32
        _mutablePassphrases = [NSMutableArray arrayWithCapacity:s_maxNumberOfPassphrases];
dz@1449
    33
    }
dz@1449
    34
    return self;
dz@1449
    35
}
dz@1449
    36
dz@1449
    37
/// Public constructor with default values.
dz@1449
    38
- (instancetype)init
dz@1449
    39
{
dz@1461
    40
    return [self initTimeout:s_defaultTimeoutInSeconds];
dz@1449
    41
}
dz@1449
    42
dz@1449
    43
- (void)addPassphrase:(NSString *)passphrase
dz@1449
    44
{
dz@1467
    45
    PEPPassphraseCacheEntry *entry = [[PEPPassphraseCacheEntry alloc]
dz@1467
    46
                                      initWithPassphrase:passphrase];
dz@1459
    47
    dispatch_sync(self.queue, ^{
dz@1467
    48
        [self.mutablePassphrases addObject:entry];
dz@1460
    49
        if (self.mutablePassphrases.count > s_maxNumberOfPassphrases) {
dz@1459
    50
            [self.mutablePassphrases removeObjectAtIndex:0];
dz@1459
    51
        }
dz@1459
    52
    });
dz@1449
    53
}
dz@1449
    54
dz@1449
    55
- (NSArray *)passphrases
dz@1449
    56
{
dz@1457
    57
    NSMutableArray *resultingPassphrases = [NSMutableArray
dz@1460
    58
                                            arrayWithCapacity:s_maxNumberOfPassphrases + 1];
dz@1457
    59
    [resultingPassphrases addObject:@""];
dz@1457
    60
    dispatch_sync(self.queue, ^{
dz@1467
    61
        for (PEPPassphraseCacheEntry *entry in self.mutablePassphrases) {
dz@1467
    62
            [resultingPassphrases addObject:entry.passphrase];
dz@1457
    63
        }
dz@1457
    64
    });
dz@1457
    65
    return [NSArray arrayWithArray:resultingPassphrases];
dz@1449
    66
}
dz@1449
    67
dz@1449
    68
@end