1616
1717#import " GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
1818
19+ @import GTMAppAuth;
20+
1921#ifdef SWIFT_PACKAGE
2022@import AppAuth;
21- @import GTMAppAuth;
2223#else
2324#import < AppAuth/AppAuth.h>
24- #import < GTMAppAuth/GTMAppAuth.h>
25- #import < GTMAppAuth/GTMKeychain.h>
2625#endif
2726
2827NS_ASSUME_NONNULL_BEGIN
3938// Keychain service name used to store the last used fingerprint value.
4039static NSString *const kFingerprintService = @" fingerprint" ;
4140
41+ @interface GIDAuthStateMigration ()
42+
43+ @property (nonatomic , strong ) GTMKeychainStore *keychainStore;
44+
45+ @end
46+
4247@implementation GIDAuthStateMigration
4348
44- + (void )migrateIfNeededWithTokenURL : (NSURL *)tokenURL
49+ - (instancetype )initWithKeychainStore : (GTMKeychainStore *)keychainStore {
50+ self = [super init ];
51+ if (self) {
52+ _keychainStore = keychainStore;
53+ }
54+ return self;
55+ }
56+
57+ - (instancetype )init {
58+ GTMKeychainStore *keychainStore = [[GTMKeychainStore alloc ] initWithItemName: @" auth" ];
59+ return [self initWithKeychainStore: keychainStore];
60+ }
61+
62+ - (void )migrateIfNeededWithTokenURL : (NSURL *)tokenURL
4563 callbackPath : (NSString *)callbackPath
4664 keychainName : (NSString *)keychainName
4765 isFreshInstall : (BOOL )isFreshInstall {
@@ -55,14 +73,15 @@ + (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
5573 // action and go on to mark the migration check as having been performed.
5674 if (!isFreshInstall) {
5775 // Attempt migration
58- GTMAppAuthFetcherAuthorization *authorization =
59- [self extractAuthorizationWithTokenURL : tokenURL callbackPath: callbackPath];
76+ GTMAuthSession *authSession =
77+ [self extractAuthSessionWithTokenURL : tokenURL callbackPath: callbackPath];
6078
6179 // If migration was successful, save our migrated state to the keychain.
62- if (authorization) {
80+ if (authSession) {
81+ NSError *err;
82+ [self .keychainStore saveAuthSession: authSession error: &err];
6383 // If we're unable to save to the keychain, return without marking migration performed.
64- if (![GTMAppAuthFetcherAuthorization saveAuthorization: authorization
65- toKeychainForName: keychainName]) {
84+ if (err) {
6685 return ;
6786 };
6887 }
@@ -72,19 +91,21 @@ + (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
7291 [defaults setBool: YES forKey: kMigrationCheckPerformedKey ];
7392}
7493
75- // Returns a |GTMAppAuthFetcherAuthorization | object containing any old auth state or |nil| if none
94+ // Returns a |GTMAuthSession | object containing any old auth state or |nil| if none
7695// was found or the migration failed.
77- + (nullable GTMAppAuthFetcherAuthorization *)
78- extractAuthorizationWithTokenURL:( NSURL *) tokenURL callbackPath : (NSString *)callbackPath {
96+ - (nullable GTMAuthSession *)extractAuthSessionWithTokenURL : ( NSURL *) tokenURL
97+ callbackPath : (NSString *)callbackPath {
7998 // Retrieve the last used fingerprint.
8099 NSString *fingerprint = [GIDAuthStateMigration passwordForService: kFingerprintService ];
81100 if (!fingerprint) {
82101 return nil ;
83102 }
84103
85104 // Retrieve the GTMOAuth2 persistence string.
86- NSString *GTMOAuth2PersistenceString = [GTMKeychain passwordFromKeychainForName: fingerprint];
87- if (!GTMOAuth2PersistenceString) {
105+ NSError *passwordError;
106+ NSString *GTMOAuth2PersistenceString =
107+ [self .keychainStore.keychainHelper passwordForService: fingerprint error: &passwordError];
108+ if (passwordError) {
88109 return nil ;
89110 }
90111
@@ -126,16 +147,17 @@ + (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
126147 additionalTokenRequestParameters];
127148 }
128149
129- // Use |GTMOAuth2KeychainCompatibility | to generate a |GTMAppAuthFetcherAuthorization | from the
150+ // Use |GTMOAuth2Compatibility | to generate a |GTMAuthSession | from the
130151 // persistence string, redirect URI, client ID, and token endpoint URL.
131- GTMAppAuthFetcherAuthorization *authorization = [GTMOAuth2KeychainCompatibility
132- authorizeFromPersistenceString: persistenceString
133- tokenURL: tokenURL
134- redirectURI: redirectURI
135- clientID: clientID
136- clientSecret: nil ];
137-
138- return authorization;
152+ GTMAuthSession *authSession =
153+ [GTMOAuth2Compatibility authSessionForPersistenceString: persistenceString
154+ tokenURL: tokenURL
155+ redirectURI: redirectURI
156+ clientID: clientID
157+ clientSecret: nil
158+ error: nil ];
159+
160+ return authSession;
139161}
140162
141163// Returns the password string for a given service string stored by an old version of the SDK or
0 commit comments