From 8e5d3c8e86d17b0ce397db416014edaaabaed05d Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Fri, 26 May 2023 12:14:34 -0400 Subject: [PATCH] Extract Firebase symbols from GACAppAttestArtifactStorage (#11334) Removed the Firebase-specific references `appName` and `appID` from `GACAppAttestArtifactStorage`. They have been moved up one layer into `GACAppAttestProvider`. --- AppCheck.podspec | 4 +- .../AppAttestProvider/GACAppAttestProvider.m | 7 +-- .../Storage/GACAppAttestArtifactStorage.h | 24 +++---- .../Storage/GACAppAttestArtifactStorage.m | 26 +++----- .../GACAppAttestArtifactStorageTests.m | 63 +++++++++++-------- AppCheckInterop.podspec | 2 +- 6 files changed, 61 insertions(+), 65 deletions(-) diff --git a/AppCheck.podspec b/AppCheck.podspec index 164078f..1bc2937 100644 --- a/AppCheck.podspec +++ b/AppCheck.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'AppCheck' - s.version = '10.10.0' + s.version = '10.11.0' s.summary = 'App Check SDK.' s.description = <<-DESC @@ -45,7 +45,7 @@ Pod::Spec.new do |s| s.osx.weak_framework = 'DeviceCheck' s.tvos.weak_framework = 'DeviceCheck' - s.dependency 'AppCheckInterop', '~> 10.10' + s.dependency 'AppCheckInterop', '~> 10.11' s.dependency 'FirebaseCore', '~> 10.0' s.dependency 'PromisesObjC', '~> 2.1' s.dependency 'GoogleUtilities/Environment', '~> 7.8' diff --git a/AppCheck/Sources/AppAttestProvider/GACAppAttestProvider.m b/AppCheck/Sources/AppAttestProvider/GACAppAttestProvider.m index 5a88df6..583e3ce 100644 --- a/AppCheck/Sources/AppAttestProvider/GACAppAttestProvider.m +++ b/AppCheck/Sources/AppAttestProvider/GACAppAttestProvider.m @@ -166,10 +166,9 @@ static NSString *const kHeartbeatKey = @"X-firebase-client"; initWithAPIService:APIService resourceName:[GACAppAttestProvider resourceNameFromApp:app]]; - GACAppAttestArtifactStorage *artifactStorage = - [[GACAppAttestArtifactStorage alloc] initWithAppName:app.name - appID:app.options.googleAppID - accessGroup:app.options.appGroupID]; + GACAppAttestArtifactStorage *artifactStorage = [[GACAppAttestArtifactStorage alloc] + initWithKeySuffix:[GACAppAttestProvider storageKeySuffixForApp:app] + accessGroup:app.options.appGroupID]; GACAppCheckBackoffWrapper *backoffWrapper = [[GACAppCheckBackoffWrapper alloc] init]; diff --git a/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.h b/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.h index 0ea4841..c9a312b 100644 --- a/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.h +++ b/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.h @@ -48,26 +48,20 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; /// Default convenience initializer. -/// @param appName A Firebase App name (`FirebaseApp.name`). The app name will be used as a part of -/// the key to store the token for the storage instance. -/// @param appID A Firebase App identifier (`FirebaseOptions.googleAppID`). The app ID will be used -/// as a part of the key to store the token for the storage instance. +/// @param keySuffix A unique suffix that will be used as a part of the key to store the token for +/// the storage instance. /// @param accessGroup The Keychain Access Group. -- (instancetype)initWithAppName:(NSString *)appName - appID:(NSString *)appID - accessGroup:(nullable NSString *)accessGroup; +- (instancetype)initWithKeySuffix:(NSString *)keySuffix + accessGroup:(nullable NSString *)accessGroup; /// Designated initializer. -/// @param appName A Firebase App name (`FirebaseApp.name`). The app name will be used as a part of -/// the key to store the token for the storage instance. -/// @param appID A Firebase App identifier (`FirebaseOptions.googleAppID`). The app ID will be used -/// as a part of the key to store the token for the storage instance. +/// @param keySuffix A unique suffix that will be used as a part of the key to store the token for +/// the storage instance. /// @param keychainStorage An instance of `GULKeychainStorage` used as an underlying secure storage. /// @param accessGroup The Keychain Access Group. -- (instancetype)initWithAppName:(NSString *)appName - appID:(NSString *)appID - keychainStorage:(GULKeychainStorage *)keychainStorage - accessGroup:(nullable NSString *)accessGroup NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithKeySuffix:(NSString *)keySuffix + keychainStorage:(GULKeychainStorage *)keychainStorage + accessGroup:(nullable NSString *)accessGroup NS_DESIGNATED_INITIALIZER; @end diff --git a/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.m b/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.m index 70ca39a..a674d34 100644 --- a/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.m +++ b/AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.m @@ -33,8 +33,7 @@ static NSString *const kKeychainService = @"com.firebase.app_check.app_attest_ar @interface GACAppAttestArtifactStorage () -@property(nonatomic, readonly) NSString *appName; -@property(nonatomic, readonly) NSString *appID; +@property(nonatomic, readonly) NSString *keySuffix; @property(nonatomic, readonly) GULKeychainStorage *keychainStorage; @property(nonatomic, readonly, nullable) NSString *accessGroup; @@ -42,29 +41,23 @@ static NSString *const kKeychainService = @"com.firebase.app_check.app_attest_ar @implementation GACAppAttestArtifactStorage -- (instancetype)initWithAppName:(NSString *)appName - appID:(NSString *)appID - keychainStorage:(GULKeychainStorage *)keychainStorage - accessGroup:(nullable NSString *)accessGroup { +- (instancetype)initWithKeySuffix:(NSString *)keySuffix + keychainStorage:(GULKeychainStorage *)keychainStorage + accessGroup:(nullable NSString *)accessGroup { self = [super init]; if (self) { - _appName = [appName copy]; - _appID = [appID copy]; + _keySuffix = [keySuffix copy]; _keychainStorage = keychainStorage; _accessGroup = [accessGroup copy]; } return self; } -- (instancetype)initWithAppName:(NSString *)appName - appID:(NSString *)appID - accessGroup:(nullable NSString *)accessGroup { +- (instancetype)initWithKeySuffix:(NSString *)keySuffix + accessGroup:(nullable NSString *)accessGroup { GULKeychainStorage *keychainStorage = [[GULKeychainStorage alloc] initWithService:kKeychainService]; - return [self initWithAppName:appName - appID:appID - keychainStorage:keychainStorage - accessGroup:accessGroup]; + return [self initWithKeySuffix:keySuffix keychainStorage:keychainStorage accessGroup:accessGroup]; } - (FBLPromise *)getArtifactForKey:(NSString *)keyID { @@ -116,8 +109,7 @@ static NSString *const kKeychainService = @"com.firebase.app_check.app_attest_ar } - (NSString *)artifactKey { - return - [NSString stringWithFormat:@"app_check_app_attest_artifact.%@.%@", self.appName, self.appID]; + return [NSString stringWithFormat:@"app_check_app_attest_artifact.%@", self.keySuffix]; } @end diff --git a/AppCheck/Tests/Unit/AppAttestProvider/Storage/GACAppAttestArtifactStorageTests.m b/AppCheck/Tests/Unit/AppAttestProvider/Storage/GACAppAttestArtifactStorageTests.m index 2d0070e..87e3f74 100644 --- a/AppCheck/Tests/Unit/AppAttestProvider/Storage/GACAppAttestArtifactStorageTests.m +++ b/AppCheck/Tests/Unit/AppAttestProvider/Storage/GACAppAttestArtifactStorageTests.m @@ -36,10 +36,12 @@ #import "AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestArtifactStorage.h" #import "AppCheck/Sources/Core/Errors/GACAppCheckErrorUtil.h" +static NSString *const kAppName = @"GACAppAttestArtifactStorageTests"; +static NSString *const kAppID = @"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"; + @interface GACAppAttestArtifactStorageTests : XCTestCase -@property(nonatomic) NSString *appName; -@property(nonatomic) NSString *appID; +@property(nonatomic) NSString *keySuffix; @property(nonatomic) GACAppAttestArtifactStorage *storage; @end @@ -49,12 +51,11 @@ - (void)setUp { [super setUp]; - self.appName = @"GACAppAttestArtifactStorageTests"; - self.appID = @"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"; + self.keySuffix = [GACAppAttestArtifactStorageTests artifactKeySuffixForAppName:kAppName + appID:kAppID]; - self.storage = [[GACAppAttestArtifactStorage alloc] initWithAppName:self.appName - appID:self.appID - accessGroup:nil]; + self.storage = [[GACAppAttestArtifactStorage alloc] initWithKeySuffix:self.keySuffix + accessGroup:nil]; } - (void)tearDown { @@ -87,15 +88,15 @@ - (void)testSetAndGetPerApp { // Assert storages for apps with the same name can independently set/get artifact. - [self assertIndependentSetGetForStoragesWithAppName1:self.appName - appID1:@"app_id" - appName2:self.appName + [self assertIndependentSetGetForStoragesWithAppName1:kAppName + appID1:@"app_id_1" + appName2:kAppName appID2:@"app_id_2"]; // Assert storages for apps with the same app ID can independently set/get artifact. [self assertIndependentSetGetForStoragesWithAppName1:@"app_1" - appID1:self.appID + appID1:kAppID appName2:@"app_2" - appID2:self.appID]; + appID2:kAppID]; // Assert storages for apps with different info can independently set/get artifact. [self assertIndependentSetGetForStoragesWithAppName1:@"app_1" appID1:@"app_id_1" @@ -134,10 +135,9 @@ // 1. Set up storage mock. id mockKeychainStorage = OCMClassMock([GULKeychainStorage class]); GACAppAttestArtifactStorage *artifactStorage = - [[GACAppAttestArtifactStorage alloc] initWithAppName:self.appName - appID:self.appID - keychainStorage:mockKeychainStorage - accessGroup:nil]; + [[GACAppAttestArtifactStorage alloc] initWithKeySuffix:self.keySuffix + keychainStorage:mockKeychainStorage + accessGroup:nil]; // 2. Create and expect keychain error. NSError *gulsKeychainError = [NSError errorWithDomain:@"com.guls.keychain" code:-1 userInfo:nil]; @@ -161,10 +161,9 @@ // 1. Set up storage mock. id mockKeychainStorage = OCMClassMock([GULKeychainStorage class]); GACAppAttestArtifactStorage *artifactStorage = - [[GACAppAttestArtifactStorage alloc] initWithAppName:self.appName - appID:self.appID - keychainStorage:mockKeychainStorage - accessGroup:nil]; + [[GACAppAttestArtifactStorage alloc] initWithKeySuffix:self.keySuffix + keychainStorage:mockKeychainStorage + accessGroup:nil]; // 2. Create and expect keychain error. NSError *gulsKeychainError = [NSError errorWithDomain:@"com.guls.keychain" code:-1 userInfo:nil]; OCMExpect([mockKeychainStorage setObject:[OCMArg any] @@ -188,10 +187,9 @@ // 1. Set up storage mock. id mockKeychainStorage = OCMClassMock([GULKeychainStorage class]); GACAppAttestArtifactStorage *artifactStorage = - [[GACAppAttestArtifactStorage alloc] initWithAppName:self.appName - appID:self.appID - keychainStorage:mockKeychainStorage - accessGroup:nil]; + [[GACAppAttestArtifactStorage alloc] initWithKeySuffix:self.keySuffix + keychainStorage:mockKeychainStorage + accessGroup:nil]; // 2. Create and expect keychain error. NSError *gulsKeychainError = [NSError errorWithDomain:@"com.guls.keychain" code:-1 userInfo:nil]; @@ -242,12 +240,16 @@ appName2:(NSString *)appName2 appID2:(NSString *)appID2 { NSString *keyID = [NSUUID UUID].UUIDString; + NSString *keySuffix1 = [GACAppAttestArtifactStorageTests artifactKeySuffixForAppName:appName1 + appID:appID1]; + NSString *keySuffix2 = [GACAppAttestArtifactStorageTests artifactKeySuffixForAppName:appName2 + appID:appID2]; // Create two storages. GACAppAttestArtifactStorage *storage1 = - [[GACAppAttestArtifactStorage alloc] initWithAppName:appName1 appID:appID1 accessGroup:nil]; + [[GACAppAttestArtifactStorage alloc] initWithKeySuffix:keySuffix1 accessGroup:nil]; GACAppAttestArtifactStorage *storage2 = - [[GACAppAttestArtifactStorage alloc] initWithAppName:appName2 appID:appID2 accessGroup:nil]; + [[GACAppAttestArtifactStorage alloc] initWithKeySuffix:keySuffix2 accessGroup:nil]; // 1. Independently set artifacts for the two storages. NSData *artifact1 = [@"app_attest_artifact1" dataUsingEncoding:NSUTF8StringEncoding]; FBLPromise *setPromise1 = [storage1 setArtifact:artifact1 forKey:keyID]; @@ -281,6 +283,15 @@ XCTAssert(FBLWaitForPromisesWithTimeout(0.5)); } +// TODO(andrewheard): Remove from generic App Check SDK. +// FIREBASE_APP_CHECK_ONLY_BEGIN + ++ (NSString *)artifactKeySuffixForAppName:(NSString *)appName appID:(NSString *)appID { + return [NSString stringWithFormat:@"%@.%@", appName, appID]; +} + +// FIREBASE_APP_CHECK_ONLY_END + @end #endif // !TARGET_OS_MACCATALYST && !TARGET_OS_OSX diff --git a/AppCheckInterop.podspec b/AppCheckInterop.podspec index 37f7a91..641e96c 100644 --- a/AppCheckInterop.podspec +++ b/AppCheckInterop.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'AppCheckInterop' - s.version = '10.10.0' + s.version = '10.11.0' s.summary = 'Interfaces that allow other SDKs to use AppCheck functionality.' s.description = <<-DESC