GACDeviceCheckAPIService.m 4.0 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 "AppCheckCore/Sources/DeviceCheckProvider/API/GACDeviceCheckAPIService.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 "AppCheckCore/Sources/Core/APIService/GACAppCheckAPIService.h"
  24. #import "AppCheckCore/Sources/Core/APIService/GACAppCheckToken+APIResponse.h"
  25. #import "AppCheckCore/Sources/Core/Errors/GACAppCheckErrorUtil.h"
  26. #import "AppCheckCore/Sources/Core/GACAppCheckLogger+Internal.h"
  27. NS_ASSUME_NONNULL_BEGIN
  28. static NSString *const kContentTypeKey = @"Content-Type";
  29. static NSString *const kJSONContentType = @"application/json";
  30. static NSString *const kDeviceTokenField = @"device_token";
  31. @interface GACDeviceCheckAPIService ()
  32. @property(nonatomic, readonly) id<GACAppCheckAPIServiceProtocol> APIService;
  33. @property(nonatomic, readonly) NSString *resourceName;
  34. @end
  35. @implementation GACDeviceCheckAPIService
  36. - (instancetype)initWithAPIService:(id<GACAppCheckAPIServiceProtocol>)APIService
  37. resourceName:(NSString *)resourceName {
  38. self = [super init];
  39. if (self) {
  40. _APIService = APIService;
  41. _resourceName = resourceName;
  42. }
  43. return self;
  44. }
  45. #pragma mark - Public API
  46. - (FBLPromise<GACAppCheckToken *> *)appCheckTokenWithDeviceToken:(NSData *)deviceToken {
  47. NSString *URLString = [NSString stringWithFormat:@"%@/%@:exchangeDeviceCheckToken",
  48. self.APIService.baseURL, self.resourceName];
  49. NSURL *URL = [NSURL URLWithString:URLString];
  50. return [self HTTPBodyWithDeviceToken:deviceToken]
  51. .then(^FBLPromise<GULURLSessionDataResponse *> *(NSData *HTTPBody) {
  52. return [self.APIService sendRequestWithURL:URL
  53. HTTPMethod:@"POST"
  54. body:HTTPBody
  55. additionalHeaders:@{kContentTypeKey : kJSONContentType}];
  56. })
  57. .then(^id _Nullable(GULURLSessionDataResponse *_Nullable response) {
  58. return [self.APIService appCheckTokenWithAPIResponse:response];
  59. });
  60. }
  61. - (FBLPromise<NSData *> *)HTTPBodyWithDeviceToken:(NSData *)deviceToken {
  62. if (deviceToken.length <= 0) {
  63. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  64. [rejectedPromise reject:[GACAppCheckErrorUtil
  65. errorWithFailureReason:@"DeviceCheck token must not be empty."]];
  66. return rejectedPromise;
  67. }
  68. return [FBLPromise onQueue:[self backgroundQueue]
  69. do:^id _Nullable {
  70. NSString *base64EncodedToken =
  71. [deviceToken base64EncodedStringWithOptions:0];
  72. NSError *encodingError;
  73. NSData *payloadJSON = [NSJSONSerialization
  74. dataWithJSONObject:@{kDeviceTokenField : base64EncodedToken}
  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