Просмотр исходного кода

[VwG] Add "Refresh" button to sample app to refresh tokens. (#453)

Brianna Morales 1 год назад
Родитель
Сommit
d71f99a0b6

+ 4 - 6
GoogleSignIn/Sources/GIDVerifyAccountDetail/Fake/GIDVerifiedAccountDetailHandlingFake.m

@@ -31,9 +31,8 @@
 
 @implementation GIDVerifiedAccountDetailHandlingFake
 
-- (instancetype)initWithLastTokenResponse:(OIDTokenResponse *)tokenResponse
-                           accountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
-                                authState:(OIDAuthState *)authState {
+- (instancetype)initWithAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
+                             authState:(OIDAuthState *)authState {
   self = [super init];
   if (self) {
     NSAssert(false, @"This class is only to be used in testing. Do not use.");
@@ -64,9 +63,8 @@
   [self updateVerifiedDetailsWithTokenResponse:_tokenResponse];
 
   GIDVerifiedAccountDetailResult *result =
-      [[GIDVerifiedAccountDetailResult alloc] initWithLastTokenResponse:_tokenResponse
-                                                         accountDetails:_verifiedAccountDetails
-                                                              authState:_verifiedAuthState];
+      [[GIDVerifiedAccountDetailResult alloc] initWithAccountDetails:_verifiedAccountDetails
+                                                           authState:_verifiedAuthState];
   completion(result, _error);
 }
 

+ 18 - 25
GoogleSignIn/Sources/GIDVerifyAccountDetail/Implementations/GIDVerifiedAccountDetailResult.m

@@ -36,14 +36,10 @@ NS_ASSUME_NONNULL_BEGIN
 
 @implementation GIDVerifiedAccountDetailResult
 
-- (instancetype)initWithLastTokenResponse:(OIDTokenResponse *)tokenResponse
-                           accountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
-                                authState:(OIDAuthState *)authState {
+- (instancetype)initWithAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
+                             authState:(OIDAuthState *)authState {
   self = [super init];
   if (self) {
-    _expirationDate = tokenResponse.accessTokenExpirationDate;
-    _accessTokenString = tokenResponse.accessToken;
-    _refreshTokenString = tokenResponse.refreshToken;
     _verifiedAccountDetails = accountDetails;
     _verifiedAuthState = authState;
   }
@@ -53,23 +49,12 @@ NS_ASSUME_NONNULL_BEGIN
 // TODO: Migrate refresh logic to `GIDGoogleuser` (#441).
 - (void)refreshTokensWithCompletion:(nullable void (^)(GIDVerifiedAccountDetailResult *,
                                                       NSError *))completion {
-  OIDAuthorizationResponse *authResponse = self.verifiedAuthState.lastAuthorizationResponse;
-  OIDAuthorizationRequest *request = authResponse.request;
-
-  OIDTokenRequest *refreshRequest = 
-      [[OIDTokenRequest alloc] initWithConfiguration:request.configuration
-                                           grantType:OIDGrantTypeAuthorizationCode
-                                   authorizationCode:authResponse.authorizationCode
-                                         redirectURL:request.redirectURL
-                                            clientID:request.clientID
-                                        clientSecret:request.clientSecret
-                                               scope:request.scope                     
-                                        refreshToken:self.refreshTokenString
-                                        codeVerifier:request.codeVerifier
-                                additionalParameters:request.additionalParameters];
+  NSDictionary<NSString *, NSString *> *additionalParameters = 
+      self.verifiedAuthState.lastAuthorizationResponse.request.additionalParameters;
+  OIDTokenRequest *refreshRequest =
+      [self.verifiedAuthState tokenRefreshRequestWithAdditionalHeaders:additionalParameters];
 
   [OIDAuthorizationService performTokenRequest:refreshRequest
-                 originalAuthorizationResponse:authResponse
                                       callback:^(OIDTokenResponse * _Nullable tokenResponse, 
                                                  NSError * _Nullable error) {
     if (tokenResponse) {
@@ -84,10 +69,6 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (void)updateVerifiedDetailsWithTokenResponse:(nullable OIDTokenResponse *)tokenResponse {
   if (tokenResponse) {
-    _expirationDate = tokenResponse.accessTokenExpirationDate;
-    _accessTokenString = tokenResponse.accessToken;
-    _refreshTokenString = tokenResponse.refreshToken;
-
     NSArray<NSString *> *accountDetailsString =
         [OIDScopeUtilities scopesArrayWithString:tokenResponse.scope];
     NSMutableArray<GIDVerifiableAccountDetail *> *verifiedAccountDetails = [NSMutableArray array];
@@ -104,6 +85,18 @@ NS_ASSUME_NONNULL_BEGIN
   }
 }
 
+- (nullable NSString *)accessTokenString {
+  return self.verifiedAuthState.lastTokenResponse.accessToken;
+}
+
+- (nullable NSString *)refreshTokenString {
+  return self.verifiedAuthState.refreshToken;
+}
+
+- (nullable NSDate *)expirationDate {
+  return self.verifiedAuthState.lastTokenResponse.accessTokenExpirationDate;
+}
+
 - (BOOL)isEqual:(id)object {
   if (![object isKindOfClass:[GIDVerifiedAccountDetailResult class]]) {
     return NO;

+ 2 - 3
GoogleSignIn/Sources/GIDVerifyAccountDetail/Implementations/GIDVerifyAccountDetail.m

@@ -271,9 +271,8 @@ NSErrorDomain const kGIDVerifyErrorDomain = @"com.google.GIDVerifyAccountDetail"
         } else {
           OIDAuthState *authState = handlerAuthFlow.authState;
           GIDVerifiedAccountDetailResult *verifiedResult = [[GIDVerifiedAccountDetailResult alloc]
-              initWithLastTokenResponse:authState.lastTokenResponse
-                         accountDetails:self->_accountDetails
-                              authState:authState];
+              initWithAccountDetails:self->_accountDetails
+                           authState:authState];
           completion(verifiedResult, nil);
         }
       });

+ 2 - 4
GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiedAccountDetailHandling.h

@@ -31,14 +31,12 @@ NS_ASSUME_NONNULL_BEGIN
 
 /// Initialize a `GIDVerifiedAccountDetailHandling` object by specifying all available properties.
 ///
-/// @param tokenResponse The last token response with expiration date, access token, and refresh token.
 /// @param accountDetails A list of verified account details.
 /// @param authState An updated to update the token response or authorization error.
 ///
 /// @return An initialized `GIDVerifiedAccountDetailHandling` instance with expiration date, access token, and refresh token.
-- (instancetype)initWithLastTokenResponse:(OIDTokenResponse *)tokenResponse
-                           accountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
-                                authState:(OIDAuthState *)authState;
+- (instancetype)initWithAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
+                             authState:(OIDAuthState *)authState;
 
 /// Refresh the access token and refresh token with the current authorization state.
 ///

+ 5 - 7
GoogleSignIn/Tests/Unit/GIDVerifiedAccountDetailResultTest.m

@@ -41,9 +41,8 @@
       @[verifiedAccountDetail, verifiedAccountDetail];
 
   GIDVerifiedAccountDetailResult *result = 
-      [[GIDVerifiedAccountDetailResult alloc] initWithLastTokenResponse:authState.lastTokenResponse
-                                                         accountDetails:verifiedList
-                                                              authState:authState];
+      [[GIDVerifiedAccountDetailResult alloc] initWithAccountDetails:verifiedList
+                                                           authState:authState];
 
   XCTAssertEqual(result.verifiedAuthState, authState);
   XCTAssertEqual(result.verifiedAccountDetails, verifiedList);
@@ -68,9 +67,8 @@
   NSArray<GIDVerifiableAccountDetail *> *expectedVerifiedList =
       @[verifiedAccountDetail];
   GIDVerifiedAccountDetailResult *expectedResult =
-      [[GIDVerifiedAccountDetailResult alloc] initWithLastTokenResponse:authState.lastTokenResponse
-                                                         accountDetails:expectedVerifiedList
-                                                              authState:authState];
+      [[GIDVerifiedAccountDetailResult alloc] initWithAccountDetails:expectedVerifiedList
+                                                           authState:authState];
 
   XCTestExpectation *expectation =
       [self expectationWithDescription:@"Refreshed verified account details completion called"];
@@ -97,7 +95,7 @@
                                                                     error:expectedError];
 
   XCTestExpectation *expectation = 
-    [self expectationWithDescription:@"Refreshed verified account details completion called"];
+      [self expectationWithDescription:@"Refreshed verified account details completion called"];
   [result refreshTokensWithCompletion:^(GIDVerifiedAccountDetailResult * _Nullable refreshedResult,
                                       NSError * _Nullable error) {
     XCTAssertNotNil(error);

+ 12 - 0
Samples/Swift/DaysUntilBirthday/Shared/Views/VerificationView.swift

@@ -46,6 +46,12 @@ struct VerificationView: View {
         Spacer()
       }
       .navigationTitle("Verified Account!")
+      .toolbar {
+        ToolbarItemGroup(placement: .navigationBarTrailing) {
+          Button(NSLocalizedString("Refresh", comment: "Refresh button"), 
+                 action:{refresh(results: result)})
+        }
+      }
     case .unverified:
       ProgressView()
         .navigationTitle(NSLocalizedString("Unverified account",
@@ -59,4 +65,10 @@ struct VerificationView: View {
     dateFormatter.timeStyle = .short
     return dateFormatter.string(from: date)
   }
+
+  func refresh(results: GIDVerifiedAccountDetailResult) {
+    results.refreshTokens { (result, error) in
+      verifiedAgeViewModel.verificationState = .verified(result)
+    }
+  }
 }