Skip to content

Commit

Permalink
[AppCheckCore] Migrate to GoogleUtilities's storage container (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 authored Apr 12, 2024
1 parent c218c20 commit 7d2688d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
5 changes: 3 additions & 2 deletions AppCheckCore.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'AppCheckCore'
s.version = '10.18.2'
s.version = '10.19.0'
s.summary = 'App Check Core SDK.'

s.description = <<-DESC
Expand Down Expand Up @@ -44,7 +44,8 @@ Pod::Spec.new do |s|
s.tvos.weak_framework = 'DeviceCheck'

s.dependency 'PromisesObjC', '~> 2.3'
s.dependency 'GoogleUtilities/Environment', '~> 7.11'
s.dependency 'GoogleUtilities/Environment', '~> 7.13'
s.dependency 'GoogleUtilities/UserDefaults', '~> 7.13'

s.pod_target_xcconfig = {
'GCC_C_LANGUAGE_STANDARD' => 'c99',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
#import "FBLPromises.h"
#endif

#import <GoogleUtilities/GULUserDefaults.h>

#import "AppCheckCore/Sources/Core/Errors/GACAppCheckErrorUtil.h"

/// The `NSUserDefaults` suite name for the storage location of the app attest key ID.
/// The `GULUserDefaults` 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;
/// The app attest key ID is stored using `GULUserDefaults` .
@property(nonatomic, readonly) GULUserDefaults *userDefaults;

@end

Expand All @@ -42,7 +44,7 @@ - (instancetype)initWithKeySuffix:(NSString *)keySuffix {
self = [super init];
if (self) {
_keySuffix = [keySuffix copy];
_userDefaults = [[NSUserDefaults alloc] initWithSuiteName:kKeyIDStorageDefaultsSuiteName];
_userDefaults = [[GULUserDefaults alloc] initWithSuiteName:kKeyIDStorageDefaultsSuiteName];
}
return self;
}
Expand Down
6 changes: 4 additions & 2 deletions AppCheckCore/Sources/DebugProvider/GACAppCheckDebugProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#import "FBLPromises.h"
#endif

#import <GoogleUtilities/GULUserDefaults.h>

#import "AppCheckCore/Sources/Core/APIService/GACAppCheckAPIService.h"
#import "AppCheckCore/Sources/Core/GACAppCheckLogger+Internal.h"
#import "AppCheckCore/Sources/DebugProvider/API/GACAppCheckDebugProviderAPIService.h"
Expand Down Expand Up @@ -122,12 +124,12 @@ - (void)getTokenWithLimitedUse:(BOOL)limitedUse
}

static NSString *_Nullable StoredDebugToken(void) {
return [[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey];
return [[GULUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey];
}

static NSString *GenerateAndStoreDebugToken(void) {
NSString *token = [NSUUID UUID].UUIDString;
[[NSUserDefaults standardUserDefaults] setObject:token forKey:kDebugTokenUserDefaultsKey];
[[GULUserDefaults standardUserDefaults] setObject:token forKey:kDebugTokenUserDefaultsKey];
return token;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import <XCTest/XCTest.h>

#import <GoogleUtilities/GULUserDefaults.h>
#import <OCMock/OCMock.h>
#import "FBLPromise+Testing.h"

Expand Down Expand Up @@ -57,14 +58,14 @@ - (void)tearDown {
self.provider = nil;
[self.processInfoMock stopMocking];
self.processInfoMock = nil;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kDebugTokenUserDefaultsKey];
[[GULUserDefaults standardUserDefaults] removeObjectForKey:kDebugTokenUserDefaultsKey];
}

#pragma mark - Debug token generating/storing

- (void)testCurrentTokenWhenEnvironmentVariableSetAndTokenStored {
[[NSUserDefaults standardUserDefaults] setObject:@"stored token"
forKey:kDebugTokenUserDefaultsKey];
[[GULUserDefaults standardUserDefaults] setObject:@"stored token"
forKey:kDebugTokenUserDefaultsKey];
NSString *envToken = @"env token";
OCMExpect([self.processInfoMock processInfo]).andReturn(self.processInfoMock);
OCMExpect([self.processInfoMock environment]).andReturn(@{kDebugTokenEnvKey : envToken});
Expand All @@ -74,8 +75,8 @@ - (void)testCurrentTokenWhenEnvironmentVariableSetAndTokenStored {
}

- (void)testCurrentTokenWhenFirebaseAndCoreEnvironmentVariablesSetAndTokenStored {
[[NSUserDefaults standardUserDefaults] setObject:@"stored token"
forKey:kDebugTokenUserDefaultsKey];
[[GULUserDefaults standardUserDefaults] setObject:@"stored token"
forKey:kDebugTokenUserDefaultsKey];
NSString *envToken = @"env token";
OCMExpect([self.processInfoMock processInfo]).andReturn(self.processInfoMock);
OCMExpect([self.processInfoMock environment])
Expand All @@ -87,8 +88,8 @@ - (void)testCurrentTokenWhenFirebaseAndCoreEnvironmentVariablesSetAndTokenStored
}

- (void)testCurrentTokenWhenFirebaseEnvironmentVariableSetAndTokenStored {
[[NSUserDefaults standardUserDefaults] setObject:@"stored token"
forKey:kDebugTokenUserDefaultsKey];
[[GULUserDefaults standardUserDefaults] setObject:@"stored token"
forKey:kDebugTokenUserDefaultsKey];
NSString *envToken = @"env token";
OCMExpect([self.processInfoMock processInfo]).andReturn(self.processInfoMock);
OCMExpect([self.processInfoMock environment]).andReturn((@{
Expand All @@ -112,24 +113,24 @@ - (void)testCurrentTokenWhenFirebaseAndCoreEnvironmentVariablesSet {

- (void)testCurrentTokenWhenNoEnvironmentVariableAndTokenStored {
NSString *storedToken = @"stored token";
[[NSUserDefaults standardUserDefaults] setObject:storedToken forKey:kDebugTokenUserDefaultsKey];
[[GULUserDefaults standardUserDefaults] setObject:storedToken forKey:kDebugTokenUserDefaultsKey];

XCTAssertNil(NSProcessInfo.processInfo.environment[kDebugTokenEnvKey]);

XCTAssertEqualObjects([self.provider currentDebugToken], storedToken);
}

- (void)testCurrentTokenWhenNoEnvironmentVariableAndNoTokenStored {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kDebugTokenUserDefaultsKey];
[[GULUserDefaults standardUserDefaults] removeObjectForKey:kDebugTokenUserDefaultsKey];
XCTAssertNil(NSProcessInfo.processInfo.environment[kDebugTokenEnvKey]);
XCTAssertNil([[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey]);
XCTAssertNil([[GULUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey]);

NSString *generatedToken = [self.provider currentDebugToken];
XCTAssertNotNil(generatedToken);

// Check if the generated token is stored to the user defaults.
XCTAssertEqualObjects(
[[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey],
[[GULUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey],
generatedToken);

// Check if the same token is used once generated.
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 10.19.0
- [changed] Removed usages of user defaults API to eliminate required reason impact.

# 10.18.2
- [changed] Added data hashes and system version to App Attest error messages. (#50)

Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ let package = Package(
dependencies: [
.product(name: "FBLPromises", package: "Promises"),
.product(name: "GULEnvironment", package: "GoogleUtilities"),
.product(name: "GULUserDefaults", package: "GoogleUtilities"),
],
path: "AppCheckCore/Sources",
publicHeadersPath: "Public",
Expand Down

0 comments on commit 7d2688d

Please sign in to comment.