Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Auth] Refactor MultiFactor.swift #14241

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 57 additions & 77 deletions FirebaseAuth/Sources/Swift/MultiFactor/MultiFactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,99 +67,79 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or of the same value?

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")
}
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)
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)
}
}
}
} catch {
if let completion {
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
)
}
return
} else if assertion.factorID != PhoneMultiFactorInfo.PhoneMultiFactorID {
return
}
let phoneAssertion = assertion as? PhoneMultiFactorAssertion
guard let credential = phoneAssertion?.authCredential else {
fatalError("Internal Error: Missing credential")

guard let request else {
// Assertion is not a phone assertion or TOTP assertion.
return
}
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)
}
}
Task {
do {
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 {
completion(nil)
}
} catch {
if let completion {
}
} catch {
if let completion {
DispatchQueue.main.async {
completion(error)
}
}
Expand Down
Loading