Skip to content

Commit 0352614

Browse files
authored
Use the data protection keychain on macOS (google#127)
* Require GTMAppAuth >= 1.3.0. * Use the data protection keychain on macOS. * Update tests. * Only perform migration for iOS. * Improve comments. * Use a BOOL value rather than OCMOCK_ANY.
1 parent f866493 commit 0352614

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

GoogleSignIn.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The Google Sign-In SDK allows users to sign in with their Google account from th
3333
s.ios.framework = 'UIKit'
3434
s.osx.framework = 'AppKit'
3535
s.dependency 'AppAuth', '~> 1.5'
36-
s.dependency 'GTMAppAuth', '>= 1.2.3', '< 2.0'
36+
s.dependency 'GTMAppAuth', '>= 1.3.0', '< 2.0'
3737
s.dependency 'GTMSessionFetcher/Core', '~> 1.1'
3838
s.resource_bundle = {
3939
'GoogleSignIn' => ['GoogleSignIn/Sources/{Resources,Strings}/*']

GoogleSignIn/Sources/GIDSignIn.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#import "GoogleSignIn/Sources/GIDCallbackQueue.h"
2727
#import "GoogleSignIn/Sources/GIDScopes.h"
2828
#import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
29+
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
2930
#import "GoogleSignIn/Sources/GIDAuthStateMigration.h"
31+
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
3032
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
3133
#import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
3234
#endif
@@ -493,11 +495,13 @@ - (id)initPrivate {
493495
initWithAuthorizationEndpoint:[NSURL URLWithString:authorizationEnpointURL]
494496
tokenEndpoint:[NSURL URLWithString:tokenEndpointURL]];
495497

496-
// Perform migration of auth state from old versions of the SDK if needed.
498+
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
499+
// Perform migration of auth state from old (before 5.0) versions of the SDK if needed.
497500
[GIDAuthStateMigration migrateIfNeededWithTokenURL:_appAuthConfiguration.tokenEndpoint
498501
callbackPath:kBrowserCallbackPath
499502
keychainName:kGTMAppAuthKeychainName
500503
isFreshInstall:isFreshInstall];
504+
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
501505
}
502506
return self;
503507
}
@@ -1010,19 +1014,22 @@ - (BOOL)isFreshInstall {
10101014
}
10111015

10121016
- (void)removeAllKeychainEntries {
1013-
[GTMAppAuthFetcherAuthorization removeAuthorizationFromKeychainForName:kGTMAppAuthKeychainName];
1017+
[GTMAppAuthFetcherAuthorization removeAuthorizationFromKeychainForName:kGTMAppAuthKeychainName
1018+
useDataProtectionKeychain:YES];
10141019
}
10151020

10161021
- (BOOL)saveAuthState:(OIDAuthState *)authState {
10171022
GTMAppAuthFetcherAuthorization *authorization =
10181023
[[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
10191024
return [GTMAppAuthFetcherAuthorization saveAuthorization:authorization
1020-
toKeychainForName:kGTMAppAuthKeychainName];
1025+
toKeychainForName:kGTMAppAuthKeychainName
1026+
useDataProtectionKeychain:YES];
10211027
}
10221028

10231029
- (OIDAuthState *)loadAuthState {
10241030
GTMAppAuthFetcherAuthorization *authorization =
1025-
[GTMAppAuthFetcherAuthorization authorizationFromKeychainForName:kGTMAppAuthKeychainName];
1031+
[GTMAppAuthFetcherAuthorization authorizationFromKeychainForName:kGTMAppAuthKeychainName
1032+
useDataProtectionKeychain:YES];
10261033
return authorization.authState;
10271034
}
10281035

GoogleSignIn/Tests/Unit/GIDSignInTest.m

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
static NSString * const kScope = @"FakeScope";
9191
static NSString * const kScope2 = @"FakeScope2";
9292
static NSString * const kAuthCode = @"FakeAuthCode";
93-
static NSString * const kFakeKeychainName = @"FakeKeychainName";
93+
static NSString * const kKeychainName = @"auth";
9494
static NSString * const kUserEmail = @"FakeUserEmail";
9595
static NSString * const kVerifier = @"FakeVerifier";
9696
static NSString * const kOpenIDRealm = @"FakeRealm";
@@ -300,15 +300,19 @@ - (void)setUp {
300300
_tokenResponse = OCMStrictClassMock([OIDTokenResponse class]);
301301
_tokenRequest = OCMStrictClassMock([OIDTokenRequest class]);
302302
_authorization = OCMStrictClassMock([GTMAppAuthFetcherAuthorization class]);
303-
OCMStub([_authorization authorizationFromKeychainForName:OCMOCK_ANY]).andReturn(_authorization);
303+
OCMStub([_authorization authorizationFromKeychainForName:OCMOCK_ANY
304+
useDataProtectionKeychain:YES]).andReturn(_authorization);
304305
OCMStub([_authorization alloc]).andReturn(_authorization);
305306
OCMStub([_authorization initWithAuthState:OCMOCK_ANY]).andReturn(_authorization);
306-
OCMStub([_authorization saveAuthorization:OCMOCK_ANY toKeychainForName:OCMOCK_ANY])
307+
OCMStub([_authorization saveAuthorization:OCMOCK_ANY
308+
toKeychainForName:OCMOCK_ANY
309+
useDataProtectionKeychain:YES])
307310
.andDo(^(NSInvocation *invocation) {
308311
self->_keychainSaved = self->_saveAuthorizationReturnValue;
309312
[invocation setReturnValue:&self->_saveAuthorizationReturnValue];
310313
});
311-
OCMStub([_authorization removeAuthorizationFromKeychainForName:OCMOCK_ANY])
314+
OCMStub([_authorization removeAuthorizationFromKeychainForName:OCMOCK_ANY
315+
useDataProtectionKeychain:YES])
312316
.andDo(^(NSInvocation *invocation) {
313317
self->_keychainRemoved = YES;
314318
});
@@ -718,6 +722,9 @@ - (void)testSignOut {
718722
XCTAssertTrue(_keychainRemoved, @"should remove keychain");
719723
XCTAssertTrue([_changedKeyPaths containsObject:NSStringFromSelector(@selector(currentUser))],
720724
@"should notify observers that signed in user changed");
725+
726+
OCMVerify([_authorization removeAuthorizationFromKeychainForName:kKeychainName
727+
useDataProtectionKeychain:YES]);
721728
}
722729

723730
- (void)testNotHandleWrongScheme {
@@ -1394,6 +1401,14 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
13941401
[self waitForExpectationsWithTimeout:1 handler:nil];
13951402
XCTAssertFalse(_keychainRemoved, @"should not remove keychain");
13961403
XCTAssertFalse(_keychainSaved, @"should not save to keychain again");
1404+
1405+
if (restoredSignIn) {
1406+
OCMVerify([_authorization authorizationFromKeychainForName:kKeychainName
1407+
useDataProtectionKeychain:YES]);
1408+
OCMVerify([_authorization saveAuthorization:OCMOCK_ANY
1409+
toKeychainForName:kKeychainName
1410+
useDataProtectionKeychain:YES]);
1411+
}
13971412
}
13981413

13991414

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let package = Package(
4848
.package(
4949
name: "GTMAppAuth",
5050
url: "https://github.com/google/GTMAppAuth.git",
51-
"1.2.3" ..< "2.0.0"),
51+
"1.3.0" ..< "2.0.0"),
5252
.package(
5353
name: "GTMSessionFetcher",
5454
url: "https://github.com/google/gtm-session-fetcher.git",

0 commit comments

Comments
 (0)