Skip to content

Commit efec2de

Browse files
committed
[auth-swift] Merge AppCheck integration (#11056)
1 parent 8411c67 commit efec2de

14 files changed

+154
-133
lines changed

FirebaseAuth/Sources/Auth/FIRAuth.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#import <GoogleUtilities/GULSceneDelegateSwizzler.h>
2828
#import "FirebaseAuth/Sources/Public/FirebaseAuth/FirebaseAuth.h"
2929

30-
#import "FirebaseAuth-Swift.h"
3130
#import "FirebaseAppCheck/Interop/FIRAppCheckInterop.h"
31+
#import "FirebaseAuth-Swift.h"
3232
#import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
3333
#import "FirebaseAuth/Sources/SystemService/FIRAuthStoredUserManager.h"
3434
#import "FirebaseAuth/Sources/Utilities/FIRAuthExceptionUtils.h"
@@ -419,12 +419,12 @@ - (instancetype)initWithApp:(FIRApp *)app
419419
_app = app;
420420
_mainBundleUrlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
421421
_listenerHandles = [NSMutableArray array];
422-
_requestConfiguration =
423-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:app.options.APIKey
424-
appID:app.options.googleAppID
425-
auth:self
426-
heartbeatLogger:app.heartbeatLogger
427-
appCheck:FIR_COMPONENT(FIRAppCheckInterop, app.container)];
422+
_requestConfiguration = [[FIRAuthRequestConfiguration alloc]
423+
initWithAPIKey:app.options.APIKey
424+
appID:app.options.googleAppID
425+
auth:self
426+
heartbeatLogger:app.heartbeatLogger
427+
appCheck:FIR_COMPONENT(FIRAppCheckInterop, app.container)];
428428
_firebaseAppName = [app.name copy];
429429

430430
if (![(Class)keychainStorageProvider conformsToProtocol:@protocol(FIRAuthStorage)] ||

FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ import CommonCrypto
375375
urlString = "https://\(authDomain ?? "")/__/auth/handler?\(argumentsString)"
376376
}
377377
guard let percentEncoded = urlString.addingPercentEncoding(
378-
withAllowedCharacters: CharacterSet.urlFragmentAllowed) else {
378+
withAllowedCharacters: CharacterSet.urlFragmentAllowed
379+
) else {
379380
fatalError("Internal Auth Error: failed to percent encode a string")
380381
}
381382
var components = URLComponents(string: percentEncoded)
@@ -384,7 +385,7 @@ import CommonCrypto
384385
if let error = tokenResult.error {
385386
AuthLog.logWarning(code: "I-AUT000018",
386387
message: "Error getting App Check token; using placeholder " +
387-
"token instead. Error: \(error)")
388+
"token instead. Error: \(error)")
388389
}
389390
let appCheckTokenFragment = "fac=\(tokenResult.token)"
390391
components?.fragment = appCheckTokenFragment

FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ import Foundation
589589
if let error = tokenResult.error {
590590
AuthLog.logWarning(code: "I-AUT000018",
591591
message: "Error getting App Check token; using placeholder " +
592-
"token instead. Error: \(error)")
592+
"token instead. Error: \(error)")
593593
}
594594
let appCheckTokenFragment = "fac=\(tokenResult.token)"
595595
components?.fragment = appCheckTokenFragment

FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class AuthBackendRPCIssuerImplementation: NSObject, AuthBackendRPCIssuer
112112
public class func request(withURL url: URL,
113113
contentType: String,
114114
requestConfiguration: AuthRequestConfiguration,
115-
completion: @escaping (URLRequest) -> Void) -> Void {
115+
completion: @escaping (URLRequest) -> Void) {
116116
var request = URLRequest(url: url)
117117
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
118118
let additionalFrameworkMarker = requestConfiguration
@@ -140,7 +140,7 @@ public class AuthBackendRPCIssuerImplementation: NSObject, AuthBackendRPCIssuer
140140
if let error = tokenResult.error {
141141
AuthLog.logWarning(code: "I-AUT000018",
142142
message: "Error getting App Check token; using placeholder " +
143-
"token instead. Error: \(error)")
143+
"token instead. Error: \(error)")
144144
}
145145
request.setValue(tokenResult.token, forHTTPHeaderField: "X-Firebase-AppCheck")
146146
completion(request)
@@ -151,7 +151,6 @@ public class AuthBackendRPCIssuerImplementation: NSObject, AuthBackendRPCIssuer
151151
}
152152
}
153153

154-
155154
protocol AuthBackendImplementation {
156155
func post(withRequest request: AuthRPCRequest,
157156
callback: @escaping ((AuthRPCResponse?, Error?) -> Void))

FirebaseAuth/Tests/Unit/AuthBackendRPCImplentationTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ class AuthBackendRPCImplementationTests: RPCBaseTests {
639639
}
640640
rpcImplementation?.post(withRequest: request) { response, error in
641641
// The callback never happens and it's fine since we only need to verify the request.
642+
XCTFail("Should not be a callback")
642643
}
643644

644645
// Then
@@ -650,6 +651,29 @@ class AuthBackendRPCImplementationTests: RPCBaseTests {
650651
XCTAssertEqual(headerValue, expectedHeader)
651652
}
652653

654+
/** @fn testRequest_IncludesAppCheckHeader
655+
@brief This test checks the behavior of @c postWithRequest:response:callback:
656+
to verify that a appCheck token is attached as a header to an
657+
outgoing request.
658+
*/
659+
func testRequest_IncludesAppCheckHeader() throws {
660+
// Given
661+
let fakeAppCheck = FakeAppCheck()
662+
let requestConfiguration = AuthRequestConfiguration(apiKey: kFakeAPIKey,
663+
appID: kFakeAppID,
664+
appCheck: fakeAppCheck)
665+
666+
let request = FakeRequest(withRequestBody: [:], requestConfiguration: requestConfiguration)
667+
668+
rpcImplementation?.post(withRequest: request) { response, error in
669+
// The callback never happens and it's fine since we only need to verify the request.
670+
XCTFail("Should not be a callback")
671+
}
672+
let completeRequest = try XCTUnwrap(rpcIssuer?.completeRequest)
673+
let headerValue = completeRequest.value(forHTTPHeaderField: "X-Firebase-AppCheck")
674+
XCTAssertEqual(headerValue, fakeAppCheck.fakeAppCheckToken)
675+
}
676+
653677
/** @fn testRequest_DoesNotIncludeAHeartbeatPayload_WhenNoHeartbeatsNeedSending
654678
@brief This test checks the behavior of @c postWithRequest:response:callback:
655679
to verify that a request header does not contain heartbeat data in the

FirebaseAuth/Tests/Unit/FIRFakeAppCheck.h

Lines changed: 0 additions & 56 deletions
This file was deleted.

FirebaseAuth/Tests/Unit/FIRFakeAppCheck.m

Lines changed: 0 additions & 41 deletions
This file was deleted.

FirebaseAuth/Tests/Unit/FIRGitHubAuthProviderTests.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ @implementation FIRGitHubAuthProviderTests
4646
*/
4747
- (void)testCredentialWithToken {
4848
FIRAuthRequestConfiguration *requestConfiguration =
49-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
50-
heartbeatLogger:nil appCheck:nil];
49+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
50+
appID:kFirebaseAppID
51+
auth:nil
52+
heartbeatLogger:nil
53+
appCheck:nil];
5154
FIRAuthCredential *credential = [FIRGitHubAuthProvider credentialWithToken:kGitHubToken];
5255
FIRVerifyAssertionRequest *request =
5356
[[FIRVerifyAssertionRequest alloc] initWithProviderID:FIRGitHubAuthProvider.id

FirebaseAuth/Tests/Unit/FIRIdentityToolkitRequestTests.m

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ @implementation FIRIdentityToolkitRequestTests
5353
*/
5454
- (void)testInitWithEndpointExpectedRequestURL {
5555
FIRAuthRequestConfiguration *requestConfiguration =
56-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
57-
heartbeatLogger:nil appCheck:nil];
56+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
57+
appID:kFirebaseAppID
58+
auth:nil
59+
heartbeatLogger:nil
60+
appCheck:nil];
5861
FIRIdentityToolkitRequest *request =
5962
[[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
6063
requestConfiguration:requestConfiguration
@@ -73,8 +76,11 @@ - (void)testInitWithEndpointExpectedRequestURL {
7376
*/
7477
- (void)testInitWithEndpointUseStagingExpectedRequestURL {
7578
FIRAuthRequestConfiguration *requestConfiguration =
76-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
77-
heartbeatLogger:nil appCheck:nil];
79+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
80+
appID:kFirebaseAppID
81+
auth:nil
82+
heartbeatLogger:nil
83+
appCheck:nil];
7884
FIRIdentityToolkitRequest *request =
7985
[[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
8086
requestConfiguration:requestConfiguration
@@ -94,8 +100,11 @@ - (void)testInitWithEndpointUseStagingExpectedRequestURL {
94100
*/
95101
- (void)testInitWithEndpointUseIdentityPlatformExpectedRequestURL {
96102
FIRAuthRequestConfiguration *requestConfiguration =
97-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
98-
heartbeatLogger:nil appCheck:nil];
103+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
104+
appID:kFirebaseAppID
105+
auth:nil
106+
heartbeatLogger:nil
107+
appCheck:nil];
99108
FIRIdentityToolkitRequest *request =
100109
[[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
101110
requestConfiguration:requestConfiguration
@@ -113,8 +122,11 @@ - (void)testInitWithEndpointUseIdentityPlatformExpectedRequestURL {
113122
*/
114123
- (void)testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL {
115124
FIRAuthRequestConfiguration *requestConfiguration =
116-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
117-
heartbeatLogger:nil appCheck:nil];
125+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
126+
appID:kFirebaseAppID
127+
auth:nil
128+
heartbeatLogger:nil
129+
appCheck:nil];
118130
FIRIdentityToolkitRequest *request =
119131
[[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
120132
requestConfiguration:requestConfiguration
@@ -133,8 +145,11 @@ - (void)testInitWithEndpointUseIdentityPlatformUseStagingExpectedRequestURL {
133145
*/
134146
- (void)testInitWithEndpointUseEmulatorExpectedRequestURL {
135147
FIRAuthRequestConfiguration *requestConfiguration =
136-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
137-
heartbeatLogger:nil appCheck:nil];
148+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
149+
appID:kFirebaseAppID
150+
auth:nil
151+
heartbeatLogger:nil
152+
appCheck:nil];
138153
requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
139154
FIRIdentityToolkitRequest *request =
140155
[[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
@@ -154,8 +169,11 @@ - (void)testInitWithEndpointUseEmulatorExpectedRequestURL {
154169
*/
155170
- (void)testInitWithEndpointUseIdentityPlatformUseEmulatorExpectedRequestURL {
156171
FIRAuthRequestConfiguration *requestConfiguration =
157-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
158-
heartbeatLogger:nil appCheck:nil];
172+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
173+
appID:kFirebaseAppID
174+
auth:nil
175+
heartbeatLogger:nil
176+
appCheck:nil];
159177
requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
160178
FIRIdentityToolkitRequest *request =
161179
[[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint
@@ -180,7 +198,8 @@ - (void)testExpectedTenantIDWithNonDefaultFIRApp {
180198
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
181199
appID:kFirebaseAppID
182200
auth:nonDefaultAuth
183-
heartbeatLogger:nil appCheck:nil];
201+
heartbeatLogger:nil
202+
appCheck:nil];
184203
requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
185204
FIRIdentityToolkitRequest *request =
186205
[[FIRIdentityToolkitRequest alloc] initWithEndpoint:kEndpoint

FirebaseAuth/Tests/Unit/FIRSecureTokenRequestTests.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ @implementation FIRSecureTokenRequestTests
5252
*/
5353
- (void)testRequestURL {
5454
FIRAuthRequestConfiguration *requestConfiguration =
55-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
56-
heartbeatLogger:nil appCheck:nil];
55+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
56+
appID:kFirebaseAppID
57+
auth:nil
58+
heartbeatLogger:nil
59+
appCheck:nil];
5760
FIRSecureTokenRequest *request =
5861
[FIRSecureTokenRequest refreshRequestWithRefreshToken:kRefreshToken
5962
requestConfiguration:requestConfiguration];
@@ -70,8 +73,11 @@ - (void)testRequestURL {
7073
*/
7174
- (void)testRequestURLUseEmulator {
7275
FIRAuthRequestConfiguration *requestConfiguration =
73-
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey appID:kFirebaseAppID auth:nil
74-
heartbeatLogger:nil appCheck:nil];
76+
[[FIRAuthRequestConfiguration alloc] initWithAPIKey:kAPIKey
77+
appID:kFirebaseAppID
78+
auth:nil
79+
heartbeatLogger:nil
80+
appCheck:nil];
7581
requestConfiguration.emulatorHostAndPort = kEmulatorHostAndPort;
7682
FIRSecureTokenRequest *request =
7783
[FIRSecureTokenRequest refreshRequestWithRefreshToken:kRefreshToken

0 commit comments

Comments
 (0)