From 965f0e3eb1814e2cece84cbd6fdc8fa72b3b2d30 Mon Sep 17 00:00:00 2001 From: Nick Cooke Date: Tue, 10 Dec 2024 17:49:51 -0500 Subject: [PATCH 1/2] [Auth] Refactor MultiFactor.swift --- .../Swift/MultiFactor/MultiFactor.swift | 74 ++++++++++++++++++- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift b/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift index 5b1675fdfa0..a2d7e43289d 100644 --- a/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift +++ b/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift @@ -67,6 +67,19 @@ import Foundation displayName: String?, completion: ((Error?) -> Void)?) { // TODO: Refactor classes so this duplicated code isn't necessary for phone and totp. + + guard + assertion.factorID == PhoneMultiFactorInfo.TOTPMultiFactorID || + assertion.factorID == PhoneMultiFactorInfo.TOTPMultiFactorID + else { + return + } + + guard let user = user, let auth = user.auth else { + fatalError("Internal Auth error: failed to get user enrolling in MultiFactor") + } + + var request: FinalizeMFAEnrollmentRequest? = nil if assertion.factorID == PhoneMultiFactorInfo.TOTPMultiFactorID { guard let totpAssertion = assertion as? TOTPMultiFactorAssertion else { fatalError("Auth Internal Error: Failed to find TOTPMultiFactorAssertion") @@ -74,13 +87,11 @@ import Foundation switch totpAssertion.secretOrID { case .enrollmentID: fatalError("Missing secret in totpAssertion") case let .secret(secret): - guard let user = user, let auth = user.auth else { - fatalError("Internal Auth error: failed to get user enrolling in MultiFactor") - } let finalizeMFATOTPRequestInfo = AuthProtoFinalizeMFATOTPEnrollmentRequestInfo(sessionInfo: secret.sessionInfo, verificationCode: totpAssertion .oneTimePassword) +<<<<<<< Updated upstream let request = FinalizeMFAEnrollmentRequest(idToken: self.user?.rawAccessToken(), displayName: displayName, totpVerificationInfo: finalizeMFATOTPRequestInfo, @@ -106,13 +117,63 @@ import Foundation completion(error) } } +======= + request = FinalizeMFAEnrollmentRequest(idToken: self.user?.rawAccessToken(), + displayName: displayName, + totpVerificationInfo: finalizeMFATOTPRequestInfo, + requestConfiguration: user + .requestConfiguration) + } + } else if assertion.factorID == PhoneMultiFactorInfo.PhoneMultiFactorID { + let phoneAssertion = assertion as? PhoneMultiFactorAssertion + guard let credential = phoneAssertion?.authCredential else { + fatalError("Internal Error: Missing credential") + } + switch credential.credentialKind { + case .phoneNumber: fatalError("Internal Error: Missing verificationCode") + case let .verification(verificationID, code): + let finalizeMFAPhoneRequestInfo = + AuthProtoFinalizeMFAPhoneRequestInfo( + sessionInfo: verificationID, + verificationCode: code + ) + request = FinalizeMFAEnrollmentRequest( + idToken: self.user?.rawAccessToken(), + displayName: displayName, + phoneVerificationInfo: finalizeMFAPhoneRequestInfo, + requestConfiguration: user.requestConfiguration + ) + } + } + + guard let request else { + // Assertion is not a phone assertion or TOTP assertion. + return + } + + Task { + do { + let response = try await AuthBackend.call(with: request) + do { + let user = try await auth.completeSignIn(withAccessToken: response.idToken, + accessTokenExpirationDate: nil, + refreshToken: response.refreshToken, + anonymous: false) + try auth.updateCurrentUser(user, byForce: false, savingToDisk: true) + if let completion { + DispatchQueue.main.async { + completion(nil) +>>>>>>> Stashed changes } - } catch { + } + } catch { + DispatchQueue.main.async { if let completion { completion(error) } } } +<<<<<<< Updated upstream } return } else if assertion.factorID != PhoneMultiFactorInfo.PhoneMultiFactorID { @@ -162,6 +223,11 @@ import Foundation if let completion { completion(error) } +======= + } catch { + if let completion { + completion(error) +>>>>>>> Stashed changes } } } From d4bf5227ea8529aa7d2e67f0fc8d14b99d6fc8e5 Mon Sep 17 00:00:00 2001 From: Nick Cooke Date: Tue, 10 Dec 2024 17:55:23 -0500 Subject: [PATCH 2/2] Roll changes forward: --- .../Swift/MultiFactor/MultiFactor.swift | 108 ++---------------- 1 file changed, 11 insertions(+), 97 deletions(-) diff --git a/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift b/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift index a2d7e43289d..980fabefb25 100644 --- a/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift +++ b/FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift @@ -91,33 +91,6 @@ import Foundation AuthProtoFinalizeMFATOTPEnrollmentRequestInfo(sessionInfo: secret.sessionInfo, verificationCode: totpAssertion .oneTimePassword) -<<<<<<< Updated upstream - let request = FinalizeMFAEnrollmentRequest(idToken: self.user?.rawAccessToken(), - displayName: displayName, - totpVerificationInfo: finalizeMFATOTPRequestInfo, - requestConfiguration: user - .requestConfiguration) - Task { - do { - let response = try await auth.backend.call(with: request) - do { - let user = try await auth.completeSignIn(withAccessToken: response.idToken, - accessTokenExpirationDate: nil, - refreshToken: response.refreshToken, - anonymous: false) - try auth.updateCurrentUser(user, byForce: false, savingToDisk: true) - if let completion { - DispatchQueue.main.async { - completion(nil) - } - } - } catch { - DispatchQueue.main.async { - if let completion { - completion(error) - } - } -======= request = FinalizeMFAEnrollmentRequest(idToken: self.user?.rawAccessToken(), displayName: displayName, totpVerificationInfo: finalizeMFATOTPRequestInfo, @@ -153,81 +126,22 @@ import Foundation Task { do { - let response = try await AuthBackend.call(with: request) - do { - let user = try await auth.completeSignIn(withAccessToken: response.idToken, - accessTokenExpirationDate: nil, - refreshToken: response.refreshToken, - anonymous: false) - try auth.updateCurrentUser(user, byForce: false, savingToDisk: true) - if let completion { - DispatchQueue.main.async { - completion(nil) ->>>>>>> Stashed changes - } - } - } catch { + let response = try await auth.backend.call(with: request) + let user = try await auth.completeSignIn(withAccessToken: response.idToken, + accessTokenExpirationDate: nil, + refreshToken: response.refreshToken, + anonymous: false) + try auth.updateCurrentUser(user, byForce: false, savingToDisk: true) + if let completion { DispatchQueue.main.async { - if let completion { - completion(error) - } + completion(nil) } } -<<<<<<< Updated upstream - } - return - } else if assertion.factorID != PhoneMultiFactorInfo.PhoneMultiFactorID { - return - } - let phoneAssertion = assertion as? PhoneMultiFactorAssertion - guard let credential = phoneAssertion?.authCredential else { - fatalError("Internal Error: Missing credential") - } - switch credential.credentialKind { - case .phoneNumber: fatalError("Internal Error: Missing verificationCode") - case let .verification(verificationID, code): - let finalizeMFAPhoneRequestInfo = - AuthProtoFinalizeMFAPhoneRequestInfo(sessionInfo: verificationID, verificationCode: code) - guard let user = user, let auth = user.auth else { - fatalError("Internal Auth error: failed to get user enrolling in MultiFactor") - } - let request = FinalizeMFAEnrollmentRequest( - idToken: self.user?.rawAccessToken(), - displayName: displayName, - phoneVerificationInfo: finalizeMFAPhoneRequestInfo, - requestConfiguration: user.requestConfiguration - ) - - Task { - do { - let response = try await auth.backend.call(with: request) - do { - let user = try await auth.completeSignIn(withAccessToken: response.idToken, - accessTokenExpirationDate: nil, - refreshToken: response.refreshToken, - anonymous: false) - try auth.updateCurrentUser(user, byForce: false, savingToDisk: true) - if let completion { - DispatchQueue.main.async { - completion(nil) - } - } - } catch { - DispatchQueue.main.async { - if let completion { - completion(error) - } - } - } - } catch { - if let completion { - completion(error) - } -======= } catch { if let completion { - completion(error) ->>>>>>> Stashed changes + DispatchQueue.main.async { + completion(error) + } } } }