FIRInstanceIDTokenDeleteOperation.m 5.1 KB

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