FIRDeviceCheckProvider.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckAvailability.h"
  17. #if FIR_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 "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRDeviceCheckProvider.h"
  25. #import "FirebaseAppCheck/Sources/Core/APIService/FIRAppCheckAPIService.h"
  26. #import "FirebaseAppCheck/Sources/Core/Backoff/FIRAppCheckBackoffWrapper.h"
  27. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"
  28. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckValidator.h"
  29. #import "FirebaseAppCheck/Sources/DeviceCheckProvider/API/FIRDeviceCheckAPIService.h"
  30. #import "FirebaseAppCheck/Sources/DeviceCheckProvider/DCDevice+FIRDeviceCheckTokenGenerator.h"
  31. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h"
  32. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  33. NS_ASSUME_NONNULL_BEGIN
  34. @interface FIRDeviceCheckProvider ()
  35. @property(nonatomic, readonly) id<FIRDeviceCheckAPIServiceProtocol> APIService;
  36. @property(nonatomic, readonly) id<FIRDeviceCheckTokenGenerator> deviceTokenGenerator;
  37. @property(nonatomic, readonly) id<FIRAppCheckBackoffWrapperProtocol> backoffWrapper;
  38. - (instancetype)initWithAPIService:(id<FIRDeviceCheckAPIServiceProtocol>)APIService
  39. deviceTokenGenerator:(id<FIRDeviceCheckTokenGenerator>)deviceTokenGenerator
  40. backoffWrapper:(id<FIRAppCheckBackoffWrapperProtocol>)backoffWrapper
  41. NS_DESIGNATED_INITIALIZER;
  42. @end
  43. @implementation FIRDeviceCheckProvider
  44. - (instancetype)initWithAPIService:(id<FIRDeviceCheckAPIServiceProtocol>)APIService
  45. deviceTokenGenerator:(id<FIRDeviceCheckTokenGenerator>)deviceTokenGenerator
  46. backoffWrapper:(id<FIRAppCheckBackoffWrapperProtocol>)backoffWrapper {
  47. self = [super init];
  48. if (self) {
  49. _APIService = APIService;
  50. _deviceTokenGenerator = deviceTokenGenerator;
  51. _backoffWrapper = backoffWrapper;
  52. }
  53. return self;
  54. }
  55. - (instancetype)initWithAPIService:(id<FIRDeviceCheckAPIServiceProtocol>)APIService {
  56. FIRAppCheckBackoffWrapper *backoffWrapper = [[FIRAppCheckBackoffWrapper alloc] init];
  57. return [self initWithAPIService:APIService
  58. deviceTokenGenerator:[DCDevice currentDevice]
  59. backoffWrapper:backoffWrapper];
  60. }
  61. - (nullable instancetype)initWithApp:(FIRApp *)app {
  62. NSArray<NSString *> *missingOptionsFields =
  63. [FIRAppCheckValidator tokenExchangeMissingFieldsInOptions:app.options];
  64. if (missingOptionsFields.count > 0) {
  65. FIRLogError(kFIRLoggerAppCheck,
  66. kFIRLoggerAppCheckMessageDeviceCheckProviderIncompleteFIROptions,
  67. @"Cannot instantiate `FIRDeviceCheckProvider` for app: %@. The following "
  68. @"`FirebaseOptions` fields are missing: %@",
  69. app.name, [missingOptionsFields componentsJoinedByString:@", "]);
  70. return nil;
  71. }
  72. NSURLSession *URLSession = [NSURLSession
  73. sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
  74. FIRAppCheckAPIService *APIService =
  75. [[FIRAppCheckAPIService alloc] initWithURLSession:URLSession
  76. APIKey:app.options.APIKey
  77. projectID:app.options.projectID
  78. appID:app.options.googleAppID];
  79. FIRDeviceCheckAPIService *deviceCheckAPIService =
  80. [[FIRDeviceCheckAPIService alloc] initWithAPIService:APIService
  81. projectID:app.options.projectID
  82. appID:app.options.googleAppID];
  83. return [self initWithAPIService:deviceCheckAPIService];
  84. }
  85. #pragma mark - FIRAppCheckProvider
  86. - (void)getTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable token,
  87. NSError *_Nullable error))handler {
  88. [self.backoffWrapper
  89. applyBackoffToOperation:^FBLPromise *_Nonnull {
  90. return [self getTokenPromise];
  91. }
  92. errorHandler:[self.backoffWrapper defaultAppCheckProviderErrorHandler]]
  93. // Call the handler with either token or error.
  94. .then(^id(FIRAppCheckToken *appCheckToken) {
  95. handler(appCheckToken, nil);
  96. return nil;
  97. })
  98. .catch(^void(NSError *error) {
  99. handler(nil, error);
  100. });
  101. }
  102. - (FBLPromise<FIRAppCheckToken *> *)getTokenPromise {
  103. // Get DeviceCheck token
  104. return [self deviceToken]
  105. // Exchange DeviceCheck token for FAC token.
  106. .then(^FBLPromise<FIRAppCheckToken *> *(NSData *deviceToken) {
  107. return [self.APIService appCheckTokenWithDeviceToken:deviceToken];
  108. });
  109. }
  110. #pragma mark - DeviceCheck
  111. - (FBLPromise<NSData *> *)deviceToken {
  112. return [FBLPromise
  113. wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
  114. [self.deviceTokenGenerator generateTokenWithCompletionHandler:handler];
  115. }];
  116. }
  117. @end
  118. NS_ASSUME_NONNULL_END
  119. #endif // FIR_DEVICE_CHECK_SUPPORTED_TARGETS