From 25958fb4067cafa26f1a3dec773bb2eb6855247c Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 13 Apr 2024 20:25:05 +0300 Subject: [PATCH] Combine phone number verification methods into a single API method. --- SplitSource.php | 2 +- td/generate/scheme/td_api.tl | 57 +++++++++++---------------- td/telegram/PhoneNumberManager.cpp | 47 +++++++++++----------- td/telegram/PhoneNumberManager.h | 11 ++---- td/telegram/Td.cpp | 63 +++++------------------------- td/telegram/Td.h | 18 ++------- td/telegram/cli.cpp | 37 ++++++++---------- 7 files changed, 81 insertions(+), 154 deletions(-) diff --git a/SplitSource.php b/SplitSource.php index f4dcd16f9..c9c36c4f0 100644 --- a/SplitSource.php +++ b/SplitSource.php @@ -383,7 +383,7 @@ function split_file($file, $chunks, $undo) { 'option_manager[_(-](?![.]get[(][)])|OptionManager' => 'OptionManager', 'password_manager[_(-](?![.]get[(][)])|PasswordManager' => 'PasswordManager', 'people_nearby_manager[_(-](?![.]get[(][)])|PeopleNearbyManager' => 'PeopleNearbyManager', - '[a-z_]*phone_number_manager[_(-](?![.]get[(][)])|PhoneNumberManager' => 'PhoneNumberManager', + 'phone_number_manager[_(-](?![.]get[(][)])|PhoneNumberManager' => 'PhoneNumberManager', 'PhotoSizeSource' => 'PhotoSizeSource', 'poll_manager[_(-](?![.]get[(][)])|PollManager' => 'PollManager', 'privacy_manager[_(-](?![.]get[(][)])|PrivacyManager' => 'PrivacyManager', diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2a74258dc..67928353f 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -6039,8 +6039,8 @@ internalLinkTypeMessageDraft text:formattedText contains_link:Bool = InternalLin //-If empty, then onActivityResult method must be used to return response on Android, or the link tgbot{bot_user_id}://passport/success or tgbot{bot_user_id}://passport/cancel must be opened otherwise internalLinkTypePassportDataRequest bot_user_id:int53 scope:string public_key:string nonce:string callback_url:string = InternalLinkType; -//@description The link can be used to confirm ownership of a phone number to prevent account deletion. Call sendPhoneNumberConfirmationCode with the given hash and phone number to process the link. -//-If succeeded, call checkPhoneNumberConfirmationCode to check entered by the user code, or resendPhoneNumberConfirmationCode to resend it +//@description The link can be used to confirm ownership of a phone number to prevent account deletion. Call sendPhoneNumberCode with the given phone number and with phoneNumberCodeTypeConfirmOwnership with the given hash to process the link. +//-If succeeded, call checkPhoneNumberCode to check entered by the user code, or resendPhoneNumberCode to resend it //@hash Hash value from the link //@phone_number Phone number value from the link internalLinkTypePhoneNumberConfirmation hash:string phone_number:string = InternalLinkType; @@ -6730,6 +6730,19 @@ botCommandScopeChatAdministrators chat_id:int53 = BotCommandScope; botCommandScopeChatMember chat_id:int53 user_id:int53 = BotCommandScope; +//@class PhoneNumberCodeType @description Describes type of the request for which a code is sent to a phone number + +//@description Checks ownership of a new phone number to change the user's authentication phone number; for official Android and iOS applications only. +phoneNumberCodeTypeChange = PhoneNumberCodeType; + +//@description Verifies ownership of a phone number to be added to the user's Telegram Passport +phoneNumberCodeTypeVerify = PhoneNumberCodeType; + +//@description Confirms ownership of a phone number to prevent account deletion while handling links of the type internalLinkTypePhoneNumberConfirmation +//@hash Hash value from the link +phoneNumberCodeTypeConfirmOwnership hash:string = PhoneNumberCodeType; + + //@class Update @description Contains notifications about data changes //@description The user authorization state has changed @authorization_state New authorization state @@ -9754,16 +9767,17 @@ setBusinessAwayMessageSettings away_message_settings:businessAwayMessageSettings setBusinessStartPage start_page:inputBusinessStartPage = Ok; -//@description Changes the phone number of the user and sends an authentication code to the user's new phone number; for official Android and iOS applications only. On success, returns information about the sent code -//@phone_number The new phone number of the user in international format +//@description Sends a code to the specified phone number. Aborts previous phone number verification if any. On success, returns information about the sent code +//@phone_number The phone number, in international format //@settings Settings for the authentication of the user's phone number; pass null to use default settings -changePhoneNumber phone_number:string settings:phoneNumberAuthenticationSettings = AuthenticationCodeInfo; +//@type Type of the request for which the code is sent +sendPhoneNumberCode phone_number:string settings:phoneNumberAuthenticationSettings type:PhoneNumberCodeType = AuthenticationCodeInfo; -//@description Resends the authentication code sent to confirm a new phone number for the current user. Works only if the previously received authenticationCodeInfo next_code_type was not null and the server-specified timeout has passed -resendChangePhoneNumberCode = AuthenticationCodeInfo; +//@description Resends the authentication code sent to a phone number. Works only if the previously received authenticationCodeInfo next_code_type was not null and the server-specified timeout has passed +resendPhoneNumberCode = AuthenticationCodeInfo; -//@description Checks the authentication code sent to confirm a new phone number of the user @code Authentication code to check -checkChangePhoneNumberCode code:string = Ok; +//@description Check the authentication code and completes the request for which the code was sent if appropriate @code Authentication code to check +checkPhoneNumberCode code:string = Ok; //@description Returns the business bot that is connected to the current user account. Returns a 404 error if there is no connected bot @@ -10340,18 +10354,6 @@ setPassportElementErrors user_id:int53 errors:vector getPreferredCountryLanguage country_code:string = Text; -//@description Sends a code to verify a phone number to be added to a user's Telegram Passport -//@phone_number The phone number of the user, in international format -//@settings Settings for the authentication of the user's phone number; pass null to use default settings -sendPhoneNumberVerificationCode phone_number:string settings:phoneNumberAuthenticationSettings = AuthenticationCodeInfo; - -//@description Resends the code to verify a phone number to be added to a user's Telegram Passport -resendPhoneNumberVerificationCode = AuthenticationCodeInfo; - -//@description Checks the phone number verification code for Telegram Passport @code Verification code to check -checkPhoneNumberVerificationCode code:string = Ok; - - //@description Sends a code to verify an email address to be added to a user's Telegram Passport @email_address Email address sendEmailAddressVerificationCode email_address:string = EmailAddressAuthenticationCodeInfo; @@ -10380,19 +10382,6 @@ getPassportAuthorizationFormAvailableElements authorization_form_id:int32 passwo sendPassportAuthorizationForm authorization_form_id:int32 types:vector = Ok; -//@description Sends phone number confirmation code to handle links of the type internalLinkTypePhoneNumberConfirmation -//@hash Hash value from the link -//@phone_number Phone number value from the link -//@settings Settings for the authentication of the user's phone number; pass null to use default settings -sendPhoneNumberConfirmationCode hash:string phone_number:string settings:phoneNumberAuthenticationSettings = AuthenticationCodeInfo; - -//@description Resends phone number confirmation code -resendPhoneNumberConfirmationCode = AuthenticationCodeInfo; - -//@description Checks phone number confirmation code @code Confirmation code to check -checkPhoneNumberConfirmationCode code:string = Ok; - - //@description Informs the server about the number of pending bot updates if they haven't been processed for a long time; for bots only @pending_update_count The number of pending updates @error_message The last error message setBotUpdatesStatus pending_update_count:int32 error_message:string = Ok; diff --git a/td/telegram/PhoneNumberManager.cpp b/td/telegram/PhoneNumberManager.cpp index 0d18a50b6..2e9333b97 100644 --- a/td/telegram/PhoneNumberManager.cpp +++ b/td/telegram/PhoneNumberManager.cpp @@ -8,6 +8,7 @@ #include "td/telegram/ConfigManager.h" #include "td/telegram/Global.h" +#include "td/telegram/misc.h" #include "td/telegram/SuggestedAction.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" @@ -180,45 +181,47 @@ void PhoneNumberManager::on_send_code_result(Result settings, + td_api::object_ptr type, Promise> &&promise) { + inc_generation(); if (phone_number.empty()) { return promise.set_error(Status::Error(400, "Phone number must be non-empty")); } + if (type == nullptr) { + return promise.set_error(Status::Error(400, "Type must be non-empty")); + } - inc_generation(); - type_ = type; - switch (type_) { - case Type::ChangePhone: + switch (type->get_id()) { + case td_api::phoneNumberCodeTypeChange::ID: + type_ = Type::ChangePhone; send_closure(G()->config_manager(), &ConfigManager::hide_suggested_action, SuggestedAction{SuggestedAction::Type::CheckPhoneNumber}); return send_new_send_code_query(send_code_helper_.send_change_phone_code(phone_number, settings), std::move(promise)); - case Type::VerifyPhone: + case td_api::phoneNumberCodeTypeVerify::ID: + type_ = Type::VerifyPhone; return send_new_send_code_query(send_code_helper_.send_verify_phone_code(phone_number, settings), std::move(promise)); - case Type::ConfirmPhone: + case td_api::phoneNumberCodeTypeConfirmOwnership::ID: { + auto hash = std::move(static_cast(type.get())->hash_); + if (!clean_input_string(hash)) { + return promise.set_error(Status::Error(400, "Hash must be encoded in UTF-8")); + } + if (hash.empty()) { + return promise.set_error(Status::Error(400, "Hash must be non-empty")); + } + + type_ = Type::ConfirmPhone; + return send_new_send_code_query(send_code_helper_.send_confirm_phone_code(hash, phone_number, settings), + std::move(promise)); + } default: UNREACHABLE(); } } -void PhoneNumberManager::set_phone_number_and_hash( - string hash, string phone_number, td_api::object_ptr settings, - Promise> &&promise) { - if (phone_number.empty()) { - return promise.set_error(Status::Error(400, "Phone number must be non-empty")); - } - if (hash.empty()) { - return promise.set_error(Status::Error(400, "Hash must be non-empty")); - } - - inc_generation(); - type_ = Type::ConfirmPhone; - send_new_send_code_query(send_code_helper_.send_confirm_phone_code(hash, phone_number, settings), std::move(promise)); -} - void PhoneNumberManager::resend_authentication_code( Promise> &&promise) { if (state_ != State::WaitCode) { diff --git a/td/telegram/PhoneNumberManager.h b/td/telegram/PhoneNumberManager.h index 8873f641c..eb4d5d9cf 100644 --- a/td/telegram/PhoneNumberManager.h +++ b/td/telegram/PhoneNumberManager.h @@ -23,21 +23,16 @@ class PhoneNumberManager final : public Actor { public: PhoneNumberManager(Td *td, ActorShared<> parent); - enum class Type : int32 { ChangePhone, VerifyPhone, ConfirmPhone }; - - void set_phone_number(Type type, string phone_number, - td_api::object_ptr settings, + void set_phone_number(string phone_number, td_api::object_ptr settings, + td_api::object_ptr type, Promise> &&promise); - void set_phone_number_and_hash(string hash, string phone_number, - td_api::object_ptr settings, - Promise> &&promise); - void resend_authentication_code(Promise> &&promise); void check_code(string code, Promise &&promise); private: + enum class Type : int32 { ChangePhone, VerifyPhone, ConfirmPhone }; enum class State : int32 { Ok, WaitCode } state_ = State::Ok; void tear_down() final; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 15cf92545..52f3e1f40 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4610,27 +4610,27 @@ void Td::on_request(uint64 id, td_api::deleteAccount &request) { send_closure(auth_manager_actor_, &AuthManager::delete_account, id, request.reason_, request.password_); } -void Td::on_request(uint64 id, td_api::changePhoneNumber &request) { +void Td::on_request(uint64 id, td_api::sendPhoneNumberCode &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.phone_number_); CREATE_REQUEST_PROMISE(); - phone_number_manager_->set_phone_number(PhoneNumberManager::Type::ChangePhone, std::move(request.phone_number_), - std::move(request.settings_), std::move(promise)); + phone_number_manager_->set_phone_number(std::move(request.phone_number_), std::move(request.settings_), + std::move(request.type_), std::move(promise)); } -void Td::on_request(uint64 id, td_api::checkChangePhoneNumberCode &request) { +void Td::on_request(uint64 id, const td_api::resendPhoneNumberCode &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + phone_number_manager_->resend_authentication_code(std::move(promise)); +} + +void Td::on_request(uint64 id, td_api::checkPhoneNumberCode &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.code_); CREATE_OK_REQUEST_PROMISE(); phone_number_manager_->check_code(std::move(request.code_), std::move(promise)); } -void Td::on_request(uint64 id, td_api::resendChangePhoneNumberCode &request) { - CHECK_IS_USER(); - CREATE_REQUEST_PROMISE(); - phone_number_manager_->resend_authentication_code(std::move(promise)); -} - void Td::on_request(uint64 id, const td_api::getUserLink &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); @@ -9088,27 +9088,6 @@ void Td::on_request(uint64 id, td_api::getPreferredCountryLanguage &request) { std::move(promise)); } -void Td::on_request(uint64 id, td_api::sendPhoneNumberVerificationCode &request) { - CHECK_IS_USER(); - CLEAN_INPUT_STRING(request.phone_number_); - CREATE_REQUEST_PROMISE(); - phone_number_manager_->set_phone_number(PhoneNumberManager::Type::VerifyPhone, std::move(request.phone_number_), - std::move(request.settings_), std::move(promise)); -} - -void Td::on_request(uint64 id, const td_api::resendPhoneNumberVerificationCode &request) { - CHECK_IS_USER(); - CREATE_REQUEST_PROMISE(); - phone_number_manager_->resend_authentication_code(std::move(promise)); -} - -void Td::on_request(uint64 id, td_api::checkPhoneNumberVerificationCode &request) { - CHECK_IS_USER(); - CLEAN_INPUT_STRING(request.code_); - CREATE_OK_REQUEST_PROMISE(); - phone_number_manager_->check_code(std::move(request.code_), std::move(promise)); -} - void Td::on_request(uint64 id, td_api::sendEmailAddressVerificationCode &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.email_address_); @@ -9183,28 +9162,6 @@ void Td::on_request(uint64 id, td_api::sendPassportAuthorizationForm &request) { get_secure_value_types_td_api(request.types_), std::move(promise)); } -void Td::on_request(uint64 id, td_api::sendPhoneNumberConfirmationCode &request) { - CHECK_IS_USER(); - CLEAN_INPUT_STRING(request.phone_number_); - CLEAN_INPUT_STRING(request.hash_); - CREATE_REQUEST_PROMISE(); - phone_number_manager_->set_phone_number_and_hash(std::move(request.hash_), std::move(request.phone_number_), - std::move(request.settings_), std::move(promise)); -} - -void Td::on_request(uint64 id, const td_api::resendPhoneNumberConfirmationCode &request) { - CHECK_IS_USER(); - CREATE_REQUEST_PROMISE(); - phone_number_manager_->resend_authentication_code(std::move(promise)); -} - -void Td::on_request(uint64 id, td_api::checkPhoneNumberConfirmationCode &request) { - CHECK_IS_USER(); - CLEAN_INPUT_STRING(request.code_); - CREATE_OK_REQUEST_PROMISE(); - phone_number_manager_->check_code(std::move(request.code_), std::move(promise)); -} - void Td::on_request(uint64 id, const td_api::getSupportUser &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 07454a520..dbbad975b 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -567,11 +567,11 @@ class Td final : public Actor { void on_request(uint64 id, td_api::deleteAccount &request); - void on_request(uint64 id, td_api::changePhoneNumber &request); + void on_request(uint64 id, td_api::sendPhoneNumberCode &request); - void on_request(uint64 id, td_api::checkChangePhoneNumberCode &request); + void on_request(uint64 id, const td_api::resendPhoneNumberCode &request); - void on_request(uint64 id, td_api::resendChangePhoneNumberCode &request); + void on_request(uint64 id, td_api::checkPhoneNumberCode &request); void on_request(uint64 id, const td_api::getUserLink &request); @@ -1737,12 +1737,6 @@ class Td final : public Actor { void on_request(uint64 id, td_api::getPreferredCountryLanguage &request); - void on_request(uint64 id, td_api::sendPhoneNumberVerificationCode &request); - - void on_request(uint64 id, const td_api::resendPhoneNumberVerificationCode &request); - - void on_request(uint64 id, td_api::checkPhoneNumberVerificationCode &request); - void on_request(uint64 id, td_api::sendEmailAddressVerificationCode &request); void on_request(uint64 id, const td_api::resendEmailAddressVerificationCode &request); @@ -1755,12 +1749,6 @@ class Td final : public Actor { void on_request(uint64 id, td_api::sendPassportAuthorizationForm &request); - void on_request(uint64 id, td_api::sendPhoneNumberConfirmationCode &request); - - void on_request(uint64 id, const td_api::resendPhoneNumberConfirmationCode &request); - - void on_request(uint64 id, td_api::checkPhoneNumberConfirmationCode &request); - void on_request(uint64 id, const td_api::getSupportUser &request); void on_request(uint64 id, const td_api::getInstalledBackgrounds &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 0f8f2792e..8bc1ecd4c 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2558,12 +2558,6 @@ class CliClient final : public Actor { td_api::make_object(form_id, as_passport_element_types(types))); } else if (op == "gpcl") { send_request(td_api::make_object(args)); - } else if (op == "spnvc" || op == "SendPhoneNumberVerificationCode") { - send_request(td_api::make_object(args, nullptr)); - } else if (op == "cpnvc" || op == "CheckPhoneNumberVerificationCode") { - send_request(td_api::make_object(args)); - } else if (op == "rpnvc" || op == "ResendPhoneNumberVerificationCode") { - send_request(td_api::make_object()); } else if (op == "seavc" || op == "SendEmailAddressVerificationCode") { send_request(td_api::make_object(args)); } else if (op == "ceavc" || op == "CheckEmailAddressVerificationCode") { @@ -2589,15 +2583,6 @@ class CliClient final : public Actor { send_request(td_api::make_object()); } else if (op == "creav") { send_request(td_api::make_object()); - } else if (op == "spncc") { - string hash; - string phone_number; - get_args(args, hash, phone_number); - send_request(td_api::make_object(hash, phone_number, nullptr)); - } else if (op == "cpncc") { - send_request(td_api::make_object(args)); - } else if (op == "rpncc") { - send_request(td_api::make_object()); } else { op_not_found_count++; } @@ -2729,12 +2714,22 @@ class CliClient final : public Actor { PrivacyRules rules; get_args(args, setting, rules); send_request(td_api::make_object(as_user_privacy_setting(setting), rules)); - } else if (op == "cp" || op == "ChangePhone") { - send_request(td_api::make_object(args, nullptr)); - } else if (op == "ccpc" || op == "CheckChangePhoneCode") { - send_request(td_api::make_object(args)); - } else if (op == "rcpc" || op == "ResendChangePhoneCode") { - send_request(td_api::make_object()); + } else if (op == "spncc") { + send_request(td_api::make_object( + args, nullptr, td_api::make_object())); + } else if (op == "spncv") { + send_request(td_api::make_object( + args, nullptr, td_api::make_object())); + } else if (op == "spncco") { + string hash; + string phone_number; + get_args(args, hash, phone_number); + send_request(td_api::make_object( + phone_number, nullptr, td_api::make_object(hash))); + } else if (op == "rpnc") { + send_request(td_api::make_object()); + } else if (op == "cpnc") { + send_request(td_api::make_object(args)); } else if (op == "gco") { if (args.empty()) { send_request(td_api::make_object());