| 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/FIRMessagingTokenOperation.h"
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  | 
        
        
            | 
            | 
           20 | 
           #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  | 
        
        
            | 
            | 
           21 | 
           #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  | 
        
        
            | 
            | 
           22 | 
           #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  | 
        
        
            | 
            | 
           23 | 
           #import "FirebaseMessaging/Sources/FIRMessaging_Private.h"
  | 
        
        
            | 
            | 
           24 | 
           #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  | 
        
        
            | 
            | 
           25 | 
           #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinPreferences.h"
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           static const NSInteger kFIRMessagingPlatformVersionIOS = 2;
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           // Scope parameter that defines the service using the token
  | 
        
        
            | 
            | 
           30 | 
           static NSString *const kFIRMessagingParamScope = @"X-scope";
  | 
        
        
            | 
            | 
           31 | 
           // Defines the SDK version
  | 
        
        
            | 
            | 
           32 | 
           static NSString *const kFIRMessagingParamFCMLibVersion = @"X-cliv";
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           @interface FIRMessagingTokenOperation () {
  | 
        
        
            | 
            | 
           35 | 
             BOOL _isFinished;
  | 
        
        
            | 
            | 
           36 | 
             BOOL _isExecuting;
  | 
        
        
            | 
            | 
           37 | 
             NSMutableArray<FIRMessagingTokenOperationCompletion> *_completionHandlers;
  | 
        
        
            | 
            | 
           38 | 
             FIRMessagingCheckinPreferences *_checkinPreferences;
  | 
        
        
            | 
            | 
           39 | 
           }
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
           @property(nonatomic, readwrite, strong) NSString *instanceID;
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           @property(atomic, strong, nullable) NSString *FISAuthToken;
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
           @end
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
           @implementation FIRMessagingTokenOperation
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
           - (instancetype)initWithAction:(FIRMessagingTokenAction)action
  | 
        
        
            | 
            | 
           50 | 
                      forAuthorizedEntity:(NSString *)authorizedEntity
  | 
        
        
            | 
            | 
           51 | 
                                    scope:(NSString *)scope
  | 
        
        
            | 
            | 
           52 | 
                                  options:(NSDictionary<NSString *, NSString *> *)options
  | 
        
        
            | 
            | 
           53 | 
                       checkinPreferences:(FIRMessagingCheckinPreferences *)checkinPreferences
  | 
        
        
            | 
            | 
           54 | 
                               instanceID:(NSString *)instanceID {
  | 
        
        
            | 
            | 
           55 | 
             self = [super init];
  | 
        
        
            | 
            | 
           56 | 
             if (self) {
  | 
        
        
            | 
            | 
           57 | 
               _action = action;
  | 
        
        
            | 
            | 
           58 | 
               _authorizedEntity = [authorizedEntity copy];
  | 
        
        
            | 
            | 
           59 | 
               _scope = [scope copy];
  | 
        
        
            | 
            | 
           60 | 
               _options = [options copy];
  | 
        
        
            | 
            | 
           61 | 
               _checkinPreferences = checkinPreferences;
  | 
        
        
            | 
            | 
           62 | 
               _instanceID = instanceID;
  | 
        
        
            | 
            | 
           63 | 
               _completionHandlers = [[NSMutableArray alloc] init];
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
               _isExecuting = NO;
  | 
        
        
            | 
            | 
           66 | 
               _isFinished = NO;
  | 
        
        
            | 
            | 
           67 | 
             }
  | 
        
        
            | 
            | 
           68 | 
             return self;
  | 
        
        
            | 
            | 
           69 | 
           }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
           - (void)dealloc {
  | 
        
        
            | 
            | 
           72 | 
             [_completionHandlers removeAllObjects];
  | 
        
        
            | 
            | 
           73 | 
           }
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
           - (void)addCompletionHandler:(FIRMessagingTokenOperationCompletion)handler {
  | 
        
        
            | 
            | 
           76 | 
             [_completionHandlers addObject:[handler copy]];
  | 
        
        
            | 
            | 
           77 | 
           }
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
           - (BOOL)isAsynchronous {
  | 
        
        
            | 
            | 
           80 | 
             return YES;
  | 
        
        
            | 
            | 
           81 | 
           }
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
           - (BOOL)isExecuting {
  | 
        
        
            | 
            | 
           84 | 
             return _isExecuting;
  | 
        
        
            | 
            | 
           85 | 
           }
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           - (void)setExecuting:(BOOL)executing {
  | 
        
        
            | 
            | 
           88 | 
             [self willChangeValueForKey:@"isExecuting"];
  | 
        
        
            | 
            | 
           89 | 
             _isExecuting = executing;
  | 
        
        
            | 
            | 
           90 | 
             [self didChangeValueForKey:@"isExecuting"];
  | 
        
        
            | 
            | 
           91 | 
           }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
           - (BOOL)isFinished {
  | 
        
        
            | 
            | 
           94 | 
             return _isFinished;
  | 
        
        
            | 
            | 
           95 | 
           }
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
           - (void)setFinished:(BOOL)finished {
  | 
        
        
            | 
            | 
           98 | 
             [self willChangeValueForKey:@"isFinished"];
  | 
        
        
            | 
            | 
           99 | 
             _isFinished = finished;
  | 
        
        
            | 
            | 
           100 | 
             [self didChangeValueForKey:@"isFinished"];
  | 
        
        
            | 
            | 
           101 | 
           }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
           - (void)start {
  | 
        
        
            | 
            | 
           104 | 
             if (self.isCancelled) {
  | 
        
        
            | 
            | 
           105 | 
               [self finishWithResult:FIRMessagingTokenOperationCancelled token:nil error:nil];
  | 
        
        
            | 
            | 
           106 | 
               return;
  | 
        
        
            | 
            | 
           107 | 
             }
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
             // Quickly validate whether or not the operation has all it needs to begin
  | 
        
        
            | 
            | 
           110 | 
             BOOL checkinfoAvailable = [self.checkinPreferences hasCheckinInfo];
  | 
        
        
            | 
            | 
           111 | 
             if (!checkinfoAvailable) {
  | 
        
        
            | 
            | 
           112 | 
               FIRMessagingErrorCode errorCode = kFIRMessagingErrorCodeRegistrarFailedToCheckIn;
  | 
        
        
            | 
            | 
           113 | 
               [self finishWithResult:FIRMessagingTokenOperationError
  | 
        
        
            | 
            | 
           114 | 
                                token:nil
  | 
        
        
            | 
            | 
           115 | 
                                error:[NSError messagingErrorWithCode:errorCode
  | 
        
        
            | 
            | 
           116 | 
                                                        failureReason:
  | 
        
        
            | 
            | 
           117 | 
                                                            @"Failed to checkin before token registration."]];
  | 
        
        
            | 
            | 
           118 | 
               return;
  | 
        
        
            | 
            | 
           119 | 
             }
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
             [self setExecuting:YES];
  | 
        
        
            | 
            | 
           122 | 
              | 
        
        
            | 
            | 
           123 | 
             [[FIRInstallations installations]
  | 
        
        
            | 
            | 
           124 | 
                 authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  | 
        
        
            | 
            | 
           125 | 
                                           NSError *_Nullable error) {
  | 
        
        
            | 
            | 
           126 | 
                   if (tokenResult.authToken.length > 0) {
  | 
        
        
            | 
            | 
           127 | 
                     self.FISAuthToken = tokenResult.authToken;
  | 
        
        
            | 
            | 
           128 | 
                     [self performTokenOperation];
  | 
        
        
            | 
            | 
           129 | 
                   } else {
  | 
        
        
            | 
            | 
           130 | 
                     [self finishWithResult:FIRMessagingTokenOperationError token:nil error:error];
  | 
        
        
            | 
            | 
           131 | 
                   }
  | 
        
        
            | 
            | 
           132 | 
                 }];
  | 
        
        
            | 
            | 
           133 | 
           }
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
           - (void)finishWithResult:(FIRMessagingTokenOperationResult)result
  | 
        
        
            | 
            | 
           136 | 
                              token:(nullable NSString *)token
  | 
        
        
            | 
            | 
           137 | 
                              error:(nullable NSError *)error {
  | 
        
        
            | 
            | 
           138 | 
             // Add a check to prevent this finish from being called more than once.
  | 
        
        
            | 
            | 
           139 | 
             if (self.isFinished) {
  | 
        
        
            | 
            | 
           140 | 
               return;
  | 
        
        
            | 
            | 
           141 | 
             }
  | 
        
        
            | 
            | 
           142 | 
             self.dataTask = nil;
  | 
        
        
            | 
            | 
           143 | 
             _result = result;
  | 
        
        
            | 
            | 
           144 | 
             for (FIRMessagingTokenOperationCompletion completionHandler in _completionHandlers) {
  | 
        
        
            | 
            | 
           145 | 
               completionHandler(result, token, error);
  | 
        
        
            | 
            | 
           146 | 
             }
  | 
        
        
            | 
            | 
           147 | 
              | 
        
        
            | 
            | 
           148 | 
             [self setExecuting:NO];
  | 
        
        
            | 
            | 
           149 | 
             [self setFinished:YES];
  | 
        
        
            | 
            | 
           150 | 
           }
  | 
        
        
            | 
            | 
           151 | 
              | 
        
        
            | 
            | 
           152 | 
           - (void)cancel {
  | 
        
        
            | 
            | 
           153 | 
             [super cancel];
  | 
        
        
            | 
            | 
           154 | 
             [self.dataTask cancel];
  | 
        
        
            | 
            | 
           155 | 
             [self finishWithResult:FIRMessagingTokenOperationCancelled token:nil error:nil];
  | 
        
        
            | 
            | 
           156 | 
           }
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
            | 
            | 
           158 | 
           - (void)performTokenOperation {
  | 
        
        
            | 
            | 
           159 | 
           }
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
           - (NSMutableURLRequest *)tokenRequest {
  | 
        
        
            | 
            | 
           162 | 
             NSString *authHeader =
  | 
        
        
            | 
            | 
           163 | 
                 [FIRMessagingTokenOperation HTTPAuthHeaderFromCheckin:self.checkinPreferences];
  | 
        
        
            | 
            | 
           164 | 
             return [[self class] requestWithAuthHeader:authHeader FISAuthToken:self.FISAuthToken];
  | 
        
        
            | 
            | 
           165 | 
           }
  | 
        
        
            | 
            | 
           166 | 
              | 
        
        
            | 
            | 
           167 | 
           #pragma mark - Request Construction
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
           + (NSMutableURLRequest *)requestWithAuthHeader:(NSString *)authHeaderString
  | 
        
        
            | 
            | 
           170 | 
                                             FISAuthToken:(NSString *)FISAuthToken {
  | 
        
        
            | 
            | 
           171 | 
             NSURL *url = [NSURL URLWithString:FIRMessagingTokenRegisterServer()];
  | 
        
        
            | 
            | 
           172 | 
             NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
             // Add HTTP headers
  | 
        
        
            | 
            | 
           175 | 
             [request setValue:authHeaderString forHTTPHeaderField:@"Authorization"];
  | 
        
        
            | 
            | 
           176 | 
             [request setValue:FIRMessagingAppIdentifier() forHTTPHeaderField:@"app"];
  | 
        
        
            | 
            | 
           177 | 
             if (FISAuthToken) {
  | 
        
        
            | 
            | 
           178 | 
               [request setValue:FISAuthToken forHTTPHeaderField:@"x-goog-firebase-installations-auth"];
  | 
        
        
            | 
            | 
           179 | 
             }
  | 
        
        
            | 
            | 
           180 | 
             request.HTTPMethod = @"POST";
  | 
        
        
            | 
            | 
           181 | 
             return request;
  | 
        
        
            | 
            | 
           182 | 
           }
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
           + (NSMutableArray<NSURLQueryItem *> *)standardQueryItemsWithDeviceID:(NSString *)deviceID
  | 
        
        
            | 
            | 
           185 | 
                                                                          scope:(NSString *)scope {
  | 
        
        
            | 
            | 
           186 | 
             NSMutableArray<NSURLQueryItem *> *queryItems = [NSMutableArray arrayWithCapacity:8];
  | 
        
        
            | 
            | 
           187 | 
              | 
        
        
            | 
            | 
           188 | 
             // E.g. X-osv=10.2.1
  | 
        
        
            | 
            | 
           189 | 
             NSString *systemVersion = [GULAppEnvironmentUtil systemVersion];
  | 
        
        
            | 
            | 
           190 | 
             [queryItems addObject:[NSURLQueryItem queryItemWithName:@"X-osv" value:systemVersion]];
  | 
        
        
            | 
            | 
           191 | 
             // E.g. device=
  | 
        
        
            | 
            | 
           192 | 
             if (deviceID) {
  | 
        
        
            | 
            | 
           193 | 
               [queryItems addObject:[NSURLQueryItem queryItemWithName:@"device" value:deviceID]];
  | 
        
        
            | 
            | 
           194 | 
             }
  | 
        
        
            | 
            | 
           195 | 
             // E.g. X-scope=fcm
  | 
        
        
            | 
            | 
           196 | 
             if (scope) {
  | 
        
        
            | 
            | 
           197 | 
               [queryItems addObject:[NSURLQueryItem queryItemWithName:kFIRMessagingParamScope value:scope]];
  | 
        
        
            | 
            | 
           198 | 
             }
  | 
        
        
            | 
            | 
           199 | 
             // E.g. plat=2
  | 
        
        
            | 
            | 
           200 | 
             NSString *platform = [NSString stringWithFormat:@"%ld", (long)kFIRMessagingPlatformVersionIOS];
  | 
        
        
            | 
            | 
           201 | 
             [queryItems addObject:[NSURLQueryItem queryItemWithName:@"plat" value:platform]];
  | 
        
        
            | 
            | 
           202 | 
             // E.g. app=com.myapp.foo
  | 
        
        
            | 
            | 
           203 | 
             NSString *appIdentifier = FIRMessagingAppIdentifier();
  | 
        
        
            | 
            | 
           204 | 
             [queryItems addObject:[NSURLQueryItem queryItemWithName:@"app" value:appIdentifier]];
  | 
        
        
            | 
            | 
           205 | 
             // E.g. app_ver=1.5
  | 
        
        
            | 
            | 
           206 | 
             NSString *appVersion = FIRMessagingCurrentAppVersion();
  | 
        
        
            | 
            | 
           207 | 
             [queryItems addObject:[NSURLQueryItem queryItemWithName:@"app_ver" value:appVersion]];
  | 
        
        
            | 
            | 
           208 | 
             // E.g. X-cliv=fiid-1.2.3
  | 
        
        
            | 
            | 
           209 | 
             NSString *fcmLibraryVersion =
  | 
        
        
            | 
            | 
           210 | 
                 [NSString stringWithFormat:@"fiid-%@", [FIRMessaging FIRMessagingSDKVersion]];
  | 
        
        
            | 
            | 
           211 | 
             if (fcmLibraryVersion.length) {
  | 
        
        
            | 
            | 
           212 | 
               NSURLQueryItem *gcmLibVersion =
  | 
        
        
            | 
            | 
           213 | 
                   [NSURLQueryItem queryItemWithName:kFIRMessagingParamFCMLibVersion value:fcmLibraryVersion];
  | 
        
        
            | 
            | 
           214 | 
               [queryItems addObject:gcmLibVersion];
  | 
        
        
            | 
            | 
           215 | 
             }
  | 
        
        
            | 
            | 
           216 | 
              | 
        
        
            | 
            | 
           217 | 
             return queryItems;
  | 
        
        
            | 
            | 
           218 | 
           }
  | 
        
        
            | 
            | 
           219 | 
              | 
        
        
            | 
            | 
           220 | 
           #pragma mark -  Header
  | 
        
        
            | 
            | 
           221 | 
              | 
        
        
            | 
            | 
           222 | 
           + (NSString *)HTTPAuthHeaderFromCheckin:(FIRMessagingCheckinPreferences *)checkin {
  | 
        
        
            | 
            | 
           223 | 
             NSString *deviceID = checkin.deviceID;
  | 
        
        
            | 
            | 
           224 | 
             NSString *secret = checkin.secretToken;
  | 
        
        
            | 
            | 
           225 | 
             return [NSString stringWithFormat:@"AidLogin %@:%@", deviceID, secret];
  | 
        
        
            | 
            | 
           226 | 
           }
  | 
        
        
            | 
            | 
           227 | 
           @end
  |