GACDeviceCheckProvider.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2020 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 "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckAvailability.h"
  17. #if GAC_DEVICE_CHECK_SUPPORTED_TARGETS
  18. #import <Foundation/Foundation.h>
  19. #if __has_include(<FBLPromises/FBLPromises.h>)
  20. #import <FBLPromises/FBLPromises.h>
  21. #else
  22. #import "FBLPromises.h"
  23. #endif
  24. #import "AppCheckCore/Sources/Public/AppCheckCore/GACDeviceCheckProvider.h"
  25. #import "AppCheckCore/Sources/Core/APIService/GACAppCheckAPIService.h"
  26. #import "AppCheckCore/Sources/Core/Backoff/GACAppCheckBackoffWrapper.h"
  27. #import "AppCheckCore/Sources/Core/GACAppCheckLogger+Internal.h"
  28. #import "AppCheckCore/Sources/DeviceCheckProvider/API/GACDeviceCheckAPIService.h"
  29. #import "AppCheckCore/Sources/DeviceCheckProvider/DCDevice+GACDeviceCheckTokenGenerator.h"
  30. #import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h"
  31. NS_ASSUME_NONNULL_BEGIN
  32. @interface GACDeviceCheckProvider ()
  33. @property(nonatomic, readonly) id<GACDeviceCheckAPIServiceProtocol> APIService;
  34. @property(nonatomic, readonly) id<GACDeviceCheckTokenGenerator> deviceTokenGenerator;
  35. @property(nonatomic, readonly) id<GACAppCheckBackoffWrapperProtocol> backoffWrapper;
  36. - (instancetype)initWithAPIService:(id<GACDeviceCheckAPIServiceProtocol>)APIService
  37. deviceTokenGenerator:(id<GACDeviceCheckTokenGenerator>)deviceTokenGenerator
  38. backoffWrapper:(id<GACAppCheckBackoffWrapperProtocol>)backoffWrapper
  39. NS_DESIGNATED_INITIALIZER;
  40. @end
  41. @implementation GACDeviceCheckProvider
  42. - (instancetype)initWithAPIService:(id<GACDeviceCheckAPIServiceProtocol>)APIService
  43. deviceTokenGenerator:(id<GACDeviceCheckTokenGenerator>)deviceTokenGenerator
  44. backoffWrapper:(id<GACAppCheckBackoffWrapperProtocol>)backoffWrapper {
  45. self = [super init];
  46. if (self) {
  47. _APIService = APIService;
  48. _deviceTokenGenerator = deviceTokenGenerator;
  49. _backoffWrapper = backoffWrapper;
  50. }
  51. return self;
  52. }
  53. - (instancetype)initWithAPIService:(id<GACDeviceCheckAPIServiceProtocol>)APIService {
  54. GACAppCheckBackoffWrapper *backoffWrapper = [[GACAppCheckBackoffWrapper alloc] init];
  55. return [self initWithAPIService:APIService
  56. deviceTokenGenerator:[DCDevice currentDevice]
  57. backoffWrapper:backoffWrapper];
  58. }
  59. - (instancetype)initWithServiceName:(NSString *)serviceName
  60. resourceName:(NSString *)resourceName
  61. APIKey:(nullable NSString *)APIKey
  62. requestHooks:(nullable NSArray<GACAppCheckAPIRequestHook> *)requestHooks {
  63. NSURLSession *URLSession = [NSURLSession
  64. sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
  65. GACAppCheckAPIService *APIService =
  66. [[GACAppCheckAPIService alloc] initWithURLSession:URLSession
  67. baseURL:nil
  68. APIKey:APIKey
  69. requestHooks:requestHooks];
  70. GACDeviceCheckAPIService *deviceCheckAPIService =
  71. [[GACDeviceCheckAPIService alloc] initWithAPIService:APIService resourceName:resourceName];
  72. return [self initWithAPIService:deviceCheckAPIService];
  73. }
  74. #pragma mark - GACAppCheckProvider
  75. - (void)getTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable token,
  76. NSError *_Nullable error))handler {
  77. [self.backoffWrapper
  78. applyBackoffToOperation:^FBLPromise *_Nonnull {
  79. return [self getTokenPromise];
  80. }
  81. errorHandler:[self.backoffWrapper defaultAppCheckProviderErrorHandler]]
  82. // Call the handler with either token or error.
  83. .then(^id(GACAppCheckToken *appCheckToken) {
  84. handler(appCheckToken, nil);
  85. return nil;
  86. })
  87. .catch(^void(NSError *error) {
  88. handler(nil, error);
  89. });
  90. }
  91. - (FBLPromise<GACAppCheckToken *> *)getTokenPromise {
  92. // Get DeviceCheck token
  93. return [self deviceToken]
  94. // Exchange DeviceCheck token for FAC token.
  95. .then(^FBLPromise<GACAppCheckToken *> *(NSData *deviceToken) {
  96. return [self.APIService appCheckTokenWithDeviceToken:deviceToken];
  97. });
  98. }
  99. #pragma mark - DeviceCheck
  100. - (FBLPromise<NSData *> *)deviceToken {
  101. return [FBLPromise
  102. wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
  103. [self.deviceTokenGenerator generateTokenWithCompletionHandler:handler];
  104. }];
  105. }
  106. @end
  107. NS_ASSUME_NONNULL_END
  108. #endif // GAC_DEVICE_CHECK_SUPPORTED_TARGETS