Skip to content

Commit 55bf5d0

Browse files
authored
Use GTMAppAuth's new delegate protocol (#299)
1 parent 6a329cb commit 55bf5d0

34 files changed

+694
-396
lines changed

.github/workflows/unit_tests.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ jobs:
1717
os: [macos-12]
1818
podspec: [GoogleSignIn.podspec, GoogleSignInSwiftSupport.podspec]
1919
flag: [
20-
"",
21-
"--use-libraries",
20+
"",
2221
"--use-static-frameworks"
2322
]
2423
include:
@@ -33,7 +32,7 @@ jobs:
3332
- name: Lint podspec using local source
3433
run: |
3534
pod lib lint ${{ matrix.podspec }} --verbose \
36-
${{ matrix.includePodspecFlag }} ${{ matrix.flag }}
35+
${{ matrix.includePodspecFlag }} ${{ matrix.flag }}
3736
3837
spm-build-test:
3938
runs-on: ${{ matrix.os }}

GoogleSignIn.podspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The Google Sign-In SDK allows users to sign in with their Google account from th
1212
:git => 'https://github.com/google/GoogleSignIn-iOS.git',
1313
:tag => s.version.to_s
1414
}
15+
s.swift_version = '4.0'
1516
ios_deployment_target = '10.0'
1617
osx_deployment_target = '10.15'
1718
s.ios.deployment_target = ios_deployment_target
@@ -32,8 +33,8 @@ The Google Sign-In SDK allows users to sign in with their Google account from th
3233
]
3334
s.ios.framework = 'UIKit'
3435
s.osx.framework = 'AppKit'
35-
s.dependency 'AppAuth', '~> 1.5'
36-
s.dependency 'GTMAppAuth', '>= 1.3', '< 3.0'
36+
s.dependency 'AppAuth', '~> 1.6'
37+
s.dependency 'GTMAppAuth', '~> 4.0'
3738
s.dependency 'GTMSessionFetcher/Core', '>= 1.1', '< 4.0'
3839
s.resource_bundle = {
3940
'GoogleSignIn' => ['GoogleSignIn/Sources/{Resources,Strings}/*']

GoogleSignIn/Sources/GIDAppAuthFetcherAuthorizationWithEMMSupport.m

Lines changed: 0 additions & 129 deletions
This file was deleted.

GoogleSignIn/Sources/GIDAuthStateMigration.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19+
@class GTMKeychainStore;
20+
@class GTMAuthSession;
21+
1922
NS_ASSUME_NONNULL_BEGIN
2023

21-
// A class providing migration support for auth state saved by older versions of the SDK.
24+
/// A class providing migration support for auth state saved by older versions of the SDK.
2225
@interface GIDAuthStateMigration : NSObject
2326

24-
// Perform a one-time migration for auth state saved by GPPSignIn 1.x or GIDSignIn 1.0 - 4.x to the
25-
// GTMAppAuth storage introduced in GIDSignIn 5.0.
26-
+ (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
27+
/// Creates an instance of this migration type with the keychain storage wrapper it will use.
28+
- (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore NS_DESIGNATED_INITIALIZER;
29+
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.
32+
- (void)migrateIfNeededWithTokenURL:(NSURL *)tokenURL
2733
callbackPath:(NSString *)callbackPath
2834
keychainName:(NSString *)keychainName
2935
isFreshInstall:(BOOL)isFreshInstall;

GoogleSignIn/Sources/GIDAuthStateMigration.m

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
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

2827
NS_ASSUME_NONNULL_BEGIN
@@ -39,9 +38,28 @@
3938
// Keychain service name used to store the last used fingerprint value.
4039
static 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

GoogleSignIn/Sources/GIDEMMSupport.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@
2020

2121
#import <Foundation/Foundation.h>
2222

23+
@import GTMAppAuth;
24+
2325
NS_ASSUME_NONNULL_BEGIN
2426

25-
// A class to support EMM (Enterprise Mobility Management).
26-
@interface GIDEMMSupport : NSObject
27+
/// A class to support EMM (Enterprise Mobility Management).
28+
@interface GIDEMMSupport : NSObject<GTMAuthSessionDelegate>
29+
30+
- (instancetype)init NS_DESIGNATED_INITIALIZER;
2731

28-
// Handles potential EMM error from token fetch response.
32+
/// Handles potential EMM error from token fetch response.
2933
+ (void)handleTokenFetchEMMError:(nullable NSError *)error
3034
completion:(void (^)(NSError *_Nullable))completion;
3135

32-
// Gets a new set of URL parameters that contains updated EMM-related URL parameters if needed.
36+
/// Gets a new set of URL parameters that contains updated EMM-related URL parameters if needed.
3337
+ (NSDictionary *)updatedEMMParametersWithParameters:(NSDictionary *)parameters;
3438

35-
// Gets a new set of URL parameters that also contains EMM-related URL parameters if needed.
39+
/// Gets a new set of URL parameters that also contains EMM-related URL parameters if needed.
3640
+ (NSDictionary *)parametersWithParameters:(NSDictionary *)parameters
3741
emmSupport:(nullable NSString *)emmSupport
3842
isPasscodeInfoRequired:(BOOL)isPasscodeInfoRequired;

GoogleSignIn/Sources/GIDEMMSupport.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,26 @@
4444
// New UIDevice system name for iOS.
4545
static NSString *const kNewIOSSystemName = @"iOS";
4646

47+
// The error key in the server response.
48+
static NSString *const kErrorKey = @"error";
49+
50+
// Optional separator between error prefix and the payload.
51+
static NSString *const kErrorPayloadSeparator = @":";
52+
53+
// A list for recognized error codes.
54+
typedef NS_ENUM(NSInteger, ErrorCode) {
55+
ErrorCodeNone = 0,
56+
ErrorCodeDeviceNotCompliant,
57+
ErrorCodeScreenlockRequired,
58+
ErrorCodeAppVerificationRequired,
59+
};
60+
4761
@implementation GIDEMMSupport
4862

63+
- (instancetype)init {
64+
return [super init];
65+
}
66+
4967
+ (void)handleTokenFetchEMMError:(nullable NSError *)error
5068
completion:(void (^)(NSError *_Nullable))completion {
5169
NSDictionary *errorJSON = error.userInfo[OIDOAuthErrorResponseErrorKey];
@@ -94,6 +112,22 @@ + (NSDictionary *)parametersWithParameters:(NSDictionary *)parameters
94112
return allParameters;
95113
}
96114

115+
#pragma mark - GTMAuthSessionDelegate
116+
117+
- (nullable NSDictionary<NSString *,NSString *> *)
118+
additionalTokenRefreshParametersForAuthSession:(GTMAuthSession *)authSession {
119+
return [GIDEMMSupport updatedEMMParametersWithParameters:
120+
authSession.authState.lastTokenResponse.additionalParameters];
121+
}
122+
123+
- (void)updateErrorForAuthSession:(GTMAuthSession *)authSession
124+
originalError:(NSError *)originalError
125+
completion:(void (^)(NSError * _Nullable))completion {
126+
[GIDEMMSupport handleTokenFetchEMMError:originalError completion:^(NSError *_Nullable error) {
127+
completion(error);
128+
}];
129+
}
130+
97131
@end
98132

99133
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)