FIRAppCheckDebugProvider.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/FIRAppCheckDebugProvider.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/Core/APIService/FIRAppCheckAPIService.h"
  23. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"
  24. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckValidator.h"
  25. #import "FirebaseAppCheck/Sources/DebugProvider/API/FIRAppCheckDebugProviderAPIService.h"
  26. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h"
  27. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  28. NS_ASSUME_NONNULL_BEGIN
  29. static NSString *const kDebugTokenEnvKey = @"FIRAAppCheckDebugToken";
  30. static NSString *const kDebugTokenUserDefaultsKey = @"FIRAAppCheckDebugToken";
  31. @interface FIRAppCheckDebugProvider ()
  32. @property(nonatomic, readonly) id<FIRAppCheckDebugProviderAPIServiceProtocol> APIService;
  33. @end
  34. @implementation FIRAppCheckDebugProvider
  35. - (instancetype)initWithAPIService:(id<FIRAppCheckDebugProviderAPIServiceProtocol>)APIService {
  36. self = [super init];
  37. if (self) {
  38. _APIService = APIService;
  39. }
  40. return self;
  41. }
  42. - (nullable instancetype)initWithApp:(FIRApp *)app {
  43. NSArray<NSString *> *missingOptionsFields =
  44. [FIRAppCheckValidator tokenExchangeMissingFieldsInOptions:app.options];
  45. if (missingOptionsFields.count > 0) {
  46. FIRLogError(kFIRLoggerAppCheck, kFIRLoggerAppCheckMessageCodeUnknown,
  47. @"Cannot instantiate `FIRAppCheckDebugProvider` for app: %@. The following "
  48. @"`FirebaseOptions` fields are missing: %@",
  49. app.name, [missingOptionsFields componentsJoinedByString:@", "]);
  50. return nil;
  51. }
  52. NSURLSession *URLSession = [NSURLSession
  53. sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
  54. FIRAppCheckAPIService *APIService =
  55. [[FIRAppCheckAPIService alloc] initWithURLSession:URLSession
  56. APIKey:app.options.APIKey
  57. projectID:app.options.projectID
  58. appID:app.options.googleAppID];
  59. FIRAppCheckDebugProviderAPIService *debugAPIService =
  60. [[FIRAppCheckDebugProviderAPIService alloc] initWithAPIService:APIService
  61. projectID:app.options.projectID
  62. appID:app.options.googleAppID];
  63. return [self initWithAPIService:debugAPIService];
  64. }
  65. - (NSString *)currentDebugToken {
  66. NSString *envVariableValue = [[NSProcessInfo processInfo] environment][kDebugTokenEnvKey];
  67. if (envVariableValue.length > 0) {
  68. return envVariableValue;
  69. } else {
  70. return [self localDebugToken];
  71. }
  72. }
  73. - (NSString *)localDebugToken {
  74. return [self storedDebugToken] ?: [self generateAndStoreDebugToken];
  75. }
  76. - (nullable NSString *)storedDebugToken {
  77. return [[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey];
  78. }
  79. - (void)storeDebugToken:(nullable NSString *)token {
  80. [[NSUserDefaults standardUserDefaults] setObject:token forKey:kDebugTokenUserDefaultsKey];
  81. }
  82. - (NSString *)generateAndStoreDebugToken {
  83. NSString *token = [NSUUID UUID].UUIDString;
  84. [self storeDebugToken:token];
  85. return token;
  86. }
  87. #pragma mark - FIRAppCheckProvider
  88. - (void)getTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable token,
  89. NSError *_Nullable error))handler {
  90. [FBLPromise do:^NSString * {
  91. return [self currentDebugToken];
  92. }]
  93. .then(^FBLPromise<FIRAppCheckToken *> *(NSString *debugToken) {
  94. return [self.APIService appCheckTokenWithDebugToken:debugToken];
  95. })
  96. .then(^id(FIRAppCheckToken *appCheckToken) {
  97. handler(appCheckToken, nil);
  98. return nil;
  99. })
  100. .catch(^void(NSError *error) {
  101. FIRLogDebug(kFIRLoggerAppCheck, kFIRLoggerAppCheckMessageCodeUnknown,
  102. @"Failed to exchange debug token to app check token: %@", error);
  103. handler(nil, error);
  104. });
  105. }
  106. @end
  107. NS_ASSUME_NONNULL_END