Files
app-check/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestKeyIDStorage.m
T
Andrew Heard 35fbca8254 Remove Firebase IDs from GACAppAttestKeyIDStorage (#11309)
Removed the Firebase-specific identifiers `appName` and `appID` from `GACAppAttestKeyIDStorage`, replacing them with a generic `keySuffix`.
2023-07-20 10:35:11 -04:00

88 lines
2.6 KiB
Objective-C

/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestKeyIDStorage.h"
#if __has_include(<FBLPromises/FBLPromises.h>)
#import <FBLPromises/FBLPromises.h>
#else
#import "FBLPromises.h"
#endif
#import "AppCheck/Sources/Core/Errors/GACAppCheckErrorUtil.h"
/// The `NSUserDefaults` suite name for the storage location of the app attest key ID.
static NSString *const kKeyIDStorageDefaultsSuiteName = @"com.firebase.GACAppAttestKeyIDStorage";
@interface GACAppAttestKeyIDStorage ()
@property(nonatomic, readonly) NSString *keySuffix;
/// The app attest key ID is stored using `NSUserDefaults` .
@property(nonatomic, readonly) NSUserDefaults *userDefaults;
@end
@implementation GACAppAttestKeyIDStorage
- (instancetype)initWithKeySuffix:(NSString *)keySuffix {
self = [super init];
if (self) {
_keySuffix = [keySuffix copy];
_userDefaults = [[NSUserDefaults alloc] initWithSuiteName:kKeyIDStorageDefaultsSuiteName];
}
return self;
}
- (nonnull FBLPromise<NSString *> *)setAppAttestKeyID:(nullable NSString *)keyID {
[self storeAppAttestKeyID:keyID];
return [FBLPromise resolvedWith:keyID];
}
- (nonnull FBLPromise<NSString *> *)getAppAttestKeyID {
NSString *appAttestKeyID = [self appAttestKeyIDFromStorage];
if (appAttestKeyID) {
return [FBLPromise resolvedWith:appAttestKeyID];
} else {
NSError *error = [GACAppCheckErrorUtil appAttestKeyIDNotFound];
FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
[rejectedPromise reject:error];
return rejectedPromise;
}
}
#pragma mark - Helpers
- (void)storeAppAttestKeyID:(nullable NSString *)keyID {
if (keyID) {
[self.userDefaults setObject:keyID forKey:[self keyIDStorageKey]];
} else {
[self.userDefaults removeObjectForKey:[self keyIDStorageKey]];
}
}
- (nullable NSString *)appAttestKeyIDFromStorage {
NSString *appAttestKeyID = nil;
appAttestKeyID = [self.userDefaults objectForKey:[self keyIDStorageKey]];
return appAttestKeyID;
}
- (NSString *)keyIDStorageKey {
return [NSString stringWithFormat:@"app_attest_keyID.%@", self.keySuffix];
}
@end