diff --git a/GoogleSignIn/Sources/GIDGoogleUser.m b/GoogleSignIn/Sources/GIDGoogleUser.m index ca35edfa..4291ba38 100644 --- a/GoogleSignIn/Sources/GIDGoogleUser.m +++ b/GoogleSignIn/Sources/GIDGoogleUser.m @@ -108,7 +108,7 @@ - (GIDConfiguration *)configuration { return _cachedConfiguration; } -- (void)doWithFreshTokens:(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 56490155..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 doWithFreshTokens:^(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 ee080a27..342c0ab4 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h @@ -76,12 +76,12 @@ 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)refreshTokensIfNeededWithCompletion:(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 user's grants. +/// 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` /// instance will be returned reflecting the new scopes and saved sign-in state will be updated. @@ -100,7 +100,7 @@ NS_ASSUME_NONNULL_BEGIN #elif TARGET_OS_OSX -/// Starts an interactive consent flow on macOS to add scopes to the user's grants. +/// 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 `GIDUserAuth` /// instance will be returned reflecting the new scopes and saved sign-in state will be updated. diff --git a/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m b/GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m index 5261872a..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 `doWithFreshTokens:` +#pragma mark - Test `refreshTokensIfNeededWithCompletion:` -- (void)testDoWithFreshTokens_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)testDoWithFreshTokens_refresh_givenBothTokensExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -263,7 +264,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. @@ -277,7 +278,8 @@ - (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 refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); XCTAssertEqualObjects(user.accessToken.tokenString, kNewAccessToken); @@ -290,7 +292,7 @@ - (void)testDoWithRefreshTokens_refresh_givenBothTokensExpired_NoNewIDToken { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testDoWithFreshTokens_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)testDoWithFreshTokens_refresh_givenAccessTokenExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(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)testDoWithFreshTokens_refresh_givenAccessTokenExpired { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testDoWithFreshTokens_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)testDoWithFreshTokens_refresh_givenIDTokenExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(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)testDoWithFreshTokens_refresh_givenIDTokenExpired { [self waitForExpectationsWithTimeout:1 handler:nil]; } -- (void)testDoWithFreshTokens_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)testDoWithFreshTokens_noRefresh_givenBothTokensNotExpired { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser * _Nullable user, + NSError * _Nullable error) { [expectation fulfill]; XCTAssertNil(error); }]; @@ -373,7 +378,7 @@ - (void)testDoWithFreshTokens_noRefresh_givenBothTokensNotExpired { [self verifyUser:user idTokenExpiresIn:expiresIn]; } -- (void)testDoWithFreshTokens_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)testDoWithFreshTokens_noRefresh_givenRefreshErrors { XCTestExpectation *expectation = [self expectationWithDescription:@"Callback is called"]; // Save the intermediate states. - [user doWithFreshTokens:^(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)testDoWithFreshTokens_noRefresh_givenRefreshErrors { [self verifyUser:user idTokenExpiresIn:expiresIn]; } -- (void)testDoWithFreshTokens_handleConcurrentRefresh { +- (void)testRefreshTokensIfNeededWithCompletion_handleConcurrentRefresh { // Both tokens expired 10 second ago. NSTimeInterval expiresIn = -10; GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:expiresIn @@ -415,7 +421,7 @@ - (void)testDoWithFreshTokens_handleConcurrentRefresh { XCTestExpectation *firstExpectation = [self expectationWithDescription:@"First callback is called"]; - [user doWithFreshTokens:^(GIDGoogleUser *user, NSError *error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser *user, NSError *error) { [firstExpectation fulfill]; XCTAssertNil(error); @@ -427,7 +433,7 @@ - (void)testDoWithFreshTokens_handleConcurrentRefresh { }]; XCTestExpectation *secondExpectation = [self expectationWithDescription:@"Second callback is called"]; - [user doWithFreshTokens:^(GIDGoogleUser *user, NSError *error) { + [user refreshTokensIfNeededWithCompletion:^(GIDGoogleUser *user, NSError *error) { [secondExpectation fulfill]; XCTAssertNil(error); @@ -443,6 +449,8 @@ - (void)testDoWithFreshTokens_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 1c70cf0c..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] doWithFreshTokens:SAVE_TO_ARG_BLOCK(completion)]; + [[_user expect] refreshTokensIfNeededWithCompletion: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..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?.do { user, error in + GIDSignIn.sharedInstance.currentUser?.refreshTokensIfNeeded { user, error in guard let token = user?.accessToken.tokenString else { completion(.failure(.couldNotCreateURLSession(error))) return