From e4a1b7e94d70c340a0ff80894bb286e27b6e87da Mon Sep 17 00:00:00 2001 From: G Date: Mon, 7 Mar 2022 21:45:12 +0300 Subject: [PATCH] 1. fixes based on PR #11 comments; 2. remove failed passcode attempts older than 30 days. --- .../Telegram-iOS/be.lproj/Localizable.strings | 2 +- .../Telegram-iOS/en.lproj/Localizable.strings | 2 +- .../Telegram-iOS/ru.lproj/Localizable.strings | 2 +- .../Sources/PasscodeEntryController.swift | 16 +++++++++++----- .../PasscodeOptionsController.swift | 14 +++++--------- .../Sources/PresentationPasscodeSettings.swift | 18 +++++++++--------- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Telegram/Telegram-iOS/be.lproj/Localizable.strings b/Telegram/Telegram-iOS/be.lproj/Localizable.strings index cee4f7fadec..6341ddb37f6 100644 --- a/Telegram/Telegram-iOS/be.lproj/Localizable.strings +++ b/Telegram/Telegram-iOS/be.lproj/Localizable.strings @@ -11,7 +11,7 @@ "Settings.Version" = "Telegram для iOS v%1$@ (%2$@)\nPTelegram версія %3$@"; "PasscodeSettings.BadAttempts" = "Няўдалыя спробы ўвода кода блакіроўкі"; -"PasscodeSettings.BadAttempts.Info" = "Паглядзіце ўсе няўдалыя спробы ўводу кода блакіроўкі."; +"PasscodeSettings.BadAttempts.Help" = "Паглядзіце ўсе няўдалыя спробы ўводу кода блакіроўкі."; "PasscodeSettings.BadAttempts.AppLogin" = "Разблакіроўка дадатка"; "PasscodeSettings.BadAttempts.SettingsLogin" = "Уваход у налады кода блакіроўкі"; "PasscodeSettings.BadAttempts.FakePasscode" = "Несапраўдны код блакіроўкі"; diff --git a/Telegram/Telegram-iOS/en.lproj/Localizable.strings b/Telegram/Telegram-iOS/en.lproj/Localizable.strings index d48ef48d266..6bdd2af73f4 100644 --- a/Telegram/Telegram-iOS/en.lproj/Localizable.strings +++ b/Telegram/Telegram-iOS/en.lproj/Localizable.strings @@ -7237,7 +7237,7 @@ Sorry for the inconvenience."; "Settings.Version" = "Telegram for iOS v%1$@ (%2$@)\nPTelegram version %3$@"; "PasscodeSettings.BadAttempts" = "Bad Passcode Attempts"; -"PasscodeSettings.BadAttempts.Info" = "See all bad passcode attempts"; +"PasscodeSettings.BadAttempts.Help" = "See all bad passcode attempts"; "PasscodeSettings.BadAttempts.AppLogin" = "App Login"; "PasscodeSettings.BadAttempts.SettingsLogin" = "Settings Login"; "PasscodeSettings.BadAttempts.FakePasscode" = "Fake Passcode"; diff --git a/Telegram/Telegram-iOS/ru.lproj/Localizable.strings b/Telegram/Telegram-iOS/ru.lproj/Localizable.strings index 5171bdd44ca..ba3dc87d2c0 100644 --- a/Telegram/Telegram-iOS/ru.lproj/Localizable.strings +++ b/Telegram/Telegram-iOS/ru.lproj/Localizable.strings @@ -11,7 +11,7 @@ "Settings.Version" = "Telegram для iOS v%1$@ (%2$@)\nPTelegram версия %3$@"; "PasscodeSettings.BadAttempts" = "Неудачные попытки ввода кода-пароля"; -"PasscodeSettings.BadAttempts.Info" = "Посмотрите все неудачные попытки ввода кода-пароля."; +"PasscodeSettings.BadAttempts.Help" = "Посмотрите все неудачные попытки ввода кода-пароля."; "PasscodeSettings.BadAttempts.AppLogin" = "Разблокировка приложения"; "PasscodeSettings.BadAttempts.SettingsLogin" = "Вход в настройки кода-пароля"; "PasscodeSettings.BadAttempts.FakePasscode" = "Ложный код-пароль"; diff --git a/submodules/PasscodeUI/Sources/PasscodeEntryController.swift b/submodules/PasscodeUI/Sources/PasscodeEntryController.swift index 00b852849c1..51d8c0d965b 100644 --- a/submodules/PasscodeUI/Sources/PasscodeEntryController.swift +++ b/submodules/PasscodeUI/Sources/PasscodeEntryController.swift @@ -178,11 +178,7 @@ public final class PasscodeEntryController: ViewController { strongSelf.appLockContext.failedUnlockAttempt() strongSelf.controllerNode.animateError() - let _ = updatePresentationPasscodeSettingsInteractively(accountManager: strongSelf.accountManager, { passcodeSettings in - var badPasscodeAttempts = passcodeSettings.badPasscodeAttempts ?? [] - badPasscodeAttempts.append(BadPasscodeAttempt(type: BadPasscodeAttempt.AppUnlockType, isFakePasscode: false)) - return passcodeSettings.withUpdatedBadPasscodeAttempts(badPasscodeAttempts) - }).start() + addBadPasscodeAttempt(accountManager: strongSelf.accountManager, bpa: BadPasscodeAttempt(type: BadPasscodeAttempt.AppUnlockType, isFakePasscode: false)) } } self.controllerNode.requestBiometrics = { [weak self] in @@ -296,3 +292,13 @@ public final class PasscodeEntryController: ViewController { } } } + +public func addBadPasscodeAttempt(accountManager: AccountManager, bpa: BadPasscodeAttempt) { + let _ = updatePresentationPasscodeSettingsInteractively(accountManager: accountManager, { passcodeSettings in + var badPasscodeAttempts = passcodeSettings.badPasscodeAttempts + let removeBeforeDate = CFAbsoluteTimeGetCurrent() - 30 * 24 * 60 * 60 // remove records older than 30 days + badPasscodeAttempts.removeAll(where: { $0.date < removeBeforeDate }) + badPasscodeAttempts.append(bpa) + return passcodeSettings.withUpdatedBadPasscodeAttempts(badPasscodeAttempts) + }).start() +} diff --git a/submodules/SettingsUI/Sources/Privacy and Security/PasscodeOptionsController.swift b/submodules/SettingsUI/Sources/Privacy and Security/PasscodeOptionsController.swift index 6198ec16d8a..ea0bfd21ff5 100644 --- a/submodules/SettingsUI/Sources/Privacy and Security/PasscodeOptionsController.swift +++ b/submodules/SettingsUI/Sources/Privacy and Security/PasscodeOptionsController.swift @@ -332,8 +332,8 @@ private func passcodeOptionsControllerEntries(presentationData: PresentationData entries.append(.addFakePasscode(presentationData.theme, presentationData.strings.PasscodeSettings_AddFakePasscode)) entries.append(.fakePasscodeInfo(presentationData.theme, presentationData.strings.PasscodeSettings_FakePasscodeHelp)) - entries.append(.badPasscodeAttempts(presentationData.theme, presentationData.strings.PasscodeSettings_BadAttempts, passcodeOptionsData.presentationSettings.badPasscodeAttempts?.count ?? 0)) - entries.append(.badPasscodeAttemptsInfo(presentationData.theme, presentationData.strings.PasscodeSettings_BadAttempts_Info)) + entries.append(.badPasscodeAttempts(presentationData.theme, presentationData.strings.PasscodeSettings_BadAttempts, passcodeOptionsData.presentationSettings.badPasscodeAttempts.count)) + entries.append(.badPasscodeAttemptsInfo(presentationData.theme, presentationData.strings.PasscodeSettings_BadAttempts_Help)) } return entries @@ -535,10 +535,10 @@ func passcodeOptionsController(context: AccountContext) -> ViewController { let bpaController = BadPasscodeAttemptsController(context: context) bpaController.clear = { let _ = (passcodeOptionsDataPromise.get() |> take(1)).start(next: { [weak passcodeOptionsDataPromise] data in - passcodeOptionsDataPromise?.set(.single(data.withUpdatedPresentationSettings(data.presentationSettings.withUpdatedBadPasscodeAttempts(nil)))) + passcodeOptionsDataPromise?.set(.single(data.withUpdatedPresentationSettings(data.presentationSettings.withUpdatedBadPasscodeAttempts([])))) let _ = updatePresentationPasscodeSettingsInteractively(accountManager: context.sharedContext.accountManager, { current in - return current.withUpdatedBadPasscodeAttempts(nil) + return current.withUpdatedBadPasscodeAttempts([]) }).start() }) @@ -615,11 +615,7 @@ public func passcodeOptionsAccessController(context: AccountContext, animateIn: if succeed { completion(true) } else { - let _ = updatePresentationPasscodeSettingsInteractively(accountManager: context.sharedContext.accountManager, { passcodeSettings in - var badPasscodeAttempts = passcodeSettings.badPasscodeAttempts ?? [] - badPasscodeAttempts.append(BadPasscodeAttempt(type: BadPasscodeAttempt.PasscodeSettingsType, isFakePasscode: false)) - return passcodeSettings.withUpdatedBadPasscodeAttempts(badPasscodeAttempts) - }).start() + addBadPasscodeAttempt(accountManager: context.sharedContext.accountManager, bpa: BadPasscodeAttempt(type: BadPasscodeAttempt.PasscodeSettingsType, isFakePasscode: false)) } return succeed } diff --git a/submodules/TelegramUIPreferences/Sources/PresentationPasscodeSettings.swift b/submodules/TelegramUIPreferences/Sources/PresentationPasscodeSettings.swift index a0575c499d5..ba8839ab2b4 100644 --- a/submodules/TelegramUIPreferences/Sources/PresentationPasscodeSettings.swift +++ b/submodules/TelegramUIPreferences/Sources/PresentationPasscodeSettings.swift @@ -7,9 +7,9 @@ public struct BadPasscodeAttempt: Codable, Equatable { public static let AppUnlockType: Int32 = 0 public static let PasscodeSettingsType: Int32 = 1 - public var type: Int32 - public var isFakePasscode: Bool - public var date: CFAbsoluteTime + public let type: Int32 + public let isFakePasscode: Bool + public let date: CFAbsoluteTime public init(type: Int32, isFakePasscode: Bool, date: CFAbsoluteTime = CFAbsoluteTimeGetCurrent()) { self.type = type @@ -24,13 +24,13 @@ public struct PresentationPasscodeSettings: Codable, Equatable { public var biometricsDomainState: Data? public var shareBiometricsDomainState: Data? - public var badPasscodeAttempts: [BadPasscodeAttempt]? + public var badPasscodeAttempts: [BadPasscodeAttempt] public static var defaultSettings: PresentationPasscodeSettings { - return PresentationPasscodeSettings(enableBiometrics: false, autolockTimeout: nil, biometricsDomainState: nil, shareBiometricsDomainState: nil, badPasscodeAttempts: nil) + return PresentationPasscodeSettings(enableBiometrics: false, autolockTimeout: nil, biometricsDomainState: nil, shareBiometricsDomainState: nil, badPasscodeAttempts: []) } - public init(enableBiometrics: Bool, autolockTimeout: Int32?, biometricsDomainState: Data?, shareBiometricsDomainState: Data?, badPasscodeAttempts: [BadPasscodeAttempt]?) { + public init(enableBiometrics: Bool, autolockTimeout: Int32?, biometricsDomainState: Data?, shareBiometricsDomainState: Data?, badPasscodeAttempts: [BadPasscodeAttempt]) { self.enableBiometrics = enableBiometrics self.autolockTimeout = autolockTimeout self.biometricsDomainState = biometricsDomainState @@ -47,7 +47,7 @@ public struct PresentationPasscodeSettings: Codable, Equatable { self.biometricsDomainState = try container.decodeIfPresent(Data.self, forKey: "ds") self.shareBiometricsDomainState = try container.decodeIfPresent(Data.self, forKey: "sds") - self.badPasscodeAttempts = try container.decodeIfPresent([BadPasscodeAttempt].self, forKey: "pt_bpa") + self.badPasscodeAttempts = try container.decodeIfPresent([BadPasscodeAttempt].self, forKey: "pt_bpa") ?? [] } public func encode(to encoder: Encoder) throws { @@ -58,7 +58,7 @@ public struct PresentationPasscodeSettings: Codable, Equatable { try container.encodeIfPresent(self.biometricsDomainState, forKey: "ds") try container.encodeIfPresent(self.shareBiometricsDomainState, forKey: "sds") - try container.encodeIfPresent(self.badPasscodeAttempts, forKey: "pt_bpa") + try container.encode(self.badPasscodeAttempts, forKey: "pt_bpa") } public static func ==(lhs: PresentationPasscodeSettings, rhs: PresentationPasscodeSettings) -> Bool { @@ -81,7 +81,7 @@ public struct PresentationPasscodeSettings: Codable, Equatable { return PresentationPasscodeSettings(enableBiometrics: self.enableBiometrics, autolockTimeout: autolockTimeout, biometricsDomainState: self.biometricsDomainState, shareBiometricsDomainState: shareBiometricsDomainState, badPasscodeAttempts: self.badPasscodeAttempts) } - public func withUpdatedBadPasscodeAttempts(_ badPasscodeAttempts: [BadPasscodeAttempt]?) -> PresentationPasscodeSettings { + public func withUpdatedBadPasscodeAttempts(_ badPasscodeAttempts: [BadPasscodeAttempt]) -> PresentationPasscodeSettings { return PresentationPasscodeSettings(enableBiometrics: self.enableBiometrics, autolockTimeout: self.autolockTimeout, biometricsDomainState: self.biometricsDomainState, shareBiometricsDomainState: self.shareBiometricsDomainState, badPasscodeAttempts: badPasscodeAttempts) } }