Skip to content

Commit 7c44268

Browse files
committed
add migration logic
1 parent 90a9f4d commit 7c44268

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

GoogleSignIn/Sources/GIDAuthStateMigration.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
2727
/// Creates an instance of this migration type with the keychain storage wrapper it will use.
2828
- (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore NS_DESIGNATED_INITIALIZER;
2929

30-
/// Perform a one-time migration for auth state saved by GPPSignIn 1.x or GIDSignIn 1.0 - 4.x to the
31-
/// GTMAppAuth storage introduced in GIDSignIn 5.0.
30+
/// Perform necessary migrations from legacy auth state storage to most recent GTMAppAuth version.
3231
- (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
3332
callbackPath:(NSString *)callbackPath
3433
keychainName:(NSString *)keychainName

GoogleSignIn/Sources/GIDAuthStateMigration.m

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,20 @@ - (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
7272
// If this is not a fresh install, attempt to migrate state. If this is a fresh install, take no
7373
// action and go on to mark the migration check as having been performed.
7474
if (!isFreshInstall) {
75-
// Attempt migration
76-
GTMAuthSession *authSession =
77-
[self extractAuthSessionWithTokenURL:tokenURL callbackPath:callbackPath];
78-
75+
GTMAuthSession *authSession = nil;
76+
#if TARGET_OS_MAC || TARGET_OS_MACCATALYST
77+
// Migrate from the fileBasedKeychain to dataProtectedKeychain with GTMAppAuth 5.0.
78+
GTMKeychainAttribute *fileBasedKeychain = [GTMKeychainAttribute useFileBasedKeychain];
79+
NSSet *attributes = [NSSet setWithArray:@[fileBasedKeychain]];
80+
GTMKeychainStore *keychainStoreLegacy =
81+
[[GTMKeychainStore alloc] initWithItemName:self.keychainStore.itemName
82+
keychainAttributes:attributes];
83+
authSession = [keychainStoreLegacy retrieveAuthSessionWithError:nil];
84+
#else // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
85+
// Migrate from GPPSignIn 1.x or GIDSignIn 1.0 - 4.x to the GTMAppAuth storage introduced in
86+
// GIDSignIn 5.0.
87+
authSession = [self extractAuthSessionWithTokenURL:tokenURL callbackPath:callbackPath];
88+
#endif
7989
// If migration was successful, save our migrated state to the keychain.
8090
if (authSession) {
8191
NSError *err;
@@ -84,6 +94,9 @@ - (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
8494
if (err) {
8595
return;
8696
};
97+
#if TARGET_OS_MAC || TARGET_OS_MACCATALYST
98+
[keychainStoreLegacy removeAuthSessionWithError:nil];
99+
#endif
87100
}
88101
}
89102

GoogleSignIn/Sources/GIDSignIn.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
#import "GoogleSignIn/Sources/GIDCallbackQueue.h"
2828
#import "GoogleSignIn/Sources/GIDScopes.h"
2929
#import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
30+
#import "GoogleSignIn/Sources/GIDAuthStateMigration.h"
3031
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
3132
#import <AppCheckCore/GACAppCheckToken.h>
3233
#import "GoogleSignIn/Sources/GIDAppCheck/Implementations/GIDAppCheck.h"
3334
#import "GoogleSignIn/Sources/GIDAppCheck/UI/GIDActivityIndicatorViewController.h"
34-
#import "GoogleSignIn/Sources/GIDAuthStateMigration.h"
3535
#import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
3636
#import "GoogleSignIn/Sources/GIDTimedLoader/GIDTimedLoader.h"
3737
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
@@ -560,15 +560,13 @@ - (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore {
560560
initWithAuthorizationEndpoint:[NSURL URLWithString:authorizationEnpointURL]
561561
tokenEndpoint:[NSURL URLWithString:tokenEndpointURL]];
562562
_keychainStore = keychainStore;
563-
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
564563
// Perform migration of auth state from old (before 5.0) versions of the SDK if needed.
565564
GIDAuthStateMigration *migration =
566565
[[GIDAuthStateMigration alloc] initWithKeychainStore:_keychainStore];
567566
[migration migrateIfNeededWithTokenURL:_appAuthConfiguration.tokenEndpoint
568567
callbackPath:kBrowserCallbackPath
569568
keychainName:kGTMAppAuthKeychainName
570569
isFreshInstall:isFreshInstall];
571-
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
572570
}
573571
return self;
574572
}

0 commit comments

Comments
 (0)