2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +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/AuthManager.h"
|
|
|
|
|
2022-03-25 14:00:06 +01:00
|
|
|
#include "td/telegram/AttachMenuManager.h"
|
2018-11-26 18:05:06 +01:00
|
|
|
#include "td/telegram/AuthManager.hpp"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
2023-03-27 16:44:13 +02:00
|
|
|
#include "td/telegram/DialogFilterManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2018-08-04 08:55:49 +02:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2023-07-01 17:40:19 +02:00
|
|
|
#include "td/telegram/logevent/LogEventHelper.h"
|
2020-06-03 18:21:59 +02:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/misc.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2021-06-28 20:36:54 +02:00
|
|
|
#include "td/telegram/NewPasswordState.h"
|
2019-03-05 04:01:49 +01:00
|
|
|
#include "td/telegram/NotificationManager.h"
|
2022-08-18 16:00:18 +02:00
|
|
|
#include "td/telegram/OptionManager.h"
|
2018-08-04 08:55:49 +02:00
|
|
|
#include "td/telegram/PasswordManager.h"
|
2023-08-09 11:59:48 +02:00
|
|
|
#include "td/telegram/ReactionManager.h"
|
2023-07-01 17:40:19 +02:00
|
|
|
#include "td/telegram/SendCodeHelper.hpp"
|
2020-08-05 19:36:13 +02:00
|
|
|
#include "td/telegram/StateManager.h"
|
2020-04-17 15:12:12 +02:00
|
|
|
#include "td/telegram/StickersManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2023-07-01 13:53:04 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2021-09-13 14:55:01 +02:00
|
|
|
#include "td/telegram/ThemeManager.h"
|
2019-03-05 05:35:17 +01:00
|
|
|
#include "td/telegram/TopDialogManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
2023-07-01 17:40:19 +02:00
|
|
|
#include "td/telegram/Version.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-17 17:17:57 +01:00
|
|
|
#include "td/utils/base64.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2019-12-23 18:48:30 +01:00
|
|
|
#include "td/utils/misc.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2019-02-17 14:52:34 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2023-07-01 17:40:19 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
2023-07-01 17:40:19 +02:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2023-07-01 17:40:19 +02:00
|
|
|
struct AuthManager::DbState {
|
|
|
|
State state_;
|
|
|
|
int32 api_id_;
|
|
|
|
string api_hash_;
|
|
|
|
double expires_at_;
|
|
|
|
|
|
|
|
// WaitEmailAddress and WaitEmailCode
|
|
|
|
bool allow_apple_id_ = false;
|
|
|
|
bool allow_google_id_ = false;
|
|
|
|
|
|
|
|
// WaitEmailCode
|
|
|
|
string email_address_;
|
|
|
|
SentEmailCode email_code_info_;
|
|
|
|
int32 reset_available_period_ = -1;
|
|
|
|
int32 reset_pending_date_ = -1;
|
|
|
|
|
|
|
|
// WaitEmailAddress, WaitEmailCode, WaitCode and WaitRegistration
|
|
|
|
SendCodeHelper send_code_helper_;
|
|
|
|
|
|
|
|
// WaitQrCodeConfirmation
|
|
|
|
vector<UserId> other_user_ids_;
|
|
|
|
string login_token_;
|
|
|
|
double login_token_expires_at_ = 0;
|
|
|
|
|
|
|
|
// WaitPassword
|
|
|
|
WaitPasswordState wait_password_state_;
|
|
|
|
|
|
|
|
// WaitRegistration
|
|
|
|
TermsOfService terms_of_service_;
|
|
|
|
|
|
|
|
DbState() = default;
|
|
|
|
|
|
|
|
static DbState wait_email_address(int32 api_id, string api_hash, bool allow_apple_id, bool allow_google_id,
|
|
|
|
SendCodeHelper send_code_helper) {
|
|
|
|
DbState state(State::WaitEmailAddress, api_id, std::move(api_hash));
|
|
|
|
state.send_code_helper_ = std::move(send_code_helper);
|
|
|
|
state.allow_apple_id_ = allow_apple_id;
|
|
|
|
state.allow_google_id_ = allow_google_id;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DbState wait_email_code(int32 api_id, string api_hash, bool allow_apple_id, bool allow_google_id,
|
|
|
|
string email_address, SentEmailCode email_code_info, int32 reset_available_period,
|
|
|
|
int32 reset_pending_date, SendCodeHelper send_code_helper) {
|
|
|
|
DbState state(State::WaitEmailCode, api_id, std::move(api_hash));
|
|
|
|
state.send_code_helper_ = std::move(send_code_helper);
|
|
|
|
state.allow_apple_id_ = allow_apple_id;
|
|
|
|
state.allow_google_id_ = allow_google_id;
|
|
|
|
state.email_address_ = std::move(email_address);
|
|
|
|
state.email_code_info_ = std::move(email_code_info);
|
|
|
|
state.reset_available_period_ = reset_available_period;
|
|
|
|
state.reset_pending_date_ = reset_pending_date;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DbState wait_code(int32 api_id, string api_hash, SendCodeHelper send_code_helper) {
|
|
|
|
DbState state(State::WaitCode, api_id, std::move(api_hash));
|
|
|
|
state.send_code_helper_ = std::move(send_code_helper);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DbState wait_qr_code_confirmation(int32 api_id, string api_hash, vector<UserId> other_user_ids,
|
|
|
|
string login_token, double login_token_expires_at) {
|
|
|
|
DbState state(State::WaitQrCodeConfirmation, api_id, std::move(api_hash));
|
|
|
|
state.other_user_ids_ = std::move(other_user_ids);
|
|
|
|
state.login_token_ = std::move(login_token);
|
|
|
|
state.login_token_expires_at_ = login_token_expires_at;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DbState wait_password(int32 api_id, string api_hash, WaitPasswordState wait_password_state) {
|
|
|
|
DbState state(State::WaitPassword, api_id, std::move(api_hash));
|
|
|
|
state.wait_password_state_ = std::move(wait_password_state);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DbState wait_registration(int32 api_id, string api_hash, SendCodeHelper send_code_helper,
|
|
|
|
TermsOfService terms_of_service) {
|
|
|
|
DbState state(State::WaitRegistration, api_id, std::move(api_hash));
|
|
|
|
state.send_code_helper_ = std::move(send_code_helper);
|
|
|
|
state.terms_of_service_ = std::move(terms_of_service);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
|
|
|
|
|
|
|
private:
|
|
|
|
DbState(State state, int32 api_id, string &&api_hash)
|
|
|
|
: state_(state), api_id_(api_id), api_hash_(std::move(api_hash)) {
|
|
|
|
auto state_timeout = [state] {
|
|
|
|
switch (state) {
|
|
|
|
case State::WaitPassword:
|
|
|
|
case State::WaitRegistration:
|
|
|
|
return 86400;
|
|
|
|
case State::WaitEmailAddress:
|
|
|
|
case State::WaitEmailCode:
|
|
|
|
case State::WaitCode:
|
|
|
|
case State::WaitQrCodeConfirmation:
|
|
|
|
return 5 * 60;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
expires_at_ = Time::now() + state_timeout;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void AuthManager::DbState::store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
bool has_terms_of_service = !terms_of_service_.get_id().empty();
|
|
|
|
bool is_pbkdf2_supported = true;
|
|
|
|
bool is_srp_supported = true;
|
|
|
|
bool is_wait_registration_supported = true;
|
|
|
|
bool is_wait_registration_stores_phone_number = true;
|
|
|
|
bool is_wait_qr_code_confirmation_supported = true;
|
|
|
|
bool is_time_store_supported = true;
|
|
|
|
bool is_reset_email_address_supported = true;
|
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(has_terms_of_service);
|
|
|
|
STORE_FLAG(is_pbkdf2_supported);
|
|
|
|
STORE_FLAG(is_srp_supported);
|
|
|
|
STORE_FLAG(is_wait_registration_supported);
|
|
|
|
STORE_FLAG(is_wait_registration_stores_phone_number);
|
|
|
|
STORE_FLAG(is_wait_qr_code_confirmation_supported);
|
|
|
|
STORE_FLAG(allow_apple_id_);
|
|
|
|
STORE_FLAG(allow_google_id_);
|
|
|
|
STORE_FLAG(is_time_store_supported);
|
|
|
|
STORE_FLAG(is_reset_email_address_supported);
|
|
|
|
END_STORE_FLAGS();
|
|
|
|
store(state_, storer);
|
|
|
|
store(api_id_, storer);
|
|
|
|
store(api_hash_, storer);
|
|
|
|
store_time(expires_at_, storer);
|
|
|
|
|
|
|
|
if (has_terms_of_service) {
|
|
|
|
store(terms_of_service_, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state_ == State::WaitEmailAddress) {
|
|
|
|
store(send_code_helper_, storer);
|
|
|
|
} else if (state_ == State::WaitEmailCode) {
|
|
|
|
store(send_code_helper_, storer);
|
|
|
|
store(email_address_, storer);
|
|
|
|
store(email_code_info_, storer);
|
|
|
|
store(reset_available_period_, storer);
|
|
|
|
store(reset_pending_date_, storer);
|
|
|
|
} else if (state_ == State::WaitCode) {
|
|
|
|
store(send_code_helper_, storer);
|
|
|
|
} else if (state_ == State::WaitQrCodeConfirmation) {
|
|
|
|
store(other_user_ids_, storer);
|
|
|
|
store(login_token_, storer);
|
|
|
|
store_time(login_token_expires_at_, storer);
|
|
|
|
} else if (state_ == State::WaitPassword) {
|
|
|
|
store(wait_password_state_, storer);
|
|
|
|
} else if (state_ == State::WaitRegistration) {
|
|
|
|
store(send_code_helper_, storer);
|
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void AuthManager::DbState::parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
|
|
|
bool has_terms_of_service = false;
|
|
|
|
bool is_pbkdf2_supported = false;
|
|
|
|
bool is_srp_supported = false;
|
|
|
|
bool is_wait_registration_supported = false;
|
|
|
|
bool is_wait_registration_stores_phone_number = false;
|
|
|
|
bool is_wait_qr_code_confirmation_supported = false;
|
|
|
|
bool is_time_store_supported = false;
|
|
|
|
bool is_reset_email_address_supported = false;
|
|
|
|
if (parser.version() >= static_cast<int32>(Version::AddTermsOfService)) {
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(has_terms_of_service);
|
|
|
|
PARSE_FLAG(is_pbkdf2_supported);
|
|
|
|
PARSE_FLAG(is_srp_supported);
|
|
|
|
PARSE_FLAG(is_wait_registration_supported);
|
|
|
|
PARSE_FLAG(is_wait_registration_stores_phone_number);
|
|
|
|
PARSE_FLAG(is_wait_qr_code_confirmation_supported);
|
|
|
|
PARSE_FLAG(allow_apple_id_);
|
|
|
|
PARSE_FLAG(allow_google_id_);
|
|
|
|
PARSE_FLAG(is_time_store_supported);
|
|
|
|
PARSE_FLAG(is_reset_email_address_supported);
|
|
|
|
END_PARSE_FLAGS();
|
|
|
|
}
|
|
|
|
if (!is_reset_email_address_supported) {
|
|
|
|
return parser.set_error("Have no reset email address support");
|
|
|
|
}
|
|
|
|
CHECK(is_pbkdf2_supported);
|
|
|
|
CHECK(is_srp_supported);
|
|
|
|
CHECK(is_wait_registration_supported);
|
|
|
|
CHECK(is_wait_registration_stores_phone_number);
|
|
|
|
CHECK(is_wait_qr_code_confirmation_supported);
|
|
|
|
CHECK(is_time_store_supported);
|
|
|
|
|
|
|
|
parse(state_, parser);
|
|
|
|
parse(api_id_, parser);
|
|
|
|
parse(api_hash_, parser);
|
|
|
|
parse_time(expires_at_, parser);
|
|
|
|
|
|
|
|
if (has_terms_of_service) {
|
|
|
|
parse(terms_of_service_, parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state_ == State::WaitEmailAddress) {
|
|
|
|
parse(send_code_helper_, parser);
|
|
|
|
} else if (state_ == State::WaitEmailCode) {
|
|
|
|
parse(send_code_helper_, parser);
|
|
|
|
parse(email_address_, parser);
|
|
|
|
parse(email_code_info_, parser);
|
|
|
|
parse(reset_available_period_, parser);
|
|
|
|
parse(reset_pending_date_, parser);
|
|
|
|
} else if (state_ == State::WaitCode) {
|
|
|
|
parse(send_code_helper_, parser);
|
|
|
|
} else if (state_ == State::WaitQrCodeConfirmation) {
|
|
|
|
parse(other_user_ids_, parser);
|
|
|
|
parse(login_token_, parser);
|
|
|
|
parse_time(login_token_expires_at_, parser);
|
|
|
|
} else if (state_ == State::WaitPassword) {
|
|
|
|
parse(wait_password_state_, parser);
|
|
|
|
} else if (state_ == State::WaitRegistration) {
|
|
|
|
parse(send_code_helper_, parser);
|
|
|
|
} else {
|
|
|
|
parser.set_error(PSTRING() << "Unexpected " << tag("state", static_cast<int32>(state_)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
AuthManager::AuthManager(int32 api_id, const string &api_hash, ActorShared<> parent)
|
|
|
|
: parent_(std::move(parent)), api_id_(api_id), api_hash_(api_hash) {
|
|
|
|
string auth_str = G()->td_db()->get_binlog_pmc()->get("auth");
|
|
|
|
if (auth_str == "ok") {
|
|
|
|
string is_bot_str = G()->td_db()->get_binlog_pmc()->get("auth_is_bot");
|
|
|
|
if (is_bot_str == "true") {
|
|
|
|
is_bot_ = true;
|
|
|
|
}
|
2018-03-08 22:42:25 +01:00
|
|
|
auto my_id = ContactsManager::load_my_id();
|
|
|
|
if (my_id.is_valid()) {
|
|
|
|
// just in case
|
2021-07-26 20:06:59 +02:00
|
|
|
LOG(INFO) << "Logged in as " << my_id;
|
2022-08-18 16:00:18 +02:00
|
|
|
td_->option_manager_->set_option_integer("my_id", my_id.get());
|
2018-03-09 02:17:38 +01:00
|
|
|
update_state(State::Ok);
|
2018-03-09 01:52:12 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Restore unknown my_id";
|
2018-03-09 02:17:38 +01:00
|
|
|
ContactsManager::send_get_me_query(
|
2021-11-08 13:20:38 +01:00
|
|
|
td_, PromiseCreator::lambda([this](Result<Unit> result) { update_state(State::Ok); }));
|
2018-03-08 22:42:25 +01:00
|
|
|
}
|
2022-05-02 17:34:01 +02:00
|
|
|
G()->net_query_dispatcher().check_authorization_is_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (auth_str == "logout") {
|
2021-06-27 04:52:43 +02:00
|
|
|
LOG(WARNING) << "Continue to log out";
|
2018-12-31 20:04:05 +01:00
|
|
|
update_state(State::LoggingOut);
|
2017-12-29 21:34:39 +01:00
|
|
|
} else if (auth_str == "destroy") {
|
2021-12-01 14:16:18 +01:00
|
|
|
LOG(WARNING) << "Continue to destroy auth keys";
|
2017-12-29 21:34:39 +01:00
|
|
|
update_state(State::DestroyingKeys);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-03-14 19:04:41 +01:00
|
|
|
if (!load_state()) {
|
|
|
|
update_state(State::WaitPhoneNumber);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::start_up() {
|
|
|
|
if (state_ == State::LoggingOut) {
|
2020-08-17 16:35:15 +02:00
|
|
|
send_log_out_query();
|
2017-12-29 21:34:39 +01:00
|
|
|
} else if (state_ == State::DestroyingKeys) {
|
2022-06-26 16:11:27 +02:00
|
|
|
G()->net_query_dispatcher().destroy_auth_keys(PromiseCreator::lambda([](Result<Unit> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure_later(G()->td(), &Td::destroy);
|
|
|
|
}
|
|
|
|
}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void AuthManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AuthManager::is_bot() const {
|
2020-01-05 13:24:37 +01:00
|
|
|
if (net_query_id_ != 0 && net_query_type_ == NetQueryType::BotAuthentication) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-22 20:28:20 +02:00
|
|
|
return is_bot_ && was_authorized();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AuthManager::was_authorized() const {
|
|
|
|
return state_ == State::Ok || state_ == State::LoggingOut || state_ == State::DestroyingKeys ||
|
|
|
|
state_ == State::Closing;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AuthManager::is_authorized() const {
|
|
|
|
return state_ == State::Ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::AuthorizationState> AuthManager::get_authorization_state_object(State authorization_state) const {
|
|
|
|
switch (authorization_state) {
|
|
|
|
case State::WaitPhoneNumber:
|
|
|
|
return make_tl_object<td_api::authorizationStateWaitPhoneNumber>();
|
2022-09-06 11:31:54 +02:00
|
|
|
case State::WaitEmailAddress:
|
|
|
|
return make_tl_object<td_api::authorizationStateWaitEmailAddress>(allow_apple_id_, allow_google_id_);
|
2023-03-19 23:40:05 +01:00
|
|
|
case State::WaitEmailCode: {
|
2023-03-20 12:45:11 +01:00
|
|
|
td_api::object_ptr<td_api::EmailAddressResetState> reset_state;
|
|
|
|
if (reset_pending_date_ > 0) {
|
|
|
|
reset_state =
|
|
|
|
td_api::make_object<td_api::emailAddressResetStatePending>(max(reset_pending_date_ - G()->unix_time(), 0));
|
|
|
|
} else if (reset_available_period_ >= 0) {
|
|
|
|
reset_state = td_api::make_object<td_api::emailAddressResetStateAvailable>(reset_available_period_);
|
|
|
|
}
|
2022-09-06 15:08:10 +02:00
|
|
|
return make_tl_object<td_api::authorizationStateWaitEmailCode>(
|
2022-09-06 17:15:43 +02:00
|
|
|
allow_apple_id_, allow_google_id_, email_code_info_.get_email_address_authentication_code_info_object(),
|
2023-03-20 12:45:11 +01:00
|
|
|
std::move(reset_state));
|
2023-03-19 23:40:05 +01:00
|
|
|
}
|
2019-12-17 17:17:57 +01:00
|
|
|
case State::WaitCode:
|
|
|
|
return send_code_helper_.get_authorization_state_wait_code();
|
|
|
|
case State::WaitQrCodeConfirmation:
|
2019-12-31 14:20:29 +01:00
|
|
|
return make_tl_object<td_api::authorizationStateWaitOtherDeviceConfirmation>("tg://login?token=" +
|
2019-12-17 17:17:57 +01:00
|
|
|
base64url_encode(login_token_));
|
2018-12-31 20:04:05 +01:00
|
|
|
case State::WaitPassword:
|
2018-03-15 11:06:26 +01:00
|
|
|
return make_tl_object<td_api::authorizationStateWaitPassword>(
|
2023-02-22 13:59:36 +01:00
|
|
|
wait_password_state_.hint_, wait_password_state_.has_recovery_, wait_password_state_.has_secure_values_,
|
|
|
|
wait_password_state_.email_address_pattern_);
|
2019-12-17 17:17:57 +01:00
|
|
|
case State::WaitRegistration:
|
|
|
|
return make_tl_object<td_api::authorizationStateWaitRegistration>(
|
|
|
|
terms_of_service_.get_terms_of_service_object());
|
|
|
|
case State::Ok:
|
|
|
|
return make_tl_object<td_api::authorizationStateReady>();
|
2018-12-31 20:04:05 +01:00
|
|
|
case State::LoggingOut:
|
2017-12-29 21:34:39 +01:00
|
|
|
case State::DestroyingKeys:
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<td_api::authorizationStateLoggingOut>();
|
|
|
|
case State::Closing:
|
|
|
|
return make_tl_object<td_api::authorizationStateClosing>();
|
|
|
|
case State::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 18:33:41 +02:00
|
|
|
tl_object_ptr<td_api::AuthorizationState> AuthManager::get_current_authorization_state_object() const {
|
|
|
|
if (state_ == State::None) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return get_authorization_state_object(state_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AuthManager::get_state(uint64 query_id) {
|
2018-03-09 02:17:38 +01:00
|
|
|
if (state_ == State::None) {
|
|
|
|
pending_get_authorization_state_requests_.push_back(query_id);
|
|
|
|
} else {
|
|
|
|
send_closure(G()->td(), &Td::send_result, query_id, get_authorization_state_object(state_));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::check_bot_token(uint64 query_id, string bot_token) {
|
2019-07-28 20:17:47 +02:00
|
|
|
if (state_ == State::WaitPhoneNumber && net_query_id_ == 0) {
|
|
|
|
// can ignore previous checks
|
2020-09-25 00:11:17 +02:00
|
|
|
was_check_bot_token_ = false; // TODO can we remove was_check_bot_token_?
|
2019-07-28 20:17:47 +02:00
|
|
|
}
|
2020-09-25 00:11:17 +02:00
|
|
|
if (state_ != State::WaitPhoneNumber) {
|
2020-05-18 21:26:44 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to checkAuthenticationBotToken unexpected"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-12-17 17:17:57 +01:00
|
|
|
if (!send_code_helper_.phone_number().empty() || was_qr_code_request_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return on_query_error(
|
2020-09-25 00:11:17 +02:00
|
|
|
query_id, Status::Error(400, "Cannot set bot token after authentication began. You need to log out first"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (was_check_bot_token_ && bot_token_ != bot_token) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Cannot change bot token. You need to log out first"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
2021-10-19 17:11:16 +02:00
|
|
|
bot_token_ = std::move(bot_token);
|
2018-12-31 20:04:05 +01:00
|
|
|
was_check_bot_token_ = true;
|
|
|
|
start_net_query(NetQueryType::BotAuthentication,
|
2020-03-15 02:51:14 +01:00
|
|
|
G()->net_query_creator().create_unauth(
|
2020-03-15 23:01:14 +01:00
|
|
|
telegram_api::auth_importBotAuthorization(0, api_id_, api_hash_, bot_token_)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 23:50:15 +02:00
|
|
|
void AuthManager::request_qr_code_authentication(uint64 query_id, vector<UserId> other_user_ids) {
|
2019-12-17 17:17:57 +01:00
|
|
|
if (state_ != State::WaitPhoneNumber) {
|
2022-09-06 15:08:10 +02:00
|
|
|
if ((state_ == State::WaitEmailAddress || state_ == State::WaitEmailCode || state_ == State::WaitCode ||
|
|
|
|
state_ == State::WaitPassword || state_ == State::WaitRegistration) &&
|
2019-12-17 17:17:57 +01:00
|
|
|
net_query_id_ == 0) {
|
|
|
|
// ok
|
|
|
|
} else {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to requestQrCodeAuthentication unexpected"));
|
2019-12-17 17:17:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (was_check_bot_token_) {
|
|
|
|
return on_query_error(
|
|
|
|
query_id,
|
2021-09-24 09:59:51 +02:00
|
|
|
Status::Error(400,
|
2019-12-17 17:17:57 +01:00
|
|
|
"Cannot request QR code authentication after bot token was entered. You need to log out first"));
|
|
|
|
}
|
|
|
|
for (auto &other_user_id : other_user_ids) {
|
2021-03-28 23:50:15 +02:00
|
|
|
if (!other_user_id.is_valid()) {
|
2019-12-17 17:17:57 +01:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Invalid user_id among other user_ids"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
other_user_ids_ = std::move(other_user_ids);
|
|
|
|
send_code_helper_ = SendCodeHelper();
|
|
|
|
terms_of_service_ = TermsOfService();
|
|
|
|
was_qr_code_request_ = true;
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
|
|
|
send_export_login_token_query();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::send_export_login_token_query() {
|
|
|
|
poll_export_login_code_timeout_.cancel_timeout();
|
|
|
|
start_net_query(NetQueryType::RequestQrCode,
|
2021-03-28 23:50:15 +02:00
|
|
|
G()->net_query_creator().create_unauth(telegram_api::auth_exportLoginToken(
|
|
|
|
api_id_, api_hash_, UserId::get_input_user_ids(other_user_ids_))));
|
2019-12-17 17:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::set_login_token_expires_at(double login_token_expires_at) {
|
|
|
|
login_token_expires_at_ = login_token_expires_at;
|
|
|
|
poll_export_login_code_timeout_.cancel_timeout();
|
|
|
|
poll_export_login_code_timeout_.set_callback(std::move(on_update_login_token_static));
|
2021-11-08 13:20:38 +01:00
|
|
|
poll_export_login_code_timeout_.set_callback_data(static_cast<void *>(td_));
|
2019-12-17 17:17:57 +01:00
|
|
|
poll_export_login_code_timeout_.set_timeout_at(login_token_expires_at_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_update_login_token_static(void *td) {
|
2021-11-13 20:37:41 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-17 17:17:57 +01:00
|
|
|
static_cast<Td *>(td)->auth_manager_->on_update_login_token();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_update_login_token() {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state_ != State::WaitQrCodeConfirmation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_export_login_token_query();
|
|
|
|
}
|
|
|
|
|
2019-05-03 04:44:59 +02:00
|
|
|
void AuthManager::set_phone_number(uint64 query_id, string phone_number,
|
|
|
|
td_api::object_ptr<td_api::phoneNumberAuthenticationSettings> settings) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ != State::WaitPhoneNumber) {
|
2022-09-06 15:08:10 +02:00
|
|
|
if ((state_ == State::WaitEmailAddress || state_ == State::WaitEmailCode || state_ == State::WaitCode ||
|
|
|
|
state_ == State::WaitPassword || state_ == State::WaitRegistration) &&
|
2019-07-16 21:08:34 +02:00
|
|
|
net_query_id_ == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// ok
|
|
|
|
} else {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to setAuthenticationPhoneNumber unexpected"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (was_check_bot_token_) {
|
|
|
|
return on_query_error(
|
2021-09-24 09:59:51 +02:00
|
|
|
query_id, Status::Error(400, "Cannot set phone number after bot token was entered. You need to log out first"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (phone_number.empty()) {
|
2022-09-06 13:04:39 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Phone number must be non-empty"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:17:57 +01:00
|
|
|
other_user_ids_.clear();
|
|
|
|
was_qr_code_request_ = false;
|
|
|
|
|
2022-09-06 15:08:10 +02:00
|
|
|
allow_apple_id_ = false;
|
|
|
|
allow_google_id_ = false;
|
|
|
|
email_address_ = {};
|
2022-09-06 16:42:19 +02:00
|
|
|
email_code_info_ = {};
|
2023-03-19 23:40:05 +01:00
|
|
|
reset_available_period_ = -1;
|
2023-03-20 12:45:11 +01:00
|
|
|
reset_pending_date_ = -1;
|
2022-09-06 16:42:19 +02:00
|
|
|
code_ = string();
|
2022-09-06 22:29:57 +02:00
|
|
|
email_code_ = {};
|
2022-09-06 15:08:10 +02:00
|
|
|
|
2019-12-18 17:08:05 +01:00
|
|
|
if (send_code_helper_.phone_number() != phone_number) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_code_helper_ = SendCodeHelper();
|
2018-06-07 20:42:17 +02:00
|
|
|
terms_of_service_ = TermsOfService();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
start_net_query(NetQueryType::SendCode, G()->net_query_creator().create_unauth(send_code_helper_.send_code(
|
|
|
|
std::move(phone_number), settings, api_id_, api_hash_)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 14:45:47 +01:00
|
|
|
void AuthManager::set_firebase_token(uint64 query_id, string token) {
|
|
|
|
if (state_ != State::WaitCode) {
|
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to sendAuthenticationFirebaseSms unexpected"));
|
|
|
|
}
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
|
|
|
start_net_query(NetQueryType::RequestFirebaseSms,
|
|
|
|
G()->net_query_creator().create_unauth(send_code_helper_.request_firebase_sms(token)));
|
|
|
|
}
|
|
|
|
|
2022-09-06 13:04:39 +02:00
|
|
|
void AuthManager::set_email_address(uint64 query_id, string email_address) {
|
|
|
|
if (state_ != State::WaitEmailAddress) {
|
2022-09-06 15:08:10 +02:00
|
|
|
if (state_ == State::WaitEmailCode && net_query_id_ == 0) {
|
|
|
|
// ok
|
|
|
|
} else {
|
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to setAuthenticationEmailAddress unexpected"));
|
|
|
|
}
|
2022-09-06 13:04:39 +02:00
|
|
|
}
|
|
|
|
if (email_address.empty()) {
|
|
|
|
return on_query_error(query_id, Status::Error(400, "Email address must be non-empty"));
|
|
|
|
}
|
|
|
|
|
|
|
|
email_address_ = std::move(email_address);
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
|
|
|
start_net_query(NetQueryType::SendEmailCode,
|
|
|
|
G()->net_query_creator().create_unauth(send_code_helper_.send_verify_email_code(email_address_)));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AuthManager::resend_authentication_code(uint64 query_id) {
|
2019-12-17 17:17:57 +01:00
|
|
|
if (state_ != State::WaitCode) {
|
2022-09-07 18:42:21 +02:00
|
|
|
if (state_ == State::WaitEmailCode) {
|
|
|
|
on_new_query(query_id);
|
|
|
|
start_net_query(NetQueryType::SendEmailCode,
|
|
|
|
G()->net_query_creator().create_unauth(send_code_helper_.send_verify_email_code(email_address_)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to resendAuthenticationCode unexpected"));
|
2018-12-31 20:04:05 +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()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 16:42:19 +02:00
|
|
|
void AuthManager::send_auth_sign_in_query() {
|
2022-09-06 22:29:57 +02:00
|
|
|
bool is_email = !email_code_.is_empty();
|
2022-09-06 16:42:19 +02:00
|
|
|
int32 flags =
|
|
|
|
is_email ? telegram_api::auth_signIn::EMAIL_VERIFICATION_MASK : telegram_api::auth_signIn::PHONE_CODE_MASK;
|
|
|
|
start_net_query(NetQueryType::SignIn,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::auth_signIn(
|
|
|
|
flags, send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code_,
|
2022-09-06 22:29:57 +02:00
|
|
|
is_email ? email_code_.get_input_email_verification() : nullptr)));
|
2022-09-06 16:42:19 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 22:29:57 +02:00
|
|
|
void AuthManager::check_email_code(uint64 query_id, EmailVerification &&code) {
|
|
|
|
if (code.is_empty()) {
|
2022-09-06 16:17:39 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Code must be non-empty"));
|
|
|
|
}
|
2022-09-06 22:29:57 +02:00
|
|
|
if (state_ != State::WaitEmailCode && !(state_ == State::WaitEmailAddress && code.is_email_code())) {
|
2022-09-06 16:17:39 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to checkAuthenticationEmailCode unexpected"));
|
|
|
|
}
|
|
|
|
|
2022-09-06 16:42:19 +02:00
|
|
|
code_ = string();
|
|
|
|
email_code_ = std::move(code);
|
|
|
|
|
2022-09-06 16:17:39 +02:00
|
|
|
on_new_query(query_id);
|
2022-09-06 16:42:19 +02:00
|
|
|
if (email_address_.empty()) {
|
|
|
|
send_auth_sign_in_query();
|
|
|
|
} else {
|
|
|
|
start_net_query(
|
|
|
|
NetQueryType::VerifyEmailAddress,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::account_verifyEmail(
|
2022-09-06 22:29:57 +02:00
|
|
|
send_code_helper_.get_email_verify_purpose_login_setup(), email_code_.get_input_email_verification())));
|
2022-09-06 16:42:19 +02:00
|
|
|
}
|
2022-09-06 16:17:39 +02:00
|
|
|
}
|
|
|
|
|
2023-03-20 13:17:51 +01:00
|
|
|
void AuthManager::reset_email_address(uint64 query_id) {
|
|
|
|
if (state_ != State::WaitEmailCode) {
|
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to resetAuthenticationEmailAddress unexpected"));
|
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
start_net_query(NetQueryType::ResetEmailAddress,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::auth_resetLoginEmail(
|
|
|
|
send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str())));
|
|
|
|
}
|
|
|
|
|
2019-07-16 21:08:34 +02:00
|
|
|
void AuthManager::check_code(uint64 query_id, string code) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ != State::WaitCode) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to checkAuthenticationCode unexpected"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-16 21:08:34 +02:00
|
|
|
code_ = std::move(code);
|
2022-09-06 22:29:57 +02:00
|
|
|
email_code_ = {};
|
2022-09-06 16:42:19 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
on_new_query(query_id);
|
2022-09-06 16:42:19 +02:00
|
|
|
send_auth_sign_in_query();
|
2019-07-16 21:08:34 +02:00
|
|
|
}
|
2018-06-07 20:42:17 +02:00
|
|
|
|
2019-07-16 21:08:34 +02:00
|
|
|
void AuthManager::register_user(uint64 query_id, string first_name, string last_name) {
|
|
|
|
if (state_ != State::WaitRegistration) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to registerUser unexpected"));
|
2019-07-16 21:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
first_name = clean_name(first_name, MAX_NAME_LENGTH);
|
|
|
|
if (first_name.empty()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(Status::Error(400, "First name must be non-empty"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-16 21:08:34 +02:00
|
|
|
|
|
|
|
last_name = clean_name(last_name, MAX_NAME_LENGTH);
|
2020-03-15 23:01:14 +01:00
|
|
|
start_net_query(NetQueryType::SignUp, G()->net_query_creator().create_unauth(telegram_api::auth_signUp(
|
|
|
|
send_code_helper_.phone_number().str(),
|
|
|
|
send_code_helper_.phone_code_hash().str(), first_name, last_name)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::check_password(uint64 query_id, string password) {
|
|
|
|
if (state_ != State::WaitPassword) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to checkAuthenticationPassword unexpected"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG(INFO) << "Have SRP ID " << wait_password_state_.srp_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
on_new_query(query_id);
|
2023-02-04 11:36:01 +01:00
|
|
|
checking_password_ = true;
|
2018-08-13 11:24:37 +02:00
|
|
|
password_ = std::move(password);
|
2021-06-28 20:36:54 +02:00
|
|
|
recovery_code_.clear();
|
|
|
|
new_password_.clear();
|
|
|
|
new_hint_.clear();
|
2018-08-13 11:24:37 +02:00
|
|
|
start_net_query(NetQueryType::GetPassword,
|
2020-03-15 23:01:14 +01:00
|
|
|
G()->net_query_creator().create_unauth(telegram_api::account_getPassword()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::request_password_recovery(uint64 query_id) {
|
|
|
|
if (state_ != State::WaitPassword) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to requestAuthenticationPasswordRecovery unexpected"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
start_net_query(NetQueryType::RequestPasswordRecovery,
|
2020-03-15 23:01:14 +01:00
|
|
|
G()->net_query_creator().create_unauth(telegram_api::auth_requestPasswordRecovery()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:33:08 +02:00
|
|
|
void AuthManager::check_password_recovery_code(uint64 query_id, string code) {
|
|
|
|
if (state_ != State::WaitPassword) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to checkAuthenticationPasswordRecoveryCode unexpected"));
|
2021-06-28 21:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
start_net_query(NetQueryType::CheckPasswordRecoveryCode,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::auth_checkRecoveryPassword(code)));
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:47:23 +02:00
|
|
|
void AuthManager::recover_password(uint64 query_id, string code, string new_password, string new_hint) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ != State::WaitPassword) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Call to recoverAuthenticationPassword unexpected"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
2023-02-04 11:36:01 +01:00
|
|
|
checking_password_ = true;
|
2021-06-28 20:36:54 +02:00
|
|
|
if (!new_password.empty()) {
|
|
|
|
password_.clear();
|
|
|
|
recovery_code_ = std::move(code);
|
|
|
|
new_password_ = std::move(new_password);
|
|
|
|
new_hint_ = std::move(new_hint);
|
|
|
|
start_net_query(NetQueryType::GetPassword,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::account_getPassword()));
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
start_net_query(NetQueryType::RecoverPassword,
|
2021-06-28 14:55:11 +02:00
|
|
|
G()->net_query_creator().create_unauth(telegram_api::auth_recoverPassword(0, code, nullptr)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 23:19:47 +02:00
|
|
|
void AuthManager::log_out(uint64 query_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ == State::Closing) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Already logged out"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
if (state_ == State::LoggingOut || state_ == State::DestroyingKeys) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Already logging out"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
on_new_query(query_id);
|
|
|
|
if (state_ != State::Ok) {
|
|
|
|
// TODO: could skip full logout if still no authorization
|
|
|
|
// TODO: send auth.cancelCode if state_ == State::WaitCode
|
2021-06-27 04:52:43 +02:00
|
|
|
LOG(WARNING) << "Destroying auth keys by user request";
|
2019-04-21 01:23:26 +02:00
|
|
|
destroy_auth_keys();
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2021-06-27 04:52:43 +02:00
|
|
|
LOG(WARNING) << "Logging out by user request";
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth", "logout");
|
|
|
|
update_state(State::LoggingOut);
|
2020-08-18 17:29:45 +02:00
|
|
|
send_log_out_query();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:35:15 +02:00
|
|
|
void AuthManager::send_log_out_query() {
|
2022-09-07 13:39:33 +02:00
|
|
|
// we can lose authorization while logging out, but still may need to resend the request,
|
2022-08-30 13:32:12 +02:00
|
|
|
// so we pretend that it doesn't require authorization
|
|
|
|
auto query = G()->net_query_creator().create_unauth(telegram_api::auth_logOut());
|
2020-08-17 16:35:15 +02:00
|
|
|
query->set_priority(1);
|
|
|
|
start_net_query(NetQueryType::LogOut, std::move(query));
|
|
|
|
}
|
|
|
|
|
2022-07-01 20:25:34 +02:00
|
|
|
void AuthManager::delete_account(uint64 query_id, string reason, string password) {
|
2018-06-27 20:26:52 +02:00
|
|
|
if (state_ != State::Ok && state_ != State::WaitPassword) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Need to log in first"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-07-01 20:25:34 +02:00
|
|
|
if (password.empty() || state_ != State::Ok) {
|
|
|
|
on_new_query(query_id);
|
|
|
|
LOG(INFO) << "Deleting account";
|
|
|
|
start_net_query(NetQueryType::DeleteAccount,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::account_deleteAccount(0, reason, nullptr)));
|
|
|
|
} else {
|
|
|
|
send_closure(G()->password_manager(), &PasswordManager::get_input_check_password_srp, password,
|
|
|
|
PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), query_id, reason = std::move(reason)](
|
|
|
|
Result<tl_object_ptr<telegram_api::InputCheckPasswordSRP>> r_input_password) mutable {
|
|
|
|
send_closure(actor_id, &AuthManager::do_delete_account, query_id, std::move(reason),
|
|
|
|
std::move(r_input_password));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::do_delete_account(uint64 query_id, string reason,
|
|
|
|
Result<tl_object_ptr<telegram_api::InputCheckPasswordSRP>> r_input_password) {
|
|
|
|
if (r_input_password.is_error()) {
|
|
|
|
return on_query_error(query_id, r_input_password.move_as_error());
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
on_new_query(query_id);
|
2022-07-01 20:25:34 +02:00
|
|
|
LOG(INFO) << "Deleting account with password";
|
|
|
|
int32 flags = telegram_api::account_deleteAccount::PASSWORD_MASK;
|
|
|
|
start_net_query(NetQueryType::DeleteAccount, G()->net_query_creator().create(telegram_api::account_deleteAccount(
|
|
|
|
flags, reason, r_input_password.move_as_ok())));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-12-26 20:04:46 +01:00
|
|
|
void AuthManager::on_closing(bool destroy_flag) {
|
|
|
|
if (destroy_flag) {
|
|
|
|
update_state(State::LoggingOut);
|
|
|
|
} else {
|
|
|
|
update_state(State::Closing);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_new_query(uint64 query_id) {
|
|
|
|
if (query_id_ != 0) {
|
2023-09-02 21:11:01 +02:00
|
|
|
on_current_query_error(Status::Error(400, "Another authorization query has started"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-02-04 11:36:01 +01:00
|
|
|
checking_password_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
query_id_ = query_id;
|
|
|
|
// TODO: cancel older net_query
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:11:01 +02:00
|
|
|
void AuthManager::on_current_query_error(Status status) {
|
|
|
|
if (query_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id = query_id_;
|
|
|
|
query_id_ = 0;
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
2023-02-04 11:36:01 +01:00
|
|
|
checking_password_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
on_query_error(id, std::move(status));
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void AuthManager::on_query_error(uint64 query_id, Status status) {
|
|
|
|
send_closure(G()->td(), &Td::send_error, query_id, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:39:26 +02:00
|
|
|
void AuthManager::on_current_query_ok() {
|
|
|
|
if (query_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id = query_id_;
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
query_id_ = 0;
|
|
|
|
send_ok(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::send_ok(uint64 query_id) {
|
|
|
|
send_closure(G()->td(), &Td::send_result, query_id, td_api::make_object<td_api::ok>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::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));
|
|
|
|
}
|
|
|
|
|
2023-01-06 10:42:16 +01:00
|
|
|
void AuthManager::on_sent_code(telegram_api::object_ptr<telegram_api::auth_SentCode> &&sent_code_ptr) {
|
2023-03-20 13:17:51 +01:00
|
|
|
LOG(INFO) << "Receive " << to_string(sent_code_ptr);
|
2023-01-06 10:42:16 +01:00
|
|
|
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);
|
2023-01-06 10:53:24 +01:00
|
|
|
auto sent_code_success = move_tl_object_as<telegram_api::auth_sentCodeSuccess>(sent_code_ptr);
|
|
|
|
return on_get_authorization(std::move(sent_code_success->authorization_));
|
2023-01-06 10:42:16 +01:00
|
|
|
}
|
|
|
|
auto sent_code = telegram_api::move_object_as<telegram_api::auth_sentCode>(sent_code_ptr);
|
2022-09-06 16:42:19 +02:00
|
|
|
auto code_type_id = sent_code->type_->get_id();
|
|
|
|
if (code_type_id == telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID) {
|
2022-09-06 11:31:54 +02:00
|
|
|
auto code_type = move_tl_object_as<telegram_api::auth_sentCodeTypeSetUpEmailRequired>(std::move(sent_code->type_));
|
2022-09-06 13:04:39 +02:00
|
|
|
send_code_helper_.on_phone_code_hash(std::move(sent_code->phone_code_hash_));
|
2022-09-06 11:31:54 +02:00
|
|
|
allow_apple_id_ = code_type->apple_signin_allowed_;
|
|
|
|
allow_google_id_ = code_type->google_signin_allowed_;
|
|
|
|
update_state(State::WaitEmailAddress, true);
|
2022-09-06 16:42:19 +02:00
|
|
|
} else if (code_type_id == telegram_api::auth_sentCodeTypeEmailCode::ID) {
|
|
|
|
auto code_type = move_tl_object_as<telegram_api::auth_sentCodeTypeEmailCode>(std::move(sent_code->type_));
|
|
|
|
send_code_helper_.on_phone_code_hash(std::move(sent_code->phone_code_hash_));
|
|
|
|
allow_apple_id_ = code_type->apple_signin_allowed_;
|
|
|
|
allow_google_id_ = code_type->google_signin_allowed_;
|
|
|
|
email_address_.clear();
|
2023-03-20 13:17:51 +01:00
|
|
|
if (!code_type->email_pattern_.empty() || email_code_info_.is_empty()) {
|
|
|
|
email_code_info_ = SentEmailCode(std::move(code_type->email_pattern_), code_type->length_);
|
|
|
|
}
|
2023-03-20 12:45:11 +01:00
|
|
|
reset_available_period_ = -1;
|
|
|
|
reset_pending_date_ = -1;
|
|
|
|
if (code_type->reset_pending_date_ > 0) {
|
|
|
|
reset_pending_date_ = code_type->reset_pending_date_;
|
|
|
|
} else if ((code_type->flags_ & telegram_api::auth_sentCodeTypeEmailCode::RESET_AVAILABLE_PERIOD_MASK) != 0) {
|
2023-03-19 23:40:05 +01:00
|
|
|
reset_available_period_ = max(code_type->reset_available_period_, 0);
|
|
|
|
}
|
2022-09-06 16:42:19 +02:00
|
|
|
if (email_code_info_.is_empty()) {
|
|
|
|
email_code_info_ = SentEmailCode("<unknown>", code_type->length_);
|
|
|
|
CHECK(!email_code_info_.is_empty());
|
|
|
|
}
|
|
|
|
update_state(State::WaitEmailCode, true);
|
2022-09-06 11:31:54 +02:00
|
|
|
} else {
|
|
|
|
send_code_helper_.on_sent_code(std::move(sent_code));
|
|
|
|
update_state(State::WaitCode, true);
|
|
|
|
}
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2022-09-06 16:17:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_send_code_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_sent_code = fetch_result<telegram_api::auth_sendCode>(std::move(net_query));
|
2022-09-06 16:17:39 +02:00
|
|
|
if (r_sent_code.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_sent_code.move_as_error());
|
2022-09-06 16:17:39 +02:00
|
|
|
}
|
2023-03-20 13:17:51 +01:00
|
|
|
on_sent_code(r_sent_code.move_as_ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_send_email_code_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_sent_code = fetch_result<telegram_api::account_sendVerifyEmailCode>(std::move(net_query));
|
2022-09-06 13:04:39 +02:00
|
|
|
if (r_sent_code.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_sent_code.move_as_error());
|
2022-09-06 13:04:39 +02:00
|
|
|
}
|
|
|
|
auto sent_code = r_sent_code.move_as_ok();
|
|
|
|
|
|
|
|
LOG(INFO) << "Receive " << to_string(sent_code);
|
|
|
|
|
2022-09-06 16:42:19 +02:00
|
|
|
email_code_info_ = SentEmailCode(std::move(sent_code));
|
|
|
|
if (email_code_info_.is_empty()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(Status::Error(500, "Receive invalid response"));
|
2022-09-06 15:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update_state(State::WaitEmailCode, true);
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2022-09-06 13:04:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_verify_email_address_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_email_verified = fetch_result<telegram_api::account_verifyEmail>(std::move(net_query));
|
2022-09-06 16:17:39 +02:00
|
|
|
if (r_email_verified.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_email_verified.move_as_error());
|
2022-09-06 16:17:39 +02:00
|
|
|
}
|
|
|
|
auto email_verified = r_email_verified.move_as_ok();
|
|
|
|
|
|
|
|
LOG(INFO) << "Receive " << to_string(email_verified);
|
|
|
|
if (email_verified->get_id() != telegram_api::account_emailVerifiedLogin::ID) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(Status::Error(500, "Receive invalid response"));
|
2022-09-06 16:17:39 +02:00
|
|
|
}
|
2023-03-19 23:40:05 +01:00
|
|
|
reset_available_period_ = -1;
|
2023-03-20 12:45:11 +01:00
|
|
|
reset_pending_date_ = -1;
|
2022-09-06 16:17:39 +02:00
|
|
|
|
|
|
|
auto verified_login = telegram_api::move_object_as<telegram_api::account_emailVerifiedLogin>(email_verified);
|
|
|
|
on_sent_code(std::move(verified_login->sent_code_));
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_reset_email_address_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_sent_code = fetch_result<telegram_api::auth_resetLoginEmail>(std::move(net_query));
|
2023-03-20 13:17:51 +01:00
|
|
|
if (r_sent_code.is_error()) {
|
|
|
|
if (reset_available_period_ > 0 && reset_pending_date_ == -1 &&
|
|
|
|
r_sent_code.error().message() == "TASK_ALREADY_EXISTS") {
|
|
|
|
reset_pending_date_ = G()->unix_time() + reset_available_period_;
|
|
|
|
reset_available_period_ = -1;
|
|
|
|
update_state(State::WaitEmailCode, true);
|
|
|
|
}
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_sent_code.move_as_error());
|
2023-03-20 13:17:51 +01:00
|
|
|
}
|
|
|
|
on_sent_code(r_sent_code.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_request_qr_code_result(NetQueryPtr &&net_query, bool is_import) {
|
|
|
|
auto r_login_token = fetch_result<telegram_api::auth_exportLoginToken>(std::move(net_query));
|
2023-09-02 01:47:06 +02:00
|
|
|
if (r_login_token.is_ok()) {
|
|
|
|
auto login_token = r_login_token.move_as_ok();
|
|
|
|
|
|
|
|
if (is_import) {
|
|
|
|
CHECK(DcId::is_valid(imported_dc_id_));
|
|
|
|
G()->net_query_dispatcher().set_main_dc_id(imported_dc_id_);
|
|
|
|
imported_dc_id_ = -1;
|
2019-12-17 17:17:57 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 01:47:06 +02:00
|
|
|
on_get_login_token(std::move(login_token));
|
|
|
|
return;
|
2019-12-17 17:17:57 +01:00
|
|
|
}
|
2023-09-02 01:47:06 +02:00
|
|
|
auto status = r_login_token.move_as_error();
|
2019-12-17 17:17:57 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Receive " << status << " for login token " << (is_import ? "import" : "export");
|
|
|
|
if (is_import) {
|
|
|
|
imported_dc_id_ = -1;
|
|
|
|
}
|
|
|
|
if (query_id_ != 0) {
|
2023-09-02 21:11:01 +02:00
|
|
|
on_current_query_error(std::move(status));
|
2019-12-17 17:17:57 +01:00
|
|
|
} else {
|
2019-12-23 18:48:30 +01:00
|
|
|
login_code_retry_delay_ = clamp(2 * login_code_retry_delay_, 1, 60);
|
2019-12-17 17:17:57 +01:00
|
|
|
set_login_token_expires_at(Time::now() + login_code_retry_delay_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_get_login_token(tl_object_ptr<telegram_api::auth_LoginToken> login_token) {
|
|
|
|
LOG(INFO) << "Receive " << to_string(login_token);
|
|
|
|
|
|
|
|
login_code_retry_delay_ = 0;
|
|
|
|
|
|
|
|
CHECK(login_token != nullptr);
|
|
|
|
switch (login_token->get_id()) {
|
|
|
|
case telegram_api::auth_loginToken::ID: {
|
|
|
|
auto token = move_tl_object_as<telegram_api::auth_loginToken>(login_token);
|
|
|
|
login_token_ = token->token_.as_slice().str();
|
|
|
|
set_login_token_expires_at(Time::now() + td::max(token->expires_ - G()->server_time(), 1.0));
|
|
|
|
update_state(State::WaitQrCodeConfirmation, true);
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2019-12-17 17:17:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::auth_loginTokenMigrateTo::ID: {
|
|
|
|
auto token = move_tl_object_as<telegram_api::auth_loginTokenMigrateTo>(login_token);
|
|
|
|
if (!DcId::is_valid(token->dc_id_)) {
|
|
|
|
LOG(ERROR) << "Receive wrong DC " << token->dc_id_;
|
|
|
|
return;
|
|
|
|
}
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2019-12-17 17:17:57 +01:00
|
|
|
|
|
|
|
imported_dc_id_ = token->dc_id_;
|
2020-03-15 23:01:14 +01:00
|
|
|
start_net_query(NetQueryType::ImportQrCode, G()->net_query_creator().create_unauth(
|
|
|
|
telegram_api::auth_importLoginToken(std::move(token->token_)),
|
|
|
|
DcId::internal(token->dc_id_)));
|
2019-12-17 17:17:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::auth_loginTokenSuccess::ID: {
|
|
|
|
auto token = move_tl_object_as<telegram_api::auth_loginTokenSuccess>(login_token);
|
|
|
|
on_get_authorization(std::move(token->authorization_));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_get_password_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_password = fetch_result<telegram_api::account_getPassword>(std::move(net_query));
|
2019-12-18 01:47:51 +01:00
|
|
|
if (r_password.is_error() && query_id_ != 0) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_password.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-12-18 01:47:51 +01:00
|
|
|
auto password = r_password.is_ok() ? r_password.move_as_ok() : nullptr;
|
2018-08-13 00:44:53 +02:00
|
|
|
LOG(INFO) << "Receive password info: " << to_string(password);
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-03-15 11:06:26 +01:00
|
|
|
wait_password_state_ = WaitPasswordState();
|
2021-06-28 20:36:54 +02:00
|
|
|
Result<NewPasswordState> r_new_password_state;
|
2019-12-18 01:47:51 +01:00
|
|
|
if (password != nullptr && password->current_algo_ != nullptr) {
|
2018-08-04 08:55:49 +02:00
|
|
|
switch (password->current_algo_->get_id()) {
|
|
|
|
case telegram_api::passwordKdfAlgoUnknown::ID:
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(Status::Error(400, "Application update is needed to log in"));
|
2018-08-10 15:22:48 +02:00
|
|
|
case telegram_api::passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow::ID: {
|
|
|
|
auto algo = move_tl_object_as<telegram_api::passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow>(
|
2018-08-04 08:55:49 +02:00
|
|
|
password->current_algo_);
|
|
|
|
wait_password_state_.current_client_salt_ = algo->salt1_.as_slice().str();
|
|
|
|
wait_password_state_.current_server_salt_ = algo->salt2_.as_slice().str();
|
2018-08-10 15:22:48 +02:00
|
|
|
wait_password_state_.srp_g_ = algo->g_;
|
|
|
|
wait_password_state_.srp_p_ = algo->p_.as_slice().str();
|
|
|
|
wait_password_state_.srp_B_ = password->srp_B_.as_slice().str();
|
|
|
|
wait_password_state_.srp_id_ = password->srp_id_;
|
2018-08-04 08:55:49 +02:00
|
|
|
wait_password_state_.hint_ = std::move(password->hint_);
|
2021-11-01 19:53:23 +01:00
|
|
|
wait_password_state_.has_recovery_ = password->has_recovery_;
|
2023-02-22 13:59:36 +01:00
|
|
|
wait_password_state_.has_secure_values_ = password->has_secure_values_;
|
2018-08-04 08:55:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2021-06-28 20:36:54 +02:00
|
|
|
|
|
|
|
r_new_password_state =
|
|
|
|
get_new_password_state(std::move(password->new_algo_), std::move(password->new_secure_algo_));
|
2019-12-17 17:17:57 +01:00
|
|
|
} else if (was_qr_code_request_) {
|
2019-12-18 01:47:51 +01:00
|
|
|
imported_dc_id_ = -1;
|
2019-12-23 18:48:30 +01:00
|
|
|
login_code_retry_delay_ = clamp(2 * login_code_retry_delay_, 1, 60);
|
2019-12-18 01:47:51 +01:00
|
|
|
set_login_token_expires_at(Time::now() + login_code_retry_delay_);
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-09-06 16:42:19 +02:00
|
|
|
send_auth_sign_in_query();
|
2018-08-13 00:44:53 +02:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2019-12-18 01:47:51 +01:00
|
|
|
if (imported_dc_id_ != -1) {
|
|
|
|
G()->net_query_dispatcher().set_main_dc_id(imported_dc_id_);
|
|
|
|
imported_dc_id_ = -1;
|
|
|
|
}
|
|
|
|
|
2023-02-04 11:36:01 +01:00
|
|
|
if (state_ == State::WaitPassword && checking_password_) {
|
2021-06-28 20:36:54 +02:00
|
|
|
if (!new_password_.empty()) {
|
|
|
|
if (r_new_password_state.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_new_password_state.move_as_error());
|
2021-06-28 20:36:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto r_new_settings = PasswordManager::get_password_input_settings(std::move(new_password_), std::move(new_hint_),
|
|
|
|
r_new_password_state.ok());
|
|
|
|
if (r_new_settings.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_new_settings.move_as_error());
|
2021-06-28 20:36:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 flags = telegram_api::auth_recoverPassword::NEW_SETTINGS_MASK;
|
|
|
|
start_net_query(NetQueryType::RecoverPassword,
|
|
|
|
G()->net_query_creator().create_unauth(
|
|
|
|
telegram_api::auth_recoverPassword(flags, recovery_code_, r_new_settings.move_as_ok())));
|
|
|
|
return;
|
|
|
|
}
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG(INFO) << "Have SRP ID " << wait_password_state_.srp_id_;
|
2018-08-13 11:24:37 +02:00
|
|
|
auto hash = PasswordManager::get_input_check_password(password_, wait_password_state_.current_client_salt_,
|
|
|
|
wait_password_state_.current_server_salt_,
|
|
|
|
wait_password_state_.srp_g_, wait_password_state_.srp_p_,
|
|
|
|
wait_password_state_.srp_B_, wait_password_state_.srp_id_);
|
|
|
|
|
2020-03-15 23:01:14 +01:00
|
|
|
start_net_query(NetQueryType::CheckPassword,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::auth_checkPassword(std::move(hash))));
|
2018-08-13 11:24:37 +02:00
|
|
|
} else {
|
|
|
|
update_state(State::WaitPassword);
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2018-08-13 11:24:37 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_request_password_recovery_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_email_address_pattern = fetch_result<telegram_api::auth_requestPasswordRecovery>(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_email_address_pattern.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_email_address_pattern.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto email_address_pattern = r_email_address_pattern.move_as_ok();
|
|
|
|
CHECK(email_address_pattern->get_id() == telegram_api::auth_passwordRecovery::ID);
|
2018-08-04 08:55:49 +02:00
|
|
|
wait_password_state_.email_address_pattern_ = std::move(email_address_pattern->email_pattern_);
|
2018-01-05 11:44:36 +01:00
|
|
|
update_state(State::WaitPassword, true);
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_check_password_recovery_code_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_success = fetch_result<telegram_api::auth_checkRecoveryPassword>(std::move(net_query));
|
2021-06-28 21:33:08 +02:00
|
|
|
if (r_success.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_success.move_as_error());
|
2021-06-28 21:33:08 +02:00
|
|
|
}
|
|
|
|
if (!r_success.ok()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(Status::Error(400, "Invalid recovery code"));
|
2021-06-28 21:33:08 +02:00
|
|
|
}
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2021-06-28 21:33:08 +02:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_request_firebase_sms_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_bool = fetch_result<telegram_api::auth_requestFirebaseSms>(std::move(net_query));
|
2023-01-19 14:45:47 +01:00
|
|
|
if (r_bool.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(r_bool.move_as_error());
|
2023-01-19 14:45:47 +01:00
|
|
|
}
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2023-01-19 14:45:47 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_authentication_result(NetQueryPtr &&net_query, bool is_from_current_query) {
|
|
|
|
auto r_sign_in = fetch_result<telegram_api::auth_signIn>(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_sign_in.is_error()) {
|
2023-09-02 21:11:01 +02:00
|
|
|
if (is_from_current_query) {
|
|
|
|
return on_current_query_error(r_sign_in.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-12-17 17:17:57 +01:00
|
|
|
on_get_authorization(r_sign_in.move_as_ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_log_out_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_log_out = fetch_result<telegram_api::auth_logOut>(std::move(net_query));
|
2023-09-02 01:47:06 +02:00
|
|
|
if (r_log_out.is_ok()) {
|
|
|
|
auto logged_out = r_log_out.move_as_ok();
|
|
|
|
if (!logged_out->future_auth_token_.empty()) {
|
|
|
|
td_->option_manager_->set_option_string("authentication_token",
|
|
|
|
base64url_encode(logged_out->future_auth_token_.as_slice()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-02 01:47:06 +02:00
|
|
|
} else if (r_log_out.error().code() != 401) {
|
|
|
|
LOG(ERROR) << "Receive error for auth.logOut: " << r_log_out.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 23:19:47 +02:00
|
|
|
// state_ will stay LoggingOut, so no queries will work.
|
2017-12-29 21:34:39 +01:00
|
|
|
destroy_auth_keys();
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-12-15 18:30:25 +01:00
|
|
|
void AuthManager::on_authorization_lost(string source) {
|
2022-06-20 16:43:08 +02:00
|
|
|
if (state_ == State::LoggingOut && net_query_type_ == NetQueryType::LogOut) {
|
|
|
|
LOG(INFO) << "Ignore authorization loss because of " << source << ", while logging out";
|
|
|
|
return;
|
|
|
|
}
|
2022-08-17 19:01:03 +02:00
|
|
|
if (state_ == State::Closing || state_ == State::DestroyingKeys) {
|
|
|
|
LOG(INFO) << "Ignore duplicate authorization loss because of " << source;
|
|
|
|
return;
|
|
|
|
}
|
2021-06-27 04:52:43 +02:00
|
|
|
LOG(WARNING) << "Lost authorization because of " << source;
|
2017-12-29 21:34:39 +01:00
|
|
|
destroy_auth_keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::destroy_auth_keys() {
|
2021-06-27 04:52:43 +02:00
|
|
|
if (state_ == State::Closing || state_ == State::DestroyingKeys) {
|
2017-12-29 21:34:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
update_state(State::DestroyingKeys);
|
2022-06-26 16:11:27 +02:00
|
|
|
auto promise = PromiseCreator::lambda([](Result<Unit> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
G()->net_query_dispatcher().destroy_auth_keys(PromiseCreator::lambda([](Result<Unit> result) {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure_later(G()->td(), &Td::destroy);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2017-12-29 21:34:39 +01:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth", "destroy");
|
|
|
|
G()->td_db()->get_binlog_pmc()->force_sync(std::move(promise));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_delete_account_result(NetQueryPtr &&net_query) {
|
|
|
|
auto r_delete_account = fetch_result<telegram_api::account_deleteAccount>(std::move(net_query));
|
2023-09-02 01:47:06 +02:00
|
|
|
if (r_delete_account.is_ok()) {
|
|
|
|
if (!r_delete_account.ok()) {
|
|
|
|
// status = Status::Error(500, "Receive false as result of the request");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-02 01:47:06 +02:00
|
|
|
auto status = r_delete_account.move_as_error();
|
|
|
|
if (status.message() != "USER_DEACTIVATED") {
|
|
|
|
LOG(WARNING) << "Request account.deleteAccount failed: " << status;
|
|
|
|
// TODO handle some errors
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_current_query_error(std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-02 01:47:06 +02:00
|
|
|
|
|
|
|
destroy_auth_keys();
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:17:57 +01:00
|
|
|
void AuthManager::on_get_authorization(tl_object_ptr<telegram_api::auth_Authorization> auth_ptr) {
|
2019-02-16 16:35:01 +01:00
|
|
|
if (state_ == State::Ok) {
|
2023-02-26 11:41:49 +01:00
|
|
|
LOG(WARNING) << "Ignore duplicate auth.Authorization";
|
2023-09-02 21:39:26 +02:00
|
|
|
return on_current_query_ok();
|
2019-11-08 17:23:05 +01:00
|
|
|
}
|
|
|
|
CHECK(auth_ptr != nullptr);
|
|
|
|
if (auth_ptr->get_id() == telegram_api::auth_authorizationSignUpRequired::ID) {
|
|
|
|
auto sign_up_required = telegram_api::move_object_as<telegram_api::auth_authorizationSignUpRequired>(auth_ptr);
|
|
|
|
terms_of_service_ = TermsOfService(std::move(sign_up_required->terms_of_service_));
|
|
|
|
update_state(State::WaitRegistration);
|
2023-09-02 21:39:26 +02:00
|
|
|
return on_current_query_ok();
|
2019-02-16 16:35:01 +01:00
|
|
|
}
|
2019-11-08 17:23:05 +01:00
|
|
|
auto auth = telegram_api::move_object_as<telegram_api::auth_authorization>(auth_ptr);
|
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
td_->option_manager_->set_option_integer("authorization_date", G()->unix_time());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (was_check_bot_token_) {
|
|
|
|
is_bot_ = true;
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth_is_bot", "true");
|
|
|
|
}
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth", "ok");
|
2018-08-13 11:24:37 +02:00
|
|
|
code_.clear();
|
|
|
|
password_.clear();
|
2021-06-28 20:36:54 +02:00
|
|
|
recovery_code_.clear();
|
|
|
|
new_password_.clear();
|
|
|
|
new_hint_.clear();
|
2018-03-08 22:42:25 +01:00
|
|
|
state_ = State::Ok;
|
2023-06-23 17:22:58 +02:00
|
|
|
if (auth->user_->get_id() == telegram_api::user::ID) {
|
|
|
|
auto *user = static_cast<telegram_api::user *>(auth->user_.get());
|
|
|
|
int32 mask = 1 << 10;
|
|
|
|
if ((user->flags_ & mask) == 0) {
|
|
|
|
LOG(ERROR) << "Receive invalid authorization for " << to_string(auth->user_);
|
|
|
|
user->flags_ |= mask;
|
|
|
|
user->self_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
td_->contacts_manager_->on_get_user(std::move(auth->user_), "on_get_authorization");
|
2018-03-08 22:42:25 +01:00
|
|
|
update_state(State::Ok, true);
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->contacts_manager_->get_my_id().is_valid()) {
|
2023-06-23 17:22:58 +02:00
|
|
|
LOG(ERROR) << "Server didsn't send proper authorization";
|
2023-09-02 21:11:01 +02:00
|
|
|
on_current_query_error(Status::Error(500, "Server didn't send proper authorization"));
|
2020-07-22 23:19:47 +02:00
|
|
|
log_out(0);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-01-06 11:00:21 +01:00
|
|
|
if (auth->tmp_sessions_ > 0) {
|
2022-08-18 16:00:18 +02:00
|
|
|
td_->option_manager_->set_option_integer("session_count", auth->tmp_sessions_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-11-26 14:22:39 +01:00
|
|
|
if (auth->setup_password_required_ && auth->otherwise_relogin_days_ > 0) {
|
2022-08-18 16:00:18 +02:00
|
|
|
td_->option_manager_->set_option_integer("otherwise_relogin_days", auth->otherwise_relogin_days_);
|
2021-11-26 14:22:39 +01:00
|
|
|
}
|
2023-01-06 11:00:21 +01:00
|
|
|
if (!auth->future_auth_token_.empty()) {
|
|
|
|
td_->option_manager_->set_option_string("authentication_token",
|
|
|
|
base64url_encode(auth->future_auth_token_.as_slice()));
|
|
|
|
}
|
2022-03-25 14:00:06 +01:00
|
|
|
td_->attach_menu_manager_->init();
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->messages_manager_->on_authorization_success();
|
2023-03-27 16:44:13 +02:00
|
|
|
td_->dialog_filter_manager_->on_authorization_success(); // must be after MessagesManager::on_authorization_success()
|
|
|
|
// to have folders created
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->notification_manager_->init();
|
2023-08-09 11:59:48 +02:00
|
|
|
td_->reaction_manager_->init();
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->init();
|
|
|
|
td_->theme_manager_->init();
|
|
|
|
td_->top_dialog_manager_->init();
|
|
|
|
td_->updates_manager_->get_difference("on_get_authorization");
|
|
|
|
td_->on_online_updated(false, true);
|
2018-06-27 23:08:44 +02:00
|
|
|
if (!is_bot()) {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->schedule_get_terms_of_service(0);
|
2023-03-22 17:52:06 +01:00
|
|
|
td_->reload_promo_data();
|
2018-06-27 23:08:44 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("fetched_marks_as_unread", "1");
|
2021-01-11 20:22:18 +01:00
|
|
|
} else {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->set_is_bot_online(true);
|
2018-06-27 23:08:44 +02:00
|
|
|
}
|
2022-06-06 19:36:15 +02:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::request_config, false);
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void AuthManager::on_result(NetQueryPtr net_query) {
|
2018-12-31 20:04:05 +01:00
|
|
|
NetQueryType type = NetQueryType::None;
|
2023-09-02 21:20:05 +02:00
|
|
|
LOG(INFO) << "Receive result of query " << net_query->id() << ", expecting " << net_query_id_ << " with type "
|
2019-12-17 17:17:57 +01:00
|
|
|
<< static_cast<int32>(net_query_type_);
|
2023-09-02 21:20:05 +02:00
|
|
|
if (net_query->id() == net_query_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
net_query_id_ = 0;
|
|
|
|
type = net_query_type_;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
2023-09-02 21:20:05 +02:00
|
|
|
if (net_query->is_error()) {
|
2022-09-06 16:17:39 +02:00
|
|
|
if ((type == NetQueryType::SendCode || type == NetQueryType::SendEmailCode ||
|
|
|
|
type == NetQueryType::VerifyEmailAddress || type == NetQueryType::SignIn ||
|
2022-09-06 13:04:39 +02:00
|
|
|
type == NetQueryType::RequestQrCode || type == NetQueryType::ImportQrCode) &&
|
2023-09-02 21:20:05 +02:00
|
|
|
net_query->error().code() == 401 && net_query->error().message() == CSlice("SESSION_PASSWORD_NEEDED")) {
|
2019-12-18 01:47:51 +01:00
|
|
|
auto dc_id = DcId::main();
|
|
|
|
if (type == NetQueryType::ImportQrCode) {
|
|
|
|
CHECK(DcId::is_valid(imported_dc_id_));
|
|
|
|
dc_id = DcId::internal(imported_dc_id_);
|
|
|
|
}
|
2023-09-02 21:20:05 +02:00
|
|
|
net_query->clear();
|
2020-03-15 23:01:14 +01:00
|
|
|
start_net_query(NetQueryType::GetPassword,
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::account_getPassword(), dc_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-09-02 21:20:05 +02:00
|
|
|
if (net_query->error().message() == CSlice("PHONE_NUMBER_BANNED")) {
|
2023-07-05 15:18:45 +02:00
|
|
|
LOG(ERROR) << "Your phone number was banned for suspicious activity. If you think that this is a mistake, "
|
|
|
|
"please try to log in from an official mobile app and send a email to recover the account by "
|
|
|
|
"following instructions provided by the app.";
|
2019-12-17 17:17:57 +01:00
|
|
|
}
|
|
|
|
if (type != NetQueryType::LogOut && type != NetQueryType::DeleteAccount) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (query_id_ != 0) {
|
|
|
|
if (state_ == State::WaitPhoneNumber) {
|
2019-12-17 17:17:57 +01:00
|
|
|
other_user_ids_.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
send_code_helper_ = SendCodeHelper();
|
2018-06-07 20:42:17 +02:00
|
|
|
terms_of_service_ = TermsOfService();
|
2019-12-17 17:17:57 +01:00
|
|
|
was_qr_code_request_ = false;
|
|
|
|
was_check_bot_token_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-02 21:20:05 +02:00
|
|
|
on_current_query_error(net_query->move_as_error());
|
2019-12-17 17:17:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-12-18 02:08:34 +01:00
|
|
|
if (type != NetQueryType::RequestQrCode && type != NetQueryType::ImportQrCode &&
|
|
|
|
type != NetQueryType::GetPassword) {
|
2023-09-02 21:11:01 +02:00
|
|
|
LOG(INFO) << "Ignore error for net query of type " << static_cast<int32>(type);
|
2023-09-02 21:20:05 +02:00
|
|
|
type = NetQueryType::None;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 21:20:05 +02:00
|
|
|
} else if (net_query->is_ok() && net_query->ok_tl_constructor() == telegram_api::auth_authorization::ID) {
|
2018-12-31 20:04:05 +01:00
|
|
|
type = NetQueryType::Authentication;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case NetQueryType::None:
|
2023-09-02 21:20:05 +02:00
|
|
|
net_query->clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::SignIn:
|
|
|
|
case NetQueryType::SignUp:
|
|
|
|
case NetQueryType::BotAuthentication:
|
|
|
|
case NetQueryType::CheckPassword:
|
|
|
|
case NetQueryType::RecoverPassword:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_authentication_result(std::move(net_query), true);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::Authentication:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_authentication_result(std::move(net_query), false);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::SendCode:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_send_code_result(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2022-09-06 13:04:39 +02:00
|
|
|
case NetQueryType::SendEmailCode:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_send_email_code_result(std::move(net_query));
|
2022-09-06 13:04:39 +02:00
|
|
|
break;
|
2022-09-06 16:17:39 +02:00
|
|
|
case NetQueryType::VerifyEmailAddress:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_verify_email_address_result(std::move(net_query));
|
2022-09-06 16:17:39 +02:00
|
|
|
break;
|
2023-03-20 13:17:51 +01:00
|
|
|
case NetQueryType::ResetEmailAddress:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_reset_email_address_result(std::move(net_query));
|
2023-03-20 13:17:51 +01:00
|
|
|
break;
|
2019-12-17 17:17:57 +01:00
|
|
|
case NetQueryType::RequestQrCode:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_request_qr_code_result(std::move(net_query), false);
|
2019-12-17 17:17:57 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::ImportQrCode:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_request_qr_code_result(std::move(net_query), true);
|
2019-12-17 17:17:57 +01:00
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
case NetQueryType::GetPassword:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_get_password_result(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::RequestPasswordRecovery:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_request_password_recovery_result(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2021-06-28 21:33:08 +02:00
|
|
|
case NetQueryType::CheckPasswordRecoveryCode:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_check_password_recovery_code_result(std::move(net_query));
|
2021-06-28 21:33:08 +02:00
|
|
|
break;
|
2023-01-19 14:45:47 +01:00
|
|
|
case NetQueryType::RequestFirebaseSms:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_request_firebase_sms_result(std::move(net_query));
|
2023-01-19 14:45:47 +01:00
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
case NetQueryType::LogOut:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_log_out_result(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::DeleteAccount:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_delete_account_result(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2023-09-02 21:11:01 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 19:04:41 +01:00
|
|
|
void AuthManager::update_state(State new_state, bool force, bool should_save_state) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ == new_state && !force) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-17 16:04:45 +02:00
|
|
|
bool skip_update = (state_ == State::LoggingOut || state_ == State::DestroyingKeys) &&
|
|
|
|
(new_state == State::LoggingOut || new_state == State::DestroyingKeys);
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = new_state;
|
2018-03-14 19:04:41 +01:00
|
|
|
if (should_save_state) {
|
|
|
|
save_state();
|
|
|
|
}
|
2020-08-05 19:36:13 +02:00
|
|
|
if (new_state == State::LoggingOut || new_state == State::DestroyingKeys) {
|
2020-08-05 18:49:34 +02:00
|
|
|
send_closure(G()->state_manager(), &StateManager::on_logging_out, true);
|
|
|
|
}
|
2020-08-17 16:04:45 +02:00
|
|
|
if (!skip_update) {
|
|
|
|
send_closure(G()->td(), &Td::send_update,
|
|
|
|
make_tl_object<td_api::updateAuthorizationState>(get_authorization_state_object(state_)));
|
|
|
|
}
|
2018-03-09 02:17:38 +01:00
|
|
|
|
|
|
|
if (!pending_get_authorization_state_requests_.empty()) {
|
|
|
|
auto query_ids = std::move(pending_get_authorization_state_requests_);
|
|
|
|
for (auto query_id : query_ids) {
|
|
|
|
send_closure(G()->td(), &Td::send_result, query_id, get_authorization_state_object(state_));
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 19:04:41 +01:00
|
|
|
bool AuthManager::load_state() {
|
|
|
|
auto data = G()->td_db()->get_binlog_pmc()->get("auth_state");
|
2018-05-14 21:00:38 +02:00
|
|
|
if (data.empty()) {
|
2021-07-26 20:06:59 +02:00
|
|
|
LOG(INFO) << "Have no saved auth_state. Waiting for phone number";
|
2018-05-14 21:00:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-14 19:04:41 +01:00
|
|
|
DbState db_state;
|
|
|
|
auto status = log_event_parse(db_state, data);
|
|
|
|
if (status.is_error()) {
|
2018-03-14 20:47:08 +01:00
|
|
|
LOG(INFO) << "Ignore auth_state: " << status;
|
2018-03-14 19:04:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (db_state.api_id_ != api_id_ || db_state.api_hash_ != api_hash_) {
|
2018-03-14 20:47:08 +01:00
|
|
|
LOG(INFO) << "Ignore auth_state: api_id or api_hash changed";
|
2018-03-14 19:04:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-15 09:36:39 +01:00
|
|
|
if (db_state.expires_at_ <= Time::now()) {
|
|
|
|
LOG(INFO) << "Ignore auth_state: expired";
|
2018-03-14 19:04:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:48:36 +01:00
|
|
|
LOG(INFO) << "Load auth_state from database: " << tag("state", static_cast<int32>(db_state.state_));
|
2022-09-06 11:31:54 +02:00
|
|
|
if (db_state.state_ == State::WaitEmailAddress) {
|
|
|
|
allow_apple_id_ = db_state.allow_apple_id_;
|
|
|
|
allow_google_id_ = db_state.allow_google_id_;
|
|
|
|
send_code_helper_ = std::move(db_state.send_code_helper_);
|
2022-09-06 15:08:10 +02:00
|
|
|
} else if (db_state.state_ == State::WaitEmailCode) {
|
|
|
|
allow_apple_id_ = db_state.allow_apple_id_;
|
|
|
|
allow_google_id_ = db_state.allow_google_id_;
|
|
|
|
email_address_ = std::move(db_state.email_address_);
|
2022-09-06 16:42:19 +02:00
|
|
|
email_code_info_ = std::move(db_state.email_code_info_);
|
2023-03-19 23:40:05 +01:00
|
|
|
reset_available_period_ = db_state.reset_available_period_;
|
|
|
|
reset_pending_date_ = db_state.reset_pending_date_;
|
2022-09-06 15:08:10 +02:00
|
|
|
send_code_helper_ = std::move(db_state.send_code_helper_);
|
2022-09-06 11:31:54 +02:00
|
|
|
} else if (db_state.state_ == State::WaitCode) {
|
2018-03-15 11:06:26 +01:00
|
|
|
send_code_helper_ = std::move(db_state.send_code_helper_);
|
2019-12-17 17:17:57 +01:00
|
|
|
} else if (db_state.state_ == State::WaitQrCodeConfirmation) {
|
|
|
|
other_user_ids_ = std::move(db_state.other_user_ids_);
|
|
|
|
login_token_ = std::move(db_state.login_token_);
|
|
|
|
set_login_token_expires_at(db_state.login_token_expires_at_);
|
2018-03-15 11:06:26 +01:00
|
|
|
} else if (db_state.state_ == State::WaitPassword) {
|
|
|
|
wait_password_state_ = std::move(db_state.wait_password_state_);
|
2019-07-16 21:08:34 +02:00
|
|
|
} else if (db_state.state_ == State::WaitRegistration) {
|
2019-11-08 17:23:05 +01:00
|
|
|
send_code_helper_ = std::move(db_state.send_code_helper_);
|
2019-07-16 21:08:34 +02:00
|
|
|
terms_of_service_ = std::move(db_state.terms_of_service_);
|
2018-03-15 11:06:26 +01:00
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
update_state(db_state.state_, false, false);
|
2018-03-14 19:04:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::save_state() {
|
2022-09-06 15:08:10 +02:00
|
|
|
if (state_ != State::WaitEmailAddress && state_ != State::WaitEmailCode && state_ != State::WaitCode &&
|
|
|
|
state_ != State::WaitQrCodeConfirmation && state_ != State::WaitPassword && state_ != State::WaitRegistration) {
|
2018-03-14 19:04:41 +01:00
|
|
|
if (state_ != State::Closing) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("auth_state");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-16 21:08:34 +02:00
|
|
|
DbState db_state = [&] {
|
2022-09-06 11:31:54 +02:00
|
|
|
if (state_ == State::WaitEmailAddress) {
|
|
|
|
return DbState::wait_email_address(api_id_, api_hash_, allow_apple_id_, allow_google_id_, send_code_helper_);
|
2022-09-06 15:08:10 +02:00
|
|
|
} else if (state_ == State::WaitEmailCode) {
|
|
|
|
return DbState::wait_email_code(api_id_, api_hash_, allow_apple_id_, allow_google_id_, email_address_,
|
2023-03-19 23:40:05 +01:00
|
|
|
email_code_info_, reset_available_period_, reset_pending_date_,
|
|
|
|
send_code_helper_);
|
2022-09-06 11:31:54 +02:00
|
|
|
} else if (state_ == State::WaitCode) {
|
2019-11-08 17:23:05 +01:00
|
|
|
return DbState::wait_code(api_id_, api_hash_, send_code_helper_);
|
2019-12-17 17:17:57 +01:00
|
|
|
} else if (state_ == State::WaitQrCodeConfirmation) {
|
|
|
|
return DbState::wait_qr_code_confirmation(api_id_, api_hash_, other_user_ids_, login_token_,
|
|
|
|
login_token_expires_at_);
|
2019-07-16 21:08:34 +02:00
|
|
|
} else if (state_ == State::WaitPassword) {
|
|
|
|
return DbState::wait_password(api_id_, api_hash_, wait_password_state_);
|
|
|
|
} else {
|
|
|
|
CHECK(state_ == State::WaitRegistration);
|
2019-11-08 17:23:05 +01:00
|
|
|
return DbState::wait_registration(api_id_, api_hash_, send_code_helper_, terms_of_service_);
|
2019-07-16 21:08:34 +02:00
|
|
|
}
|
|
|
|
}();
|
2018-03-14 19:04:41 +01:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth_state", log_event_store(db_state).as_slice().str());
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|