Skip to content

Commit c7f80c1

Browse files
authored
Add app ID header to Firebase Auth (#9046)
* Add user agent and heartbeat headers to Firebase Auth. * Add app ID header. * Fix unit tests. * Remove user agent and heartbeat headers, as backend support is not yet ready. * Fix typo. * Update changelog.
1 parent 755f55a commit c7f80c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+302
-57
lines changed

FirebaseAuth/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unreleased
2+
- [changed] Added a `X-Firebase-GMPID` header to network requests.
3+
14
# 8.9.0
25
- [changed] Improved error logging. (#8704)
36
- [added] Added MFA support for email link sign-in. (#8705)

FirebaseAuth/Sources/Auth/FIRAuth.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ + (FIRAuth *)authWithApp:(FIRApp *)app {
453453

454454
- (instancetype)initWithApp:(FIRApp *)app {
455455
[FIRAuth setKeychainServiceNameForApp:app];
456-
self = [self initWithAPIKey:app.options.APIKey appName:app.name];
456+
self = [self initWithAPIKey:app.options.APIKey appName:app.name appID:app.options.googleAppID];
457457
if (self) {
458458
_app = app;
459459
#if TARGET_OS_IOS
@@ -463,11 +463,13 @@ - (instancetype)initWithApp:(FIRApp *)app {
463463
return self;
464464
}
465465

466-
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey appName:(NSString *)appName {
466+
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey
467+
appName:(NSString *)appName
468+
appID:(NSString *)appID {
467469
self = [super init];
468470
if (self) {
469471
_listenerHandles = [NSMutableArray array];
470-
_requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey];
472+
_requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey appID:appID];
471473
_firebaseAppName = [appName copy];
472474
#if TARGET_OS_IOS
473475
_settings = [[FIRAuthSettings alloc] init];

FirebaseAuth/Sources/Auth/FIRAuth_Internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ NS_ASSUME_NONNULL_BEGIN
6666
@brief Designated initializer.
6767
@param APIKey The Google Developers Console API key for making requests from your app.
6868
@param appName The name property of the previously created @c FIRApp instance.
69+
@param appID The app ID of the Firebase application.
6970
*/
7071
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey
71-
appName:(NSString *)appName NS_DESIGNATED_INITIALIZER;
72+
appName:(NSString *)appName
73+
appID:(NSString *)appID NS_DESIGNATED_INITIALIZER;
7274

7375
/** @fn getUserID
7476
@brief Gets the identifier of the current user, if any.

FirebaseAuth/Sources/Backend/FIRAuthBackend.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
*/
8989
static NSString *const kFirebaseLocalHeader = @"X-Firebase-Locale";
9090

91+
/** @var kFirebaseAppIDHeader
92+
@brief HTTP header name for the Firebase app ID.
93+
*/
94+
static NSString *const kFirebaseAppIDHeader = @"X-Firebase-GMPID";
95+
9196
/** @var kFirebaseAuthCoreFrameworkMarker
9297
@brief The marker in the HTTP header that indicates the request comes from Firebase Auth Core.
9398
*/
@@ -640,6 +645,8 @@ - (void)asyncPostToURLWithRequestConfiguration:(FIRAuthRequestConfiguration *)re
640645
[request setValue:clientVersion forHTTPHeaderField:kClientVersionHeader];
641646
NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
642647
[request setValue:bundleID forHTTPHeaderField:kIosBundleIdentifierHeader];
648+
NSString *appID = requestConfiguration.appID;
649+
[request setValue:appID forHTTPHeaderField:kFirebaseAppIDHeader];
643650

644651
NSArray<NSString *> *preferredLocalizations = [NSBundle mainBundle].preferredLocalizations;
645652
if (preferredLocalizations.count) {

FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ NS_ASSUME_NONNULL_BEGIN
3030
*/
3131
@property(nonatomic, copy, readonly) NSString *APIKey;
3232

33+
/** @property appID
34+
@brief The Firebase appID used in the request.
35+
*/
36+
@property(nonatomic, copy, readonly) NSString *appID;
37+
3338
/** @property LanguageCode
3439
@brief The language code used in the request.
3540
*/
@@ -47,11 +52,13 @@ NS_ASSUME_NONNULL_BEGIN
4752

4853
- (instancetype)init NS_UNAVAILABLE;
4954

50-
/** @fn initWithRequestClass:APIKey:authLanguage:
55+
/** @fn initWithAPIKey:appID:
5156
@brief Designated initializer.
5257
@param APIKey The API key to be used in the request.
58+
@param appID The Firebase app ID to be passed in the request header.
5359
*/
54-
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey NS_DESIGNATED_INITIALIZER;
60+
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey
61+
appID:(NSString *)appID NS_DESIGNATED_INITIALIZER;
5562
@end
5663

5764
NS_ASSUME_NONNULL_END

FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222

2323
@implementation FIRAuthRequestConfiguration
2424

25-
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey {
25+
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey appID:(NSString *)appID {
2626
self = [super init];
2727
if (self) {
2828
_APIKey = [APIKey copy];
29+
_appID = [appID copy];
2930
}
3031
return self;
3132
}

FirebaseAuth/Sources/User/FIRUser.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@
118118
*/
119119
static NSString *const kAPIKeyCodingKey = @"APIKey";
120120

121+
/** @var kFirebaseAppIDCodingKey
122+
@brief The key used to encode the appID instance variable for NSSecureCoding.
123+
*/
124+
static NSString *const kFirebaseAppIDCodingKey = @"firebaseAppID";
125+
121126
/** @var kTokenServiceCodingKey
122127
@brief The key used to encode the tokenService instance variable for NSSecureCoding.
123128
*/
@@ -345,6 +350,7 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
345350
forKey:kMetadataCodingKey];
346351
NSString *tenantID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kTenantIDCodingKey];
347352
NSString *APIKey = [aDecoder decodeObjectOfClass:[NSString class] forKey:kAPIKeyCodingKey];
353+
NSString *appID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kFirebaseAppIDCodingKey];
348354
#if TARGET_OS_IOS
349355
FIRMultiFactor *multiFactor = [aDecoder decodeObjectOfClass:[FIRMultiFactor class]
350356
forKey:kMultiFactorCodingKey];
@@ -368,7 +374,7 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
368374
_phoneNumber = phoneNumber;
369375
_metadata = metadata ?: [[FIRUserMetadata alloc] initWithCreationDate:nil lastSignInDate:nil];
370376
_tenantID = tenantID;
371-
_requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey];
377+
_requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey appID:appID];
372378
#if TARGET_OS_IOS
373379
_multiFactor = multiFactor ?: [[FIRMultiFactor alloc] init];
374380
#endif
@@ -389,6 +395,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
389395
[aCoder encodeObject:_metadata forKey:kMetadataCodingKey];
390396
[aCoder encodeObject:_tenantID forKey:kTenantIDCodingKey];
391397
[aCoder encodeObject:_auth.requestConfiguration.APIKey forKey:kAPIKeyCodingKey];
398+
[aCoder encodeObject:_auth.requestConfiguration.appID forKey:kFirebaseAppIDCodingKey];
392399
[aCoder encodeObject:_tokenService forKey:kTokenServiceCodingKey];
393400
#if TARGET_OS_IOS
394401
[aCoder encodeObject:_multiFactor forKey:kMultiFactorCodingKey];

FirebaseAuth/Tests/Unit/FIRAuthBackendCreateAuthURITests.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
*/
3838
static NSString *const kTestAPIKey = @"apikey_value";
3939

40+
/** @var kTestFirebaseAppID
41+
@brief A test value for the Firebase app ID
42+
*/
43+
static NSString *const kTestFirebaseAppID = @"appID";
44+
4045
/** @var kTestExpectedRequestURL
4146
@brief The URL we are expecting should be requested by valid requests.
4247
*/
@@ -69,7 +74,7 @@ - (void)testRequestAndResponseEncoding {
6974
FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
7075
[FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
7176
FIRAuthRequestConfiguration *requestConfiguration =
72-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
77+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
7378
FIRCreateAuthURIRequest *request =
7479
[[FIRCreateAuthURIRequest alloc] initWithIdentifier:kTestIdentifier
7580
continueURI:kTestContinueURI

FirebaseAuth/Tests/Unit/FIRAuthBackendRPCImplementationTests.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
*/
3636
static NSString *const kFakeAPIkey = @"FAKE_API_KEY";
3737

38+
/** @var kFakeFirebaseAppID
39+
@brief Used as a fake Firebase app ID for a fake RPC request. We don't test this here.
40+
*/
41+
static NSString *const kFakeFirebaseAppID = @"FAKE_APP_ID";
42+
3843
/** @var kFakeErrorDomain
3944
@brief A value to use for fake @c NSErrors.
4045
*/
@@ -226,7 +231,7 @@ - (BOOL)containsPostBody {
226231

227232
- (FIRAuthRequestConfiguration *)requestConfiguration {
228233
FIRAuthRequestConfiguration *fakeConfiguration =
229-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIkey];
234+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIkey appID:kFakeFirebaseAppID];
230235
return fakeConfiguration;
231236
}
232237

FirebaseAuth/Tests/Unit/FIRAuthTests.m

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
*/
8181
static NSString *const kAPIKey = @"FAKE_API_KEY";
8282

83+
/** @var kFirebaseAppID
84+
@brief The fake Firebase app ID.
85+
*/
86+
static NSString *const kFirebaseAppID = @"FAKE_APP_ID";
87+
8388
/** @var kAccessToken
8489
@brief The fake access token.
8590
*/
@@ -1860,7 +1865,8 @@ - (void)testUpdateCurrentUserFailure {
18601865
[self waitForSignInWithAccessToken:kTestAccessToken APIKey:kTestAPIKey completion:nil];
18611866
NSString *kTestAPIKey2 = @"fakeAPIKey2";
18621867
FIRUser *user2 = [FIRAuth auth].currentUser;
1863-
user2.requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey2];
1868+
user2.requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey2
1869+
appID:kFirebaseAppID];
18641870
OCMExpect([_mockBackend getAccountInfo:[OCMArg any] callback:[OCMArg any]])
18651871
.andDispatchError2([FIRAuthErrorUtils invalidAPIKeyError]);
18661872
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
@@ -1883,7 +1889,8 @@ - (void)testUpdateCurrentUserFailureNetworkError {
18831889
[self waitForSignInWithAccessToken:kTestAccessToken APIKey:kTestAPIKey completion:nil];
18841890
NSString *kTestAPIKey2 = @"fakeAPIKey2";
18851891
FIRUser *user2 = [FIRAuth auth].currentUser;
1886-
user2.requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey2];
1892+
user2.requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey2
1893+
appID:kFirebaseAppID];
18871894
NSError *underlyingError = [NSError errorWithDomain:@"Test Error" code:1 userInfo:nil];
18881895
OCMExpect([_mockBackend getAccountInfo:[OCMArg any] callback:[OCMArg any]])
18891896
.andDispatchError2([FIRAuthErrorUtils networkErrorWithUnderlyingError:underlyingError]);
@@ -1973,7 +1980,8 @@ - (void)testUpdateCurrentUserSuccess {
19731980

19741981
FIRUser *user1 = [FIRAuth auth].currentUser;
19751982
NSString *kTestAPIKey = @"fakeAPIKey";
1976-
user1.requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
1983+
user1.requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
1984+
appID:kFirebaseAppID];
19771985
[[FIRAuth auth] signOut:nil];
19781986

19791987
NSString *kTestAccessToken2 = @"fakeAccessToken2";
@@ -2641,7 +2649,8 @@ - (void)waitForSignInWithAccessToken:(NSString *)accessToken
26412649
password:kFakePassword
26422650
completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
26432651
result.user.requestConfiguration =
2644-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey];
2652+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey
2653+
appID:kFirebaseAppID];
26452654
[expectation fulfill];
26462655
if (completion) {
26472656
completion(result.user, error);

0 commit comments

Comments
 (0)