GIDTokenFetchOperation.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2025 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GIDTokenFetchOperation.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  18. #import "GoogleSignIn/Sources/GIDEMMSupport.h"
  19. #import "GoogleSignIn/Sources/GIDSignInConstants.h"
  20. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  21. #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
  22. #ifdef SWIFT_PACKAGE
  23. @import AppAuth;
  24. #else
  25. #import <AppAuth/OIDAuthState.h>
  26. #import <AppAuth/OIDAuthorizationResponse.h>
  27. #endif
  28. @implementation GIDTokenFetchOperation
  29. - (instancetype)initWithAuthState:(nullable OIDAuthState *)authState
  30. options:(nullable GIDSignInInternalOptions *)options
  31. emmSupport:(nullable NSString *)emmSupport
  32. error:(nullable NSError *)error {
  33. self = [super init];
  34. if (self) {
  35. _authState = authState;
  36. _options = options;
  37. _emmSupport = emmSupport;
  38. _error = error;
  39. }
  40. }
  41. - (void)main {
  42. // Do nothing if we have an auth flow error or a restored access token that isn't near expiration.
  43. if (self.error ||
  44. (self.authState.lastTokenResponse.accessToken &&
  45. [self.authState.lastTokenResponse.accessTokenExpirationDate timeIntervalSinceNow] >
  46. kMinimumRestoredAccessTokenTimeToExpire)) {
  47. return;
  48. }
  49. NSMutableDictionary<NSString *, NSString *> *additionalParameters =
  50. [[NSMutableDictionary alloc] init];
  51. if (self.options.configuration.serverClientID) {
  52. additionalParameters[kAudienceParameter] = self.options.configuration.serverClientID;
  53. }
  54. if (self.options.configuration.openIDRealm) {
  55. additionalParameters[kOpenIDRealmParameter] = self.options.configuration.openIDRealm;
  56. }
  57. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  58. NSDictionary<NSString *, NSObject *> *params =
  59. self.authState.lastAuthorizationResponse.additionalParameters;
  60. NSString *passcodeInfoRequired = (NSString *)params[kEMMPasscodeInfoRequiredKeyName];
  61. [additionalParameters addEntriesFromDictionary:
  62. [GIDEMMSupport parametersWithParameters:@{}
  63. emmSupport:self.emmSupport
  64. isPasscodeInfoRequired:passcodeInfoRequired.length > 0]];
  65. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  66. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  67. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  68. OIDTokenRequest *tokenRequest;
  69. if (!self.authState.lastTokenResponse.accessToken &&
  70. self.authState.lastAuthorizationResponse.authorizationCode) {
  71. tokenRequest = [self.authState.lastAuthorizationResponse
  72. tokenExchangeRequestWithAdditionalParameters:additionalParameters];
  73. } else {
  74. [additionalParameters
  75. addEntriesFromDictionary:self.authState.lastTokenResponse.request.additionalParameters];
  76. tokenRequest = [self.authState tokenRefreshRequestWithAdditionalParameters:additionalParameters];
  77. }
  78. [OIDAuthorizationService performTokenRequest:tokenRequest
  79. originalAuthorizationResponse:self.authState.lastAuthorizationResponse
  80. callback:^(OIDTokenResponse *_Nullable tokenResponse,
  81. NSError *_Nullable error) {
  82. [self.authState updateWithTokenResponse:tokenResponse error:error];
  83. self.error = error;
  84. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  85. if (self.emmSupport) {
  86. [GIDEMMSupport handleTokenFetchEMMError:error completion:^(NSError *error) {
  87. self.error = error;
  88. }];
  89. }
  90. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  91. }];
  92. }
  93. @end