FIRDeviceCheckProvider.m 6.1 KB

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