16
16
17
17
#import " FirebaseRemoteConfig/Sources/RCNConfigFetch.h"
18
18
19
+ #import < FirebaseCore/FIRApp.h>
19
20
#import < FirebaseCore/FIRLogger.h>
20
21
#import < FirebaseCore/FIROptions.h>
21
- #import < FirebaseInstanceID/FIRInstanceID+Private.h>
22
- #import < FirebaseInstanceID/FIRInstanceIDCheckinPreferences.h>
22
+ #import < FirebaseInstallations/FirebaseInstallations.h>
23
23
#import < GoogleUtilities/GULNSData+zlib.h>
24
24
#import " FirebaseRemoteConfig/Sources/Private/RCNConfigSettings.h"
25
25
#import " FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
40
40
static NSString *const kServerURLQuery = @" :fetch?" ;
41
41
static NSString *const kServerURLKey = @" key=" ;
42
42
static NSString *const kRequestJSONKeyAppID = @" app_id" ;
43
- static NSString *const kRequestJSONKeyAppInstanceID = @" app_instance_id" ;
44
43
45
44
static NSString *const kHTTPMethodPost = @" POST" ; // /< HTTP request method config fetch using
46
45
static NSString *const kContentTypeHeaderName = @" Content-Type" ; // /< HTTP Header Field Name
49
48
static NSString *const kAcceptEncodingHeaderName = @" Accept-Encoding" ; // /< HTTP Header Field Name
50
49
static NSString *const kETagHeaderName = @" etag" ; // /< HTTP Header Field Name
51
50
static NSString *const kIfNoneMatchETagHeaderName = @" if-none-match" ; // /< HTTP Header Field Name
51
+ static NSString *const kInstallationsAuthTokenHeaderName = @" x-goog-firebase-installations-auth" ;
52
52
// Sends the bundle ID. Refer to b/130301479 for details.
53
53
static NSString *const kiOSBundleIdentifierHeaderName =
54
54
@" X-Ios-Bundle-Identifier" ; // /< HTTP Header Field Name
55
55
56
56
// / Config HTTP request content type proto buffer
57
57
static NSString *const kContentTypeValueJSON = @" application/json" ;
58
- static NSString *const kInstanceIDScopeConfig = @" *" ; // / InstanceID scope
59
58
60
59
// / HTTP status codes. Ref: https://cloud.google.com/apis/design/errors#error_retries
61
60
static NSInteger const kRCNFetchResponseHTTPStatusCodeOK = 200 ;
@@ -192,18 +191,22 @@ - (void)fetchConfigWithExpirationDuration:(NSTimeInterval)expirationDuration
192
191
withError: error];
193
192
}
194
193
strongSelf->_settings .isFetchInProgress = YES ;
195
- [strongSelf refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler : completionHandler];
194
+ [strongSelf refreshInstallationsTokenWithCompletionHandler : completionHandler];
196
195
});
197
196
}
198
197
199
198
#pragma mark - Fetch helpers
200
199
201
- // / Refresh instance ID token before fetching config. Instance ID is now mandatory for fetch
200
+ - (NSString *)FIRAppNameFromFullyQualifiedNamespace {
201
+ return [[_FIRNamespace componentsSeparatedByString: @" :" ] lastObject ];
202
+ }
203
+ // / Refresh installation ID token before fetching config. installation ID is now mandatory for fetch
202
204
// / requests to work.(b/14751422).
203
- - (void )refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler :
205
+ - (void )refreshInstallationsTokenWithCompletionHandler :
204
206
(FIRRemoteConfigFetchCompletion)completionHandler {
205
- FIRInstanceID *instanceID = [FIRInstanceID instanceID ];
206
- if (!_options.GCMSenderID ) {
207
+ FIRInstallations *installations = [FIRInstallations
208
+ installationsWithApp: [FIRApp appNamed: [self FIRAppNameFromFullyQualifiedNamespace ]]];
209
+ if (!installations || !_options.GCMSenderID ) {
207
210
NSString *errorDescription = @" Failed to get GCMSenderID" ;
208
211
FIRLogError (kFIRLoggerRemoteConfig , @" I-RCN000074" , @" %@ " ,
209
212
[NSString stringWithFormat: @" %@ " , errorDescription]);
@@ -217,10 +220,11 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
217
220
NSLocalizedDescriptionKey : errorDescription
218
221
}]];
219
222
}
220
- FIRInstanceIDTokenHandler instanceIDHandler = ^(NSString *token, NSError *error) {
221
- if (!token || error) {
223
+ FIRInstallationsTokenHandler installationsTokenHandler = ^(
224
+ FIRInstallationsAuthTokenResult *tokenResult, NSError *error) {
225
+ if (!tokenResult || !tokenResult.authToken || error) {
222
226
NSString *errorDescription =
223
- [NSString stringWithFormat: @" Failed to get InstanceID token. Error : %@ ." , error];
227
+ [NSString stringWithFormat: @" Failed to get installations token. Error : %@ ." , error];
224
228
FIRLogError (kFIRLoggerRemoteConfig , @" I-RCN000073" , @" %@ " ,
225
229
[NSString stringWithFormat: @" %@ " , errorDescription]);
226
230
self->_settings .isFetchInProgress = NO ;
@@ -234,20 +238,21 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
234
238
}]];
235
239
}
236
240
237
- // If the token is available, try to get the instanceID .
241
+ // We have a valid token. Get the backing installationID .
238
242
__weak RCNConfigFetch *weakSelf = self;
239
- [instanceID getIDWithHandler: ^(NSString *_Nullable identity, NSError *_Nullable error) {
243
+ [installations installationIDWithCompletion: ^(NSString *_Nullable identifier,
244
+ NSError *_Nullable error) {
240
245
RCNConfigFetch *strongSelf = weakSelf;
241
246
242
247
// Dispatch to the RC serial queue to update settings on the queue.
243
248
dispatch_async (strongSelf->_lockQueue , ^{
244
249
RCNConfigFetch *strongSelfQueue = weakSelf;
245
250
246
251
// Update config settings with the IID and token.
247
- strongSelfQueue->_settings .configInstanceIDToken = [token copy ] ;
248
- strongSelfQueue->_settings .configInstanceID = identity ;
252
+ strongSelfQueue->_settings .configInstallationsToken = tokenResult. authToken ;
253
+ strongSelfQueue->_settings .configInstallationsIdentifier = identifier ;
249
254
250
- if (!identity || error) {
255
+ if (!identifier || error) {
251
256
NSString *errorDescription =
252
257
[NSString stringWithFormat: @" Error getting iid : %@ ." , error];
253
258
FIRLogError (kFIRLoggerRemoteConfig , @" I-RCN000055" , @" %@ " ,
@@ -265,56 +270,23 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
265
270
}
266
271
267
272
FIRLogInfo (kFIRLoggerRemoteConfig , @" I-RCN000022" , @" Success to get iid : %@ ." ,
268
- strongSelfQueue->_settings .configInstanceID );
269
-
270
- // Continue the fetch regardless of whether fetch of instance ID succeeded.
271
- [strongSelfQueue fetchCheckinInfoWithCompletionHandler: completionHandler];
273
+ strongSelfQueue->_settings .configInstallationsIdentifier );
274
+ [strongSelf
275
+ getAnalyticsUserPropertiesWithCompletionHandler: ^(NSDictionary *userProperties) {
276
+ dispatch_async (strongSelf->_lockQueue , ^{
277
+ [strongSelf fetchWithUserProperties: userProperties
278
+ completionHandler: completionHandler];
279
+ });
280
+ }];
272
281
});
273
282
}];
274
283
};
275
- FIRLogDebug (kFIRLoggerRemoteConfig , @" I-RCN000039" , @" Starting requesting token." );
276
- // Note: We expect the GCMSenderID to always be available by the time this request is made.
277
- [instanceID tokenWithAuthorizedEntity: _options.GCMSenderID
278
- scope: kInstanceIDScopeConfig
279
- options: nil
280
- handler: instanceIDHandler];
281
- }
282
284
283
- // / Fetch checkin info before fetching config. Checkin info including device authentication ID,
284
- // / secret token and device data version are optional fields in config request.
285
- - (void )fetchCheckinInfoWithCompletionHandler : (FIRRemoteConfigFetchCompletion)completionHandler {
286
- FIRInstanceID *instanceID = [FIRInstanceID instanceID ];
287
- __weak RCNConfigFetch *weakSelf = self;
288
- [instanceID fetchCheckinInfoWithHandler: ^(FIRInstanceIDCheckinPreferences *preferences,
289
- NSError *error) {
290
- RCNConfigFetch *fetchCheckinInfoWithHandlerSelf = weakSelf;
291
- dispatch_async (fetchCheckinInfoWithHandlerSelf->_lockQueue , ^{
292
- RCNConfigFetch *strongSelf = fetchCheckinInfoWithHandlerSelf;
293
- if (error) {
294
- FIRLogError (kFIRLoggerRemoteConfig , @" I-RCN000023" , @" Failed to fetch checkin info: %@ ." ,
295
- error);
296
- } else {
297
- strongSelf->_settings .deviceAuthID = preferences.deviceID ;
298
- strongSelf->_settings .secretToken = preferences.secretToken ;
299
- strongSelf->_settings .deviceDataVersion = preferences.deviceDataVersion ;
300
- if (strongSelf->_settings .deviceAuthID .length && strongSelf->_settings .secretToken .length ) {
301
- FIRLogInfo (kFIRLoggerRemoteConfig , @" I-RCN000024" ,
302
- @" Success to get device authentication ID: %@ , security token: %@ ." ,
303
- self->_settings .deviceAuthID , self->_settings .secretToken );
304
- }
305
- }
306
- // Checkin info is optional, continue fetch config regardless fetch of checkin info
307
- // succeeded.
308
- [strongSelf fetchWithUserPropertiesCompletionHandler: ^(NSDictionary *userProperties) {
309
- dispatch_async (strongSelf->_lockQueue , ^{
310
- [strongSelf fetchWithUserProperties: userProperties completionHandler: completionHandler];
311
- });
312
- }];
313
- });
314
- }];
285
+ FIRLogDebug (kFIRLoggerRemoteConfig , @" I-RCN000039" , @" Starting requesting token." );
286
+ [installations authTokenWithCompletion: installationsTokenHandler];
315
287
}
316
288
317
- - (void )fetchWithUserPropertiesCompletionHandler :
289
+ - (void )getAnalyticsUserPropertiesWithCompletionHandler :
318
290
(FIRAInteropUserPropertiesCallback)completionHandler {
319
291
FIRLogDebug (kFIRLoggerRemoteConfig , @" I-RCN000060" , @" Fetch with user properties completed." );
320
292
id <FIRAnalyticsInterop> analytics = self->_analytics ;
@@ -590,6 +562,8 @@ - (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
590
562
timeoutInterval: timeoutInterval];
591
563
URLRequest.HTTPMethod = kHTTPMethodPost ;
592
564
[URLRequest setValue: kContentTypeValueJSON forHTTPHeaderField: kContentTypeHeaderName ];
565
+ [URLRequest setValue: _settings.configInstallationsToken
566
+ forHTTPHeaderField: kInstallationsAuthTokenHeaderName ];
593
567
[URLRequest setValue: [[NSBundle mainBundle ] bundleIdentifier ]
594
568
forHTTPHeaderField: kiOSBundleIdentifierHeaderName];
595
569
[URLRequest setValue: @" gzip" forHTTPHeaderField: kContentEncodingHeaderName ];
0 commit comments