FIRDeviceCheckProvider.m 6.2 KB

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