GACAppCheckDebugProviderAPIService.m 3.8 KB

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