Skip to content

Commit bb01fbe

Browse files
committed
Dependency injection work
1 parent 4c528b7 commit bb01fbe

32 files changed

+158
-128
lines changed

FirebaseAuth/Sources/Swift/Auth/Auth.swift

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ extension Auth: AuthInterop {
123123
return
124124
}
125125
// Call back with current user token.
126-
currentUser.internalGetToken(forceRefresh: forceRefresh) { token, error in
127-
DispatchQueue.main.async {
128-
callback(token, error)
126+
currentUser
127+
.internalGetToken(forceRefresh: forceRefresh, backend: strongSelf.backend) { token, error in
128+
DispatchQueue.main.async {
129+
callback(token, error)
130+
}
129131
}
130-
}
131132
}
132133
}
133134

@@ -294,7 +295,7 @@ extension Auth: AuthInterop {
294295
requestConfiguration: self.requestConfiguration)
295296
Task {
296297
do {
297-
let response = try await AuthBackend.call(with: request)
298+
let response = try await self.backend.call(with: request)
298299
Auth.wrapMainAsync(callback: completion, withParam: response.signinMethods, error: nil)
299300
} catch {
300301
Auth.wrapMainAsync(callback: completion, withParam: nil, error: error)
@@ -397,7 +398,7 @@ extension Auth: AuthInterop {
397398
let response = try await injectRecaptcha(request: request,
398399
action: AuthRecaptchaAction.signInWithPassword)
399400
#else
400-
let response = try await AuthBackend.call(with: request)
401+
let response = try await backend.call(with: request)
401402
#endif
402403
return try await completeSignIn(
403404
withAccessToken: response.idToken,
@@ -711,7 +712,7 @@ extension Auth: AuthInterop {
711712
let request = SignUpNewUserRequest(requestConfiguration: self.requestConfiguration)
712713
Task {
713714
do {
714-
let response = try await AuthBackend.call(with: request)
715+
let response = try await self.backend.call(with: request)
715716
let user = try await self.completeSignIn(
716717
withAccessToken: response.idToken,
717718
accessTokenExpirationDate: response.approximateExpirationDate,
@@ -773,7 +774,7 @@ extension Auth: AuthInterop {
773774
requestConfiguration: self.requestConfiguration)
774775
Task {
775776
do {
776-
let response = try await AuthBackend.call(with: request)
777+
let response = try await self.backend.call(with: request)
777778
let user = try await self.completeSignIn(
778779
withAccessToken: response.idToken,
779780
accessTokenExpirationDate: response.approximateExpirationDate,
@@ -883,7 +884,7 @@ extension Auth: AuthInterop {
883884
if let inResponse {
884885
response = inResponse
885886
} else {
886-
response = try await AuthBackend.call(with: request)
887+
response = try await self.backend.call(with: request)
887888
}
888889
let user = try await self.completeSignIn(
889890
withAccessToken: response.idToken,
@@ -995,7 +996,7 @@ extension Auth: AuthInterop {
995996
requestConfiguration: self.requestConfiguration)
996997
Task {
997998
do {
998-
let response = try await AuthBackend.call(with: request)
999+
let response = try await self.backend.call(with: request)
9991000

10001001
let operation = ActionCodeInfo.actionCodeOperation(forRequestType: response.requestType)
10011002
guard let email = response.email else {
@@ -1434,7 +1435,7 @@ extension Auth: AuthInterop {
14341435
/// complete, or fails. Invoked asynchronously on the main thread in the future.
14351436
@objc open func revokeToken(withAuthorizationCode authorizationCode: String,
14361437
completion: ((Error?) -> Void)? = nil) {
1437-
currentUser?.internalGetToken { idToken, error in
1438+
currentUser?.internalGetToken(backend: backend) { idToken, error in
14381439
if let error {
14391440
Auth.wrapMainAsync(completion, error)
14401441
return
@@ -1615,7 +1616,9 @@ extension Auth: AuthInterop {
16151616

16161617
// MARK: Internal methods
16171618

1618-
init(app: FirebaseApp, keychainStorageProvider: AuthKeychainStorage = AuthKeychainStorageReal()) {
1619+
init(app: FirebaseApp,
1620+
keychainStorageProvider: AuthKeychainStorage = AuthKeychainStorageReal(),
1621+
backend: AuthBackend = AuthBackend(rpcIssuer: AuthBackendRPCIssuer())) {
16191622
Auth.setKeychainServiceNameForApp(app)
16201623
self.app = app
16211624
mainBundleUrlTypes = Bundle.main
@@ -1640,6 +1643,7 @@ extension Auth: AuthInterop {
16401643
auth: nil,
16411644
heartbeatLogger: app.heartbeatLogger,
16421645
appCheck: appCheck)
1646+
self.backend = backend
16431647
super.init()
16441648
requestConfiguration.auth = self
16451649

@@ -1913,17 +1917,18 @@ extension Auth: AuthInterop {
19131917
return
19141918
}
19151919
let uid = strongSelf.currentUser?.uid
1916-
strongSelf.currentUser?.internalGetToken(forceRefresh: true) { token, error in
1917-
if strongSelf.currentUser?.uid != uid {
1918-
return
1919-
}
1920-
if error != nil {
1921-
// Kicks off exponential back off logic to retry failed attempt. Starts with one minute
1922-
// delay (60 seconds) if this is the first failed attempt.
1923-
let rescheduleDelay = retry ? min(delay * 2, 16 * 60) : 60
1924-
strongSelf.scheduleAutoTokenRefresh(withDelay: rescheduleDelay, retry: true)
1920+
strongSelf.currentUser?
1921+
.internalGetToken(forceRefresh: true, backend: strongSelf.backend) { token, error in
1922+
if strongSelf.currentUser?.uid != uid {
1923+
return
1924+
}
1925+
if error != nil {
1926+
// Kicks off exponential back off logic to retry failed attempt. Starts with one minute
1927+
// delay (60 seconds) if this is the first failed attempt.
1928+
let rescheduleDelay = retry ? min(delay * 2, 16 * 60) : 60
1929+
strongSelf.scheduleAutoTokenRefresh(withDelay: rescheduleDelay, retry: true)
1930+
}
19251931
}
1926-
}
19271932
}
19281933
}
19291934

@@ -2077,7 +2082,7 @@ extension Auth: AuthInterop {
20772082
requestConfiguration: requestConfiguration)
20782083
request.autoCreate = !isReauthentication
20792084
credential.prepare(request)
2080-
let response = try await AuthBackend.call(with: request)
2085+
let response = try await backend.call(with: request)
20812086
if response.needConfirmation {
20822087
let email = response.email
20832088
let credential = OAuthCredential(withVerifyAssertionResponse: response)
@@ -2116,7 +2121,7 @@ extension Auth: AuthInterop {
21162121
phoneNumber: phoneNumber,
21172122
operation: operation,
21182123
requestConfiguration: requestConfiguration)
2119-
return try await AuthBackend.call(with: request)
2124+
return try await backend.call(with: request)
21202125
case let .verification(verificationID, code):
21212126
guard verificationID.count > 0 else {
21222127
throw AuthErrorUtils.missingVerificationIDError(message: nil)
@@ -2128,7 +2133,7 @@ extension Auth: AuthInterop {
21282133
verificationCode: code,
21292134
operation: operation,
21302135
requestConfiguration: requestConfiguration)
2131-
return try await AuthBackend.call(with: request)
2136+
return try await backend.call(with: request)
21322137
}
21332138
}
21342139
#endif
@@ -2154,7 +2159,7 @@ extension Auth: AuthInterop {
21542159
timestamp: credential.timestamp,
21552160
displayName: credential.displayName,
21562161
requestConfiguration: requestConfiguration)
2157-
let response = try await AuthBackend.call(with: request)
2162+
let response = try await backend.call(with: request)
21582163
let user = try await completeSignIn(withAccessToken: response.idToken,
21592164
accessTokenExpirationDate: response
21602165
.approximateExpirationDate,
@@ -2186,7 +2191,7 @@ extension Auth: AuthInterop {
21862191
let request = EmailLinkSignInRequest(email: email,
21872192
oobCode: actionCode,
21882193
requestConfiguration: requestConfiguration)
2189-
let response = try await AuthBackend.call(with: request)
2194+
let response = try await backend.call(with: request)
21902195
let user = try await completeSignIn(withAccessToken: response.idToken,
21912196
accessTokenExpirationDate: response
21922197
.approximateExpirationDate,
@@ -2244,7 +2249,7 @@ extension Auth: AuthInterop {
22442249
private func wrapAsyncRPCTask(_ request: any AuthRPCRequest, _ callback: ((Error?) -> Void)?) {
22452250
Task {
22462251
do {
2247-
let _ = try await AuthBackend.call(with: request)
2252+
let _ = try await self.backend.call(with: request)
22482253
Auth.wrapMainAsync(callback, nil)
22492254
} catch {
22502255
Auth.wrapMainAsync(callback, error)
@@ -2296,7 +2301,7 @@ extension Auth: AuthInterop {
22962301
action: action)
22972302
} else {
22982303
do {
2299-
return try await AuthBackend.call(with: request)
2304+
return try await backend.call(with: request)
23002305
} catch {
23012306
let nsError = error as NSError
23022307
if let underlyingError = nsError.userInfo[NSUnderlyingErrorKey] as? NSError,
@@ -2315,7 +2320,7 @@ extension Auth: AuthInterop {
23152320
}
23162321
}
23172322
}
2318-
return try await AuthBackend.call(with: request)
2323+
return try await backend.call(with: request)
23192324
}
23202325
#endif
23212326

@@ -2332,6 +2337,8 @@ extension Auth: AuthInterop {
23322337
/// Auth's backend.
23332338
var requestConfiguration: AuthRequestConfiguration
23342339

2340+
var backend: AuthBackend
2341+
23352342
#if os(iOS)
23362343

23372344
/// The manager for APNs tokens used by phone number auth.

FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ import Foundation
412412
private func getHeadfulLiteUrl(eventID: String,
413413
sessionID: String) async throws -> URL? {
414414
let authDomain = try await AuthWebUtils
415-
.fetchAuthDomain(withRequestConfiguration: auth.requestConfiguration)
415+
.fetchAuthDomain(withRequestConfiguration: auth.requestConfiguration, backend: auth.backend)
416416
let bundleID = Bundle.main.bundleIdentifier
417417
let clientID = auth.app?.options.clientID
418418
let appID = auth.app?.options.googleAppID

FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ import Foundation
217217
.requestConfiguration)
218218

219219
do {
220-
let response = try await AuthBackend.call(with: request)
220+
let response = try await auth.backend.call(with: request)
221221
return response.verificationID
222222
} catch {
223223
return try await handleVerifyErrorWithRetry(error: error,
@@ -245,7 +245,7 @@ import Foundation
245245
requestConfiguration: auth.requestConfiguration
246246
)
247247

248-
let response = try await AuthBackend.call(with: request)
248+
let response = try await auth.backend.call(with: request)
249249
return response.verificationID
250250
}
251251
guard let session else {
@@ -263,15 +263,15 @@ import Foundation
263263
let request = StartMFAEnrollmentRequest(idToken: idToken,
264264
enrollmentInfo: startMFARequestInfo,
265265
requestConfiguration: auth.requestConfiguration)
266-
let response = try await AuthBackend.call(with: request)
266+
let response = try await auth.backend.call(with: request)
267267
return response.phoneSessionInfo?.sessionInfo
268268
} else {
269269
let request = StartMFASignInRequest(MFAPendingCredential: session.mfaPendingCredential,
270270
MFAEnrollmentID: session.multiFactorInfo?.uid,
271271
signInInfo: startMFARequestInfo,
272272
requestConfiguration: auth.requestConfiguration)
273273

274-
let response = try await AuthBackend.call(with: request)
274+
let response = try await auth.backend.call(with: request)
275275
return response.responseInfo?.sessionInfo
276276
}
277277
} catch {
@@ -328,7 +328,7 @@ import Foundation
328328
isSandbox: token.type == AuthAPNSTokenType.sandbox,
329329
requestConfiguration: auth.requestConfiguration)
330330
do {
331-
let verifyResponse = try await AuthBackend.call(with: request)
331+
let verifyResponse = try await auth.backend.call(with: request)
332332
guard let receipt = verifyResponse.receipt,
333333
let timeout = verifyResponse.suggestedTimeOutDate?.timeIntervalSinceNow else {
334334
fatalError("Internal Auth Error: invalid VerifyClientResponse")
@@ -436,7 +436,7 @@ import Foundation
436436
/// - Parameter eventID: The event ID used for this purpose.
437437
private func reCAPTCHAURL(withEventID eventID: String) async throws -> URL? {
438438
let authDomain = try await AuthWebUtils
439-
.fetchAuthDomain(withRequestConfiguration: auth.requestConfiguration)
439+
.fetchAuthDomain(withRequestConfiguration: auth.requestConfiguration, backend: auth.backend)
440440
let bundleID = Bundle.main.bundleIdentifier
441441
let clientID = auth.app?.options.clientID
442442
let appID = auth.app?.options.googleAppID

FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class AuthBackend: AuthBackendProtocol {
3232
return "FirebaseAuth.iOS/\(FirebaseVersion()) \(GTMFetcherStandardUserAgentString(nil))"
3333
}
3434

35-
static func call<T: AuthRPCRequest>(with request: T) async throws -> T.Response {
36-
return try await shared.call(with: request)
37-
}
38-
39-
private static let shared: AuthBackend = .init(rpcIssuer: AuthBackendRPCIssuer())
35+
// static func call<T: AuthRPCRequest>(with request: T) async throws -> T.Response {
36+
// return try await shared.call(with: request)
37+
// }
38+
//
39+
// private static let shared: AuthBackend = .init(rpcIssuer: AuthBackendRPCIssuer())
4040

4141
private let rpcIssuer: any AuthBackendRPCIssuerProtocol
4242

FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import Foundation
8888
.requestConfiguration)
8989
Task {
9090
do {
91-
let response = try await AuthBackend.call(with: request)
91+
let response = try await auth.backend.call(with: request)
9292
do {
9393
let user = try await auth.completeSignIn(withAccessToken: response.idToken,
9494
accessTokenExpirationDate: nil,
@@ -139,7 +139,7 @@ import Foundation
139139

140140
Task {
141141
do {
142-
let response = try await AuthBackend.call(with: request)
142+
let response = try await auth.backend.call(with: request)
143143
do {
144144
let user = try await auth.completeSignIn(withAccessToken: response.idToken,
145145
accessTokenExpirationDate: nil,
@@ -215,7 +215,7 @@ import Foundation
215215
requestConfiguration: user.requestConfiguration)
216216
Task {
217217
do {
218-
let response = try await AuthBackend.call(with: request)
218+
let response = try await auth.backend.call(with: request)
219219
do {
220220
let user = try await auth.completeSignIn(withAccessToken: response.idToken,
221221
accessTokenExpirationDate: nil,

FirebaseAuth/Sources/Swift/MultiFactor/MultiFactorResolver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import Foundation
7272
)
7373
Task {
7474
do {
75-
let response = try await AuthBackend.call(with: request)
75+
let response = try await self.auth.backend.call(with: request)
7676
let user = try await self.auth.completeSignIn(withAccessToken: response.idToken,
7777
accessTokenExpirationDate: nil,
7878
refreshToken: response.refreshToken,

FirebaseAuth/Sources/Swift/MultiFactor/TOTP/TOTPMultiFactorGenerator.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ import Foundation
3131
@objc(generateSecretWithMultiFactorSession:completion:)
3232
open class func generateSecret(with session: MultiFactorSession,
3333
completion: @escaping (TOTPSecret?, Error?) -> Void) {
34-
guard let currentUser = session.currentUser,
35-
let requestConfiguration = currentUser.auth?.requestConfiguration else {
34+
guard let currentUser = session.currentUser, let auth = currentUser.auth else {
3635
let error = AuthErrorUtils.error(code: AuthErrorCode.internalError,
3736
userInfo: [NSLocalizedDescriptionKey:
3837
"Invalid ID token."])
@@ -42,10 +41,10 @@ import Foundation
4241
let totpEnrollmentInfo = AuthProtoStartMFATOTPEnrollmentRequestInfo()
4342
let request = StartMFAEnrollmentRequest(idToken: session.idToken,
4443
totpEnrollmentInfo: totpEnrollmentInfo,
45-
requestConfiguration: requestConfiguration)
44+
requestConfiguration: auth.requestConfiguration)
4645
Task {
4746
do {
48-
let response = try await AuthBackend.call(with: request)
47+
let response = try await auth.backend.call(with: request)
4948
if let totpSessionInfo = response.totpSessionInfo {
5049
let secret = TOTPSecret(secretKey: totpSessionInfo.sharedSecretKey,
5150
hashingAlgorithm: totpSessionInfo.hashingAlgorithm,

0 commit comments

Comments
 (0)