GIDTokenFetchOperation.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = [@{} mutableCopy];
  50. if (self.options.configuration.serverClientID) {
  51. additionalParameters[kAudienceParameter] = self.options.configuration.serverClientID;
  52. }
  53. if (self.options.configuration.openIDRealm) {
  54. additionalParameters[kOpenIDRealmParameter] = self.options.configuration.openIDRealm;
  55. }
  56. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  57. NSDictionary<NSString *, NSObject *> *params =
  58. self.authState.lastAuthorizationResponse.additionalParameters;
  59. NSString *passcodeInfoRequired = (NSString *)params[kEMMPasscodeInfoRequiredKeyName];
  60. [additionalParameters addEntriesFromDictionary:
  61. [GIDEMMSupport parametersWithParameters:@{}
  62. emmSupport:self.emmSupport
  63. isPasscodeInfoRequired:passcodeInfoRequired.length > 0]];
  64. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  65. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  66. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  67. OIDTokenRequest *tokenRequest;
  68. if (!self.authState.lastTokenResponse.accessToken &&
  69. self.authState.lastAuthorizationResponse.authorizationCode) {
  70. tokenRequest = [self.authState.lastAuthorizationResponse
  71. tokenExchangeRequestWithAdditionalParameters:additionalParameters];
  72. } else {
  73. [additionalParameters
  74. addEntriesFromDictionary:self.authState.lastTokenResponse.request.additionalParameters];
  75. tokenRequest = [self.authState tokenRefreshRequestWithAdditionalParameters:additionalParameters];
  76. }
  77. [OIDAuthorizationService performTokenRequest:tokenRequest
  78. originalAuthorizationResponse:self.authState.lastAuthorizationResponse
  79. callback:^(OIDTokenResponse *_Nullable tokenResponse,
  80. NSError *_Nullable error) {
  81. [self.authState updateWithTokenResponse:tokenResponse error:error];
  82. self.error = error;
  83. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  84. if (self.emmSupport) {
  85. [GIDEMMSupport handleTokenFetchEMMError:error completion:^(NSError *error) {
  86. self.error = error;
  87. }];
  88. }
  89. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  90. }];
  91. }
  92. @end