FIRAppCheckDebugProviderAPIService.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/DebugProvider/API/FIRAppCheckDebugProviderAPIService.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import <GoogleUtilities/GULURLSessionDataResponse.h>
  23. #import <GoogleUtilities/NSURLSession+GULPromises.h>
  24. #import "FirebaseAppCheck/Sources/Core/APIService/FIRAppCheckAPIService.h"
  25. #import "FirebaseAppCheck/Sources/Core/APIService/FIRAppCheckToken+APIResponse.h"
  26. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  27. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"
  28. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  29. NS_ASSUME_NONNULL_BEGIN
  30. static NSString *const kContentTypeKey = @"Content-Type";
  31. static NSString *const kJSONContentType = @"application/json";
  32. static NSString *const kDebugTokenField = @"debug_token";
  33. @interface FIRAppCheckDebugProviderAPIService ()
  34. @property(nonatomic, readonly) id<FIRAppCheckAPIServiceProtocol> APIService;
  35. @property(nonatomic, readonly) NSString *projectID;
  36. @property(nonatomic, readonly) NSString *appID;
  37. @end
  38. @implementation FIRAppCheckDebugProviderAPIService
  39. - (instancetype)initWithAPIService:(id<FIRAppCheckAPIServiceProtocol>)APIService
  40. projectID:(NSString *)projectID
  41. appID:(NSString *)appID {
  42. self = [super init];
  43. if (self) {
  44. _APIService = APIService;
  45. _projectID = projectID;
  46. _appID = appID;
  47. }
  48. return self;
  49. }
  50. #pragma mark - Public API
  51. - (FBLPromise<FIRAppCheckToken *> *)appCheckTokenWithDebugToken:(NSString *)debugToken {
  52. NSString *URLString =
  53. [NSString stringWithFormat:@"%@/projects/%@/apps/%@:exchangeDebugToken",
  54. self.APIService.baseURL, self.projectID, self.appID];
  55. NSURL *URL = [NSURL URLWithString:URLString];
  56. return [self HTTPBodyWithDebugToken:debugToken]
  57. .then(^FBLPromise<GULURLSessionDataResponse *> *(NSData *HTTPBody) {
  58. return [self.APIService sendRequestWithURL:URL
  59. HTTPMethod:@"POST"
  60. body:HTTPBody
  61. additionalHeaders:@{kContentTypeKey : kJSONContentType}];
  62. })
  63. .then(^id _Nullable(GULURLSessionDataResponse *_Nullable response) {
  64. return [self.APIService appCheckTokenWithAPIResponse:response];
  65. });
  66. }
  67. #pragma mark - Helpers
  68. - (FBLPromise<NSData *> *)HTTPBodyWithDebugToken:(NSString *)debugToken {
  69. if (debugToken.length <= 0) {
  70. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  71. [rejectedPromise
  72. reject:[FIRAppCheckErrorUtil errorWithFailureReason:@"Debug token must not be empty."]];
  73. return rejectedPromise;
  74. }
  75. return [FBLPromise onQueue:[self backgroundQueue]
  76. do:^id _Nullable {
  77. NSError *encodingError;
  78. NSData *payloadJSON = [NSJSONSerialization
  79. dataWithJSONObject:@{kDebugTokenField : debugToken}
  80. options:0
  81. error:&encodingError];
  82. if (payloadJSON != nil) {
  83. return payloadJSON;
  84. } else {
  85. return [FIRAppCheckErrorUtil JSONSerializationError:encodingError];
  86. }
  87. }];
  88. }
  89. - (dispatch_queue_t)backgroundQueue {
  90. return dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
  91. }
  92. @end
  93. NS_ASSUME_NONNULL_END