From 4a9c3dacc8315fdbe7a4bd47e20a1e125db14ef5 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 30 Jun 2022 16:44:17 +0300 Subject: [PATCH] Update account deletion API --- .../Sources/ChannelVisibilityController.swift | 2 +- .../Sources/DeleteAccountDataController.swift | 12 +++--- submodules/TelegramApi/Sources/Api27.swift | 8 ++-- .../TelegramCore/Sources/Authorization.swift | 2 +- .../Auth/TelegramEngineAuth.swift | 39 ++++++++++++++++--- .../Sources/ApplicationContext.swift | 2 +- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/submodules/PeerInfoUI/Sources/ChannelVisibilityController.swift b/submodules/PeerInfoUI/Sources/ChannelVisibilityController.swift index 5313ec7bbe..c7cb1f99d1 100644 --- a/submodules/PeerInfoUI/Sources/ChannelVisibilityController.swift +++ b/submodules/PeerInfoUI/Sources/ChannelVisibilityController.swift @@ -1147,7 +1147,7 @@ public func channelVisibilityController(context: AccountContext, updatedPresenta let _ = combineLatest( queue: Queue.mainQueue(), adminedPublicChannels.get() |> filter { $0 != nil } |> take(1), - context.engine.data.subscribe(TelegramEngine.EngineData.Item.Peer.Peer(id: context.account.peerId)), + context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: context.account.peerId)), context.engine.data.get( TelegramEngine.EngineData.Item.Configuration.UserLimits(isPremium: false), TelegramEngine.EngineData.Item.Configuration.UserLimits(isPremium: true) diff --git a/submodules/SettingsUI/Sources/DeleteAccountDataController.swift b/submodules/SettingsUI/Sources/DeleteAccountDataController.swift index 8396eb6eb5..04be040592 100644 --- a/submodules/SettingsUI/Sources/DeleteAccountDataController.swift +++ b/submodules/SettingsUI/Sources/DeleteAccountDataController.swift @@ -387,7 +387,7 @@ func deleteAccountDataController(context: AccountContext, mode: DeleteAccountDat proceedImpl = { [weak controller] in let presentationData = context.sharedContext.currentPresentationData.with { $0 } - let action: ([EnginePeer]) -> Void = { preloadedPeers in + let action: ([EnginePeer], String?) -> Void = { preloadedPeers, password in let nextMode: DeleteAccountDataMode? switch mode { case .peers: @@ -423,7 +423,7 @@ func deleteAccountDataController(context: AccountContext, mode: DeleteAccountDat let accountId = context.account.id let accountManager = context.sharedContext.accountManager - let _ = (context.engine.auth.deleteAccount(reason: "Manual") + let _ = (context.engine.auth.deleteAccount(reason: "Manual", password: password) |> deliverOnMainQueue).start(error: { _ in updateState { current in var updated = current @@ -447,7 +447,7 @@ func deleteAccountDataController(context: AccountContext, mode: DeleteAccountDat let _ = (preloadedGroupPeers.get() |> take(1) |> deliverOnMainQueue).start(next: { peers in - action(peers) + action(peers, nil) }) case .phone: var phoneNumber: String? @@ -469,7 +469,7 @@ func deleteAccountDataController(context: AccountContext, mode: DeleteAccountDat presentControllerImpl?(textAlertController(context: context, title: nil, text: presentationData.strings.DeleteAccount_InvalidPhoneNumberError, actions: [TextAlertAction(type: .defaultAction, title: presentationData.strings.Common_OK, action: {})])) return } - action([]) + action([], nil) } }) } @@ -507,13 +507,13 @@ func deleteAccountDataController(context: AccountContext, mode: DeleteAccountDat return updated } - action([]) + action([], state.password) }) return } default: - action([]) + action([], nil) } } diff --git a/submodules/TelegramApi/Sources/Api27.swift b/submodules/TelegramApi/Sources/Api27.swift index a7cbb13c79..d5594303ec 100644 --- a/submodules/TelegramApi/Sources/Api27.swift +++ b/submodules/TelegramApi/Sources/Api27.swift @@ -156,11 +156,13 @@ public extension Api.functions.account { } } public extension Api.functions.account { - static func deleteAccount(reason: String) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + static func deleteAccount(flags: Int32, reason: String, password: Api.InputCheckPasswordSRP?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() - buffer.appendInt32(1099779595) + buffer.appendInt32(-1564422284) + serializeInt32(flags, buffer: buffer, boxed: false) serializeString(reason, buffer: buffer, boxed: false) - return (FunctionDescription(name: "account.deleteAccount", parameters: [("reason", String(describing: reason))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Bool? in + if Int(flags) & Int(1 << 0) != 0 {password!.serialize(buffer, true)} + return (FunctionDescription(name: "account.deleteAccount", parameters: [("flags", String(describing: flags)), ("reason", String(describing: reason)), ("password", String(describing: password))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Bool? in let reader = BufferReader(buffer) var result: Api.Bool? if let signature = reader.readInt32() { diff --git a/submodules/TelegramCore/Sources/Authorization.swift b/submodules/TelegramCore/Sources/Authorization.swift index 6624cbd1c0..3fdc5c5ea4 100644 --- a/submodules/TelegramCore/Sources/Authorization.swift +++ b/submodules/TelegramCore/Sources/Authorization.swift @@ -494,7 +494,7 @@ public enum AccountResetError { } public func performAccountReset(account: UnauthorizedAccount) -> Signal { - return account.network.request(Api.functions.account.deleteAccount(reason: "")) + return account.network.request(Api.functions.account.deleteAccount(flags: 0, reason: "", password: nil)) |> map { _ -> Int32? in return nil } |> `catch` { error -> Signal in if error.errorDescription.hasPrefix("2FA_CONFIRM_WAIT_") { diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Auth/TelegramEngineAuth.swift b/submodules/TelegramCore/Sources/TelegramEngine/Auth/TelegramEngineAuth.swift index 09b6201252..b5b82e059f 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Auth/TelegramEngineAuth.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Auth/TelegramEngineAuth.swift @@ -90,12 +90,41 @@ public extension TelegramEngine { return _internal_updateTwoStepVerificationPassword(network: self.account.network, currentPassword: currentPassword, updatedPassword: updatedPassword) } - public func deleteAccount(reason: String) -> Signal { - return self.account.network.request(Api.functions.account.deleteAccount(reason: reason)) - |> mapError { _ -> DeleteAccountError in - return .generic + public func deleteAccount(reason: String, password: String?) -> Signal { + let network = self.account.network + + let passwordSignal: Signal + if let password = password { + passwordSignal = _internal_twoStepAuthData(network) + |> mapError { _ -> DeleteAccountError in + return .generic + } + |> mapToSignal { authData -> Signal in + if let currentPasswordDerivation = authData.currentPasswordDerivation, let srpSessionData = authData.srpSessionData { + guard let kdfResult = passwordKDF(encryptionProvider: network.encryptionProvider, password: password, derivation: currentPasswordDerivation, srpSessionData: srpSessionData) else { + return .fail(.generic) + } + return .single(.inputCheckPasswordSRP(srpId: kdfResult.id, A: Buffer(data: kdfResult.A), M1: Buffer(data: kdfResult.M1))) + } else { + return .single(nil) + } + } + } else { + passwordSignal = .single(nil) + } + + return passwordSignal + |> mapToSignal { password -> Signal in + var flags: Int32 = 0 + if let _ = password { + flags |= (1 << 0) + } + return self.account.network.request(Api.functions.account.deleteAccount(flags: flags, reason: reason, password: password)) + |> mapError { _ -> DeleteAccountError in + return .generic + } + |> ignoreValues } - |> ignoreValues } public func updateTwoStepVerificationEmail(currentPassword: String, updatedEmail: String) -> Signal { diff --git a/submodules/TelegramUI/Sources/ApplicationContext.swift b/submodules/TelegramUI/Sources/ApplicationContext.swift index edba458be9..adef8abf1a 100644 --- a/submodules/TelegramUI/Sources/ApplicationContext.swift +++ b/submodules/TelegramUI/Sources/ApplicationContext.swift @@ -486,7 +486,7 @@ final class AuthorizedApplicationContext { } let accountId = strongSelf.context.account.id let accountManager = strongSelf.context.sharedContext.accountManager - let _ = (strongSelf.context.engine.auth.deleteAccount(reason: "GDPR") + let _ = (strongSelf.context.engine.auth.deleteAccount(reason: "GDPR", password: nil) |> deliverOnMainQueue).start(error: { _ in guard let strongSelf = self else { return