| 1 | 
           efrain | 
           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 | 
              | 
        
        
            | 
            | 
           17 | 
           #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenDeleteOperation.h"
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  | 
        
        
            | 
            | 
           20 | 
           #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  | 
        
        
            | 
            | 
           21 | 
           #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  | 
        
        
            | 
            | 
           22 | 
           #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  | 
        
        
            | 
            | 
           23 | 
           #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  | 
        
        
            | 
            | 
           24 | 
           #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinPreferences.h"
  | 
        
        
            | 
            | 
           25 | 
           #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenOperation.h"
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           @implementation FIRMessagingTokenDeleteOperation
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           - (instancetype)initWithAuthorizedEntity:(NSString *)authorizedEntity
  | 
        
        
            | 
            | 
           30 | 
                                              scope:(NSString *)scope
  | 
        
        
            | 
            | 
           31 | 
                                 checkinPreferences:(FIRMessagingCheckinPreferences *)checkinPreferences
  | 
        
        
            | 
            | 
           32 | 
                                         instanceID:(NSString *)instanceID
  | 
        
        
            | 
            | 
           33 | 
                                             action:(FIRMessagingTokenAction)action {
  | 
        
        
            | 
            | 
           34 | 
             return [super initWithAction:action
  | 
        
        
            | 
            | 
           35 | 
                      forAuthorizedEntity:authorizedEntity
  | 
        
        
            | 
            | 
           36 | 
                                    scope:scope
  | 
        
        
            | 
            | 
           37 | 
                                  options:nil
  | 
        
        
            | 
            | 
           38 | 
                       checkinPreferences:checkinPreferences
  | 
        
        
            | 
            | 
           39 | 
                               instanceID:instanceID];
  | 
        
        
            | 
            | 
           40 | 
           }
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
           - (void)performTokenOperation {
  | 
        
        
            | 
            | 
           43 | 
             NSMutableURLRequest *request = [self tokenRequest];
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
             // Build form-encoded body
  | 
        
        
            | 
            | 
           46 | 
             NSString *deviceAuthID = self.checkinPreferences.deviceID;
  | 
        
        
            | 
            | 
           47 | 
             NSMutableArray<NSURLQueryItem *> *queryItems =
  | 
        
        
            | 
            | 
           48 | 
                 [FIRMessagingTokenOperation standardQueryItemsWithDeviceID:deviceAuthID scope:self.scope];
  | 
        
        
            | 
            | 
           49 | 
             [queryItems addObject:[NSURLQueryItem queryItemWithName:@"delete" value:@"true"]];
  | 
        
        
            | 
            | 
           50 | 
             if (self.action == FIRMessagingTokenActionDeleteTokenAndIID) {
  | 
        
        
            | 
            | 
           51 | 
               [queryItems addObject:[NSURLQueryItem queryItemWithName:@"iid-operation" value:@"delete"]];
  | 
        
        
            | 
            | 
           52 | 
             }
  | 
        
        
            | 
            | 
           53 | 
             if (self.authorizedEntity) {
  | 
        
        
            | 
            | 
           54 | 
               [queryItems addObject:[NSURLQueryItem queryItemWithName:@"sender" value:self.authorizedEntity]];
  | 
        
        
            | 
            | 
           55 | 
             }
  | 
        
        
            | 
            | 
           56 | 
             // Typically we include our public key-signed url items, but in some cases (like deleting all FCM
  | 
        
        
            | 
            | 
           57 | 
             // tokens), we don't.
  | 
        
        
            | 
            | 
           58 | 
             if (self.instanceID.length > 0) {
  | 
        
        
            | 
            | 
           59 | 
               [queryItems addObject:[NSURLQueryItem queryItemWithName:kFIRMessagingParamInstanceID
  | 
        
        
            | 
            | 
           60 | 
                                                                 value:self.instanceID]];
  | 
        
        
            | 
            | 
           61 | 
             }
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
             NSURLComponents *components = [[NSURLComponents alloc] init];
  | 
        
        
            | 
            | 
           64 | 
             components.queryItems = queryItems;
  | 
        
        
            | 
            | 
           65 | 
             NSString *content = components.query;
  | 
        
        
            | 
            | 
           66 | 
             request.HTTPBody = [content dataUsingEncoding:NSUTF8StringEncoding];
  | 
        
        
            | 
            | 
           67 | 
             FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenDeleteOperationFetchRequest,
  | 
        
        
            | 
            | 
           68 | 
                                     @"Unregister request to %@ content: %@",
  | 
        
        
            | 
            | 
           69 | 
                                     FIRMessagingTokenRegisterServer(), content);
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
             FIRMessaging_WEAKIFY(self);
  | 
        
        
            | 
            | 
           72 | 
             void (^requestHandler)(NSData *, NSURLResponse *, NSError *) =
  | 
        
        
            | 
            | 
           73 | 
                 ^(NSData *data, NSURLResponse *response, NSError *error) {
  | 
        
        
            | 
            | 
           74 | 
                   FIRMessaging_STRONGIFY(self);
  | 
        
        
            | 
            | 
           75 | 
                   [self handleResponseWithData:data response:response error:error];
  | 
        
        
            | 
            | 
           76 | 
                 };
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
             NSURLSessionConfiguration *config = NSURLSessionConfiguration.defaultSessionConfiguration;
  | 
        
        
            | 
            | 
           79 | 
             config.timeoutIntervalForResource = 60.0f;  // 1 minute
  | 
        
        
            | 
            | 
           80 | 
             NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
  | 
        
        
            | 
            | 
           81 | 
             self.dataTask = [session dataTaskWithRequest:request completionHandler:requestHandler];
  | 
        
        
            | 
            | 
           82 | 
             [self.dataTask resume];
  | 
        
        
            | 
            | 
           83 | 
           }
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
           - (void)handleResponseWithData:(NSData *)data
  | 
        
        
            | 
            | 
           86 | 
                                 response:(NSURLResponse *)response
  | 
        
        
            | 
            | 
           87 | 
                                    error:(NSError *)error {
  | 
        
        
            | 
            | 
           88 | 
             if (error) {
  | 
        
        
            | 
            | 
           89 | 
               FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenDeleteOperationRequestError,
  | 
        
        
            | 
            | 
           90 | 
                                       @"Device unregister HTTP fetch error. Error code: %ld",
  | 
        
        
            | 
            | 
           91 | 
                                       (long)error.code);
  | 
        
        
            | 
            | 
           92 | 
               [self finishWithResult:FIRMessagingTokenOperationError token:nil error:error];
  | 
        
        
            | 
            | 
           93 | 
               return;
  | 
        
        
            | 
            | 
           94 | 
             }
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
             NSString *dataResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  | 
        
        
            | 
            | 
           97 | 
             if (dataResponse.length == 0) {
  | 
        
        
            | 
            | 
           98 | 
               NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  | 
        
        
            | 
            | 
           99 | 
                                                  failureReason:@"Empty response."];
  | 
        
        
            | 
            | 
           100 | 
               [self finishWithResult:FIRMessagingTokenOperationError token:nil error:error];
  | 
        
        
            | 
            | 
           101 | 
               return;
  | 
        
        
            | 
            | 
           102 | 
             }
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
             if (![dataResponse hasPrefix:@"deleted="] && ![dataResponse hasPrefix:@"token="]) {
  | 
        
        
            | 
            | 
           105 | 
               NSString *failureReason =
  | 
        
        
            | 
            | 
           106 | 
                   [NSString stringWithFormat:@"Invalid unregister response %@", response];
  | 
        
        
            | 
            | 
           107 | 
               FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenDeleteOperationBadResponse, @"%@",
  | 
        
        
            | 
            | 
           108 | 
                                       failureReason);
  | 
        
        
            | 
            | 
           109 | 
               NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  | 
        
        
            | 
            | 
           110 | 
                                                  failureReason:failureReason];
  | 
        
        
            | 
            | 
           111 | 
               [self finishWithResult:FIRMessagingTokenOperationError token:nil error:error];
  | 
        
        
            | 
            | 
           112 | 
               return;
  | 
        
        
            | 
            | 
           113 | 
             }
  | 
        
        
            | 
            | 
           114 | 
             [self finishWithResult:FIRMessagingTokenOperationSucceeded token:nil error:nil];
  | 
        
        
            | 
            | 
           115 | 
           }
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
           @end
  |