|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import FirebaseAuth |
| 16 | +import SwiftUI |
| 17 | + |
| 18 | +struct MFALoginView: View { |
| 19 | + @Environment(\.dismiss) private var dismiss |
| 20 | + |
| 21 | + @State private var factorSelection: MultiFactorInfo? |
| 22 | + // This is only needed for phone MFA. |
| 23 | + @State private var verificationId: String? |
| 24 | + // This is needed for both phone and TOTP MFA. |
| 25 | + @State private var verificationCode: String = "" |
| 26 | + |
| 27 | + private let resolver: MultiFactorResolver |
| 28 | + private weak var delegate: (any LoginDelegate)? |
| 29 | + |
| 30 | + init(resolver: MultiFactorResolver, delegate: (any LoginDelegate)?) { |
| 31 | + self.resolver = resolver |
| 32 | + self.delegate = delegate |
| 33 | + } |
| 34 | + |
| 35 | + var body: some View { |
| 36 | + Text("Choose a second factor to continue.") |
| 37 | + .padding(.top) |
| 38 | + List(resolver.hints, id: \.self, selection: $factorSelection) { |
| 39 | + Text($0.displayName ?? "No display name provided.") |
| 40 | + } |
| 41 | + .frame(height: 300) |
| 42 | + .clipShape(RoundedRectangle(cornerRadius: 15)) |
| 43 | + .padding() |
| 44 | + |
| 45 | + if let factorSelection { |
| 46 | + // TODO(ncooke3): This logic handles both phone and TOTP MFA states. Investigate how to make |
| 47 | + // more clear with better APIs. |
| 48 | + if factorSelection.factorID == PhoneMultiFactorID, verificationId == nil { |
| 49 | + MFAViewButton( |
| 50 | + text: "Send Verification Code", |
| 51 | + accentColor: .white, |
| 52 | + backgroundColor: .orange |
| 53 | + ) { |
| 54 | + Task { await startMfALogin() } |
| 55 | + } |
| 56 | + .padding() |
| 57 | + } else { |
| 58 | + TextField("Enter verification code.", text: $verificationCode) |
| 59 | + .textFieldStyle(SymbolTextField(symbolName: "lock.circle.fill")) |
| 60 | + .padding() |
| 61 | + MFAViewButton( |
| 62 | + text: "Sign in", |
| 63 | + accentColor: .white, |
| 64 | + backgroundColor: .orange |
| 65 | + ) { |
| 66 | + Task { await finishMfALogin() } |
| 67 | + } |
| 68 | + .padding() |
| 69 | + } |
| 70 | + } |
| 71 | + Spacer() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +extension MFALoginView { |
| 76 | + private func startMfALogin() async { |
| 77 | + guard let factorSelection else { return } |
| 78 | + switch factorSelection.factorID { |
| 79 | + case PhoneMultiFactorID: |
| 80 | + await startPhoneMultiFactorSignIn(hint: factorSelection as? PhoneMultiFactorInfo) |
| 81 | + case TOTPMultiFactorID: break // TODO(ncooke3): Indicate to user to get verification code. |
| 82 | + default: return |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private func startPhoneMultiFactorSignIn(hint: PhoneMultiFactorInfo?) async { |
| 87 | + guard let hint else { return } |
| 88 | + do { |
| 89 | + verificationId = try await PhoneAuthProvider.provider().verifyPhoneNumber( |
| 90 | + with: hint, |
| 91 | + uiDelegate: nil, |
| 92 | + multiFactorSession: resolver.session |
| 93 | + ) |
| 94 | + } catch { |
| 95 | + print(error) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private func finishMfALogin() async { |
| 100 | + guard let factorSelection else { return } |
| 101 | + switch factorSelection.factorID { |
| 102 | + case PhoneMultiFactorID: |
| 103 | + await finishPhoneMultiFactorSignIn() |
| 104 | + case TOTPMultiFactorID: |
| 105 | + await finishTOTPMultiFactorSignIn(hint: factorSelection) |
| 106 | + default: return |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private func finishPhoneMultiFactorSignIn() async { |
| 111 | + guard let verificationId else { return } |
| 112 | + let credential = PhoneAuthProvider.provider().credential( |
| 113 | + withVerificationID: verificationId, |
| 114 | + verificationCode: verificationCode |
| 115 | + ) |
| 116 | + let assertion = PhoneMultiFactorGenerator.assertion(with: credential) |
| 117 | + do { |
| 118 | + _ = try await resolver.resolveSignIn(with: assertion) |
| 119 | + // MFA login was successful. |
| 120 | + await MainActor.run { |
| 121 | + dismiss() |
| 122 | + delegate?.loginDidOccur(resolver: nil) |
| 123 | + } |
| 124 | + } catch { |
| 125 | + print(error) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private func finishTOTPMultiFactorSignIn(hint: MultiFactorInfo) async { |
| 130 | + // TODO(ncooke3): Disable button if verification code textfield contents is empty. |
| 131 | + guard verificationCode.count > 0 else { return } |
| 132 | + let assertion = TOTPMultiFactorGenerator.assertionForSignIn( |
| 133 | + withEnrollmentID: hint.uid, |
| 134 | + oneTimePassword: verificationCode |
| 135 | + ) |
| 136 | + do { |
| 137 | + _ = try await resolver.resolveSignIn(with: assertion) |
| 138 | + // MFA login was successful. |
| 139 | + await MainActor.run { |
| 140 | + dismiss() |
| 141 | + delegate?.loginDidOccur(resolver: nil) |
| 142 | + } |
| 143 | + } catch { |
| 144 | + // Wrong or expired OTP. Re-prompt the user. |
| 145 | + // TODO(ncooke3): Show error to user. |
| 146 | + print(error) |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +private struct MFAViewButton: View { |
| 152 | + let text: String |
| 153 | + let accentColor: Color |
| 154 | + let backgroundColor: Color |
| 155 | + let action: () -> Void |
| 156 | + |
| 157 | + var body: some View { |
| 158 | + Button(action: action) { |
| 159 | + HStack { |
| 160 | + Spacer() |
| 161 | + Text(text) |
| 162 | + .bold() |
| 163 | + .accentColor(accentColor) |
| 164 | + Spacer() |
| 165 | + } |
| 166 | + .padding() |
| 167 | + .background(backgroundColor) |
| 168 | + .cornerRadius(14) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +private struct SymbolTextField: TextFieldStyle { |
| 174 | + let symbolName: String |
| 175 | + |
| 176 | + func _body(configuration: TextField<Self._Label>) -> some View { |
| 177 | + HStack { |
| 178 | + Image(systemName: symbolName) |
| 179 | + .foregroundColor(.orange) |
| 180 | + .imageScale(.large) |
| 181 | + .padding(.leading) |
| 182 | + configuration |
| 183 | + .padding([.vertical, .trailing]) |
| 184 | + } |
| 185 | + .background(Color(uiColor: .secondarySystemBackground)) |
| 186 | + .cornerRadius(14) |
| 187 | + .textInputAutocapitalization(.never) |
| 188 | + } |
| 189 | +} |
0 commit comments