FIRAppCheckDebugProvider.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 AppCheckCore;
  23. #import "FirebaseAppCheck/Sources/Core/FIRApp+AppCheck.h"
  24. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"
  25. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"
  26. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckValidator.h"
  27. #import "FirebaseAppCheck/Sources/Core/FIRHeartbeatLogger+AppCheck.h"
  28. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  29. NS_ASSUME_NONNULL_BEGIN
  30. @interface FIRAppCheckDebugProvider ()
  31. @property(nonatomic, readonly) GACAppCheckDebugProvider *debugProvider;
  32. @end
  33. @implementation FIRAppCheckDebugProvider
  34. - (instancetype)initWithDebugProvider:(GACAppCheckDebugProvider *)debugProvider {
  35. self = [super init];
  36. if (self) {
  37. _debugProvider = debugProvider;
  38. }
  39. return self;
  40. }
  41. - (nullable instancetype)initWithApp:(FIRApp *)app {
  42. NSArray<NSString *> *missingOptionsFields =
  43. [FIRAppCheckValidator tokenExchangeMissingFieldsInOptions:app.options];
  44. if (missingOptionsFields.count > 0) {
  45. FIRLogError(kFIRLoggerAppCheck, kFIRLoggerAppCheckMessageDebugProviderIncompleteFIROptions,
  46. @"Cannot instantiate `FIRAppCheckDebugProvider` for app: %@. The following "
  47. @"`FirebaseOptions` fields are missing: %@",
  48. app.name, [missingOptionsFields componentsJoinedByString:@", "]);
  49. return nil;
  50. }
  51. GACAppCheckDebugProvider *debugProvider =
  52. [[GACAppCheckDebugProvider alloc] initWithServiceName:app.name
  53. resourceName:app.resourceName
  54. APIKey:app.options.APIKey
  55. requestHooks:@[ [app.heartbeatLogger requestHook] ]];
  56. return [self initWithDebugProvider:debugProvider];
  57. }
  58. - (NSString *)currentDebugToken {
  59. return [self.debugProvider currentDebugToken];
  60. }
  61. - (NSString *)localDebugToken {
  62. return [self.debugProvider localDebugToken];
  63. }
  64. #pragma mark - FIRAppCheckProvider
  65. - (void)getTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable token,
  66. NSError *_Nullable error))handler {
  67. [self.debugProvider getTokenWithCompletion:^(GACAppCheckToken *_Nullable internalToken,
  68. NSError *_Nullable error) {
  69. if (error) {
  70. handler(nil, error);
  71. return;
  72. }
  73. handler([[FIRAppCheckToken alloc] initWithInternalToken:internalToken], nil);
  74. }];
  75. }
  76. @end
  77. NS_ASSUME_NONNULL_END