GIDEMMSupport.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. @implementation GIDEMMSupport
  37. + (void)handleTokenFetchEMMError:(nullable NSError *)error
  38. completion:(void (^)(NSError *_Nullable))completion {
  39. NSDictionary *errorJSON = error.userInfo[OIDOAuthErrorResponseErrorKey];
  40. if (errorJSON) {
  41. __block BOOL handled = NO;
  42. handled = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:errorJSON
  43. completion:^() {
  44. if (handled) {
  45. completion([NSError errorWithDomain:kGIDSignInErrorDomain
  46. code:kGIDSignInErrorCodeEMM
  47. userInfo:error.userInfo]);
  48. } else {
  49. completion(error);
  50. }
  51. }];
  52. } else {
  53. completion(error);
  54. }
  55. }
  56. + (NSDictionary *)updatedEMMParametersWithParameters:(NSDictionary *)parameters {
  57. return [self parametersWithParameters:parameters
  58. emmSupport:parameters[kEMMSupportParameterName]
  59. isPasscodeInfoRequired:parameters[kEMMPasscodeInfoParameterName] != nil];
  60. }
  61. + (NSDictionary *)parametersWithParameters:(NSDictionary *)parameters
  62. emmSupport:(nullable NSString *)emmSupport
  63. isPasscodeInfoRequired:(BOOL)isPasscodeInfoRequired {
  64. if (!emmSupport) {
  65. return parameters;
  66. }
  67. NSMutableDictionary *allParameters = [(parameters ?: @{}) mutableCopy];
  68. allParameters[kEMMSupportParameterName] = emmSupport;
  69. UIDevice *device = [UIDevice currentDevice];
  70. NSString *systemName = device.systemName;
  71. if ([systemName isEqualToString:kOldIOSSystemName]) {
  72. systemName = kNewIOSSystemName;
  73. }
  74. allParameters[kEMMOSVersionParameterName] =
  75. [NSString stringWithFormat:@"%@ %@", systemName, device.systemVersion];
  76. if (isPasscodeInfoRequired) {
  77. allParameters[kEMMPasscodeInfoParameterName] = [GIDMDMPasscodeState passcodeState].info;
  78. }
  79. return allParameters;
  80. }
  81. @end
  82. NS_ASSUME_NONNULL_END
  83. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST