Files
firebase-ios-sdk/Firebase/InstanceID/FIRInstanceIDTokenFetchOperation.m
T
Maksym MalyhinandChen Liang f62c7ddcec IID: version dependent on FirebaseInstallations (#4533)
* Firebase Installations: Instance ID now uses FIS under the hood. (#3398)

* Fix merge errors

* Use modular import for FIRInstanceID_Private.h

* Use modular imports for FirebaseInstanceID.h (#3509)

* Changes missing during the fix.

* Changes missing during merge.

* Fix header import.

* ./scripts/style.sh

* Fix FCM tests. (#3888)

* Fix IID FCM token check.

* FirebaseInstallation: merge master into FIS integration branch (#4076)

* Fix IID FCM token check.

* Unused constant deleted.

* FCM tests: fix bad merge.

* Remote Config test app: use local IID and FIS. (#4089)

* token request should pass through if token is not fresh (#4104)

* Merge errors fix.

* IID with FIS: send FIS auth token with IID token request (#4152)

* FIRInstanceIDTokenOperation: add FIS auth token to the IID token request.

* IID tests: don't use FIS private headers.

* IID: indication of compatibility with FIS (#4262)

* FIS: IID incompatible version exception.

* FirebaseInstallations.podspec: set preprocessor flags based on env vars.

* FIS Travis: set required env vars

* /scripts/style.sh

* IID: method to indicate FIS usege added.

* FIS: FIRInstantiationTimingEagerInDefaultApp

* FIS: FIRInstantiationTimingAlwaysEager

* IID: make compatibility method static.

* Make -[FIRInstanceID appInstanceID:] safe to be called from any thread (#4482)

* IID: use a cached value of FID for -[FIRInstanceID appInstanceID:]

* TODO comment

* TODO updated

* FirebaseInstallations: prepare IID with FIS dependency for release (#4522)

* Bump FIrebaseInstallations to 1.0.0

* Travis: remove mm/fis-integration-master branch

* Travis: if_changed script updated to reflect IID dependency on FIS.

* Revert "Travis: remove mm/fis-integration-master branch"

This reverts commit 534dff990a.

* Travis: remove mm/fis-integration-master branch (#4526)

* Formatting and spelling.

* Firebase example proj: pod deintegrate

Co-authored-by: Chen Liang <chliang@google.com>
2019-12-20 10:54:11 -05:00

208 lines
9.0 KiB
Objective-C

/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FIRInstanceIDTokenFetchOperation.h"
#import "FIRInstanceIDCheckinPreferences.h"
#import "FIRInstanceIDConstants.h"
#import "FIRInstanceIDDefines.h"
#import "FIRInstanceIDLogger.h"
#import "FIRInstanceIDTokenOperation+Private.h"
#import "FIRInstanceIDURLQueryItem.h"
#import "FIRInstanceIDUtilities.h"
#import "NSError+FIRInstanceID.h"
#import <FirebaseCore/FIRAppInternal.h>
#import <FirebaseCore/FIRHeartbeatInfo.h>
// We can have a static int since this error should theoretically only
// happen once (for the first time). If it repeats there is something
// else that is wrong.
static int phoneRegistrationErrorRetryCount = 0;
static const int kMaxPhoneRegistrationErrorRetryCount = 10;
NSString *const kFIRInstanceIDFirebaseUserAgentKey = @"X-firebase-client";
NSString *const kFIRInstanceIDFirebaseHeartbeatKey = @"X-firebase-client-log-type";
NSString *const kFIRInstanceIDHeartbeatTag = @"fire-iid";
@implementation FIRInstanceIDTokenFetchOperation
- (instancetype)initWithAuthorizedEntity:(NSString *)authorizedEntity
scope:(NSString *)scope
options:(nullable NSDictionary<NSString *, NSString *> *)options
checkinPreferences:(FIRInstanceIDCheckinPreferences *)checkinPreferences
instanceID:(NSString *)instanceID {
self = [super initWithAction:FIRInstanceIDTokenActionFetch
forAuthorizedEntity:authorizedEntity
scope:scope
options:options
checkinPreferences:checkinPreferences
instanceID:instanceID];
if (self) {
}
return self;
}
- (void)performTokenOperation {
NSMutableURLRequest *request = [self tokenRequest];
NSString *checkinVersionInfo = self.checkinPreferences.versionInfo;
[request setValue:checkinVersionInfo forHTTPHeaderField:@"info"];
[request setValue:[FIRApp firebaseUserAgent]
forHTTPHeaderField:kFIRInstanceIDFirebaseUserAgentKey];
[request setValue:@([FIRHeartbeatInfo heartbeatCodeForTag:kFIRInstanceIDHeartbeatTag]).stringValue
forHTTPHeaderField:kFIRInstanceIDFirebaseHeartbeatKey];
// Build form-encoded body
NSString *deviceAuthID = self.checkinPreferences.deviceID;
NSMutableArray<FIRInstanceIDURLQueryItem *> *queryItems =
[[self class] standardQueryItemsWithDeviceID:deviceAuthID scope:self.scope];
[queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"sender"
value:self.authorizedEntity]];
[queryItems addObject:[FIRInstanceIDURLQueryItem queryItemWithName:@"X-subtype"
value:self.authorizedEntity]];
[queryItems addObjectsFromArray:[self queryItemsWithInstanceID:self.instanceID]];
// Create query items from passed-in options
id apnsTokenData = self.options[kFIRInstanceIDTokenOptionsAPNSKey];
id apnsSandboxValue = self.options[kFIRInstanceIDTokenOptionsAPNSIsSandboxKey];
if ([apnsTokenData isKindOfClass:[NSData class]] &&
[apnsSandboxValue isKindOfClass:[NSNumber class]]) {
NSString *APNSString = FIRInstanceIDAPNSTupleStringForTokenAndServerType(
apnsTokenData, ((NSNumber *)apnsSandboxValue).boolValue);
// The name of the query item happens to be the same as the dictionary key
FIRInstanceIDURLQueryItem *item =
[FIRInstanceIDURLQueryItem queryItemWithName:kFIRInstanceIDTokenOptionsAPNSKey
value:APNSString];
[queryItems addObject:item];
}
id firebaseAppID = self.options[kFIRInstanceIDTokenOptionsFirebaseAppIDKey];
if ([firebaseAppID isKindOfClass:[NSString class]]) {
// The name of the query item happens to be the same as the dictionary key
FIRInstanceIDURLQueryItem *item =
[FIRInstanceIDURLQueryItem queryItemWithName:kFIRInstanceIDTokenOptionsFirebaseAppIDKey
value:(NSString *)firebaseAppID];
[queryItems addObject:item];
}
NSString *content = FIRInstanceIDQueryFromQueryItems(queryItems);
request.HTTPBody = [content dataUsingEncoding:NSUTF8StringEncoding];
FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenFetchOperationFetchRequest,
@"Register request to %@ content: %@", FIRInstanceIDRegisterServer(),
content);
FIRInstanceID_WEAKIFY(self);
void (^requestHandler)(NSData *, NSURLResponse *, NSError *) =
^(NSData *data, NSURLResponse *response, NSError *error) {
FIRInstanceID_STRONGIFY(self);
[self handleResponseWithData:data response:response error:error];
};
// Test block
if (self.testBlock) {
self.testBlock(request, requestHandler);
return;
}
NSURLSession *session = [FIRInstanceIDTokenOperation sharedURLSession];
self.dataTask = [session dataTaskWithRequest:request completionHandler:requestHandler];
[self.dataTask resume];
}
#pragma mark - Request Handling
- (void)handleResponseWithData:(NSData *)data
response:(NSURLResponse *)response
error:(NSError *)error {
if (error) {
FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenFetchOperationRequestError,
@"Token fetch HTTP error. Error Code: %ld", (long)error.code);
[self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
return;
}
NSString *dataResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (dataResponse.length == 0) {
NSError *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeUnknown];
[self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:error];
return;
}
NSDictionary *parsedResponse = [self parseFetchTokenResponse:dataResponse];
if ([parsedResponse[@"token"] length]) {
[self finishWithResult:FIRInstanceIDTokenOperationSucceeded
token:parsedResponse[@"token"]
error:nil];
return;
}
NSString *errorValue = parsedResponse[@"Error"];
NSError *responseError;
if (errorValue.length) {
NSArray *errorComponents = [errorValue componentsSeparatedByString:@":"];
// HACK (Kansas replication delay), PHONE_REGISTRATION_ERROR on App
// uninstall and reinstall.
if ([errorComponents containsObject:@"PHONE_REGISTRATION_ERROR"]) {
// Encountered issue http://b/27043795
// Retry register until successful or another error encountered or a
// certain number of tries are over.
if (phoneRegistrationErrorRetryCount < kMaxPhoneRegistrationErrorRetryCount) {
const int nextRetryInterval = 1 << phoneRegistrationErrorRetryCount;
FIRInstanceID_WEAKIFY(self);
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(nextRetryInterval * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
FIRInstanceID_STRONGIFY(self);
phoneRegistrationErrorRetryCount++;
[self performTokenOperation];
});
return;
}
} else if ([errorComponents containsObject:kFIRInstanceID_CMD_RST]) {
// Server detected the identity we use is no longer valid.
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:kFIRInstanceIDIdentityInvalidatedNotification object:nil];
FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeInternal001,
@"Identity is invalid. Server request identity reset.");
responseError =
[NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeInvalidIdentity];
}
}
if (!responseError) {
FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeTokenFetchOperationBadResponse,
@"Invalid fetch response, expected 'token' or 'Error' key");
responseError = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeUnknown];
}
[self finishWithResult:FIRInstanceIDTokenOperationError token:nil error:responseError];
}
// expect a response e.g. "token=<reg id>\nGOOG.ttl=123"
- (NSDictionary *)parseFetchTokenResponse:(NSString *)response {
NSArray *lines = [response componentsSeparatedByString:@"\n"];
NSMutableDictionary *parsedResponse = [NSMutableDictionary dictionary];
for (NSString *line in lines) {
NSArray *keyAndValue = [line componentsSeparatedByString:@"="];
if ([keyAndValue count] > 1) {
parsedResponse[keyAndValue[0]] = keyAndValue[1];
}
}
return parsedResponse;
}
@end