From 174e6b815a00935619a15da4baf6d995297b4cff Mon Sep 17 00:00:00 2001 From: pinlu Date: Thu, 7 Jul 2022 10:42:01 -0700 Subject: [PATCH 1/5] Replace GIDSignInCallback Replace GIDSignInCallback with block void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) --- GoogleSignIn/Sources/GIDSignIn.m | 31 ++++++++------ .../Sources/GIDSignInInternalOptions.h | 13 +++--- .../Sources/GIDSignInInternalOptions.m | 10 ++--- .../Sources/Public/GoogleSignIn/GIDSignIn.h | 23 +++++------ .../Public/GoogleSignIn/GoogleSignIn.h | 1 + .../Tests/Unit/GIDSignInInternalOptionsTest.m | 4 +- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 41 +++++++++++++------ 7 files changed, 71 insertions(+), 52 deletions(-) diff --git a/GoogleSignIn/Sources/GIDSignIn.m b/GoogleSignIn/Sources/GIDSignIn.m index d9cecb6b..62091666 100644 --- a/GoogleSignIn/Sources/GIDSignIn.m +++ b/GoogleSignIn/Sources/GIDSignIn.m @@ -34,6 +34,7 @@ #import "GoogleSignIn/Sources/GIDAuthentication_Private.h" #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h" #import "GoogleSignIn/Sources/GIDProfileData_Private.h" +#import "GoogleSignIn/Sources/GIDUserAuth_Private.h" #ifdef SWIFT_PACKAGE @import AppAuth; @@ -187,7 +188,7 @@ - (BOOL)hasPreviousSignIn { return [authState isAuthorized]; } -- (void)restorePreviousSignInWithCallback:(nullable GIDSignInCallback)callback { +- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback:callback]]; } @@ -217,7 +218,7 @@ - (BOOL)restorePreviousSignInNoRefresh { - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingViewController:presentingViewController @@ -231,7 +232,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingViewController:presentingViewController @@ -244,7 +245,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { [self signInWithConfiguration:configuration presentingViewController:presentingViewController hint:nil @@ -253,7 +254,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)addScopes:(NSArray *)scopes presentingViewController:(UIViewController *)presentingViewController - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { // A currentUser must be available in order to complete this flow. if (!self.currentUser) { // No currentUser is set, notify callback of failure. @@ -310,7 +311,7 @@ - (void)addScopes:(NSArray *)scopes - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingWindow:presentingWindow @@ -322,7 +323,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { [self signInWithConfiguration:configuration presentingWindow:presentingWindow hint:nil @@ -333,7 +334,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingWindow:presentingWindow @@ -346,7 +347,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)addScopes:(NSArray *)scopes presentingWindow:(NSWindow *)presentingWindow - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { // A currentUser must be available in order to complete this flow. if (!self.currentUser) { // No currentUser is set, notify callback of failure. @@ -541,7 +542,8 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options { if (options.callback) { self->_currentOptions = nil; dispatch_async(dispatch_get_main_queue(), ^{ - options.callback(self->_currentUser, nil); + GIDUserAuth *userAuth = [[GIDUserAuth alloc]initWithGoogleUser:self->_currentUser serverAuthCode:nil]; + options.callback(userAuth, nil); }); } } @@ -601,7 +603,7 @@ - (void)authenticateInteractivelyWithOptions:(GIDSignInInternalOptions *)options _currentAuthorizationFlow = [OIDAuthorizationService presentAuthorizationRequest:request presentingViewController:options.presentingViewController - callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse, + callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse, NSError *_Nullable error) { [self processAuthorizationResponse:authorizationResponse error:error @@ -882,10 +884,13 @@ - (void)addCompletionCallback:(GIDAuthFlow *)authFlow { [authFlow addCallback:^() { GIDAuthFlow *handlerAuthFlow = weakAuthFlow; if (self->_currentOptions.callback) { - GIDSignInCallback callback = self->_currentOptions.callback; + void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = self->_currentOptions.callback; self->_currentOptions = nil; dispatch_async(dispatch_get_main_queue(), ^{ - callback(self->_currentUser, handlerAuthFlow.error); + OIDAuthState *authState = handlerAuthFlow.authState; + NSString * _Nullable serverAuthCode = [authState.lastTokenResponse.additionalParameters[@"server_code"] copy]; + GIDUserAuth * userAuth = [[GIDUserAuth alloc]initWithGoogleUser:self->_currentUser serverAuthCode:serverAuthCode]; + callback(userAuth, handlerAuthFlow.error); }); } }]; diff --git a/GoogleSignIn/Sources/GIDSignInInternalOptions.h b/GoogleSignIn/Sources/GIDSignInInternalOptions.h index 72f9b7fa..ba4b4f9a 100644 --- a/GoogleSignIn/Sources/GIDSignInInternalOptions.h +++ b/GoogleSignIn/Sources/GIDSignInInternalOptions.h @@ -25,6 +25,7 @@ #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h" @class GIDConfiguration; +@class GIDUserAuth; NS_ASSUME_NONNULL_BEGIN @@ -55,7 +56,7 @@ NS_ASSUME_NONNULL_BEGIN #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST /// The callback block to be called at the completion of the flow. -@property(nonatomic, readonly, nullable) GIDSignInCallback callback; +@property(nonatomic, readonly, nullable) void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error); /// The scopes to be used during the flow. @property(nonatomic, copy, nullable) NSArray *scopes; @@ -69,32 +70,32 @@ NS_ASSUME_NONNULL_BEGIN presentingViewController:(nullable UIViewController *)presentingViewController loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingViewController:(nullable UIViewController *)presentingViewController loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; #elif TARGET_OS_OSX + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST /// Creates the options to sign in silently. -+ (instancetype)silentOptionsWithCallback:(GIDSignInCallback)callback; ++ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; /// Creates options with the same values as the receiver, except for the "extra parameters", and /// continuation flag, which are replaced by the arguments passed to this method. diff --git a/GoogleSignIn/Sources/GIDSignInInternalOptions.m b/GoogleSignIn/Sources/GIDSignInInternalOptions.m index f4ed1488..4962eaa5 100644 --- a/GoogleSignIn/Sources/GIDSignInInternalOptions.m +++ b/GoogleSignIn/Sources/GIDSignInInternalOptions.m @@ -31,14 +31,14 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { #elif TARGET_OS_OSX + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init]; @@ -64,13 +64,13 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con presentingViewController:(nullable UIViewController *)presentingViewController loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { #elif TARGET_OS_OSX + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable GIDSignInCallback)callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST GIDSignInInternalOptions *options = [self defaultOptionsWithConfiguration:configuration #if TARGET_OS_IOS || TARGET_OS_MACCATALYST @@ -85,7 +85,7 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con return options; } -+ (instancetype)silentOptionsWithCallback:(GIDSignInCallback)callback { ++ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { GIDSignInInternalOptions *options = [self defaultOptionsWithConfiguration:nil #if TARGET_OS_IOS || TARGET_OS_MACCATALYST presentingViewController:nil diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h index 094dffb1..65c409f0 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h @@ -25,6 +25,7 @@ @class GIDConfiguration; @class GIDGoogleUser; +@class GIDUserAuth; NS_ASSUME_NONNULL_BEGIN @@ -50,10 +51,6 @@ typedef NS_ERROR_ENUM(kGIDSignInErrorDomain, GIDSignInErrorCode) { kGIDSignInErrorCodeScopesAlreadyGranted = -8, }; -/// Represents a callback block that takes a `GIDGoogleUser` on success or an error if the operation -/// was unsuccessful. -typedef void (^GIDSignInCallback)(GIDGoogleUser *_Nullable user, NSError *_Nullable error); - /// Represents a callback block that takes an error if the operation was unsuccessful. typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); @@ -93,7 +90,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// /// @param callback The `GIDSignInCallback` block that is called on completion. This block will be /// called asynchronously on the main queue. -- (void)restorePreviousSignInWithCallback:(nullable GIDSignInCallback)callback; +- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; /// Marks current user as being in the signed out state. - (void)signOut; @@ -121,7 +118,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// called asynchronously on the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController - callback:(nullable GIDSignInCallback)callback + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback NS_EXTENSION_UNAVAILABLE("The sign-in flow is not supported in App Extensions."); /// Starts an interactive sign-in flow on iOS using the provided configuration and a login hint. @@ -142,7 +139,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint - callback:(nullable GIDSignInCallback)callback + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback NS_EXTENSION_UNAVAILABLE("The sign-in flow is not supported in App Extensions."); /// Starts an interactive sign-in flow on iOS using the provided configuration and a login hint. @@ -165,7 +162,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; /// Starts an interactive consent flow on iOS to add scopes to the current user's grants. /// @@ -180,7 +177,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// called asynchronously on the main queue. - (void)addScopes:(NSArray *)scopes presentingViewController:(UIViewController *)presentingViewController - callback:(nullable GIDSignInCallback)callback + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions."); #elif TARGET_OS_OSX @@ -197,7 +194,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// called asynchronously on the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; /// Starts an interactive sign-in flow on macOS using the provided configuration and a login hint. /// @@ -215,7 +212,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; /// Starts an interactive sign-in flow on macOS using the provided configuration and a login hint. /// @@ -236,7 +233,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; /// Starts an interactive consent flow on macOS to add scopes to the current user's grants. /// @@ -249,7 +246,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// called asynchronously on the main queue. - (void)addScopes:(NSArray *)scopes presentingWindow:(NSWindow *)presentingWindow - callback:(nullable GIDSignInCallback)callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; #endif diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h index 091f2c1a..df002612 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h @@ -20,6 +20,7 @@ #import "GIDGoogleUser.h" #import "GIDProfileData.h" #import "GIDSignIn.h" +#import "GIDUserAuth.h" #if TARGET_OS_IOS || TARGET_OS_MACCATALYST #import "GIDSignInButton.h" #endif diff --git a/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m b/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m index c786bba6..212125d9 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m @@ -37,7 +37,7 @@ - (void)testDefaultOptions { id presentingWindow = OCMStrictClassMock([NSWindow class]); #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST NSString *loginHint = @"login_hint"; - GIDSignInCallback callback = ^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {}; + void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) {}; GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration @@ -63,7 +63,7 @@ - (void)testDefaultOptions { } - (void)testSilentOptions { - GIDSignInCallback callback = ^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {}; + void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) {}; GIDSignInInternalOptions *options = [GIDSignInInternalOptions silentOptionsWithCallback:callback]; XCTAssertFalse(options.interactive); XCTAssertFalse(options.continuation); diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index 070fead1..64e4e352 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -241,7 +241,7 @@ @interface GIDSignInTest : XCTestCase { NSString *_hint; // The callback to be used when testing |GIDSignIn|. - GIDSignInCallback _callback; + void (^_callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error); // The saved authorization request. OIDAuthorizationRequest *_savedAuthorizationRequest; @@ -346,10 +346,10 @@ - (void)setUp { _hint = nil; __weak GIDSignInTest *weakSelf = self; - _callback = ^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + _callback = ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) { GIDSignInTest *strongSelf = weakSelf; - if (!user) { - XCTAssertNotNil(error, @"should have an error if user is nil"); + if (!userAuth) { + XCTAssertNotNil(error, @"should have an error if the userAuth is nil"); } XCTAssertFalse(strongSelf->_callbackCalled, @"callback already called"); strongSelf->_callbackCalled = YES; @@ -456,7 +456,7 @@ - (void)testRestorePreviousSignInWhenSignedOut { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called."]; - [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user, + [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNotNil(error, @"error should not have been nil"); @@ -1073,6 +1073,7 @@ - (void)testTokenEndpointEMMError { NSError *handledError = [NSError errorWithDomain:kGIDSignInErrorDomain code:kGIDSignInErrorCodeEMM userInfo:emmError.userInfo]; + completion(handledError); [self waitForExpectationsWithTimeout:1 handler:nil]; @@ -1220,10 +1221,16 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow } } else { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback called"]; - GIDSignInCallback callback = ^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = + ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) { [expectation fulfill]; - if (!user) { - XCTAssertNotNil(error, @"should have an error if user is nil"); + if (userAuth) { + BOOL hasError = authError || modalCancel || tokenError; + if (!hasError) { + XCTAssertEqualObjects(userAuth.serverAuthCode, kServerAuthCode); + } + } else { + XCTAssertNotNil(error, @"should have an error if the userAuth is nil"); } XCTAssertFalse(self->_callbackCalled, @"callback already called"); self->_callbackCalled = YES; @@ -1286,7 +1293,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse]; [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse]; } - + // Simulate auth endpoint response if (modalCancel) { NSError *error = [NSError errorWithDomain:OIDGeneralErrorDomain @@ -1305,7 +1312,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow if (restoredSignIn && oldAccessToken) { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; - [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user, + [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error, @"should have no error"); @@ -1319,6 +1326,9 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow // OIDTokenCallback if (tokenError) { [[_authState expect] updateWithTokenResponse:nil error:tokenError]; + + // CompletionCallback + [[[_authState expect] andReturn:nil] lastTokenResponse]; } else { [[_authState expect] updateWithTokenResponse:[OCMArg any] error:nil]; } @@ -1348,10 +1358,13 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow profileData:SAVE_TO_ARG_BLOCK(profileData)]; } } + + // CompletionCallback - mock server auth code parsing + [[[_authState expect] andReturn:tokenResponse] lastTokenResponse]; if (restoredSignIn && !oldAccessToken) { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; - [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user, + [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error, @"should have no error"); @@ -1361,11 +1374,13 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow _savedTokenCallback(tokenResponse, nil); } - [_authState verify]; if (keychainError) { return; } [self waitForExpectationsWithTimeout:1 handler:nil]; + + [_authState verify]; + XCTAssertTrue(_keychainSaved, @"should save to keychain"); XCTAssertNotNil(authState); // Check fat ID token decoding @@ -1390,7 +1405,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; - [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user, + [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error, @"should have no error"); From 5d23b412486e0e32c96a27a8dcf6a71763ab5eb8 Mon Sep 17 00:00:00 2001 From: pinlu Date: Mon, 11 Jul 2022 23:00:47 -0700 Subject: [PATCH 2/5] Change GIDSignInCallback in sample apps. --- .../Sources/GIDSignInInternalOptions.h | 2 -- .../ObjC/SignInSample/Source/AppDelegate.m | 2 +- .../Source/SignInViewController.m | 4 ++-- .../Shared/DaysUntilBirthday.swift | 6 ++--- .../Services/GoogleSignInAuthenticator.swift | 24 +++++++++---------- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/GoogleSignIn/Sources/GIDSignInInternalOptions.h b/GoogleSignIn/Sources/GIDSignInInternalOptions.h index ba4b4f9a..e4ac384a 100644 --- a/GoogleSignIn/Sources/GIDSignInInternalOptions.h +++ b/GoogleSignIn/Sources/GIDSignInInternalOptions.h @@ -22,8 +22,6 @@ #import #endif -#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h" - @class GIDConfiguration; @class GIDUserAuth; diff --git a/Samples/ObjC/SignInSample/Source/AppDelegate.m b/Samples/ObjC/SignInSample/Source/AppDelegate.m index 45927392..a458ff4a 100644 --- a/Samples/ObjC/SignInSample/Source/AppDelegate.m +++ b/Samples/ObjC/SignInSample/Source/AppDelegate.m @@ -30,7 +30,7 @@ - (BOOL)application:(UIApplication *)application // succeeds, we'll have a currentUser and the view will be able to draw its UI for the signed-in // state. If the restore fails, currentUser will be nil and we'll draw the signed-out state // prompting the user to sign in. - [GIDSignIn.sharedInstance restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user, + [GIDSignIn.sharedInstance restorePreviousSignInWithCallback:^(GIDUserAuth * _Nullable userAuth, NSError * _Nullable error) { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; SignInViewController *masterViewController = diff --git a/Samples/ObjC/SignInSample/Source/SignInViewController.m b/Samples/ObjC/SignInSample/Source/SignInViewController.m index 009e698c..50544436 100644 --- a/Samples/ObjC/SignInSample/Source/SignInViewController.m +++ b/Samples/ObjC/SignInSample/Source/SignInViewController.m @@ -259,7 +259,7 @@ - (void)updateButtons { - (IBAction)signIn:(id)sender { [GIDSignIn.sharedInstance signInWithConfiguration:_configuration presentingViewController:self - callback:^(GIDGoogleUser * _Nullable user, + callback:^(GIDUserAuth * _Nullable userAuth, NSError * _Nullable error) { if (error) { self->_signInAuthStatus.text = @@ -293,7 +293,7 @@ - (IBAction)disconnect:(id)sender { - (IBAction)addScopes:(id)sender { [GIDSignIn.sharedInstance addScopes:@[ @"https://www.googleapis.com/auth/user.birthday.read" ] presentingViewController:self - callback:^(GIDGoogleUser * _Nullable user, + callback:^(GIDUserAuth * _Nullable userAuth, NSError * _Nullable error) { if (error) { self->_signInAuthStatus.text = [NSString stringWithFormat:@"Status: Failed to add scopes: %@", diff --git a/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift b/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift index afa3083f..2d648d02 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift @@ -26,9 +26,9 @@ struct DaysUntilBirthday: App { ContentView() .environmentObject(authViewModel) .onAppear { - GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in - if let user = user { - self.authViewModel.state = .signedIn(user) + GIDSignIn.sharedInstance.restorePreviousSignIn { userAuth, error in + if let userAuth = userAuth { + self.authViewModel.state = .signedIn(userAuth.user) } else if let error = error { self.authViewModel.state = .signedOut print("There was an error restoring the previous sign-in: \(error)") diff --git a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift index 2a0e88fd..b798fb10 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift @@ -48,12 +48,12 @@ final class GoogleSignInAuthenticator: ObservableObject { } GIDSignIn.sharedInstance.signIn(with: configuration, - presenting: rootViewController) { user, error in - guard let user = user else { + presenting: rootViewController) { userAuth, error in + guard let userAuth = userAuth else { print("Error! \(String(describing: error))") return } - self.authViewModel.state = .signedIn(user) + self.authViewModel.state = .signedIn(userAuth.user) } #elseif os(macOS) @@ -63,12 +63,12 @@ final class GoogleSignInAuthenticator: ObservableObject { } GIDSignIn.sharedInstance.signIn(with: configuration, - presenting: presentingWindow) { user, error in - guard let user = user else { + presenting: presentingWindow) { userAuth, error in + guard let userAuth = userAuth else { print("Error! \(String(describing: error))") return } - self.authViewModel.state = .signedIn(user) + self.authViewModel.state = .signedIn(userAuth.user) } #endif } @@ -102,14 +102,14 @@ final class GoogleSignInAuthenticator: ObservableObject { } GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope], - presenting: rootViewController) { user, error in + presenting: rootViewController) { userAuth, error in if let error = error { print("Found error while adding birthday read scope: \(error).") return } - guard let currentUser = user else { return } - self.authViewModel.state = .signedIn(currentUser) + guard let userAuth = userAuth else { return } + self.authViewModel.state = .signedIn(userAuth.user) completion() } @@ -119,14 +119,14 @@ final class GoogleSignInAuthenticator: ObservableObject { } GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope], - presenting: presentingWindow) { user, error in + presenting: presentingWindow) { userAuth, error in if let error = error { print("Found error while adding birthday read scope: \(error).") return } - guard let currentUser = user else { return } - self.authViewModel.state = .signedIn(currentUser) + guard let userAuth = userAuth else { return } + self.authViewModel.state = .signedIn(userAuth.user) completion() } From 31133abf179ae0c988a03eb68dc651e3c2959eb6 Mon Sep 17 00:00:00 2001 From: pinlu Date: Tue, 12 Jul 2022 18:45:19 -0700 Subject: [PATCH 3/5] Minor improvements 1. Resolve comments 2. Make sure in clients callback one of userAuth and error is nil. --- GoogleSignIn/Sources/GIDSignIn.m | 12 +++++++---- .../Sources/Public/GoogleSignIn/GIDSignIn.h | 20 +++++++++---------- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 14 +++++-------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/GoogleSignIn/Sources/GIDSignIn.m b/GoogleSignIn/Sources/GIDSignIn.m index 62091666..8cbf2966 100644 --- a/GoogleSignIn/Sources/GIDSignIn.m +++ b/GoogleSignIn/Sources/GIDSignIn.m @@ -887,10 +887,14 @@ - (void)addCompletionCallback:(GIDAuthFlow *)authFlow { void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = self->_currentOptions.callback; self->_currentOptions = nil; dispatch_async(dispatch_get_main_queue(), ^{ - OIDAuthState *authState = handlerAuthFlow.authState; - NSString * _Nullable serverAuthCode = [authState.lastTokenResponse.additionalParameters[@"server_code"] copy]; - GIDUserAuth * userAuth = [[GIDUserAuth alloc]initWithGoogleUser:self->_currentUser serverAuthCode:serverAuthCode]; - callback(userAuth, handlerAuthFlow.error); + if (handlerAuthFlow.error) { + callback(nil, handlerAuthFlow.error); + } else { + OIDAuthState *authState = handlerAuthFlow.authState; + NSString * _Nullable serverAuthCode = [authState.lastTokenResponse.additionalParameters[@"server_code"] copy]; + GIDUserAuth * userAuth = [[GIDUserAuth alloc]initWithGoogleUser:self->_currentUser serverAuthCode:serverAuthCode]; + callback(userAuth, nil); + } }); } }]; diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h index 65c409f0..6343894b 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h @@ -173,8 +173,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param presentingViewController The view controller used to present `SFSafariViewContoller` on /// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on /// iOS 13+. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)addScopes:(NSArray *)scopes presentingViewController:(UIViewController *)presentingViewController callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback @@ -190,8 +190,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// /// @param configuration The configuration properties to be used for this flow. /// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; @@ -207,8 +207,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. /// @param hint An optional hint for the authorization server, for example the user's ID or email /// address, to be prefilled if possible. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint @@ -226,8 +226,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param hint An optional hint for the authorization server, for example the user's ID or email /// address, to be prefilled if possible. /// @param additionalScopes An optional array of scopes to request in addition to the basic profile scopes. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow @@ -242,8 +242,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// /// @param scopes An array of scopes to ask the user to consent to. /// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)addScopes:(NSArray *)scopes presentingWindow:(NSWindow *)presentingWindow callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index 64e4e352..2a3d2caf 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -1225,12 +1225,9 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) { [expectation fulfill]; if (userAuth) { - BOOL hasError = authError || modalCancel || tokenError; - if (!hasError) { XCTAssertEqualObjects(userAuth.serverAuthCode, kServerAuthCode); - } } else { - XCTAssertNotNil(error, @"should have an error if the userAuth is nil"); + XCTAssertNotNil(error, @"Should have an error if the userAuth is nil"); } XCTAssertFalse(self->_callbackCalled, @"callback already called"); self->_callbackCalled = YES; @@ -1293,7 +1290,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse]; [[[_authState expect] andReturn:authResponse] lastAuthorizationResponse]; } - + // Simulate auth endpoint response if (modalCancel) { NSError *error = [NSError errorWithDomain:OIDGeneralErrorDomain @@ -1326,9 +1323,6 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow // OIDTokenCallback if (tokenError) { [[_authState expect] updateWithTokenResponse:nil error:tokenError]; - - // CompletionCallback - [[[_authState expect] andReturn:nil] lastTokenResponse]; } else { [[_authState expect] updateWithTokenResponse:[OCMArg any] error:nil]; } @@ -1360,7 +1354,9 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow } // CompletionCallback - mock server auth code parsing - [[[_authState expect] andReturn:tokenResponse] lastTokenResponse]; + if (!keychainError) { + [[[_authState expect] andReturn:tokenResponse] lastTokenResponse]; + } if (restoredSignIn && !oldAccessToken) { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; From 023f285af7702263b4047c520e8303c791046224 Mon Sep 17 00:00:00 2001 From: pinlu Date: Wed, 13 Jul 2022 15:16:27 -0700 Subject: [PATCH 4/5] Change the restorePreviousSignIn callback --- GoogleSignIn/Sources/GIDSignIn.m | 11 +++++++++-- GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h | 2 +- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 8 ++++---- Samples/ObjC/SignInSample/Source/AppDelegate.m | 2 +- .../DaysUntilBirthday/Shared/DaysUntilBirthday.swift | 6 +++--- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/GoogleSignIn/Sources/GIDSignIn.m b/GoogleSignIn/Sources/GIDSignIn.m index 8cbf2966..434eebc8 100644 --- a/GoogleSignIn/Sources/GIDSignIn.m +++ b/GoogleSignIn/Sources/GIDSignIn.m @@ -20,6 +20,7 @@ #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h" #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h" #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h" +#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDUserAuth.h" #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h" #import "GoogleSignIn/Sources/GIDSignInPreferences.h" @@ -188,8 +189,14 @@ - (BOOL)hasPreviousSignIn { return [authState isAuthorized]; } -- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { - [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback:callback]]; +- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDGoogleUser *_Nullable user, NSError *_Nullable error))callback { + [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback:^(GIDUserAuth *userAuth, NSError *error){ + if (userAuth) { + callback(userAuth.user, nil); + } else { + callback(nil, error); + } + }]]; } - (BOOL)restorePreviousSignInNoRefresh { diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h index 6343894b..007338f8 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h @@ -90,7 +90,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// /// @param callback The `GIDSignInCallback` block that is called on completion. This block will be /// called asynchronously on the main queue. -- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; +- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDGoogleUser *_Nullable user, NSError *_Nullable error))callback; /// Marks current user as being in the signed out state. - (void)signOut; diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index 2a3d2caf..46e4d36e 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -456,7 +456,7 @@ - (void)testRestorePreviousSignInWhenSignedOut { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called."]; - [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, + [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser *_Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNotNil(error, @"error should not have been nil"); @@ -1309,7 +1309,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow if (restoredSignIn && oldAccessToken) { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; - [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, + [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser *_Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error, @"should have no error"); @@ -1360,7 +1360,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow if (restoredSignIn && !oldAccessToken) { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; - [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, + [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser *_Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error, @"should have no error"); @@ -1401,7 +1401,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; - [_signIn restorePreviousSignInWithCallback:^(GIDUserAuth *_Nullable userAuth, + [_signIn restorePreviousSignInWithCallback:^(GIDGoogleUser *_Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error, @"should have no error"); diff --git a/Samples/ObjC/SignInSample/Source/AppDelegate.m b/Samples/ObjC/SignInSample/Source/AppDelegate.m index a458ff4a..45927392 100644 --- a/Samples/ObjC/SignInSample/Source/AppDelegate.m +++ b/Samples/ObjC/SignInSample/Source/AppDelegate.m @@ -30,7 +30,7 @@ - (BOOL)application:(UIApplication *)application // succeeds, we'll have a currentUser and the view will be able to draw its UI for the signed-in // state. If the restore fails, currentUser will be nil and we'll draw the signed-out state // prompting the user to sign in. - [GIDSignIn.sharedInstance restorePreviousSignInWithCallback:^(GIDUserAuth * _Nullable userAuth, + [GIDSignIn.sharedInstance restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; SignInViewController *masterViewController = diff --git a/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift b/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift index 2d648d02..afa3083f 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/DaysUntilBirthday.swift @@ -26,9 +26,9 @@ struct DaysUntilBirthday: App { ContentView() .environmentObject(authViewModel) .onAppear { - GIDSignIn.sharedInstance.restorePreviousSignIn { userAuth, error in - if let userAuth = userAuth { - self.authViewModel.state = .signedIn(userAuth.user) + GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in + if let user = user { + self.authViewModel.state = .signedIn(user) } else if let error = error { self.authViewModel.state = .signedOut print("There was an error restoring the previous sign-in: \(error)") From ba214ce8c5efc4d87b91522a407c4c9a8196e2a0 Mon Sep 17 00:00:00 2001 From: pinlu Date: Thu, 14 Jul 2022 17:08:22 -0700 Subject: [PATCH 5/5] Style improvement. --- GoogleSignIn/Sources/GIDSignIn.m | 30 ++++++----- .../Sources/GIDSignInInternalOptions.h | 18 ++++--- .../Sources/GIDSignInInternalOptions.m | 17 +++--- GoogleSignIn/Sources/GIDSignIn_Private.h | 4 ++ .../Sources/Public/GoogleSignIn/GIDSignIn.h | 52 +++++++++++-------- .../Tests/Unit/GIDSignInInternalOptionsTest.m | 9 ++-- .../Source/SignInViewController.m | 10 ++-- 7 files changed, 83 insertions(+), 57 deletions(-) diff --git a/GoogleSignIn/Sources/GIDSignIn.m b/GoogleSignIn/Sources/GIDSignIn.m index 434eebc8..c9cc844b 100644 --- a/GoogleSignIn/Sources/GIDSignIn.m +++ b/GoogleSignIn/Sources/GIDSignIn.m @@ -189,8 +189,10 @@ - (BOOL)hasPreviousSignIn { return [authState isAuthorized]; } -- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDGoogleUser *_Nullable user, NSError *_Nullable error))callback { - [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback:^(GIDUserAuth *userAuth, NSError *error){ +- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDGoogleUser *_Nullable user, + NSError *_Nullable error))callback { + [self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback: + ^(GIDUserAuth *userAuth, NSError *error) { if (userAuth) { callback(userAuth.user, nil); } else { @@ -225,7 +227,7 @@ - (BOOL)restorePreviousSignInNoRefresh { - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingViewController:presentingViewController @@ -239,7 +241,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingViewController:presentingViewController @@ -252,7 +254,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { [self signInWithConfiguration:configuration presentingViewController:presentingViewController hint:nil @@ -261,7 +263,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)addScopes:(NSArray *)scopes presentingViewController:(UIViewController *)presentingViewController - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { // A currentUser must be available in order to complete this flow. if (!self.currentUser) { // No currentUser is set, notify callback of failure. @@ -318,7 +320,7 @@ - (void)addScopes:(NSArray *)scopes - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingWindow:presentingWindow @@ -330,7 +332,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { [self signInWithConfiguration:configuration presentingWindow:presentingWindow hint:nil @@ -341,7 +343,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration presentingWindow:presentingWindow @@ -354,7 +356,7 @@ - (void)signInWithConfiguration:(GIDConfiguration *)configuration - (void)addScopes:(NSArray *)scopes presentingWindow:(NSWindow *)presentingWindow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable GIDUserAuthCallback)callback { // A currentUser must be available in order to complete this flow. if (!self.currentUser) { // No currentUser is set, notify callback of failure. @@ -549,7 +551,7 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options { if (options.callback) { self->_currentOptions = nil; dispatch_async(dispatch_get_main_queue(), ^{ - GIDUserAuth *userAuth = [[GIDUserAuth alloc]initWithGoogleUser:self->_currentUser serverAuthCode:nil]; + GIDUserAuth *userAuth = [[GIDUserAuth alloc] initWithGoogleUser:self->_currentUser serverAuthCode:nil]; options.callback(userAuth, nil); }); } @@ -891,15 +893,15 @@ - (void)addCompletionCallback:(GIDAuthFlow *)authFlow { [authFlow addCallback:^() { GIDAuthFlow *handlerAuthFlow = weakAuthFlow; if (self->_currentOptions.callback) { - void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = self->_currentOptions.callback; + GIDUserAuthCallback callback = self->_currentOptions.callback; self->_currentOptions = nil; dispatch_async(dispatch_get_main_queue(), ^{ if (handlerAuthFlow.error) { callback(nil, handlerAuthFlow.error); } else { OIDAuthState *authState = handlerAuthFlow.authState; - NSString * _Nullable serverAuthCode = [authState.lastTokenResponse.additionalParameters[@"server_code"] copy]; - GIDUserAuth * userAuth = [[GIDUserAuth alloc]initWithGoogleUser:self->_currentUser serverAuthCode:serverAuthCode]; + NSString *_Nullable serverAuthCode = [authState.lastTokenResponse.additionalParameters[@"server_code"] copy]; + GIDUserAuth *userAuth = [[GIDUserAuth alloc] initWithGoogleUser:self->_currentUser serverAuthCode:serverAuthCode]; callback(userAuth, nil); } }); diff --git a/GoogleSignIn/Sources/GIDSignInInternalOptions.h b/GoogleSignIn/Sources/GIDSignInInternalOptions.h index e4ac384a..e8d456ad 100644 --- a/GoogleSignIn/Sources/GIDSignInInternalOptions.h +++ b/GoogleSignIn/Sources/GIDSignInInternalOptions.h @@ -54,7 +54,8 @@ NS_ASSUME_NONNULL_BEGIN #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST /// The callback block to be called at the completion of the flow. -@property(nonatomic, readonly, nullable) void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error); +@property(nonatomic, readonly, nullable) void (^callback)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error); /// The scopes to be used during the flow. @property(nonatomic, copy, nullable) NSArray *scopes; @@ -68,32 +69,37 @@ NS_ASSUME_NONNULL_BEGIN presentingViewController:(nullable UIViewController *)presentingViewController loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingViewController:(nullable UIViewController *)presentingViewController loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; #elif TARGET_OS_OSX + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST /// Creates the options to sign in silently. -+ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; ++ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; /// Creates options with the same values as the receiver, except for the "extra parameters", and /// continuation flag, which are replaced by the arguments passed to this method. diff --git a/GoogleSignIn/Sources/GIDSignInInternalOptions.m b/GoogleSignIn/Sources/GIDSignInInternalOptions.m index 4962eaa5..617c0291 100644 --- a/GoogleSignIn/Sources/GIDSignInInternalOptions.m +++ b/GoogleSignIn/Sources/GIDSignInInternalOptions.m @@ -31,14 +31,16 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback { #elif TARGET_OS_OSX + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow scopes:(nullable NSArray *)scopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback { #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init]; @@ -64,13 +66,15 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con presentingViewController:(nullable UIViewController *)presentingViewController loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback { #elif TARGET_OS_OSX + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration presentingWindow:(nullable NSWindow *)presentingWindow loginHint:(nullable NSString *)loginHint addScopesFlow:(BOOL)addScopesFlow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback { #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST GIDSignInInternalOptions *options = [self defaultOptionsWithConfiguration:configuration #if TARGET_OS_IOS || TARGET_OS_MACCATALYST @@ -85,7 +89,8 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con return options; } -+ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { ++ (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback { GIDSignInInternalOptions *options = [self defaultOptionsWithConfiguration:nil #if TARGET_OS_IOS || TARGET_OS_MACCATALYST presentingViewController:nil @@ -93,7 +98,7 @@ + (instancetype)silentOptionsWithCallback:(void (^)(GIDUserAuth *_Nullable userA presentingWindow:nil #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST loginHint:nil - addScopesFlow:NO + addScopesFlow:NO callback:callback]; if (options) { options->_interactive = NO; diff --git a/GoogleSignIn/Sources/GIDSignIn_Private.h b/GoogleSignIn/Sources/GIDSignIn_Private.h index 5d1a6da5..301fcda9 100644 --- a/GoogleSignIn/Sources/GIDSignIn_Private.h +++ b/GoogleSignIn/Sources/GIDSignIn_Private.h @@ -21,6 +21,10 @@ NS_ASSUME_NONNULL_BEGIN @class GIDGoogleUser; @class GIDSignInInternalOptions; +// Represents a callback block that takes a `GIDUserAuth` on success or an error if the operation +// was unsuccessful. +typedef void (^GIDUserAuthCallback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error); + // Private |GIDSignIn| methods that are used internally in this SDK and other Google SDKs. @interface GIDSignIn () diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h index 007338f8..27367565 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h @@ -88,9 +88,10 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// Attempts to restore a previously authenticated user without interaction. /// -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. -- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDGoogleUser *_Nullable user, NSError *_Nullable error))callback; +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. +- (void)restorePreviousSignInWithCallback:(nullable void (^)(GIDGoogleUser *_Nullable user, + NSError *_Nullable error))callback; /// Marks current user as being in the signed out state. - (void)signOut; @@ -114,11 +115,12 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param presentingViewController The view controller used to present `SFSafariViewContoller` on /// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on /// iOS 13+. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback NS_EXTENSION_UNAVAILABLE("The sign-in flow is not supported in App Extensions."); /// Starts an interactive sign-in flow on iOS using the provided configuration and a login hint. @@ -134,12 +136,13 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// iOS 13+. /// @param hint An optional hint for the authorization server, for example the user's ID or email /// address, to be prefilled if possible. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback NS_EXTENSION_UNAVAILABLE("The sign-in flow is not supported in App Extensions."); /// Starts an interactive sign-in flow on iOS using the provided configuration and a login hint. @@ -155,14 +158,14 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param hint An optional hint for the authorization server, for example the user's ID or email /// address, to be prefilled if possible. /// @param additionalScopes An optional array of scopes to request in addition to the basic profile scopes. -/// @param callback The `GIDSignInCallback` block that is called on completion. This block will be -/// called asynchronously on the main queue. - +/// @param callback The block that is called on completion. This block will be called asynchronously on +/// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; /// Starts an interactive consent flow on iOS to add scopes to the current user's grants. /// @@ -173,11 +176,12 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param presentingViewController The view controller used to present `SFSafariViewContoller` on /// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on /// iOS 13+. -/// @param callback The block that is called on completion. This block will be called asynchronously on +/// @param callback The block that is called on completion. This block will be called asynchronously on /// the main queue. - (void)addScopes:(NSArray *)scopes presentingViewController:(UIViewController *)presentingViewController - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions."); #elif TARGET_OS_OSX @@ -190,11 +194,12 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// /// @param configuration The configuration properties to be used for this flow. /// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. -/// @param callback The block that is called on completion. This block will be called asynchronously on +/// @param callback The block that is called on completion. This block will be called asynchronously on /// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; /// Starts an interactive sign-in flow on macOS using the provided configuration and a login hint. /// @@ -207,12 +212,13 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. /// @param hint An optional hint for the authorization server, for example the user's ID or email /// address, to be prefilled if possible. -/// @param callback The block that is called on completion. This block will be called asynchronously on +/// @param callback The block that is called on completion. This block will be called asynchronously on /// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; /// Starts an interactive sign-in flow on macOS using the provided configuration and a login hint. /// @@ -226,14 +232,15 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param hint An optional hint for the authorization server, for example the user's ID or email /// address, to be prefilled if possible. /// @param additionalScopes An optional array of scopes to request in addition to the basic profile scopes. -/// @param callback The block that is called on completion. This block will be called asynchronously on +/// @param callback The block that is called on completion. This block will be called asynchronously on /// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingWindow:(NSWindow *)presentingWindow hint:(nullable NSString *)hint additionalScopes:(nullable NSArray *)additionalScopes - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; /// Starts an interactive consent flow on macOS to add scopes to the current user's grants. /// @@ -246,7 +253,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// the main queue. - (void)addScopes:(NSArray *)scopes presentingWindow:(NSWindow *)presentingWindow - callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback; + callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))callback; #endif diff --git a/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m b/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m index 212125d9..0fddd248 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInInternalOptionsTest.m @@ -37,8 +37,8 @@ - (void)testDefaultOptions { id presentingWindow = OCMStrictClassMock([NSWindow class]); #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST NSString *loginHint = @"login_hint"; - void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) {}; - + void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = + ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) {}; GIDSignInInternalOptions *options = [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration #if TARGET_OS_IOS || TARGET_OS_MACCATALYST @@ -47,7 +47,7 @@ - (void)testDefaultOptions { presentingWindow:presentingWindow #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST loginHint:loginHint - addScopesFlow:NO + addScopesFlow:NO callback:callback]; XCTAssertTrue(options.interactive); XCTAssertFalse(options.continuation); @@ -63,7 +63,8 @@ - (void)testDefaultOptions { } - (void)testSilentOptions { - void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) {}; + void (^callback)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error) = + ^(GIDUserAuth *_Nullable userAuth, NSError * _Nullable error) {}; GIDSignInInternalOptions *options = [GIDSignInInternalOptions silentOptionsWithCallback:callback]; XCTAssertFalse(options.interactive); XCTAssertFalse(options.continuation); diff --git a/Samples/ObjC/SignInSample/Source/SignInViewController.m b/Samples/ObjC/SignInSample/Source/SignInViewController.m index 50544436..d3fad276 100644 --- a/Samples/ObjC/SignInSample/Source/SignInViewController.m +++ b/Samples/ObjC/SignInSample/Source/SignInViewController.m @@ -259,8 +259,8 @@ - (void)updateButtons { - (IBAction)signIn:(id)sender { [GIDSignIn.sharedInstance signInWithConfiguration:_configuration presentingViewController:self - callback:^(GIDUserAuth * _Nullable userAuth, - NSError * _Nullable error) { + callback:^(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error) { if (error) { self->_signInAuthStatus.text = [NSString stringWithFormat:@"Status: Authentication error: %@", error]; @@ -278,7 +278,7 @@ - (IBAction)signOut:(id)sender { } - (IBAction)disconnect:(id)sender { - [GIDSignIn.sharedInstance disconnectWithCallback:^(NSError * _Nullable error) { + [GIDSignIn.sharedInstance disconnectWithCallback:^(NSError *_Nullable error) { if (error) { self->_signInAuthStatus.text = [NSString stringWithFormat:@"Status: Failed to disconnect: %@", error]; @@ -293,8 +293,8 @@ - (IBAction)disconnect:(id)sender { - (IBAction)addScopes:(id)sender { [GIDSignIn.sharedInstance addScopes:@[ @"https://www.googleapis.com/auth/user.birthday.read" ] presentingViewController:self - callback:^(GIDUserAuth * _Nullable userAuth, - NSError * _Nullable error) { + callback:^(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error) { if (error) { self->_signInAuthStatus.text = [NSString stringWithFormat:@"Status: Failed to add scopes: %@", error];