Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GoogleSignIn/Sources/GIDGoogleUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -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(), ^{
Expand Down
2 changes: 1 addition & 1 deletion GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
40 changes: 24 additions & 16 deletions GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,18 @@ - (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];

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);
Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -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];

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);
}];
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion GoogleSignIn/Tests/Unit/GIDSignInTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class BirthdayLoader: ObservableObject {
}()

private func sessionWithFreshToken(completion: @escaping (Result<URLSession, Error>) -> 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
Expand Down