diff --git a/example/cpp/td_example.cpp b/example/cpp/td_example.cpp index dbff69e4..952e7872 100644 --- a/example/cpp/td_example.cpp +++ b/example/cpp/td_example.cpp @@ -263,8 +263,7 @@ class TdExample { std::cerr << "Enter phone number: "; std::string phone_number; std::cin >> phone_number; - send_query(td_api::make_object( - phone_number, false /*allow_flash_calls*/, false /*is_current_phone_number*/), + send_query(td_api::make_object(phone_number, nullptr), create_authentication_query_handler()); }, [this](td_api::authorizationStateWaitEncryptionKey &) { diff --git a/example/csharp/TdExample.cs b/example/csharp/TdExample.cs index f6ea3d19..4c085232 100644 --- a/example/csharp/TdExample.cs +++ b/example/csharp/TdExample.cs @@ -93,7 +93,7 @@ namespace TdExample else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber) { string phoneNumber = ReadLine("Please enter phone number: "); - _client.Send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler()); + _client.Send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, null), new AuthorizationRequestHandler()); } else if (_authorizationState is TdApi.AuthorizationStateWaitCode) { diff --git a/example/java/org/drinkless/tdlib/example/Example.java b/example/java/org/drinkless/tdlib/example/Example.java index 731a3102..106f4b42 100644 --- a/example/java/org/drinkless/tdlib/example/Example.java +++ b/example/java/org/drinkless/tdlib/example/Example.java @@ -108,7 +108,7 @@ public final class Example { break; case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: { String phoneNumber = promptString("Please enter phone number: "); - client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler()); + client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, null), new AuthorizationRequestHandler()); break; } case TdApi.AuthorizationStateWaitCode.CONSTRUCTOR: { diff --git a/example/uwp/app/MainPage.xaml.cs b/example/uwp/app/MainPage.xaml.cs index 21844dc8..1c1aa515 100644 --- a/example/uwp/app/MainPage.xaml.cs +++ b/example/uwp/app/MainPage.xaml.cs @@ -94,7 +94,7 @@ namespace TdApp { var args = command.Split(" ".ToCharArray(), 2); AcceptCommand(command); - _client.Send(new TdApi.SetAuthenticationPhoneNumber(args[1], false, false), _handler); + _client.Send(new TdApi.SetAuthenticationPhoneNumber(args[1], null), _handler); } else if (command.StartsWith("cac")) { diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 0ae4e8c1..b214c89f 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1619,6 +1619,14 @@ callStateError error:error = CallState; call id:int32 user_id:int32 is_outgoing:Bool state:CallState = Call; +//@description Contains settings for the authentication of the user's phone number +//@allow_flash_call Pass true if the authentication code may be sent via flash call to the specified phone number +//@is_current_phone_number Pass true if the authenticated phone number is used on the current device +//@app_specific_sms_token If you want to automatically receive the authentication code from SMS in Android >= 26, pass a single use app specific 11-character token received from SmsManager.createAppSpecificSmsToken. If specified, this token is preferred over app_hash_string +//@app_hash_string If you want to use Android SMS Retriever API (requires Google Play Services >= 11.0), pass an 11-character hash string that identifies your app. See https://developers.google.com/identity/sms-retriever/ for more details +phoneNumberAuthenticationSettings allow_flash_call:Bool is_current_phone_number:Bool app_specific_sms_token:string app_hash_string:string = PhoneNumberAuthenticationSettings; + + //@description Represents a list of animations @animations List of animations animations animations:vector = Animations; @@ -2720,9 +2728,10 @@ setTdlibParameters parameters:tdlibParameters = Ok; //@description Checks the database encryption key for correctness. Works only when the current authorization state is authorizationStateWaitEncryptionKey @encryption_key Encryption key to check or set up checkDatabaseEncryptionKey encryption_key:bytes = Ok; -//@description Sets the phone number of the user and sends an authentication code to the user. Works only when the current authorization state is authorizationStateWaitPhoneNumber -//@phone_number The phone number of the user, in international format @allow_flash_call Pass true if the authentication code may be sent via flash call to the specified phone number @is_current_phone_number Pass true if the phone number is used on the current device. Ignored if allow_flash_call is false -setAuthenticationPhoneNumber phone_number:string allow_flash_call:Bool is_current_phone_number:Bool = Ok; +//@description Sets the phone number of the user and sends an authentication code to the user. Works only when the current authorization state is authorizationStateWaitPhoneNumber, +//-or if there is no pending authentication query and the current authorization state is authorizationStateWaitCode or authorizationStateWaitPassword +//@phone_number The phone number of the user, in international format @settings Settings for the authentication of the user's phone number +setAuthenticationPhoneNumber phone_number:string settings:phoneNumberAuthenticationSettings = Ok; //@description Re-sends an authentication code to the user. Works only when the current authorization state is authorizationStateWaitCode and the next_code_type of the result is not null resendAuthenticationCode = Ok; @@ -2772,7 +2781,7 @@ setPassword old_password:string new_password:string new_hint:string set_recovery //@description Returns a 2-step verification recovery email address that was previously set up. This method can be used to verify a password provided by the user @password The password for the current user getRecoveryEmailAddress password:string = RecoveryEmailAddress; -//@description Changes the 2-step verification recovery email address of the user. If a new recovery email address is specified, then the change will not be applied until the new recovery email address is confirmed +//@description Changes the 2-step verification recovery email address of the user. If a new recovery email address is specified, then the change will not be applied until the new recovery email address is confirmed. //-If new_recovery_email_address is the same as the email address that is currently set up, this call succeeds immediately and aborts all other requests waiting for an email confirmation @password Password of the current user @new_recovery_email_address New recovery email address setRecoveryEmailAddress password:string new_recovery_email_address:string = PasswordState; @@ -3480,8 +3489,8 @@ setBio bio:string = Ok; setUsername username:string = Ok; //@description Changes the phone number of the user and sends an authentication code to the user's new phone number. On success, returns information about the sent code -//@phone_number The new phone number of the user in international format @allow_flash_call Pass true if the code can be sent via flash call to the specified phone number @is_current_phone_number Pass true if the phone number is used on the current device. Ignored if allow_flash_call is false -changePhoneNumber phone_number:string allow_flash_call:Bool is_current_phone_number:Bool = AuthenticationCodeInfo; +//@phone_number The new phone number of the user in international format @settings Settings for the authentication of the user's phone number +changePhoneNumber phone_number:string settings:phoneNumberAuthenticationSettings = AuthenticationCodeInfo; //@description Re-sends the authentication code sent to confirm a new phone number for the user. Works only if the previously received authenticationCodeInfo next_code_type was not null resendChangePhoneNumberCode = AuthenticationCodeInfo; @@ -3712,8 +3721,8 @@ 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 @allow_flash_call Pass true if the authentication code may be sent via flash call to the specified phone number @is_current_phone_number Pass true if the phone number is used on the current device. Ignored if allow_flash_call is false -sendPhoneNumberVerificationCode phone_number:string allow_flash_call:Bool is_current_phone_number:Bool = AuthenticationCodeInfo; +//@phone_number The phone number of the user, in international format @settings Settings for the authentication of the user's phone number +sendPhoneNumberVerificationCode phone_number:string settings:phoneNumberAuthenticationSettings = AuthenticationCodeInfo; //@description Re-sends the code to verify a phone number to be added to a user's Telegram Passport resendPhoneNumberVerificationCode = AuthenticationCodeInfo; @@ -3744,8 +3753,8 @@ sendPassportAuthorizationForm autorization_form_id:int32 types:vector settings) { if (state_ != State::WaitPhoneNumber) { if ((state_ == State::WaitCode || state_ == State::WaitPassword) && net_query_id_ == 0) { // ok @@ -183,13 +183,11 @@ void AuthManager::set_phone_number(uint64 query_id, string phone_number, bool al return on_query_error(query_id, Status::Error(8, "Phone number can't be empty")); } - auto r_send_code = - send_code_helper_.send_code(phone_number, allow_flash_call, is_current_phone_number, api_id_, api_hash_); + auto r_send_code = send_code_helper_.send_code(phone_number, settings, api_id_, api_hash_); if (r_send_code.is_error()) { send_code_helper_ = SendCodeHelper(); terms_of_service_ = TermsOfService(); - r_send_code = - send_code_helper_.send_code(phone_number, allow_flash_call, is_current_phone_number, api_id_, api_hash_); + r_send_code = send_code_helper_.send_code(phone_number, settings, api_id_, api_hash_); if (r_send_code.is_error()) { return on_query_error(query_id, r_send_code.move_as_error()); } diff --git a/td/telegram/AuthManager.h b/td/telegram/AuthManager.h index 76fbe75e..28deff85 100644 --- a/td/telegram/AuthManager.h +++ b/td/telegram/AuthManager.h @@ -32,7 +32,8 @@ class AuthManager : public NetActor { bool is_authorized() const; void get_state(uint64 query_id); - void set_phone_number(uint64 query_id, string phone_number, bool allow_flash_call, bool is_current_phone_number); + void set_phone_number(uint64 query_id, string phone_number, + td_api::object_ptr settings); void resend_authentication_code(uint64 query_id); void check_code(uint64 query_id, string code, string first_name, string last_name); void check_bot_token(uint64 query_id, string bot_token); diff --git a/td/telegram/PhoneNumberManager.cpp b/td/telegram/PhoneNumberManager.cpp index fc8e9483..9908faf8 100644 --- a/td/telegram/PhoneNumberManager.cpp +++ b/td/telegram/PhoneNumberManager.cpp @@ -47,19 +47,16 @@ void PhoneNumberManager::process_send_code_result(uint64 query_id, T r_send_code start_net_query(NetQueryType::SendCode, G()->net_query_creator().create(create_storer(r_send_code.move_as_ok()))); } -void PhoneNumberManager::set_phone_number(uint64 query_id, string phone_number, bool allow_flash_call, - bool is_current_phone_number) { +void PhoneNumberManager::set_phone_number(uint64 query_id, string phone_number, Settings settings) { if (phone_number.empty()) { return on_query_error(query_id, Status::Error(8, "Phone number can't be empty")); } switch (type_) { case Type::ChangePhone: - return process_send_code_result( - query_id, send_code_helper_.send_change_phone_code(phone_number, allow_flash_call, is_current_phone_number)); + return process_send_code_result(query_id, send_code_helper_.send_change_phone_code(phone_number, settings)); case Type::VerifyPhone: - return process_send_code_result( - query_id, send_code_helper_.send_confirm_phone_code(phone_number, allow_flash_call, is_current_phone_number)); + return process_send_code_result(query_id, send_code_helper_.send_confirm_phone_code(phone_number, settings)); case Type::ConfirmPhone: default: UNREACHABLE(); @@ -67,7 +64,7 @@ void PhoneNumberManager::set_phone_number(uint64 query_id, string phone_number, } void PhoneNumberManager::set_phone_number_and_hash(uint64 query_id, string hash, string phone_number, - bool allow_flash_call, bool is_current_phone_number) { + Settings settings) { if (phone_number.empty()) { return on_query_error(query_id, Status::Error(8, "Phone number can't be empty")); } @@ -77,8 +74,7 @@ void PhoneNumberManager::set_phone_number_and_hash(uint64 query_id, string hash, switch (type_) { case Type::ConfirmPhone: - return process_send_code_result(query_id, send_code_helper_.send_verify_phone_code( - hash, phone_number, allow_flash_call, is_current_phone_number)); + return process_send_code_result(query_id, send_code_helper_.send_verify_phone_code(hash, phone_number, settings)); case Type::ChangePhone: case Type::VerifyPhone: default: diff --git a/td/telegram/PhoneNumberManager.h b/td/telegram/PhoneNumberManager.h index 7e74508a..7f607c79 100644 --- a/td/telegram/PhoneNumberManager.h +++ b/td/telegram/PhoneNumberManager.h @@ -23,9 +23,10 @@ class PhoneNumberManager : public NetActor { PhoneNumberManager(Type type, ActorShared<> parent); void get_state(uint64 query_id); - void set_phone_number(uint64 query_id, string phone_number, bool allow_flash_call, bool is_current_phone_number); - void set_phone_number_and_hash(uint64 query_id, string hash, string phone_number, bool allow_flash_call, - bool is_current_phone_number); + using Settings = td_api::object_ptr; + + void set_phone_number(uint64 query_id, string phone_number, Settings settings); + void set_phone_number_and_hash(uint64 query_id, string hash, string phone_number, Settings settings); void resend_authentication_code(uint64 query_id); void check_code(uint64 query_id, string code); diff --git a/td/telegram/SendCodeHelper.cpp b/td/telegram/SendCodeHelper.cpp index b22108e5..5728e3d7 100644 --- a/td/telegram/SendCodeHelper.cpp +++ b/td/telegram/SendCodeHelper.cpp @@ -6,6 +6,8 @@ // #include "td/telegram/SendCodeHelper.h" +#include "td/utils/utf8.h" + namespace td { void SendCodeHelper::on_sent_code(telegram_api::object_ptr sent_code) { @@ -39,52 +41,56 @@ Result SendCodeHelper::resend_code() { return telegram_api::auth_resendCode(phone_number_, phone_code_hash_); } -telegram_api::object_ptr SendCodeHelper::get_input_code_settings( - bool allow_flash_call, bool is_current_phone_number) { +telegram_api::object_ptr SendCodeHelper::get_input_code_settings(const Settings &settings) { int32 flags = 0; - if (allow_flash_call) { - flags |= telegram_api::codeSettings::ALLOW_FLASHCALL_MASK; - } - if (is_current_phone_number) { - flags |= telegram_api::codeSettings::CURRENT_NUMBER_MASK; + string app_hash; + if (settings != nullptr) { + if (settings->allow_flash_call_) { + flags |= telegram_api::codeSettings::ALLOW_FLASHCALL_MASK; + } + if (settings->is_current_phone_number_) { + flags |= telegram_api::codeSettings::CURRENT_NUMBER_MASK; + } + if (check_utf8(settings->app_specific_sms_token_) && !settings->app_specific_sms_token_.empty()) { + app_hash = settings->app_specific_sms_token_; + } else if (check_utf8(settings->app_hash_string_) && !settings->app_hash_string_.empty()) { + flags |= telegram_api::codeSettings::APP_HASH_PERSISTENT_MASK; + app_hash = settings->app_hash_string_; + } + if (!app_hash.empty()) { + flags |= telegram_api::codeSettings::APP_HASH_MASK; + } } return telegram_api::make_object(flags, false /*ignored*/, false /*ignored*/, - false /*ignored*/, string()); + false /*ignored*/, app_hash); } -Result SendCodeHelper::send_code(Slice phone_number, bool allow_flash_call, - bool is_current_phone_number, int32 api_id, - const string &api_hash) { +Result SendCodeHelper::send_code(Slice phone_number, const Settings &settings, + int32 api_id, const string &api_hash) { if (!phone_number_.empty()) { return Status::Error(8, "Can't change phone"); } phone_number_ = phone_number.str(); - return telegram_api::auth_sendCode(phone_number_, api_id, api_hash, - get_input_code_settings(allow_flash_call, is_current_phone_number)); + return telegram_api::auth_sendCode(phone_number_, api_id, api_hash, get_input_code_settings(settings)); } Result SendCodeHelper::send_change_phone_code(Slice phone_number, - bool allow_flash_call, - bool is_current_phone_number) { + const Settings &settings) { phone_number_ = phone_number.str(); - return telegram_api::account_sendChangePhoneCode(phone_number_, - get_input_code_settings(allow_flash_call, is_current_phone_number)); + return telegram_api::account_sendChangePhoneCode(phone_number_, get_input_code_settings(settings)); } Result SendCodeHelper::send_verify_phone_code(const string &hash, Slice phone_number, - bool allow_flash_call, - bool is_current_phone_number) { + const Settings &settings) { phone_number_ = phone_number.str(); - return telegram_api::account_sendVerifyPhoneCode(hash, - get_input_code_settings(allow_flash_call, is_current_phone_number)); + return telegram_api::account_sendVerifyPhoneCode(hash, get_input_code_settings(settings)); } -Result SendCodeHelper::send_confirm_phone_code( - Slice phone_number, bool allow_flash_call, bool is_current_phone_number) { +Result SendCodeHelper::send_confirm_phone_code(Slice phone_number, + const Settings &settings) { phone_number_ = phone_number.str(); - return telegram_api::account_sendConfirmPhoneCode(phone_number_, - get_input_code_settings(allow_flash_call, is_current_phone_number)); + return telegram_api::account_sendConfirmPhoneCode(phone_number_, get_input_code_settings(settings)); } SendCodeHelper::AuthenticationCodeInfo SendCodeHelper::get_authentication_code_info( diff --git a/td/telegram/SendCodeHelper.h b/td/telegram/SendCodeHelper.h index 4c32e6ec..90041ed7 100644 --- a/td/telegram/SendCodeHelper.h +++ b/td/telegram/SendCodeHelper.h @@ -25,18 +25,19 @@ class SendCodeHelper { td_api::object_ptr get_authentication_code_info_object() const; Result resend_code(); - Result send_code(Slice phone_number, bool allow_flash_call, bool is_current_phone_number, - int32 api_id, const string &api_hash); + using Settings = td_api::object_ptr; - Result send_change_phone_code(Slice phone_number, bool allow_flash_call, - bool is_current_phone_number); + Result send_code(Slice phone_number, const Settings &settings, int32 api_id, + const string &api_hash); + + Result send_change_phone_code(Slice phone_number, + const Settings &settings); Result send_verify_phone_code(const string &hash, Slice phone_number, - bool allow_flash_call, - bool is_current_phone_number); + const Settings &settings); - Result send_confirm_phone_code(Slice phone_number, bool allow_flash_call, - bool is_current_phone_number); + Result send_confirm_phone_code(Slice phone_number, + const Settings &settings); Slice phone_number() const { return phone_number_; @@ -91,8 +92,7 @@ class SendCodeHelper { static td_api::object_ptr get_authentication_code_type_object( const AuthenticationCodeInfo &authentication_code_info); - static telegram_api::object_ptr get_input_code_settings(bool allow_flash_call, - bool is_current_phone_number); + static telegram_api::object_ptr get_input_code_settings(const Settings &settings); }; } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 4b91fa99..8e756268 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4614,7 +4614,7 @@ void Td::on_request(uint64 id, const td_api::getAuthorizationState &request) { void Td::on_request(uint64 id, td_api::setAuthenticationPhoneNumber &request) { CLEAN_INPUT_STRING(request.phone_number_); send_closure(auth_manager_actor_, &AuthManager::set_phone_number, id, std::move(request.phone_number_), - request.allow_flash_call_, request.is_current_phone_number_); + std::move(request.settings_)); } void Td::on_request(uint64 id, const td_api::resendAuthenticationCode &request) { @@ -4842,7 +4842,7 @@ void Td::on_request(uint64 id, td_api::changePhoneNumber &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.phone_number_); send_closure(change_phone_number_manager_, &PhoneNumberManager::set_phone_number, id, - std::move(request.phone_number_), request.allow_flash_call_, request.is_current_phone_number_); + std::move(request.phone_number_), std::move(request.settings_)); } void Td::on_request(uint64 id, td_api::checkChangePhoneNumberCode &request) { @@ -6845,7 +6845,7 @@ void Td::on_request(uint64 id, td_api::sendPhoneNumberVerificationCode &request) CHECK_IS_USER(); CLEAN_INPUT_STRING(request.phone_number_); send_closure(verify_phone_number_manager_, &PhoneNumberManager::set_phone_number, id, - std::move(request.phone_number_), request.allow_flash_call_, request.is_current_phone_number_); + std::move(request.phone_number_), std::move(request.settings_)); } void Td::on_request(uint64 id, const td_api::resendPhoneNumberVerificationCode &request) { @@ -6924,8 +6924,7 @@ void Td::on_request(uint64 id, td_api::sendPhoneNumberConfirmationCode &request) CLEAN_INPUT_STRING(request.phone_number_); CLEAN_INPUT_STRING(request.hash_); send_closure(confirm_phone_number_manager_, &PhoneNumberManager::set_phone_number_and_hash, id, - std::move(request.hash_), std::move(request.phone_number_), request.allow_flash_call_, - request.is_current_phone_number_); + std::move(request.hash_), std::move(request.phone_number_), std::move(request.settings_)); } void Td::on_request(uint64 id, const td_api::resendPhoneNumberConfirmationCode &request) { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 9a986ea4..24717a26 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -1243,7 +1243,7 @@ class CliClient final : public Actor { if (op == "gas") { send_request(td_api::make_object()); } else if (op == "sap") { - send_request(td_api::make_object(args, false, false)); + send_request(td_api::make_object(args, nullptr)); } else if (op == "rac") { send_request(td_api::make_object()); } else if (op == "cdek" || op == "CheckDatabaseEncryptionKey") { @@ -1365,7 +1365,7 @@ class CliClient final : public Actor { } else if (op == "gpcl") { send_request(td_api::make_object(args)); } else if (op == "spnvc" || op == "SendPhoneNumberVerificationCode") { - send_request(td_api::make_object(args, false, false)); + 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") { @@ -1388,7 +1388,7 @@ class CliClient final : public Actor { } else if (op == "rreac") { send_request(td_api::make_object()); } else if (op == "spncc") { - send_request(td_api::make_object(args, false, false)); + send_request(td_api::make_object(args, nullptr)); } else if (op == "cpncc") { send_request(td_api::make_object(args)); } else if (op == "rpncc") { @@ -1530,7 +1530,7 @@ class CliClient final : public Actor { send_request(td_api::make_object( get_user_privacy_setting(setting), td_api::make_object(std::move(rules)))); } else if (op == "cp" || op == "ChangePhone") { - send_request(td_api::make_object(args, false, false)); + 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") { diff --git a/test/tdclient.cpp b/test/tdclient.cpp index 922bbe5a..e5d222a1 100644 --- a/test/tdclient.cpp +++ b/test/tdclient.cpp @@ -212,7 +212,7 @@ class DoAuthentication : public Task { function = make_tl_object(); break; case td_api::authorizationStateWaitPhoneNumber::ID: - function = make_tl_object(phone_, false, true); + function = make_tl_object(phone_, nullptr); break; case td_api::authorizationStateWaitCode::ID: function = make_tl_object(code_, name_, "");