Dactyloidae/mobile/ios/Client/Frontend/AuthenticationManager/SetupPasscodeViewController.swift
2026-06-26 21:04:09 -07:00

68 lines
2.4 KiB
Swift

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Shared
import SwiftKeychainWrapper
let NotificationPasscodeDidCreate = "NotificationPasscodeDidCreate"
/// Displayed to the user when setting up a passcode.
class SetupPasscodeViewController: PagingPasscodeViewController, PasscodeInputViewDelegate {
fileprivate var confirmCode: String?
override init() {
super.init()
self.title = AuthenticationStrings.setPasscode
self.panes = [
PasscodePane(title: AuthenticationStrings.enterAPasscode, passcodeSize: 6),
PasscodePane(title: AuthenticationStrings.reenterPasscode, passcodeSize: 6),
]
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
panes.forEach { $0.codeInputView.delegate = self }
// Don't show the keyboard or allow typing if we're locked out. Also display the error.
if authenticationInfo?.isLocked() ?? false {
displayLockoutError()
panes.first?.codeInputView.isUserInteractionEnabled = false
} else {
panes.first?.codeInputView.becomeFirstResponder()
}
}
func passcodeInputView(_ inputView: PasscodeInputView, didFinishEnteringCode code: String) {
switch currentPaneIndex {
case 0:
confirmCode = code
scrollToNextAndSelect()
case 1:
// Constraint: The first and confirmation codes must match.
if confirmCode != code {
failMismatchPasscode()
resetAllInputFields()
scrollToPreviousAndSelect()
confirmCode = nil
return
}
createPasscodeWithCode(code)
dismissAnimated()
default:
break
}
}
fileprivate func createPasscodeWithCode(_ code: String) {
KeychainWrapper.sharedAppContainerKeychain.setAuthenticationInfo(AuthenticationKeychainInfo(passcode: code))
let notificationCenter = NotificationCenter.default
notificationCenter.post(name: Notification.Name(rawValue: NotificationPasscodeDidCreate), object: nil)
}
}