2019-02-16 17:01:47 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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"
|
2019-02-16 17:01:47 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2024-04-13 19:25:05 +02:00
|
|
|
#include "td/telegram/misc.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/telegram_api.h"
|
2024-04-02 02:52:34 +02:00
|
|
|
#include "td/telegram/UserManager.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
class SendCodeQuery final : public Td::ResultHandler {
|
|
|
|
Promise<telegram_api::object_ptr<telegram_api::auth_sentCode>> promise_;
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
public:
|
|
|
|
explicit SendCodeQuery(Promise<telegram_api::object_ptr<telegram_api::auth_sentCode>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void send(const telegram_api::Function &send_code) {
|
|
|
|
send_query(G()->net_query_creator().create(send_code));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_sendChangePhoneCode>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
switch (ptr->get_id()) {
|
|
|
|
case telegram_api::auth_sentCodeSuccess::ID:
|
|
|
|
return on_error(Status::Error(500, "Receive invalid response"));
|
|
|
|
case telegram_api::auth_sentCode::ID:
|
|
|
|
return promise_.set_value(telegram_api::move_object_as<telegram_api::auth_sentCode>(ptr));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
};
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 19:42:41 +02:00
|
|
|
class RequestFirebaseSmsQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit RequestFirebaseSmsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const telegram_api::auth_requestFirebaseSms &query) {
|
|
|
|
send_query(G()->net_query_creator().create(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::auth_requestFirebaseSms>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-22 17:50:16 +02:00
|
|
|
class ReportMissingCodeQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ReportMissingCodeQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const telegram_api::auth_reportMissingCode &query) {
|
|
|
|
send_query(G()->net_query_creator().create(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::auth_reportMissingCode>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
class ChangePhoneQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ChangePhoneQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void send(const string &phone_number, const string &phone_code_hash, const string &code) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_changePhone(phone_number, phone_code_hash, code)));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_changePhone>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
td_->user_manager_->on_get_user(result_ptr.move_as_ok(), "ChangePhoneQuery");
|
|
|
|
promise_.set_value(Unit());
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
class VerifyPhoneQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
public:
|
|
|
|
explicit VerifyPhoneQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void send(const string &phone_number, const string &phone_code_hash, const string &code) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_verifyPhone(phone_number, phone_code_hash, code)));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_verifyPhone>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
promise_.set_value(Unit());
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConfirmPhoneQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
public:
|
|
|
|
explicit ConfirmPhoneQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
2023-09-02 21:39:26 +02:00
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void send(const string &phone_code_hash, const string &code) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_confirmPhone(phone_code_hash, code)));
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_confirmPhone>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
2023-09-02 21:39:26 +02:00
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
2024-04-13 01:05:43 +02:00
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
PhoneNumberManager::PhoneNumberManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void PhoneNumberManager::tear_down() {
|
|
|
|
parent_.reset();
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void PhoneNumberManager::inc_generation() {
|
|
|
|
generation_++;
|
2021-10-20 09:46:01 +02:00
|
|
|
state_ = State::Ok;
|
2024-04-13 18:10:52 +02:00
|
|
|
send_code_helper_ = {};
|
2021-10-20 09:46:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void PhoneNumberManager::send_new_send_code_query(
|
|
|
|
const telegram_api::Function &send_code, Promise<td_api::object_ptr<td_api::authenticationCodeInfo>> &&promise) {
|
|
|
|
auto query_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), generation = generation_, promise = std::move(promise)](
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::auth_sentCode>> r_sent_code) mutable {
|
|
|
|
send_closure(actor_id, &PhoneNumberManager::on_send_code_result, std::move(r_sent_code), generation,
|
|
|
|
std::move(promise));
|
|
|
|
});
|
|
|
|
td_->create_handler<SendCodeQuery>(std::move(query_promise))->send(send_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_send_code_result(Result<telegram_api::object_ptr<telegram_api::auth_sentCode>> r_sent_code,
|
|
|
|
int64 generation,
|
|
|
|
Promise<td_api::object_ptr<td_api::authenticationCodeInfo>> &&promise) {
|
|
|
|
G()->ignore_result_if_closing(r_sent_code);
|
|
|
|
if (r_sent_code.is_error()) {
|
|
|
|
return promise.set_error(r_sent_code.move_as_error());
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
if (generation != generation_) {
|
|
|
|
return promise.set_error(Status::Error(500, "Request was canceled"));
|
|
|
|
}
|
|
|
|
auto sent_code = r_sent_code.move_as_ok();
|
|
|
|
|
|
|
|
LOG(INFO) << "Receive " << to_string(sent_code);
|
|
|
|
|
|
|
|
switch (sent_code->type_->get_id()) {
|
|
|
|
case telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID:
|
|
|
|
case telegram_api::auth_sentCodeTypeEmailCode::ID:
|
|
|
|
return promise.set_error(Status::Error(500, "Receive incorrect response"));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_code_helper_.on_sent_code(std::move(sent_code));
|
|
|
|
state_ = State::WaitCode;
|
|
|
|
|
|
|
|
promise.set_value(send_code_helper_.get_authentication_code_info_object());
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 19:25:05 +02:00
|
|
|
void PhoneNumberManager::set_phone_number(string phone_number,
|
2024-04-13 18:10:52 +02:00
|
|
|
td_api::object_ptr<td_api::phoneNumberAuthenticationSettings> settings,
|
2024-04-13 19:25:05 +02:00
|
|
|
td_api::object_ptr<td_api::PhoneNumberCodeType> type,
|
2024-04-13 18:10:52 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::authenticationCodeInfo>> &&promise) {
|
2024-04-13 19:25:05 +02:00
|
|
|
inc_generation();
|
2024-04-13 18:10:52 +02:00
|
|
|
if (phone_number.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Phone number must be non-empty"));
|
|
|
|
}
|
2024-04-13 19:25:05 +02:00
|
|
|
if (type == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Type must be non-empty"));
|
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
|
2024-04-13 19:25:05 +02:00
|
|
|
switch (type->get_id()) {
|
|
|
|
case td_api::phoneNumberCodeTypeChange::ID:
|
|
|
|
type_ = Type::ChangePhone;
|
2024-04-13 18:10:52 +02:00
|
|
|
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));
|
2024-04-13 19:25:05 +02:00
|
|
|
case td_api::phoneNumberCodeTypeVerify::ID:
|
|
|
|
type_ = Type::VerifyPhone;
|
2024-04-13 18:10:52 +02:00
|
|
|
return send_new_send_code_query(send_code_helper_.send_verify_phone_code(phone_number, settings),
|
|
|
|
std::move(promise));
|
2024-04-13 19:25:05 +02:00
|
|
|
case td_api::phoneNumberCodeTypeConfirmOwnership::ID: {
|
|
|
|
auto hash = std::move(static_cast<td_api::phoneNumberCodeTypeConfirmOwnership *>(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));
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 19:42:41 +02:00
|
|
|
void PhoneNumberManager::send_firebase_sms(const string &token, Promise<Unit> &&promise) {
|
|
|
|
if (state_ != State::WaitCode) {
|
|
|
|
return promise.set_error(Status::Error(400, "Can't send Firebase SMS"));
|
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<RequestFirebaseSmsQuery>(std::move(promise))->send(send_code_helper_.request_firebase_sms(token));
|
|
|
|
}
|
|
|
|
|
2024-04-22 17:50:16 +02:00
|
|
|
void PhoneNumberManager::report_missing_code(const string &mobile_network_code, Promise<Unit> &&promise) {
|
|
|
|
if (state_ != State::WaitCode) {
|
|
|
|
return promise.set_error(Status::Error(400, "Can't report missing code"));
|
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<ReportMissingCodeQuery>(std::move(promise))
|
|
|
|
->send(send_code_helper_.report_missing_code(mobile_network_code));
|
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void PhoneNumberManager::resend_authentication_code(
|
|
|
|
Promise<td_api::object_ptr<td_api::authenticationCodeInfo>> &&promise) {
|
|
|
|
if (state_ != State::WaitCode) {
|
|
|
|
return promise.set_error(Status::Error(400, "Can't resend code"));
|
2022-09-07 20:22:21 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
auto r_resend_code = send_code_helper_.resend_code();
|
|
|
|
if (r_resend_code.is_error()) {
|
|
|
|
return promise.set_error(r_resend_code.move_as_error());
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
send_new_send_code_query(r_resend_code.move_as_ok(), std::move(promise));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void PhoneNumberManager::check_code(string code, Promise<Unit> &&promise) {
|
|
|
|
if (state_ != State::WaitCode) {
|
|
|
|
return promise.set_error(Status::Error(400, "Can't check code"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
2024-04-13 18:10:52 +02:00
|
|
|
|
|
|
|
auto query_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), generation = generation_, promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
send_closure(actor_id, &PhoneNumberManager::on_check_code_result, std::move(result), generation,
|
|
|
|
std::move(promise));
|
|
|
|
});
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
|
|
|
td_->create_handler<ChangePhoneQuery>(std::move(query_promise))
|
|
|
|
->send(send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code);
|
2019-02-16 17:01:47 +01:00
|
|
|
break;
|
2024-04-13 18:10:52 +02:00
|
|
|
case Type::VerifyPhone:
|
|
|
|
td_->create_handler<VerifyPhoneQuery>(std::move(query_promise))
|
|
|
|
->send(send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code);
|
2019-02-16 17:01:47 +01:00
|
|
|
break;
|
2024-04-13 18:10:52 +02:00
|
|
|
case Type::ConfirmPhone:
|
|
|
|
td_->create_handler<ConfirmPhoneQuery>(std::move(query_promise))
|
|
|
|
->send(send_code_helper_.phone_code_hash().str(), code);
|
2019-02-16 17:01:47 +01:00
|
|
|
break;
|
2023-09-02 21:20:05 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 18:10:52 +02:00
|
|
|
void PhoneNumberManager::on_check_code_result(Result<Unit> result, int64 generation, Promise<Unit> &&promise) {
|
|
|
|
G()->ignore_result_if_closing(result);
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
if (generation != generation_) {
|
|
|
|
return promise.set_error(Status::Error(500, "Request was canceled"));
|
|
|
|
}
|
|
|
|
|
|
|
|
inc_generation();
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|