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