GIDEMMSupport.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2022 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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  18. #import "GoogleSignIn/Sources/GIDEMMSupport.h"
  19. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  20. #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
  21. #import "GoogleSignIn/Sources/GIDMDMPasscodeState.h"
  22. #ifdef SWIFT_PACKAGE
  23. @import AppAuth;
  24. #else
  25. #import <AppAuth/AppAuth.h>
  26. #endif
  27. NS_ASSUME_NONNULL_BEGIN
  28. // Additional parameter names for EMM.
  29. static NSString *const kEMMSupportParameterName = @"emm_support";
  30. static NSString *const kEMMOSVersionParameterName = @"device_os";
  31. static NSString *const kEMMPasscodeInfoParameterName = @"emm_passcode_info";
  32. // Old UIDevice system name for iOS.
  33. static NSString *const kOldIOSSystemName = @"iPhone OS";
  34. // New UIDevice system name for iOS.
  35. static NSString *const kNewIOSSystemName = @"iOS";
  36. // The error key in the server response.
  37. static NSString *const kErrorKey = @"error";
  38. // Optional separator between error prefix and the payload.
  39. static NSString *const kErrorPayloadSeparator = @":";
  40. // A list for recognized error codes.
  41. typedef NS_ENUM(NSInteger, ErrorCode) {
  42. ErrorCodeNone = 0,
  43. ErrorCodeDeviceNotCompliant,
  44. ErrorCodeScreenlockRequired,
  45. ErrorCodeAppVerificationRequired,
  46. };
  47. @implementation GIDEMMSupport
  48. - (instancetype)init {
  49. return [super init];
  50. }
  51. + (void)handleTokenFetchEMMError:(nullable NSError *)error
  52. completion:(void (^)(NSError *_Nullable))completion {
  53. NSDictionary *errorJSON = error.userInfo[OIDOAuthErrorResponseErrorKey];
  54. if (errorJSON) {
  55. __block BOOL handled = NO;
  56. handled = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:errorJSON
  57. completion:^() {
  58. if (handled) {
  59. completion([NSError errorWithDomain:kGIDSignInErrorDomain
  60. code:kGIDSignInErrorCodeEMM
  61. userInfo:error.userInfo]);
  62. } else {
  63. completion(error);
  64. }
  65. }];
  66. } else {
  67. completion(error);
  68. }
  69. }
  70. + (NSDictionary *)updatedEMMParametersWithParameters:(NSDictionary *)parameters {
  71. return [self parametersWithParameters:parameters
  72. emmSupport:parameters[kEMMSupportParameterName]
  73. isPasscodeInfoRequired:parameters[kEMMPasscodeInfoParameterName] != nil];
  74. }
  75. + (NSDictionary *)parametersWithParameters:(NSDictionary *)parameters
  76. emmSupport:(nullable NSString *)emmSupport
  77. isPasscodeInfoRequired:(BOOL)isPasscodeInfoRequired {
  78. if (!emmSupport) {
  79. return parameters;
  80. }
  81. NSMutableDictionary *allParameters = [(parameters ?: @{}) mutableCopy];
  82. allParameters[kEMMSupportParameterName] = emmSupport;
  83. UIDevice *device = [UIDevice currentDevice];
  84. NSString *systemName = device.systemName;
  85. if ([systemName isEqualToString:kOldIOSSystemName]) {
  86. systemName = kNewIOSSystemName;
  87. }
  88. allParameters[kEMMOSVersionParameterName] =
  89. [NSString stringWithFormat:@"%@ %@", systemName, device.systemVersion];
  90. if (isPasscodeInfoRequired) {
  91. allParameters[kEMMPasscodeInfoParameterName] = [GIDMDMPasscodeState passcodeState].info;
  92. }
  93. return allParameters;
  94. }
  95. #pragma mark - GTMAuthSessionDelegate
  96. - (nullable NSDictionary<NSString *,NSString *> *)
  97. additionalTokenRefreshParametersForAuthSession:(GTMAuthSession *)authSession {
  98. return [GIDEMMSupport updatedEMMParametersWithParameters:
  99. authSession.authState.lastTokenResponse.additionalParameters];
  100. }
  101. - (void)updateErrorForAuthSession:(GTMAuthSession *)authSession
  102. originalError:(NSError *)originalError
  103. completion:(void (^)(NSError * _Nullable))completion {
  104. [GIDEMMSupport handleTokenFetchEMMError:originalError completion:^(NSError *_Nullable error) {
  105. completion(error);
  106. }];
  107. }
  108. @end
  109. NS_ASSUME_NONNULL_END
  110. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST