2019-02-16 17:01:47 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2019-02-16 17:01:47 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/PhoneNumberManager.h"
|
|
|
|
|
2021-06-11 01:13:46 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2021-10-20 09:46:01 +02:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2021-06-11 01:13:46 +02:00
|
|
|
#include "td/telegram/SuggestedAction.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2019-02-17 14:52:34 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
void PhoneNumberManager::get_state(uint64 query_id) {
|
|
|
|
tl_object_ptr<td_api::Object> obj;
|
|
|
|
switch (state_) {
|
|
|
|
case State::Ok:
|
|
|
|
obj = make_tl_object<td_api::ok>();
|
|
|
|
break;
|
|
|
|
case State::WaitCode:
|
|
|
|
obj = send_code_helper_.get_authentication_code_info_object();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CHECK(obj);
|
|
|
|
send_closure(G()->td(), &Td::send_result, query_id, std::move(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhoneNumberManager::PhoneNumberManager(PhoneNumberManager::Type type, ActorShared<> parent)
|
|
|
|
: type_(type), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
2021-10-20 09:46:01 +02:00
|
|
|
void PhoneNumberManager::send_new_send_code_query(uint64 query_id, const telegram_api::Function &send_code) {
|
2019-02-16 17:01:47 +01:00
|
|
|
on_new_query(query_id);
|
2020-03-15 22:17:11 +01:00
|
|
|
start_net_query(NetQueryType::SendCode, G()->net_query_creator().create(send_code));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 04:44:59 +02:00
|
|
|
void PhoneNumberManager::set_phone_number(uint64 query_id, string phone_number, Settings settings) {
|
2019-02-16 17:01:47 +01:00
|
|
|
if (phone_number.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Phone number must be non-empty"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
2021-06-11 01:13:46 +02:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::hide_suggested_action,
|
|
|
|
SuggestedAction{SuggestedAction::Type::CheckPhoneNumber});
|
2021-10-20 09:46:01 +02:00
|
|
|
return send_new_send_code_query(query_id, send_code_helper_.send_change_phone_code(phone_number, settings));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::VerifyPhone:
|
2021-10-20 09:46:01 +02:00
|
|
|
return send_new_send_code_query(query_id, send_code_helper_.send_verify_phone_code(phone_number, settings));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::ConfirmPhone:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::set_phone_number_and_hash(uint64 query_id, string hash, string phone_number,
|
2019-05-03 04:44:59 +02:00
|
|
|
Settings settings) {
|
2019-02-16 17:01:47 +01:00
|
|
|
if (phone_number.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Phone number must be non-empty"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
if (hash.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Hash must be non-empty"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ConfirmPhone:
|
2021-10-20 09:46:01 +02:00
|
|
|
return send_new_send_code_query(query_id,
|
2019-09-02 17:11:47 +02:00
|
|
|
send_code_helper_.send_confirm_phone_code(hash, phone_number, settings));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::ChangePhone:
|
|
|
|
case Type::VerifyPhone:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::resend_authentication_code(uint64 query_id) {
|
|
|
|
if (state_ != State::WaitCode) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "resendAuthenticationCode unexpected"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto r_resend_code = send_code_helper_.resend_code();
|
|
|
|
if (r_resend_code.is_error()) {
|
|
|
|
return on_query_error(query_id, r_resend_code.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
2020-03-15 23:01:14 +01:00
|
|
|
start_net_query(NetQueryType::SendCode, G()->net_query_creator().create_unauth(r_resend_code.move_as_ok()));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 14:38:39 +02:00
|
|
|
void PhoneNumberManager::send_new_check_code_query(const telegram_api::Function &check_code) {
|
|
|
|
start_net_query(NetQueryType::CheckCode, G()->net_query_creator().create(check_code));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::check_code(uint64 query_id, string code) {
|
|
|
|
if (state_ != State::WaitCode) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "checkAuthenticationCode unexpected"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
|
|
|
return send_new_check_code_query(telegram_api::account_changePhone(
|
|
|
|
send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code));
|
|
|
|
case Type::ConfirmPhone:
|
|
|
|
return send_new_check_code_query(
|
|
|
|
telegram_api::account_confirmPhone(send_code_helper_.phone_code_hash().str(), code));
|
|
|
|
case Type::VerifyPhone:
|
|
|
|
return send_new_check_code_query(telegram_api::account_verifyPhone(
|
|
|
|
send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_new_query(uint64 query_id) {
|
|
|
|
if (query_id_ != 0) {
|
2021-09-24 09:59:51 +02:00
|
|
|
on_query_error(Status::Error(400, "Another authorization query has started"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
query_id_ = query_id;
|
|
|
|
// TODO: cancel older net_query
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_query_error(Status status) {
|
|
|
|
CHECK(query_id_ != 0);
|
|
|
|
auto id = query_id_;
|
|
|
|
query_id_ = 0;
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
on_query_error(id, std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_query_error(uint64 id, Status status) {
|
|
|
|
send_closure(G()->td(), &Td::send_error, id, std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_query_ok() {
|
|
|
|
CHECK(query_id_ != 0);
|
|
|
|
auto id = query_id_;
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
query_id_ = 0;
|
|
|
|
get_state(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::start_net_query(NetQueryType net_query_type, NetQueryPtr net_query) {
|
|
|
|
// TODO: cancel old net_query?
|
|
|
|
net_query_type_ = net_query_type;
|
|
|
|
net_query_id_ = net_query->id();
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this));
|
|
|
|
}
|
|
|
|
|
2021-10-20 09:46:01 +02:00
|
|
|
void PhoneNumberManager::process_check_code_result(Result<tl_object_ptr<telegram_api::User>> &&result) {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return on_query_error(result.move_as_error());
|
|
|
|
}
|
2023-06-23 17:22:58 +02:00
|
|
|
send_closure(G()->contacts_manager(), &ContactsManager::on_get_user, result.move_as_ok(),
|
|
|
|
"process_check_code_result");
|
2021-10-20 09:46:01 +02:00
|
|
|
state_ = State::Ok;
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::process_check_code_result(Result<bool> &&result) {
|
2019-02-16 17:01:47 +01:00
|
|
|
if (result.is_error()) {
|
|
|
|
return on_query_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
state_ = State::Ok;
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_check_code_result(NetQueryPtr &result) {
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
|
|
|
return process_check_code_result(fetch_result<telegram_api::account_changePhone>(result->ok()));
|
|
|
|
case Type::VerifyPhone:
|
|
|
|
return process_check_code_result(fetch_result<telegram_api::account_verifyPhone>(result->ok()));
|
|
|
|
case Type::ConfirmPhone:
|
|
|
|
return process_check_code_result(fetch_result<telegram_api::account_confirmPhone>(result->ok()));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_send_code_result(NetQueryPtr &result) {
|
|
|
|
auto r_sent_code = [&] {
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
|
|
|
return fetch_result<telegram_api::account_sendChangePhoneCode>(result->ok());
|
|
|
|
case Type::VerifyPhone:
|
|
|
|
return fetch_result<telegram_api::account_sendVerifyPhoneCode>(result->ok());
|
|
|
|
case Type::ConfirmPhone:
|
|
|
|
return fetch_result<telegram_api::account_sendConfirmPhoneCode>(result->ok());
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return fetch_result<telegram_api::account_sendChangePhoneCode>(result->ok());
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (r_sent_code.is_error()) {
|
|
|
|
return on_query_error(r_sent_code.move_as_error());
|
|
|
|
}
|
2023-01-06 10:42:16 +01:00
|
|
|
auto sent_code_ptr = r_sent_code.move_as_ok();
|
|
|
|
auto sent_code_id = sent_code_ptr->get_id();
|
|
|
|
if (sent_code_id != telegram_api::auth_sentCode::ID) {
|
|
|
|
CHECK(sent_code_id == telegram_api::auth_sentCodeSuccess::ID);
|
|
|
|
return on_query_error(Status::Error(500, "Receive invalid response"));
|
|
|
|
}
|
|
|
|
auto sent_code = telegram_api::move_object_as<telegram_api::auth_sentCode>(sent_code_ptr);
|
2019-02-16 17:01:47 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Receive " << to_string(sent_code);
|
|
|
|
|
2022-09-07 20:22:21 +02:00
|
|
|
switch (sent_code->type_->get_id()) {
|
|
|
|
case telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID:
|
|
|
|
case telegram_api::auth_sentCodeTypeEmailCode::ID:
|
|
|
|
return on_query_error(Status::Error(500, "Receive incorrect response"));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:01:47 +01:00
|
|
|
send_code_helper_.on_sent_code(std::move(sent_code));
|
|
|
|
|
|
|
|
state_ = State::WaitCode;
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_result(NetQueryPtr result) {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
result->clear();
|
|
|
|
};
|
|
|
|
NetQueryType type = NetQueryType::None;
|
|
|
|
if (result->id() == net_query_id_) {
|
|
|
|
net_query_id_ = 0;
|
|
|
|
type = net_query_type_;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
if (result->is_error()) {
|
|
|
|
if (query_id_ != 0) {
|
|
|
|
on_query_error(std::move(result->error()));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case NetQueryType::None:
|
|
|
|
result->ignore();
|
|
|
|
break;
|
|
|
|
case NetQueryType::SendCode:
|
|
|
|
on_send_code_result(result);
|
|
|
|
break;
|
|
|
|
case NetQueryType::CheckCode:
|
|
|
|
on_check_code_result(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|