You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  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. 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<FIRInstanceIDURLQueryItem *> *queryItems =
  45. [FIRInstanceIDTokenOperation standardQueryItemsWithDeviceID:deviceAuthID scope:self.scope];
  46. [queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"delete" value:@"true"]];
  47. if (self.action == FIRInstanceIDTokenActionDeleteTokenAndIID) {
  48. [queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"iid-operation"
  49. value:@"delete"]];
  50. }
  51. if (self.authorizedEntity) {
  52. [queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"sender"
  53. value:self.authorizedEntity]];
  54. }
  55. // Typically we include our public key-signed url items, but in some cases (like deleting all FCM
  56. // tokens), we don't.
  57. if (self.instanceID.length > 0) {
  58. [queryItems addObjectsFromArray:[self queryItemsWithInstanceID:self.instanceID]];
  59. }
  60. NSString *content = FIRInstanceIDQueryFromQueryItems(queryItems);
  61. request.HTTPBody = [content dataUsingEncoding:NSUTF8StringEncoding];
  62. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenDeleteOperationFetchRequest,
  63. @"Unregister request to %@ content: %@", FIRInstanceIDRegisterServer(),
  64. content);
  65. FIRInstanceID_WEAKIFY(self);
  66. void (^requestHandler)(NSData *, NSURLResponse *, NSError *) =
  67. ^(NSData *data, NSURLResponse *response, NSError *error) {
  68. FIRInstanceID_STRONGIFY(self);
  69. [self handleResponseWithData:data response:response error:error];
  70. };
  71. // Test block
  72. if (self.testBlock) {
  73. self.testBlock(request, requestHandler);
  74. return;
  75. }
  76. NSURLSession *session = [FIRInstanceIDTokenOperation sharedURLSession];
  77. self.dataTask = [session dataTaskWithRequest:request completionHandler:requestHandler];
  78. [self.dataTask resume];
  79. }
  80. - (void)handleResponseWithData:(NSData *)data
  81. response:(NSURLResponse *)response
  82. error:(NSError *)error {
  83. if (error) {
  84. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenDeleteOperationRequestError,
  85. @"Device unregister HTTP fetch error. Error code: %ld",
  86. _FIRInstanceID_L(error.code));
  87. [self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
  88. return;
  89. }
  90. NSString *dataResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  91. if (dataResponse.length == 0) {
  92. NSError *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeUnknown];
  93. [self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
  94. return;
  95. }
  96. if (![dataResponse hasPrefix:@"deleted="] && ![dataResponse hasPrefix:@"token="]) {
  97. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenDeleteOperationBadResponse,
  98. @"Invalid unregister response %@", response);
  99. NSError *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeUnknown];
  100. [self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
  101. return;
  102. }
  103. [self finishWithResult:FIRInstanceIDTokenOperationSucceeded token:nil error:nil];
  104. }
  105. @end