From d8e707dc066e2a2a1aa4194e9067da85a55fbb18 Mon Sep 17 00:00:00 2001 From: pinlu Date: Fri, 14 Oct 2022 15:55:30 -0700 Subject: [PATCH 1/9] Move the method `addScopes` into GIDGoogleUser API --- GoogleSignIn/Sources/GIDGoogleUser.m | 25 +++++++++++ GoogleSignIn/Sources/GIDSignIn_Private.h | 45 +++++++++++++++++++ .../Public/GoogleSignIn/GIDGoogleUser.h | 45 +++++++++++++++++++ .../Sources/Public/GoogleSignIn/GIDSignIn.h | 31 ------------- .../Source/SignInViewController.m | 11 ++--- .../Services/GoogleSignInAuthenticator.swift | 5 ++- 6 files changed, 124 insertions(+), 38 deletions(-) diff --git a/GoogleSignIn/Sources/GIDGoogleUser.m b/GoogleSignIn/Sources/GIDGoogleUser.m index 0c18a49e..ea3faca2 100644 --- a/GoogleSignIn/Sources/GIDGoogleUser.m +++ b/GoogleSignIn/Sources/GIDGoogleUser.m @@ -17,6 +17,7 @@ #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h" #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h" +#import "GoogleSignIn/Sources/GIDSignIn_Private.h" #import "GoogleSignIn/Sources/GIDAppAuthFetcherAuthorizationWithEMMSupport.h" #import "GoogleSignIn/Sources/GIDAuthentication.h" @@ -181,6 +182,30 @@ - (OIDAuthState *) authState{ return ((GTMAppAuthFetcherAuthorization *)self.fetcherAuthorizer).authState; } +#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST + +- (void)addScopes:(NSArray *)scopes + presentingViewController:(UIViewController *)presentingViewController + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))completion { + [[GIDSignIn sharedInstance] addScopes:scopes + presentingViewController:presentingViewController + completion:completion]; +} + +#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST + +- (void)addScopes:(NSArray *)scopes + presentingWindow:(NSWindow *)presentingWindow + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))completion { + [[GIDSignIn sharedInstance] addScopes:scopes + presentingViewController:presentingWindow + completion:completion]; +} + +#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST + #pragma mark - Private Methods #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST diff --git a/GoogleSignIn/Sources/GIDSignIn_Private.h b/GoogleSignIn/Sources/GIDSignIn_Private.h index 190a629c..68abf0a5 100644 --- a/GoogleSignIn/Sources/GIDSignIn_Private.h +++ b/GoogleSignIn/Sources/GIDSignIn_Private.h @@ -14,8 +14,16 @@ * limitations under the License. */ +#import + #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h" +#if __has_include() +#import +#elif __has_include() +#import +#endif + NS_ASSUME_NONNULL_BEGIN @class GIDGoogleUser; @@ -44,6 +52,43 @@ typedef void (^GIDUserAuthCompletion)(GIDUserAuth *_Nullable userAuth, NSError * // @return NO if there is no user restored from the keychain. - (BOOL)restorePreviousSignInNoRefresh; +#if TARGET_OS_IOS || TARGET_OS_MACCATALYST + +// Starts an interactive consent flow on iOS to add scopes to the current user's grants. +// +// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` +// instance will be returned reflecting the new scopes and saved sign-in state will be updated. +// +// @param scopes The scopes to ask the user to consent to. +// @param presentingViewController The view controller used to present `SFSafariViewContoller` on +// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on +// iOS 13+. +// @param completion The block that is called on completion. This block will be called asynchronously +// on the main queue. +- (void)addScopes:(NSArray *)scopes + presentingViewController:(UIViewController *)presentingViewController + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))completion + NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions."); + +#elif TARGET_OS_OSX + +// Starts an interactive consent flow on macOS to add scopes to the current user's grants +// +// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` +// instance will be returned reflecting the new scopes and saved sign-in state will be updated. +// +// @param scopes An array of scopes to ask the user to consent to. +// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. +// @param completion The block that is called on completion. This block will be called asynchronously +// on the main queue. +- (void)addScopes:(NSArray *)scopes + presentingWindow:(NSWindow *)presentingWindow + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))completion; + +#endif + @end NS_ASSUME_NONNULL_END diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h index 558d9a9f..b5ede573 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h @@ -15,6 +15,13 @@ */ #import +#import + +#if __has_include() +#import +#elif __has_include() +#import +#endif // We have to import GTMAppAuth because forward declaring the protocol does // not generate the `fetcherAuthorizer` property below for Swift. @@ -25,6 +32,7 @@ #endif @class GIDConfiguration; +@class GIDUserAuth; @class GIDToken; @class GIDProfileData; @@ -71,6 +79,43 @@ NS_ASSUME_NONNULL_BEGIN - (void)doWithFreshTokens:(void (^)(GIDGoogleUser *_Nullable user, NSError *_Nullable error))completion; +#if TARGET_OS_IOS || TARGET_OS_MACCATALYST + +/// Starts an interactive consent flow on iOS to add scopes to the current user's grants. +/// +/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` +/// instance will be returned reflecting the new scopes and saved sign-in state will be updated. +/// +/// @param scopes The scopes to ask the user to consent to. +/// @param presentingViewController The view controller used to present `SFSafariViewContoller` on +/// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on +/// iOS 13+. +/// @param completion The block that is called on completion. This block will be called asynchronously +/// on the main queue. +- (void)addScopes:(NSArray *)scopes + presentingViewController:(UIViewController *)presentingViewController + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))completion + NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions."); + +#elif TARGET_OS_OSX + +/// Starts an interactive consent flow on macOS to add scopes to the current user's grants. +/// +/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` +/// instance will be returned reflecting the new scopes and saved sign-in state will be updated. +/// +/// @param scopes An array of scopes to ask the user to consent to. +/// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. +/// @param completion The block that is called on completion. This block will be called asynchronously +/// on the main queue. +- (void)addScopes:(NSArray *)scopes + presentingWindow:(NSWindow *)presentingWindow + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))completion; + +#endif + @end NS_ASSUME_NONNULL_END diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h index 8fad0549..7f602459 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h @@ -165,23 +165,6 @@ typedef void (^GIDDisconnectCompletion)(NSError *_Nullable error); completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))completion; -/// Starts an interactive consent flow on iOS to add scopes to the current user's grants. -/// -/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` -/// instance will be returned reflecting the new scopes and saved sign-in state will be updated. -/// -/// @param scopes The scopes to ask the user to consent to. -/// @param presentingViewController The view controller used to present `SFSafariViewContoller` on -/// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on -/// iOS 13+. -/// @param completion The block that is called on completion. This block will be called asynchronously -/// on the main queue. -- (void)addScopes:(NSArray *)scopes - presentingViewController:(UIViewController *)presentingViewController - completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, - NSError *_Nullable error))completion - NS_EXTENSION_UNAVAILABLE("The add scopes flow is not supported in App Extensions."); - #elif TARGET_OS_OSX /// Starts an interactive sign-in flow on macOS. /// @@ -233,20 +216,6 @@ typedef void (^GIDDisconnectCompletion)(NSError *_Nullable error); completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))completion; -/// Starts an interactive consent flow on macOS to add scopes to the current user's grants. -/// -/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` -/// instance will be returned reflecting the new scopes and saved sign-in state will be updated. -/// -/// @param scopes An array of scopes to ask the user to consent to. -/// @param presentingWindow The window used to supply `presentationContextProvider` for `ASWebAuthenticationSession`. -/// @param completion The block that is called on completion. This block will be called asynchronously -/// on the main queue. -- (void)addScopes:(NSArray *)scopes - presentingWindow:(NSWindow *)presentingWindow - completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, - NSError *_Nullable error))completion; - #endif @end diff --git a/Samples/ObjC/SignInSample/Source/SignInViewController.m b/Samples/ObjC/SignInSample/Source/SignInViewController.m index e2b3d077..8524bb26 100644 --- a/Samples/ObjC/SignInSample/Source/SignInViewController.m +++ b/Samples/ObjC/SignInSample/Source/SignInViewController.m @@ -248,7 +248,7 @@ - (void)updateButtons { - (IBAction)signIn:(id)sender { [GIDSignIn.sharedInstance signInWithPresentingViewController:self - completion:^(GIDGoogleUser *user, + completion:^(GIDUserAuth *userAuth, NSError *error) { if (error) { self->_signInAuthStatus.text = @@ -280,10 +280,11 @@ - (IBAction)disconnect:(id)sender { } - (IBAction)addScopes:(id)sender { - [GIDSignIn.sharedInstance addScopes:@[ @"https://www.googleapis.com/auth/user.birthday.read" ] - presentingViewController:self - completion:^(GIDUserAuth *_Nullable userAuth, - NSError *_Nullable error) { + GIDGoogleUser *currentUser = GIDSignIn.sharedInstance.currentUser; + [currentUser addScopes:@[ @"https://www.googleapis.com/auth/user.birthday.read" ] + presentingViewController:self + completion:^(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error) { if (error) { self->_signInAuthStatus.text = [NSString stringWithFormat:@"Status: Failed to add scopes: %@", error]; diff --git a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift index ce20106f..acf788c5 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift @@ -88,8 +88,9 @@ final class GoogleSignInAuthenticator: ObservableObject { fatalError("No root view controller!") } - GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope], - presenting: rootViewController) { userAuth, error in + GIDSignIn.sharedInstance.currentUser?.addScopes( + [BirthdayLoader.birthdayReadScope], presenting: rootViewController) + { userAuth, error in if let error = error { print("Found error while adding birthday read scope: \(error).") return From ba4095f60917f4d47d378e47e0161942335ce052 Mon Sep 17 00:00:00 2001 From: pinlu Date: Fri, 14 Oct 2022 16:51:03 -0700 Subject: [PATCH 2/9] Minor improvement --- GoogleSignIn/Sources/GIDGoogleUser.m | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/GoogleSignIn/Sources/GIDGoogleUser.m b/GoogleSignIn/Sources/GIDGoogleUser.m index ea3faca2..6246b366 100644 --- a/GoogleSignIn/Sources/GIDGoogleUser.m +++ b/GoogleSignIn/Sources/GIDGoogleUser.m @@ -17,12 +17,12 @@ #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h" #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h" -#import "GoogleSignIn/Sources/GIDSignIn_Private.h" #import "GoogleSignIn/Sources/GIDAppAuthFetcherAuthorizationWithEMMSupport.h" #import "GoogleSignIn/Sources/GIDAuthentication.h" #import "GoogleSignIn/Sources/GIDEMMSupport.h" #import "GoogleSignIn/Sources/GIDProfileData_Private.h" +#import "GoogleSignIn/Sources/GIDSignIn_Private.h" #import "GoogleSignIn/Sources/GIDSignInPreferences.h" #import "GoogleSignIn/Sources/GIDToken_Private.h" @@ -188,9 +188,9 @@ - (void)addScopes:(NSArray *)scopes presentingViewController:(UIViewController *)presentingViewController completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))completion { - [[GIDSignIn sharedInstance] addScopes:scopes - presentingViewController:presentingViewController - completion:completion]; + [GIDSignIn.sharedInstance addScopes:scopes + presentingViewController:presentingViewController + completion:completion]; } #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST @@ -199,9 +199,9 @@ - (void)addScopes:(NSArray *)scopes presentingWindow:(NSWindow *)presentingWindow completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))completion { - [[GIDSignIn sharedInstance] addScopes:scopes - presentingViewController:presentingWindow - completion:completion]; + [GIDSignIn.sharedInstance addScopes:scopes + presentingViewController:presentingWindow + completion:completion]; } #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST From 3d0f25dd476e3bf89e2c41961875907cba47e7a1 Mon Sep 17 00:00:00 2001 From: pinlu Date: Mon, 17 Oct 2022 10:56:36 -0700 Subject: [PATCH 3/9] Fix typo --- GoogleSignIn/Sources/GIDGoogleUser.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GoogleSignIn/Sources/GIDGoogleUser.m b/GoogleSignIn/Sources/GIDGoogleUser.m index 6246b366..8b70277f 100644 --- a/GoogleSignIn/Sources/GIDGoogleUser.m +++ b/GoogleSignIn/Sources/GIDGoogleUser.m @@ -200,7 +200,7 @@ - (void)addScopes:(NSArray *)scopes completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))completion { [GIDSignIn.sharedInstance addScopes:scopes - presentingViewController:presentingWindow + presentingWindow:presentingWindow completion:completion]; } From c5a6797ea0580f7ca7bdf050527f7004b636fed5 Mon Sep 17 00:00:00 2001 From: pinlu Date: Mon, 17 Oct 2022 11:36:22 -0700 Subject: [PATCH 4/9] Fix Demo app and improve style --- GoogleSignIn/Sources/GIDSignIn_Private.h | 4 ++-- .../ObjC/SignInSample/Source/SignInViewController.m | 4 ++-- .../Shared/Services/GoogleSignInAuthenticator.swift | 11 ++++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/GoogleSignIn/Sources/GIDSignIn_Private.h b/GoogleSignIn/Sources/GIDSignIn_Private.h index 68abf0a5..0362d185 100644 --- a/GoogleSignIn/Sources/GIDSignIn_Private.h +++ b/GoogleSignIn/Sources/GIDSignIn_Private.h @@ -83,8 +83,8 @@ typedef void (^GIDUserAuthCompletion)(GIDUserAuth *_Nullable userAuth, NSError * // @param completion The block that is called on completion. This block will be called asynchronously // on the main queue. - (void)addScopes:(NSArray *)scopes - presentingWindow:(NSWindow *)presentingWindow - completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + presentingWindow:(NSWindow *)presentingWindow + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))completion; #endif diff --git a/Samples/ObjC/SignInSample/Source/SignInViewController.m b/Samples/ObjC/SignInSample/Source/SignInViewController.m index 8524bb26..038f754a 100644 --- a/Samples/ObjC/SignInSample/Source/SignInViewController.m +++ b/Samples/ObjC/SignInSample/Source/SignInViewController.m @@ -282,8 +282,8 @@ - (IBAction)disconnect:(id)sender { - (IBAction)addScopes:(id)sender { GIDGoogleUser *currentUser = GIDSignIn.sharedInstance.currentUser; [currentUser addScopes:@[ @"https://www.googleapis.com/auth/user.birthday.read" ] - presentingViewController:self - completion:^(GIDUserAuth *_Nullable userAuth, + presentingViewController:self + completion:^(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/Services/GoogleSignInAuthenticator.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift index acf788c5..73d04866 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift @@ -83,14 +83,15 @@ final class GoogleSignInAuthenticator: ObservableObject { /// - note: Successful requests will update the `authViewModel.state` with a new current user that /// has the granted scope. func addBirthdayReadScope(completion: @escaping () -> Void) { + let currentUser = GIDSignIn.sharedInstance.currentUser + #if os(iOS) guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else { fatalError("No root view controller!") } - GIDSignIn.sharedInstance.currentUser?.addScopes( - [BirthdayLoader.birthdayReadScope], presenting: rootViewController) - { userAuth, error in + currentUser?.addScopes([BirthdayLoader.birthdayReadScope], + presenting: rootViewController) { userAuth, error in if let error = error { print("Found error while adding birthday read scope: \(error).") return @@ -106,8 +107,8 @@ final class GoogleSignInAuthenticator: ObservableObject { fatalError("No presenting window!") } - GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope], - presenting: presentingWindow) { userAuth, error in + currentUser?.addScopes([BirthdayLoader.birthdayReadScope], + presenting: presentingWindow) { userAuth, error in if let error = error { print("Found error while adding birthday read scope: \(error).") return From e07017284885d948031e01ccef3ae6331e2dda8a Mon Sep 17 00:00:00 2001 From: pinlu Date: Mon, 17 Oct 2022 16:11:00 -0700 Subject: [PATCH 5/9] Fix Indentation --- .../Shared/Services/GoogleSignInAuthenticator.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift index 73d04866..65b2ae51 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift @@ -108,7 +108,7 @@ final class GoogleSignInAuthenticator: ObservableObject { } currentUser?.addScopes([BirthdayLoader.birthdayReadScope], - presenting: presentingWindow) { userAuth, error in + presenting: presentingWindow) { userAuth, error in if let error = error { print("Found error while adding birthday read scope: \(error).") return From e957ca7c22f70faac043c826cb9d5a8cf35783e0 Mon Sep 17 00:00:00 2001 From: pinlu Date: Tue, 18 Oct 2022 10:48:03 -0700 Subject: [PATCH 6/9] Rename GIDGoogleUser API `doWIthFreshTokens:` Rename this method to `refreshTokensWithCompletion:` --- GoogleSignIn/Sources/GIDGoogleUser.m | 2 +- GoogleSignIn/Sources/GIDSignIn.m | 2 +- .../Public/GoogleSignIn/GIDGoogleUser.h | 4 +-- GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m | 32 +++++++++---------- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 2 +- .../Shared/Services/BirthdayLoader.swift | 2 +- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/GoogleSignIn/Sources/GIDGoogleUser.m b/GoogleSignIn/Sources/GIDGoogleUser.m index 8b70277f..33b60693 100644 --- a/GoogleSignIn/Sources/GIDGoogleUser.m +++ b/GoogleSignIn/Sources/GIDGoogleUser.m @@ -107,7 +107,7 @@ - (GIDConfiguration *)configuration { return _cachedConfiguration; } -- (void)doWithFreshTokens:(GIDGoogleUserCompletion)completion { +- (void)refreshTokensWithCompletion:(GIDGoogleUserCompletion)completion { if (!([self.accessToken.expirationDate timeIntervalSinceNow] < kMinimalTimeToExpire || (self.idToken && [self.idToken.expirationDate timeIntervalSinceNow] < kMinimalTimeToExpire))) { dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/GoogleSignIn/Sources/GIDSignIn.m b/GoogleSignIn/Sources/GIDSignIn.m index cde8e205..0b8c8b84 100644 --- a/GoogleSignIn/Sources/GIDSignIn.m +++ b/GoogleSignIn/Sources/GIDSignIn.m @@ -546,7 +546,7 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options { // If this is a non-interactive flow, use cached authentication if possible. if (!options.interactive && _currentUser) { - [_currentUser doWithFreshTokens:^(GIDGoogleUser *unused, NSError *error) { + [_currentUser refreshTokensWithCompletion:^(GIDGoogleUser *unused, NSError *error) { if (error) { [self authenticateWithOptions:options]; } else { diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h index b5ede573..4fbcf5dd 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h @@ -76,8 +76,8 @@ NS_ASSUME_NONNULL_BEGIN /// /// @param completion A completion block that takes a `GIDGoogleUser` or an error if the attempt to /// refresh tokens was unsuccessful. The block will be called asynchronously on the main queue. -- (void)doWithFreshTokens:(void (^)(GIDGoogleUser *_Nullable user, - NSError *_Nullable error))completion; +- (void)refreshTokensWithCompletion:(void (^)(GIDGoogleUser *_Nullable user, + NSError *_Nullable error))completion; #if TARGET_OS_IOS || TARGET_OS_MACCATALYST diff --git a/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m b/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m index 488d3f63..1b6c3652 100644 --- a/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m +++ b/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m @@ -227,9 +227,9 @@ - (void)testFetcherAuthorizer_returnTheSameInstance { XCTAssertIdentical(fetcherAuthorizer, fetcherAuthorizer2); } -#pragma mark - Test `doWithFreshTokens:` +#pragma mark - Test `refreshTokensWithCompletion:` -- (void)testDoWithFreshTokens_refresh_givenBothTokensExpired { +- (void)testRefreshTokensWithCompletion_refresh_givenBothTokensExpired { // Both tokens expired 10 seconds ago. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:-10 idTokenExpiresIn:-10]; NSString *newIdToken = [self idTokenWithExpiresIn:kNewIDTokenExpiresIn]; @@ -237,7 +237,7 @@ - (void)testDoWithFreshTokens_refresh_givenBothTokensExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -257,7 +257,7 @@ - (void)testDoWithFreshTokens_refresh_givenBothTokensExpired { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testDoWithRefreshTokens_refresh_givenBothTokensExpired_NoNewIDToken { +- (void)testRefreshTokens_refresh_givenBothTokensExpired_NoNewIDToken { // Both tokens expired 10 seconds ago. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:-10 idTokenExpiresIn:-10]; // Creates a fake response without ID token. @@ -271,7 +271,7 @@ - (void)testDoWithRefreshTokens_refresh_givenBothTokensExpired_NoNewIDToken { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -284,7 +284,7 @@ - (void)testDoWithRefreshTokens_refresh_givenBothTokensExpired_NoNewIDToken { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testDoWithFreshTokens_refresh_givenAccessTokenExpired { +- (void)testRefreshTokensWithCompletion_refresh_givenAccessTokenExpired { // Access token expired 10 seconds ago. ID token will expire in 10 minutes. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:-10 idTokenExpiresIn:10 * 60]; // Creates a fake response. @@ -298,7 +298,7 @@ - (void)testDoWithFreshTokens_refresh_givenAccessTokenExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -312,7 +312,7 @@ - (void)testDoWithFreshTokens_refresh_givenAccessTokenExpired { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testDoWithFreshTokens_refresh_givenIDTokenExpired { +- (void)testRefreshTokensWithCompletion_refresh_givenIDTokenExpired { // ID token expired 10 seconds ago. Access token will expire in 10 minutes. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:10 * 60 idTokenExpiresIn:-10]; @@ -327,7 +327,7 @@ - (void)testDoWithFreshTokens_refresh_givenIDTokenExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -342,7 +342,7 @@ - (void)testDoWithFreshTokens_refresh_givenIDTokenExpired { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testDoWithFreshTokens_noRefresh_givenBothTokensNotExpired { +- (void)testRefreshTokensWithCompletion_noRefresh_givenBothTokensNotExpired { // Both tokens will expire in 10 min. NSTimeInterval expiresIn = 10 * 60; GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:expiresIn @@ -354,7 +354,7 @@ - (void)testDoWithFreshTokens_noRefresh_givenBothTokensNotExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); }]; @@ -367,7 +367,7 @@ - (void)testDoWithFreshTokens_noRefresh_givenBothTokensNotExpired { [self verifyUser:user idTokenExpiresIn:expiresIn]; } -- (void)testDoWithFreshTokens_noRefresh_givenRefreshErrors { +- (void)testRefreshTokensWithCompletion_noRefresh_givenRefreshErrors { // Both tokens expired 10 second ago. NSTimeInterval expiresIn = -10; GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:expiresIn @@ -379,7 +379,7 @@ - (void)testDoWithFreshTokens_noRefresh_givenRefreshErrors { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { [expectation fulfill]; XCTAssertNotNil(error); XCTAssertNil(user); @@ -394,7 +394,7 @@ - (void)testDoWithFreshTokens_noRefresh_givenRefreshErrors { [self verifyUser:user idTokenExpiresIn:expiresIn]; } -- (void)testDoWithFreshTokens_handleConcurrentRefresh { +- (void)testRefreshTokensWithCompletion_handleConcurrentRefresh { // Both tokens expired 10 second ago. NSTimeInterval expiresIn = -10; GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:expiresIn @@ -409,7 +409,7 @@ - (void)testDoWithFreshTokens_handleConcurrentRefresh { XCTestExpectation *firstExpectation = [self expectationWithDescription:@"First callback is called"]; - [user doWithFreshTokens:^(GIDGoogleUser *user, NSError *error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser *user, NSError *error) { [firstExpectation fulfill]; XCTAssertNil(error); @@ -421,7 +421,7 @@ - (void)testDoWithFreshTokens_handleConcurrentRefresh { }]; XCTestExpectation *secondExpectation = [self expectationWithDescription:@"Second callback is called"]; - [user doWithFreshTokens:^(GIDGoogleUser *user, NSError *error) { + [user refreshTokensWithCompletion:^(GIDGoogleUser *user, NSError *error) { [secondExpectation fulfill]; XCTAssertNil(error); diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index 1c70cf0c..08ce3243 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -1429,7 +1429,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow _authError = nil; __block GIDGoogleUserCompletion completion; - [[_user expect] doWithFreshTokens:SAVE_TO_ARG_BLOCK(completion)]; + [[_user expect] refreshTokensWithCompletion:SAVE_TO_ARG_BLOCK(completion)]; XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; diff --git a/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift index 1de3b3de..35d0e2a9 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift @@ -52,7 +52,7 @@ final class BirthdayLoader: ObservableObject { }() private func sessionWithFreshToken(completion: @escaping (Result) -> Void) { - GIDSignIn.sharedInstance.currentUser?.do { user, error in + GIDSignIn.sharedInstance.currentUser?.refreshTokens { user, error in guard let token = user?.accessToken.tokenString else { completion(.failure(.couldNotCreateURLSession(error))) return From 116c480fd38c74df02a68424a1bdc7bccf562667 Mon Sep 17 00:00:00 2001 From: pinlu Date: Sun, 30 Oct 2022 14:32:02 -0700 Subject: [PATCH 7/9] Resolved comments. --- .../Sources/Public/GoogleSignIn/GIDGoogleUser.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h index 4fbcf5dd..9b12ffcc 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h @@ -83,7 +83,7 @@ NS_ASSUME_NONNULL_BEGIN /// Starts an interactive consent flow on iOS to add scopes to the current user's grants. /// -/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` +/// The completion will be called at the end of this process. If successful, a `GIDUserAuth`` /// instance will be returned reflecting the new scopes and saved sign-in state will be updated. /// /// @param scopes The scopes to ask the user to consent to. @@ -102,7 +102,7 @@ NS_ASSUME_NONNULL_BEGIN /// Starts an interactive consent flow on macOS to add scopes to the current user's grants. /// -/// The completion will be called at the end of this process. If successful, a new `GIDGoogleUser` +/// The completion will be called at the end of this process. If successful, a `GIDUserAuth` /// instance will be returned reflecting the new scopes and saved sign-in state will be updated. /// /// @param scopes An array of scopes to ask the user to consent to. @@ -110,9 +110,9 @@ NS_ASSUME_NONNULL_BEGIN /// @param completion The block that is called on completion. This block will be called asynchronously /// on the main queue. - (void)addScopes:(NSArray *)scopes - presentingWindow:(NSWindow *)presentingWindow - completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, - NSError *_Nullable error))completion; + presentingWindow:(NSWindow *)presentingWindow + completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth, + NSError *_Nullable error))completion; #endif From 37b4fd95e5fdcbc641f7b5388e120d548df653cb Mon Sep 17 00:00:00 2001 From: pinlu Date: Mon, 31 Oct 2022 15:57:50 -0700 Subject: [PATCH 8/9] Rename to `refreshTokensIfNeededWithCompletion:` --- GoogleSignIn/Sources/GIDGoogleUser.m | 2 +- GoogleSignIn/Sources/GIDSignIn.m | 2 +- .../Public/GoogleSignIn/GIDGoogleUser.h | 4 +- GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m | 38 +++++++++++-------- GoogleSignIn/Tests/Unit/GIDSignInTest.m | 2 +- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/GoogleSignIn/Sources/GIDGoogleUser.m b/GoogleSignIn/Sources/GIDGoogleUser.m index b1007c42..4291ba38 100644 --- a/GoogleSignIn/Sources/GIDGoogleUser.m +++ b/GoogleSignIn/Sources/GIDGoogleUser.m @@ -108,7 +108,7 @@ - (GIDConfiguration *)configuration { return _cachedConfiguration; } -- (void)refreshTokensWithCompletion:(GIDGoogleUserCompletion)completion { +- (void)refreshTokensIfNeededWithCompletion:(GIDGoogleUserCompletion)completion { if (!([self.accessToken.expirationDate timeIntervalSinceNow] < kMinimalTimeToExpire || (self.idToken && [self.idToken.expirationDate timeIntervalSinceNow] < kMinimalTimeToExpire))) { dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/GoogleSignIn/Sources/GIDSignIn.m b/GoogleSignIn/Sources/GIDSignIn.m index 42d5dd20..b388bf56 100644 --- a/GoogleSignIn/Sources/GIDSignIn.m +++ b/GoogleSignIn/Sources/GIDSignIn.m @@ -518,7 +518,7 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options { // If this is a non-interactive flow, use cached authentication if possible. if (!options.interactive && _currentUser) { - [_currentUser refreshTokensWithCompletion:^(GIDGoogleUser *unused, NSError *error) { + [_currentUser refreshTokensIfNeededWithCompletion:^(GIDGoogleUser *unused, NSError *error) { if (error) { [self authenticateWithOptions:options]; } else { diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h index 9b12ffcc..7f5cadcc 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h @@ -76,8 +76,8 @@ NS_ASSUME_NONNULL_BEGIN /// /// @param completion A completion block that takes a `GIDGoogleUser` or an error if the attempt to /// refresh tokens was unsuccessful. The block will be called asynchronously on the main queue. -- (void)refreshTokensWithCompletion:(void (^)(GIDGoogleUser *_Nullable user, - NSError *_Nullable error))completion; +- (void)refreshTokensIfNeededWithCompletion:(void (^)(GIDGoogleUser *_Nullable user, + NSError *_Nullable error))completion; #if TARGET_OS_IOS || TARGET_OS_MACCATALYST diff --git a/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m b/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m index 29fa0567..b79d419b 100644 --- a/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m +++ b/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m @@ -233,9 +233,9 @@ - (void)testFetcherAuthorizer_returnTheSameInstance { XCTAssertIdentical(fetcherAuthorizer, fetcherAuthorizer2); } -#pragma mark - Test `refreshTokensWithCompletion:` +#pragma mark - Test `refreshTokensIfNeededWithCompletion:` -- (void)testRefreshTokensWithCompletion_refresh_givenBothTokensExpired { +- (void)testRefreshTokensIfNeededWithCompletion_refresh_givenBothTokensExpired { // Both tokens expired 10 seconds ago. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:-10 idTokenExpiresIn:-10]; NSString *newIdToken = [self idTokenWithExpiresIn:kNewIDTokenExpiresIn]; @@ -243,7 +243,8 @@ - (void)testRefreshTokensWithCompletion_refresh_givenBothTokensExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -277,7 +278,8 @@ - (void)testRefreshTokens_refresh_givenBothTokensExpired_NoNewIDToken { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -290,7 +292,7 @@ - (void)testRefreshTokens_refresh_givenBothTokensExpired_NoNewIDToken { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testRefreshTokensWithCompletion_refresh_givenAccessTokenExpired { +- (void)testRefreshTokensIfNeededWithCompletion_refresh_givenAccessTokenExpired { // Access token expired 10 seconds ago. ID token will expire in 10 minutes. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:-10 idTokenExpiresIn:10 * 60]; // Creates a fake response. @@ -304,7 +306,8 @@ - (void)testRefreshTokensWithCompletion_refresh_givenAccessTokenExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -318,7 +321,7 @@ - (void)testRefreshTokensWithCompletion_refresh_givenAccessTokenExpired { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testRefreshTokensWithCompletion_refresh_givenIDTokenExpired { +- (void)testRefreshTokensIfNeededWithCompletion_refresh_givenIDTokenExpired { // ID token expired 10 seconds ago. Access token will expire in 10 minutes. GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:10 * 60 idTokenExpiresIn:-10]; @@ -333,7 +336,8 @@ - (void)testRefreshTokensWithCompletion_refresh_givenIDTokenExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -348,7 +352,7 @@ - (void)testRefreshTokensWithCompletion_refresh_givenIDTokenExpired { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testRefreshTokensWithCompletion_noRefresh_givenBothTokensNotExpired { +- (void)testRefreshTokensIfNeededWithCompletion_noRefresh_givenBothTokensNotExpired { // Both tokens will expire in 10 min. NSTimeInterval expiresIn = 10 * 60; GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:expiresIn @@ -360,7 +364,8 @@ - (void)testRefreshTokensWithCompletion_noRefresh_givenBothTokensNotExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); }]; @@ -373,7 +378,7 @@ - (void)testRefreshTokensWithCompletion_noRefresh_givenBothTokensNotExpired { [self verifyUser:user idTokenExpiresIn:expiresIn]; } -- (void)testRefreshTokensWithCompletion_noRefresh_givenRefreshErrors { +- (void)testRefreshTokensIfNeededWithCompletion_noRefresh_givenRefreshErrors { // Both tokens expired 10 second ago. NSTimeInterval expiresIn = -10; GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:expiresIn @@ -385,7 +390,8 @@ - (void)testRefreshTokensWithCompletion_noRefresh_givenRefreshErrors { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user refreshTokensWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNotNil(error); XCTAssertNil(user); @@ -400,7 +406,7 @@ - (void)testRefreshTokensWithCompletion_noRefresh_givenRefreshErrors { [self verifyUser:user idTokenExpiresIn:expiresIn]; } -- (void)testRefreshTokensWithCompletion_handleConcurrentRefresh { +- (void)testRefreshTokensIfNeededWithCompletion_handleConcurrentRefresh { // Both tokens expired 10 second ago. NSTimeInterval expiresIn = -10; GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:expiresIn @@ -415,7 +421,7 @@ - (void)testRefreshTokensWithCompletion_handleConcurrentRefresh { XCTestExpectation *firstExpectation = [self expectationWithDescription:@"First callback is called"]; - [user refreshTokensWithCompletion:^(GIDGoogleUser *user, NSError *error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser *user, NSError *error) { [firstExpectation fulfill]; XCTAssertNil(error); @@ -427,7 +433,7 @@ - (void)testRefreshTokensWithCompletion_handleConcurrentRefresh { }]; XCTestExpectation *secondExpectation = [self expectationWithDescription:@"Second callback is called"]; - [user refreshTokensWithCompletion:^(GIDGoogleUser *user, NSError *error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser *user, NSError *error) { [secondExpectation fulfill]; XCTAssertNil(error); @@ -443,6 +449,8 @@ - (void)testRefreshTokensWithCompletion_handleConcurrentRefresh { [self waitForExpectationsWithTimeout:1 handler:nil]; } +# pragma mark - Test `addScopes:` + - (void)testAddScopes_success { id signIn = OCMClassMock([GIDSignIn class]); OCMStub([signIn sharedInstance]).andReturn(signIn); diff --git a/GoogleSignIn/Tests/Unit/GIDSignInTest.m b/GoogleSignIn/Tests/Unit/GIDSignInTest.m index 08ce3243..ea6043df 100644 --- a/GoogleSignIn/Tests/Unit/GIDSignInTest.m +++ b/GoogleSignIn/Tests/Unit/GIDSignInTest.m @@ -1429,7 +1429,7 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow _authError = nil; __block GIDGoogleUserCompletion completion; - [[_user expect] refreshTokensWithCompletion:SAVE_TO_ARG_BLOCK(completion)]; + [[_user expect] refreshTokensIfNeededWithCompletion:SAVE_TO_ARG_BLOCK(completion)]; XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be called"]; From 43493058c1ffa991b001579d1deb7a248ded9e95 Mon Sep 17 00:00:00 2001 From: pinlu Date: Mon, 31 Oct 2022 16:01:39 -0700 Subject: [PATCH 9/9] Update sample app --- GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h | 2 +- .../DaysUntilBirthday/Shared/Services/BirthdayLoader.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h index 7f5cadcc..342c0ab4 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h @@ -83,7 +83,7 @@ NS_ASSUME_NONNULL_BEGIN /// Starts an interactive consent flow on iOS to add scopes to the current user's grants. /// -/// The completion will be called at the end of this process. If successful, a `GIDUserAuth`` +/// The completion will be called at the end of this process. If successful, a `GIDUserAuth` /// instance will be returned reflecting the new scopes and saved sign-in state will be updated. /// /// @param scopes The scopes to ask the user to consent to. diff --git a/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift index 35d0e2a9..7692cb22 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift @@ -52,7 +52,7 @@ final class BirthdayLoader: ObservableObject { }() private func sessionWithFreshToken(completion: @escaping (Result) -> Void) { - GIDSignIn.sharedInstance.currentUser?.refreshTokens { user, error in + GIDSignIn.sharedInstance.currentUser?.refreshTokensIfNeeded { user, error in guard let token = user?.accessToken.tokenString else { completion(.failure(.couldNotCreateURLSession(error))) return