123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
-
-
- #import "FIRAuthAppCredentialManager.h"
-
- #import "FIRAuthAppCredential.h"
- #import "FIRAuthGlobalWorkQueue.h"
- #import "FIRAuthKeychain.h"
-
- NS_ASSUME_NONNULL_BEGIN
-
-
- static NSString *const kKeychainDataKey = @"app_credentials";
-
-
- static NSString *const kFullCredentialKey = @"full_credential";
-
-
- static NSString *const kPendingReceiptsKey = @"pending_receipts";
-
-
- static const NSUInteger kMaximumNumberOfPendingReceipts = 32;
-
- @implementation FIRAuthAppCredentialManager {
-
-
- FIRAuthKeychain *_keychain;
-
-
-
- NSMutableArray<NSString *> *_pendingReceipts;
-
-
-
- NSMutableDictionary<NSString *, FIRAuthAppCredentialCallback> *_callbacksByReceipt;
- }
-
- - (instancetype)initWithKeychain:(FIRAuthKeychain *)keychain {
- self = [super init];
- if (self) {
- _keychain = keychain;
-
- NSError *error;
- NSData *encodedData = [_keychain dataForKey:kKeychainDataKey error:&error];
- if (!error && encodedData) {
- NSKeyedUnarchiver *unarchiver =
- [[NSKeyedUnarchiver alloc] initForReadingWithData:encodedData];
- FIRAuthAppCredential *credential =
- [unarchiver decodeObjectOfClass:[FIRAuthAppCredential class]
- forKey:kFullCredentialKey];
- if ([credential isKindOfClass:[FIRAuthAppCredential class]]) {
- _credential = credential;
- }
- NSSet<Class> *allowedClasses =
- [NSSet<Class> setWithObjects:[NSArray class], [NSString class], nil];
- NSArray<NSString *> *pendingReceipts =
- [unarchiver decodeObjectOfClasses:allowedClasses forKey:kPendingReceiptsKey];
- if ([pendingReceipts isKindOfClass:[NSArray class]]) {
- _pendingReceipts = [pendingReceipts mutableCopy];
- }
- }
- if (!_pendingReceipts) {
- _pendingReceipts = [[NSMutableArray<NSString *> alloc] init];
- }
- _callbacksByReceipt =
- [[NSMutableDictionary<NSString *, FIRAuthAppCredentialCallback> alloc] init];
- }
- return self;
- }
-
- - (NSUInteger)maximumNumberOfPendingReceipts {
- return kMaximumNumberOfPendingReceipts;
- }
-
- - (void)didStartVerificationWithReceipt:(NSString *)receipt
- timeout:(NSTimeInterval)timeout
- callback:(FIRAuthAppCredentialCallback)callback {
- [_pendingReceipts removeObject:receipt];
- if (_pendingReceipts.count >= kMaximumNumberOfPendingReceipts) {
- [_pendingReceipts removeObjectAtIndex:0];
- }
- [_pendingReceipts addObject:receipt];
- _callbacksByReceipt[receipt] = callback;
- [self saveData];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)),
- FIRAuthGlobalWorkQueue(), ^{
- [self callBackWithReceipt:receipt];
- });
- }
-
- - (BOOL)canFinishVerificationWithReceipt:(NSString *)receipt secret:(NSString *)secret {
- if (![_pendingReceipts containsObject:receipt]) {
- return NO;
- }
- [_pendingReceipts removeObject:receipt];
- _credential = [[FIRAuthAppCredential alloc] initWithReceipt:receipt secret:secret];
- [self saveData];
- [self callBackWithReceipt:receipt];
- return YES;
- }
-
- - (void)clearCredential {
- _credential = nil;
- [self saveData];
- }
-
- #pragma mark - Internal methods
-
-
- - (void)saveData {
- NSMutableData *archiveData = [NSMutableData data];
- NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:archiveData];
- [archiver encodeObject:_credential forKey:kFullCredentialKey];
- [archiver encodeObject:_pendingReceipts forKey:kPendingReceiptsKey];
- [archiver finishEncoding];
- [_keychain setData:archiveData forKey:kKeychainDataKey error:NULL];
- }
-
-
- - (void)callBackWithReceipt:(NSString *)receipt {
- FIRAuthAppCredentialCallback callback = _callbacksByReceipt[receipt];
- if (!callback) {
- return;
- }
- [_callbacksByReceipt removeObjectForKey:receipt];
- if (_credential) {
- callback(_credential);
- } else {
- callback([[FIRAuthAppCredential alloc] initWithReceipt:receipt secret:nil]);
- }
- }
-
- @end
-
- NS_ASSUME_NONNULL_END
|