GACAppCheckDebugProviderAPIService.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "AppCheckCore/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 "AppCheckCore/Sources/Core/APIService/GACAppCheckAPIService.h"
  25. #import "AppCheckCore/Sources/Core/APIService/GACAppCheckToken+APIResponse.h"
  26. #import "AppCheckCore/Sources/Core/Errors/GACAppCheckErrorUtil.h"
  27. #import "AppCheckCore/Sources/Core/GACAppCheckLogger+Internal.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. static NSString *const kLimitedUseField = @"limited_use";
  33. @interface GACAppCheckDebugProviderAPIService ()
  34. @property(nonatomic, readonly) id<GACAppCheckAPIServiceProtocol> APIService;
  35. @property(nonatomic, readonly) NSString *resourceName;
  36. // TODO(andrewheard): Remove or refactor property when short-lived token feature is implemented.
  37. // When `YES`, forces a short-lived token with a 5 minute TTL.
  38. @property(nonatomic, readonly) BOOL limitedUse;
  39. @end
  40. @implementation GACAppCheckDebugProviderAPIService
  41. - (instancetype)initWithAPIService:(id<GACAppCheckAPIServiceProtocol>)APIService
  42. resourceName:(NSString *)resourceName
  43. limitedUse:(BOOL)limitedUse {
  44. self = [super init];
  45. if (self) {
  46. _APIService = APIService;
  47. _resourceName = resourceName;
  48. _limitedUse = limitedUse;
  49. }
  50. return self;
  51. }
  52. #pragma mark - Public API
  53. - (FBLPromise<GACAppCheckToken *> *)appCheckTokenWithDebugToken:(NSString *)debugToken {
  54. NSString *URLString = [NSString
  55. stringWithFormat:@"%@/%@:exchangeDebugToken", self.APIService.baseURL, self.resourceName];
  56. NSURL *URL = [NSURL URLWithString:URLString];
  57. return [self HTTPBodyWithDebugToken:debugToken]
  58. .then(^FBLPromise<GULURLSessionDataResponse *> *(NSData *HTTPBody) {
  59. return [self.APIService sendRequestWithURL:URL
  60. HTTPMethod:@"POST"
  61. body:HTTPBody
  62. additionalHeaders:@{kContentTypeKey : kJSONContentType}];
  63. })
  64. .then(^id _Nullable(GULURLSessionDataResponse *_Nullable response) {
  65. return [self.APIService appCheckTokenWithAPIResponse:response];
  66. });
  67. }
  68. #pragma mark - Helpers
  69. - (FBLPromise<NSData *> *)HTTPBodyWithDebugToken:(NSString *)debugToken {
  70. if (debugToken.length <= 0) {
  71. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  72. [rejectedPromise
  73. reject:[GACAppCheckErrorUtil errorWithFailureReason:@"Debug token must not be empty."]];
  74. return rejectedPromise;
  75. }
  76. return [FBLPromise onQueue:[self backgroundQueue]
  77. do:^id _Nullable {
  78. NSError *encodingError;
  79. NSData *payloadJSON =
  80. [NSJSONSerialization dataWithJSONObject:@{
  81. kDebugTokenField : debugToken,
  82. kLimitedUseField : @(self.limitedUse)
  83. }
  84. options:0
  85. error:&encodingError];
  86. if (payloadJSON != nil) {
  87. return payloadJSON;
  88. } else {
  89. return [GACAppCheckErrorUtil JSONSerializationError:encodingError];
  90. }
  91. }];
  92. }
  93. - (dispatch_queue_t)backgroundQueue {
  94. return dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
  95. }
  96. @end
  97. NS_ASSUME_NONNULL_END