From 12383ea1d29c2e02fce1e894e53275b47f8333df Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 29 Jun 2021 05:16:48 +0300 Subject: [PATCH] Add td_api::resetPassword. --- td/generate/scheme/td_api.tl | 23 +++++++++++++++++++---- td/telegram/PasswordManager.cpp | 27 +++++++++++++++++++++++++++ td/telegram/PasswordManager.h | 3 +++ td/telegram/Td.cpp | 6 ++++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 6 ++++-- 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 83d01e896..8b93fa18a 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -119,7 +119,7 @@ authorizationStateClosed = AuthorizationState; //@description Represents the current state of 2-step verification @has_password True, if a 2-step verification password is set @password_hint Hint for the password; may be empty //@has_recovery_email_address True, if a recovery email is set @has_passport_data True, if some Telegram Passport elements were saved //@recovery_email_address_code_info Information about the recovery email address to which the confirmation email was sent; may be null -//@pending_reset_date If not 0, point in time (Unix timestamp) after which password can be reset immediately using resetPassword +//@pending_reset_date If not 0, point in time (Unix timestamp) after which the password can be reset immediately using resetPassword passwordState has_password:Bool password_hint:string has_recovery_email_address:Bool has_passport_data:Bool recovery_email_address_code_info:emailAddressAuthenticationCodeInfo pending_reset_date:int32 = PasswordState; //@description Contains information about the current recovery email address @recovery_email_address Recovery email address @@ -2718,6 +2718,18 @@ checkStickerSetNameResultNameInvalid = CheckStickerSetNameResult; checkStickerSetNameResultNameOccupied = CheckStickerSetNameResult; +//@class ResetPasswordResult @description Represents result of 2-step verification password reset + +//@description The password was reset +resetPasswordResultOk = ResetPasswordResult; + +//@description The password reset request is pending @pending_reset_date Point in time (Unix timestamp) after which the password can be reset immediately using resetPassword +resetPasswordResultPending pending_reset_date:int32 = ResetPasswordResult; + +//@description The password reset request was declined @retry_date Point in time (Unix timestamp) when the password reset can be retried +resetPasswordResultDeclined retry_date:int32 = ResetPasswordResult; + + //@class MessageFileType @description Contains information about a file with messages exported from another app //@description The messages was exported from a private chat @name Name of the other party; may be empty if unrecognized @@ -3923,16 +3935,19 @@ checkRecoveryEmailAddressCode code:string = PasswordState; //@description Resends the 2-step verification recovery email address verification code resendRecoveryEmailAddressCode = PasswordState; -//@description Requests to send a password recovery code to an email address that was previously set up +//@description Requests to send a 2-step verification password recovery code to an email address that was previously set up requestPasswordRecovery = EmailAddressAuthenticationCodeInfo; -//@description Checks whether a password recovery code sent to an email address is valid @recovery_code Recovery code to check +//@description Checks whether a 2-step verification password recovery code sent to an email address is valid @recovery_code Recovery code to check checkPasswordRecoveryCode recovery_code:string = Ok; -//@description Recovers the password using a recovery code sent to an email address that was previously set up +//@description Recovers the 2-step verification password using a recovery code sent to an email address that was previously set up //@recovery_code Recovery code to check @new_password New password of the user; may be empty to remove the password @new_hint New password hint; may be empty recoverPassword recovery_code:string new_password:string new_hint:string = PasswordState; +//@description Removes 2-step verification password without previous password and access to recovery email address. The password can't be reset immediately and the request needs to be repeated after the specified time +resetPassword = ResetPasswordResult; + //@description Creates a new temporary password for processing payments @password Persistent user password @valid_for Time during which the temporary password will be valid, in seconds; should be between 60 and 86400 createTemporaryPassword password:string valid_for:int32 = TemporaryPasswordState; diff --git a/td/telegram/PasswordManager.cpp b/td/telegram/PasswordManager.cpp index 803727f46..2dc836c05 100644 --- a/td/telegram/PasswordManager.cpp +++ b/td/telegram/PasswordManager.cpp @@ -537,6 +537,33 @@ void PasswordManager::do_recover_password(string code, PasswordInputSettings &&n })); } +void PasswordManager::reset_password(Promise promise) { + send_with_promise( + G()->net_query_creator().create(telegram_api::account_resetPassword()), + PromiseCreator::lambda([promise = std::move(promise)](Result r_query) mutable { + auto r_result = fetch_result(std::move(r_query)); + if (r_result.is_error()) { + return promise.set_error(r_result.move_as_error()); + } + auto result_ptr = r_result.move_as_ok(); + switch (result_ptr->get_id()) { + case telegram_api::account_resetPasswordOk::ID: + return promise.set_value(td_api::make_object()); + case telegram_api::account_resetPasswordRequestedWait::ID: { + auto result = move_tl_object_as(result_ptr); + return promise.set_value(td_api::make_object(result->until_date_)); + } + case telegram_api::account_resetPasswordFailedWait::ID: { + auto result = move_tl_object_as(result_ptr); + return promise.set_value(td_api::make_object(result->retry_date_)); + } + default: + UNREACHABLE(); + break; + } + })); +} + void PasswordManager::update_password_settings(UpdateSettings update_settings, Promise promise) { auto result_promise = PromiseCreator::lambda( [actor_id = actor_id(this), promise = std::move(promise)](Result r_update_settings) mutable { diff --git a/td/telegram/PasswordManager.h b/td/telegram/PasswordManager.h index 9418ced1d..63348fdd8 100644 --- a/td/telegram/PasswordManager.h +++ b/td/telegram/PasswordManager.h @@ -53,6 +53,7 @@ class PasswordManager : public NetQueryCallback { public: using State = tl_object_ptr; using TempState = tl_object_ptr; + using ResetPasswordResult = tl_object_ptr; using PasswordInputSettings = tl_object_ptr; explicit PasswordManager(ActorShared<> parent) : parent_(std::move(parent)) { @@ -83,6 +84,8 @@ class PasswordManager : public NetQueryCallback { void check_password_recovery_code(string code, Promise promise); void recover_password(string code, string new_password, string new_hint, Promise promise); + void reset_password(Promise promise); + void get_secure_secret(string password, Promise promise); void get_input_check_password_srp(string password, Promise> &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 4fb0ab20b..d97613d8e 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4948,6 +4948,12 @@ void Td::on_request(uint64 id, td_api::recoverPassword &request) { std::move(request.new_password_), std::move(request.new_hint_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::resetPassword &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + send_closure(password_manager_, &PasswordManager::reset_password, std::move(promise)); +} + void Td::on_request(uint64 id, td_api::getTemporaryPasswordState &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index c08245573..adfb54cb3 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -440,6 +440,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, td_api::recoverPassword &request); + void on_request(uint64 id, const td_api::resetPassword &request); + void on_request(uint64 id, td_api::getTemporaryPasswordState &request); void on_request(uint64 id, td_api::createTemporaryPassword &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index b26090c32..b34084790 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -1781,17 +1781,19 @@ class CliClient final : public Actor { send_request(td_api::make_object(args)); } else if (op == "rpncc") { send_request(td_api::make_object()); - } else if (op == "rpr" || op == "RequestPasswordRecovery") { + } else if (op == "rpr") { send_request(td_api::make_object()); } else if (op == "cprc") { string recovery_code = args; send_request(td_api::make_object(recovery_code)); - } else if (op == "rp" || op == "RecoverPassword") { + } else if (op == "rp") { string recovery_code; string new_password; string new_hint; get_args(args, recovery_code, new_password, new_hint); send_request(td_api::make_object(recovery_code, new_password, new_hint)); + } else if (op == "resetp") { + send_request(td_api::make_object()); } else if (op == "gtp" || op == "GetTemporaryPassword") { send_request(td_api::make_object()); } else if (op == "ctp" || op == "CreateTemporaryPassword") {