Sin descripción

FIRInstanceIDTokenDeleteOperation.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2019 Google
  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 "FIRInstanceIDTokenDeleteOperation.h"
  17. #import "FIRInstanceIDCheckinPreferences.h"
  18. #import "FIRInstanceIDDefines.h"
  19. #import "FIRInstanceIDLogger.h"
  20. #import "FIRInstanceIDTokenOperation+Private.h"
  21. #import "FIRInstanceIDURLQueryItem.h"
  22. #import "FIRInstanceIDUtilities.h"
  23. #import "NSError+FIRInstanceID.h"
  24. @implementation FIRInstanceIDTokenDeleteOperation
  25. - (instancetype)initWithAuthorizedEntity:(NSString *)authorizedEntity
  26. scope:(NSString *)scope
  27. checkinPreferences:(FIRInstanceIDCheckinPreferences *)checkinPreferences
  28. keyPair:(FIRInstanceIDKeyPair *)keyPair
  29. action:(FIRInstanceIDTokenAction)action {
  30. self = [super initWithAction:action
  31. forAuthorizedEntity:authorizedEntity
  32. scope:scope
  33. options:nil
  34. checkinPreferences:checkinPreferences
  35. keyPair:keyPair];
  36. if (self) {
  37. }
  38. return self;
  39. }
  40. - (void)performTokenOperation {
  41. NSString *authHeader =
  42. [FIRInstanceIDTokenOperation HTTPAuthHeaderFromCheckin:self.checkinPreferences];
  43. NSMutableURLRequest *request = [FIRInstanceIDTokenOperation requestWithAuthHeader:authHeader];
  44. // Build form-encoded body
  45. NSString *deviceAuthID = self.checkinPreferences.deviceID;
  46. NSMutableArray<FIRInstanceIDURLQueryItem *> *queryItems =
  47. [FIRInstanceIDTokenOperation standardQueryItemsWithDeviceID:deviceAuthID scope:self.scope];
  48. [queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"delete" value:@"true"]];
  49. if (self.action == FIRInstanceIDTokenActionDeleteTokenAndIID) {
  50. [queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"iid-operation"
  51. value:@"delete"]];
  52. }
  53. if (self.authorizedEntity) {
  54. [queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"sender"
  55. value:self.authorizedEntity]];
  56. }
  57. // Typically we include our public key-signed url items, but in some cases (like deleting all FCM
  58. // tokens), we don't.
  59. if (self.keyPair != nil) {
  60. [queryItems addObjectsFromArray:[self queryItemsWithKeyPair:self.keyPair]];
  61. }
  62. NSString *content = FIRInstanceIDQueryFromQueryItems(queryItems);
  63. request.HTTPBody = [content dataUsingEncoding:NSUTF8StringEncoding];
  64. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenDeleteOperationFetchRequest,
  65. @"Unregister request to %@ content: %@", FIRInstanceIDRegisterServer(),
  66. content);
  67. FIRInstanceID_WEAKIFY(self);
  68. void (^requestHandler)(NSData *, NSURLResponse *, NSError *) =
  69. ^(NSData *data, NSURLResponse *response, NSError *error) {
  70. FIRInstanceID_STRONGIFY(self);
  71. [self handleResponseWithData:data response:response error:error];
  72. };
  73. // Test block
  74. if (self.testBlock) {
  75. self.testBlock(request, requestHandler);
  76. return;
  77. }
  78. NSURLSession *session = [FIRInstanceIDTokenOperation sharedURLSession];
  79. self.dataTask = [session dataTaskWithRequest:request completionHandler:requestHandler];
  80. [self.dataTask resume];
  81. }
  82. - (void)handleResponseWithData:(NSData *)data
  83. response:(NSURLResponse *)response
  84. error:(NSError *)error {
  85. if (error) {
  86. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenDeleteOperationRequestError,
  87. @"Device unregister HTTP fetch error. Error code: %ld",
  88. _FIRInstanceID_L(error.code));
  89. [self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
  90. return;
  91. }
  92. NSString *dataResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  93. if (dataResponse.length == 0) {
  94. NSError *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeUnknown];
  95. [self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
  96. return;
  97. }
  98. if (![dataResponse hasPrefix:@"deleted="] && ![dataResponse hasPrefix:@"token="]) {
  99. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenDeleteOperationBadResponse,
  100. @"Invalid unregister response %@", response);
  101. NSError *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeUnknown];
  102. [self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
  103. return;
  104. }
  105. [self finishWithResult:FIRInstanceIDTokenOperationSucceeded token:nil error:nil];
  106. }
  107. @end