FIRDeviceCheckAPIService.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/DeviceCheckProvider/API/FIRDeviceCheckAPIService.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 kDeviceTokenField = @"device_token";
  33. @interface FIRDeviceCheckAPIService ()
  34. @property(nonatomic, readonly) id<FIRAppCheckAPIServiceProtocol> APIService;
  35. @property(nonatomic, readonly) NSString *projectID;
  36. @property(nonatomic, readonly) NSString *appID;
  37. @end
  38. @implementation FIRDeviceCheckAPIService
  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 *> *)appCheckTokenWithDeviceToken:(NSData *)deviceToken {
  52. NSString *URLString =
  53. [NSString stringWithFormat:@"%@/projects/%@/apps/%@:exchangeDeviceCheckToken",
  54. self.APIService.baseURL, self.projectID, self.appID];
  55. NSURL *URL = [NSURL URLWithString:URLString];
  56. return [self HTTPBodyWithDeviceToken:deviceToken]
  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. - (FBLPromise<NSData *> *)HTTPBodyWithDeviceToken:(NSData *)deviceToken {
  68. if (deviceToken.length <= 0) {
  69. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  70. [rejectedPromise reject:[FIRAppCheckErrorUtil
  71. errorWithFailureReason:@"DeviceCheck token must not be empty."]];
  72. return rejectedPromise;
  73. }
  74. return [FBLPromise onQueue:[self backgroundQueue]
  75. do:^id _Nullable {
  76. NSString *base64EncodedToken =
  77. [deviceToken base64EncodedStringWithOptions:0];
  78. NSError *encodingError;
  79. NSData *payloadJSON = [NSJSONSerialization
  80. dataWithJSONObject:@{kDeviceTokenField : base64EncodedToken}
  81. options:0
  82. error:&encodingError];
  83. if (payloadJSON != nil) {
  84. return payloadJSON;
  85. } else {
  86. return [FIRAppCheckErrorUtil JSONSerializationError:encodingError];
  87. }
  88. }];
  89. }
  90. - (dispatch_queue_t)backgroundQueue {
  91. return dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
  92. }
  93. @end
  94. NS_ASSUME_NONNULL_END