2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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"
|
|
|
|
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.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/ConfigShared.h"
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
|
|
|
#include "td/telegram/Global.h"
|
2018-08-04 08:55:49 +02:00
|
|
|
#include "td/telegram/logevent/LogEvent.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"
|
2018-08-04 08:55:49 +02:00
|
|
|
#include "td/telegram/PasswordManager.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"
|
2018-12-27 20:24:44 +01:00
|
|
|
#include "td/telegram/UniqueId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
|
|
|
|
2018-03-09 01:52:12 +01:00
|
|
|
#include "td/actor/PromiseFuture.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"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
2019-02-17 14:52:34 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
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
|
|
|
|
G()->shared_config().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(
|
|
|
|
G()->td().get_actor_unsafe(),
|
|
|
|
PromiseCreator::lambda([this](Result<Unit> result) { update_state(State::Ok); }));
|
2018-03-08 22:42:25 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (auth_str == "logout") {
|
|
|
|
update_state(State::LoggingOut);
|
2017-12-29 21:34:39 +01:00
|
|
|
} else if (auth_str == "destroy") {
|
|
|
|
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) {
|
|
|
|
start_net_query(NetQueryType::LogOut, G()->net_query_creator().create(create_storer(telegram_api::auth_logOut())));
|
2017-12-29 21:34:39 +01:00
|
|
|
} else if (state_ == State::DestroyingKeys) {
|
|
|
|
destroy_auth_keys();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void AuthManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AuthManager::is_bot() const {
|
2019-12-30 01:21:46 +01:00
|
|
|
return is_bot_ && (state_ == State::Ok || state_ == State::LoggingOut || state_ == State::DestroyingKeys ||
|
|
|
|
state_ == State::Closing);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:35:23 +01:00
|
|
|
void AuthManager::set_is_bot(bool is_bot) {
|
|
|
|
if (!is_bot_ && is_bot && api_id_ == 23818) {
|
|
|
|
LOG(ERROR) << "Fix is_bot to " << is_bot;
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth_is_bot", "true");
|
|
|
|
is_bot_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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::Ok:
|
|
|
|
return make_tl_object<td_api::authorizationStateReady>();
|
|
|
|
case State::WaitCode:
|
2018-06-07 20:42:17 +02:00
|
|
|
return send_code_helper_.get_authorization_state_wait_code(terms_of_service_);
|
2018-12-31 20:04:05 +01:00
|
|
|
case State::WaitPhoneNumber:
|
|
|
|
return make_tl_object<td_api::authorizationStateWaitPhoneNumber>();
|
|
|
|
case State::WaitPassword:
|
2018-03-15 11:06:26 +01:00
|
|
|
return make_tl_object<td_api::authorizationStateWaitPassword>(
|
|
|
|
wait_password_state_.hint_, wait_password_state_.has_recovery_, wait_password_state_.email_address_pattern_);
|
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) {
|
|
|
|
if (state_ != State::WaitPhoneNumber && state_ != State::Ok) {
|
|
|
|
// TODO do not allow State::Ok
|
|
|
|
return on_query_error(query_id, Status::Error(8, "checkAuthenticationBotToken unexpected"));
|
|
|
|
}
|
|
|
|
if (!send_code_helper_.phone_number().empty()) {
|
|
|
|
return on_query_error(
|
|
|
|
query_id, Status::Error(8, "Cannot set bot token after authentication beginning. You need to log out first"));
|
|
|
|
}
|
|
|
|
if (was_check_bot_token_ && bot_token_ != bot_token) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "Cannot change bot token. You need to log out first"));
|
|
|
|
}
|
|
|
|
if (state_ == State::Ok) {
|
|
|
|
if (!is_bot_) {
|
|
|
|
// fix old bots
|
|
|
|
const int32 AUTH_IS_BOT_FIXED_DATE = 1500940800;
|
|
|
|
if (G()->shared_config().get_option_integer("authorization_date") < AUTH_IS_BOT_FIXED_DATE) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth_is_bot", "true");
|
|
|
|
is_bot_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return send_ok(query_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
bot_token_ = bot_token;
|
|
|
|
was_check_bot_token_ = true;
|
|
|
|
start_net_query(NetQueryType::BotAuthentication,
|
|
|
|
G()->net_query_creator().create(
|
|
|
|
create_storer(telegram_api::auth_importBotAuthorization(0, api_id_, api_hash_, bot_token_)),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::set_phone_number(uint64 query_id, string phone_number, bool allow_flash_call,
|
|
|
|
bool is_current_phone_number) {
|
|
|
|
if (state_ != State::WaitPhoneNumber) {
|
|
|
|
if ((state_ == State::WaitCode || state_ == State::WaitPassword) && net_query_id_ == 0) {
|
|
|
|
// ok
|
|
|
|
} else {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "setAuthenticationPhoneNumber unexpected"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (was_check_bot_token_) {
|
|
|
|
return on_query_error(
|
|
|
|
query_id, Status::Error(8, "Cannot set phone number after bot token was entered. You need to log out first"));
|
|
|
|
}
|
|
|
|
if (phone_number.empty()) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "Phone number can't be empty"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_send_code =
|
|
|
|
send_code_helper_.send_code(phone_number, allow_flash_call, is_current_phone_number, api_id_, api_hash_);
|
|
|
|
if (r_send_code.is_error()) {
|
|
|
|
send_code_helper_ = SendCodeHelper();
|
2018-06-07 20:42:17 +02:00
|
|
|
terms_of_service_ = TermsOfService();
|
2018-12-31 20:04:05 +01:00
|
|
|
r_send_code =
|
|
|
|
send_code_helper_.send_code(phone_number, allow_flash_call, is_current_phone_number, api_id_, api_hash_);
|
|
|
|
if (r_send_code.is_error()) {
|
|
|
|
return on_query_error(query_id, r_send_code.move_as_error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
2018-12-27 18:27:26 +01:00
|
|
|
auto unique_id = UniqueId::next();
|
2018-12-31 20:04:05 +01:00
|
|
|
start_net_query(NetQueryType::SendCode,
|
2018-12-27 18:27:26 +01:00
|
|
|
G()->net_query_creator().create(unique_id, create_storer(r_send_code.move_as_ok()), DcId::main(),
|
2018-12-31 20:04:05 +01:00
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::resend_authentication_code(uint64 query_id) {
|
|
|
|
if (state_ != State::WaitCode || was_check_bot_token_) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "resendAuthenticationCode unexpected"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
start_net_query(NetQueryType::SendCode,
|
|
|
|
G()->net_query_creator().create(create_storer(r_resend_code.move_as_ok()), DcId::main(),
|
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::check_code(uint64 query_id, string code, string first_name, string last_name) {
|
|
|
|
if (state_ != State::WaitCode) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "checkAuthenticationCode unexpected"));
|
|
|
|
}
|
|
|
|
|
2018-08-13 00:44:53 +02:00
|
|
|
code_ = code;
|
2018-12-31 20:04:05 +01:00
|
|
|
on_new_query(query_id);
|
2018-06-01 00:27:34 +02:00
|
|
|
if (send_code_helper_.phone_registered() || first_name.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
start_net_query(NetQueryType::SignIn,
|
|
|
|
G()->net_query_creator().create(
|
|
|
|
create_storer(telegram_api::auth_signIn(send_code_helper_.phone_number().str(),
|
|
|
|
send_code_helper_.phone_code_hash().str(), code)),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
} else {
|
2018-06-07 20:42:17 +02:00
|
|
|
first_name = clean_name(first_name, MAX_NAME_LENGTH);
|
|
|
|
if (first_name.empty()) {
|
|
|
|
return on_query_error(Status::Error(8, "First name can't be empty"));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
last_name = clean_name(last_name, MAX_NAME_LENGTH);
|
|
|
|
start_net_query(
|
|
|
|
NetQueryType::SignUp,
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::auth_signUp(
|
|
|
|
send_code_helper_.phone_number().str(),
|
|
|
|
send_code_helper_.phone_code_hash().str(), code, first_name, last_name)),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::check_password(uint64 query_id, string password) {
|
|
|
|
if (state_ != State::WaitPassword) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "checkAuthenticationPassword unexpected"));
|
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-08-13 11:24:37 +02:00
|
|
|
LOG(INFO) << "Have SRP id " << wait_password_state_.srp_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
on_new_query(query_id);
|
2018-08-13 11:24:37 +02:00
|
|
|
password_ = std::move(password);
|
|
|
|
start_net_query(NetQueryType::GetPassword,
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::account_getPassword()), DcId::main(),
|
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::request_password_recovery(uint64 query_id) {
|
|
|
|
if (state_ != State::WaitPassword) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "requestAuthenticationPasswordRecovery unexpected"));
|
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
start_net_query(NetQueryType::RequestPasswordRecovery,
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::auth_requestPasswordRecovery()),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::recover_password(uint64 query_id, string code) {
|
|
|
|
if (state_ != State::WaitPassword) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "recoverAuthenticationPassword unexpected"));
|
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
start_net_query(NetQueryType::RecoverPassword,
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::auth_recoverPassword(code)), DcId::main(),
|
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::logout(uint64 query_id) {
|
|
|
|
if (state_ == State::Closing) {
|
|
|
|
return on_query_error(query_id, Status::Error(8, "Already logged out"));
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
if (state_ == State::LoggingOut || state_ == State::DestroyingKeys) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return on_query_error(query_id, Status::Error(8, "Already logging out"));
|
|
|
|
}
|
|
|
|
on_new_query(query_id);
|
|
|
|
if (state_ != State::Ok) {
|
|
|
|
update_state(State::LoggingOut);
|
|
|
|
// TODO: could skip full logout if still no authorization
|
|
|
|
// TODO: send auth.cancelCode if state_ == State::WaitCode
|
|
|
|
on_query_ok();
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Logging out";
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("auth", "logout");
|
|
|
|
update_state(State::LoggingOut);
|
|
|
|
start_net_query(NetQueryType::LogOut, G()->net_query_creator().create(create_storer(telegram_api::auth_logOut())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::delete_account(uint64 query_id, const string &reason) {
|
2018-06-27 20:26:52 +02:00
|
|
|
if (state_ != State::Ok && state_ != State::WaitPassword) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return on_query_error(query_id, Status::Error(8, "Need to log in first"));
|
|
|
|
}
|
|
|
|
on_new_query(query_id);
|
|
|
|
LOG(INFO) << "Deleting account";
|
|
|
|
start_net_query(NetQueryType::DeleteAccount,
|
2018-06-27 20:26:52 +02:00
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::account_deleteAccount(reason)),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
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) {
|
|
|
|
on_query_error(Status::Error(9, "Another authorization query has started"));
|
|
|
|
}
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
query_id_ = query_id;
|
|
|
|
// TODO: cancel older net_query
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_query_error(Status status) {
|
|
|
|
CHECK(query_id_ != 0);
|
|
|
|
auto id = query_id_;
|
|
|
|
query_id_ = 0;
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
on_query_error(id, std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_query_error(uint64 id, Status status) {
|
|
|
|
send_closure(G()->td(), &Td::send_error, id, std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_query_ok() {
|
|
|
|
CHECK(query_id_ != 0);
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_send_code_result(NetQueryPtr &result) {
|
|
|
|
auto r_sent_code = fetch_result<telegram_api::auth_sendCode>(result->ok());
|
|
|
|
if (r_sent_code.is_error()) {
|
|
|
|
return on_query_error(r_sent_code.move_as_error());
|
|
|
|
}
|
|
|
|
auto sent_code = r_sent_code.move_as_ok();
|
|
|
|
|
|
|
|
LOG(INFO) << "Receive " << to_string(sent_code);
|
|
|
|
|
2018-06-07 20:42:17 +02:00
|
|
|
terms_of_service_ = TermsOfService(std::move(sent_code->terms_of_service_));
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
send_code_helper_.on_sent_code(std::move(sent_code));
|
|
|
|
|
|
|
|
update_state(State::WaitCode, true);
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_get_password_result(NetQueryPtr &result) {
|
|
|
|
auto r_password = fetch_result<telegram_api::account_getPassword>(result->ok());
|
|
|
|
if (r_password.is_error()) {
|
|
|
|
return on_query_error(r_password.move_as_error());
|
|
|
|
}
|
|
|
|
auto password = r_password.move_as_ok();
|
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();
|
2018-08-04 08:55:49 +02:00
|
|
|
if (password->current_algo_ != nullptr) {
|
|
|
|
switch (password->current_algo_->get_id()) {
|
|
|
|
case telegram_api::passwordKdfAlgoUnknown::ID:
|
2018-08-13 00:44:53 +02:00
|
|
|
return on_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_);
|
|
|
|
wait_password_state_.has_recovery_ =
|
|
|
|
(password->flags_ & telegram_api::account_password::HAS_RECOVERY_MASK) != 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-08-13 00:44:53 +02:00
|
|
|
start_net_query(NetQueryType::SignIn,
|
|
|
|
G()->net_query_creator().create(
|
|
|
|
create_storer(telegram_api::auth_signIn(send_code_helper_.phone_number().str(),
|
|
|
|
send_code_helper_.phone_code_hash().str(), code_)),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-08-13 11:24:37 +02:00
|
|
|
if (state_ == State::WaitPassword) {
|
|
|
|
LOG(INFO) << "Have SRP id " << wait_password_state_.srp_id_;
|
|
|
|
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_);
|
|
|
|
|
|
|
|
start_net_query(NetQueryType::CheckPassword,
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::auth_checkPassword(std::move(hash))),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
} else {
|
|
|
|
update_state(State::WaitPassword);
|
|
|
|
on_query_ok();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_request_password_recovery_result(NetQueryPtr &result) {
|
|
|
|
auto r_email_address_pattern = fetch_result<telegram_api::auth_requestPasswordRecovery>(result->ok());
|
|
|
|
if (r_email_address_pattern.is_error()) {
|
|
|
|
return on_query_error(r_email_address_pattern.move_as_error());
|
|
|
|
}
|
|
|
|
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);
|
2018-12-31 20:04:05 +01:00
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_authentication_result(NetQueryPtr &result, bool expected_flag) {
|
|
|
|
auto r_sign_in = fetch_result<telegram_api::auth_signIn>(result->ok());
|
|
|
|
if (r_sign_in.is_error()) {
|
|
|
|
if (expected_flag && query_id_ != 0) {
|
|
|
|
return on_query_error(r_sign_in.move_as_error());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto sign_in = r_sign_in.move_as_ok();
|
|
|
|
CHECK(sign_in->get_id() == telegram_api::auth_authorization::ID);
|
|
|
|
on_authorization(std::move(sign_in));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_log_out_result(NetQueryPtr &result) {
|
|
|
|
Status status;
|
|
|
|
if (result->is_ok()) {
|
|
|
|
auto r_log_out = fetch_result<telegram_api::auth_logOut>(result->ok());
|
|
|
|
if (r_log_out.is_ok()) {
|
|
|
|
if (!r_log_out.ok()) {
|
|
|
|
status = Status::Error(500, "auth.logOut returned false!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = r_log_out.move_as_error();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = std::move(result->error());
|
|
|
|
}
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG_IF(ERROR, status.is_error()) << "Receive error for auth.logOut: " << status;
|
2018-12-31 20:04:05 +01:00
|
|
|
// state_ will stay logout, so no queries will work.
|
2017-12-29 21:34:39 +01:00
|
|
|
destroy_auth_keys();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (query_id_ != 0) {
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
void AuthManager::on_authorization_lost() {
|
|
|
|
destroy_auth_keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::destroy_auth_keys() {
|
|
|
|
if (state_ == State::Closing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update_state(State::DestroyingKeys);
|
|
|
|
auto promise = PromiseCreator::lambda(
|
|
|
|
[](Unit) {
|
|
|
|
G()->net_query_dispatcher().destroy_auth_keys(PromiseCreator::lambda(
|
|
|
|
[](Unit) { send_closure_later(G()->td(), &Td::destroy); }, PromiseCreator::Ignore()));
|
|
|
|
},
|
|
|
|
PromiseCreator::Ignore());
|
|
|
|
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
|
|
|
|
|
|
|
void AuthManager::on_delete_account_result(NetQueryPtr &result) {
|
|
|
|
Status status;
|
|
|
|
if (result->is_ok()) {
|
|
|
|
auto r_delete_account = fetch_result<telegram_api::account_deleteAccount>(result->ok());
|
|
|
|
if (r_delete_account.is_ok()) {
|
|
|
|
if (!r_delete_account.ok()) {
|
2018-03-27 02:12:32 +02:00
|
|
|
// status = Status::Error(500, "Receive false as result of the request");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = r_delete_account.move_as_error();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = std::move(result->error());
|
|
|
|
}
|
|
|
|
if (status.is_error() && status.error().message() != "USER_DEACTIVATED") {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(WARNING) << "Request account.deleteAccount failed: " << status;
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO handle some errors
|
|
|
|
if (query_id_ != 0) {
|
|
|
|
on_query_error(std::move(status));
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-29 21:34:39 +01:00
|
|
|
destroy_auth_keys();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (query_id_ != 0) {
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_authorization(tl_object_ptr<telegram_api::auth_authorization> auth) {
|
2019-02-16 16:35:01 +01:00
|
|
|
if (state_ == State::Ok) {
|
|
|
|
LOG(WARNING) << "Ignore duplicated auth.authorization";
|
|
|
|
if (query_id_ != 0) {
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->shared_config().set_option_integer("authorization_date", G()->unix_time());
|
|
|
|
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();
|
2018-03-08 22:42:25 +01:00
|
|
|
state_ = State::Ok;
|
2019-01-12 04:48:39 +01:00
|
|
|
td->contacts_manager_->on_get_user(std::move(auth->user_), "on_authorization", true);
|
2018-03-08 22:42:25 +01:00
|
|
|
update_state(State::Ok, true);
|
2018-10-08 16:21:56 +02:00
|
|
|
if (!td->contacts_manager_->get_my_id().is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "Server doesn't send proper authorization";
|
|
|
|
if (query_id_ != 0) {
|
|
|
|
on_query_error(Status::Error(500, "Server doesn't send proper authorization"));
|
|
|
|
}
|
|
|
|
logout(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((auth->flags_ & telegram_api::auth_authorization::TMP_SESSIONS_MASK) != 0) {
|
|
|
|
G()->shared_config().set_option_integer("session_count", auth->tmp_sessions_);
|
|
|
|
}
|
|
|
|
td->updates_manager_->get_difference("on_authorization");
|
2018-11-17 23:54:25 +01:00
|
|
|
td->on_online_updated(false, true);
|
2018-06-08 00:19:34 +02:00
|
|
|
td->schedule_get_terms_of_service(0);
|
2018-06-27 23:08:44 +02:00
|
|
|
if (!is_bot()) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("fetched_marks_as_unread", "1");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::request_config);
|
|
|
|
if (query_id_ != 0) {
|
|
|
|
on_query_ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::on_result(NetQueryPtr result) {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
result->clear();
|
|
|
|
};
|
|
|
|
NetQueryType type = NetQueryType::None;
|
|
|
|
if (result->id() == net_query_id_) {
|
|
|
|
net_query_id_ = 0;
|
|
|
|
type = net_query_type_;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
if (result->is_error()) {
|
|
|
|
if (type == NetQueryType::SignIn && result->error().code() == 401 &&
|
|
|
|
result->error().message() == CSlice("SESSION_PASSWORD_NEEDED")) {
|
|
|
|
start_net_query(NetQueryType::GetPassword,
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::account_getPassword()),
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::Off));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type != NetQueryType::LogOut) {
|
|
|
|
if (query_id_ != 0) {
|
|
|
|
if (state_ == State::WaitPhoneNumber) {
|
|
|
|
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_query_error(std::move(result->error()));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (result->is_ok() && result->ok_tl_constructor() == telegram_api::auth_authorization::ID) {
|
|
|
|
type = NetQueryType::Authentication;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case NetQueryType::None:
|
|
|
|
result->ignore();
|
|
|
|
break;
|
|
|
|
case NetQueryType::SignIn:
|
|
|
|
case NetQueryType::SignUp:
|
|
|
|
case NetQueryType::BotAuthentication:
|
|
|
|
case NetQueryType::CheckPassword:
|
|
|
|
case NetQueryType::RecoverPassword:
|
|
|
|
on_authentication_result(result, true);
|
|
|
|
break;
|
|
|
|
case NetQueryType::Authentication:
|
|
|
|
on_authentication_result(result, false);
|
|
|
|
break;
|
|
|
|
case NetQueryType::SendCode:
|
|
|
|
on_send_code_result(result);
|
|
|
|
break;
|
|
|
|
case NetQueryType::GetPassword:
|
|
|
|
on_get_password_result(result);
|
|
|
|
break;
|
|
|
|
case NetQueryType::RequestPasswordRecovery:
|
|
|
|
on_request_password_recovery_result(result);
|
|
|
|
break;
|
|
|
|
case NetQueryType::LogOut:
|
|
|
|
on_log_out_result(result);
|
|
|
|
break;
|
|
|
|
case NetQueryType::DeleteAccount:
|
|
|
|
on_delete_account_result(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
state_ = new_state;
|
2018-03-14 19:04:41 +01:00
|
|
|
if (should_save_state) {
|
|
|
|
save_state();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
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()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (!db_state.state_timestamp_.is_in_past()) {
|
2018-03-14 20:47:08 +01:00
|
|
|
LOG(INFO) << "Ignore auth_state: timestamp in future";
|
2018-03-14 19:04:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Timestamp::at(db_state.state_timestamp_.at() + 5 * 60).is_in_past()) {
|
2018-03-14 20:47:08 +01:00
|
|
|
LOG(INFO) << "Ignore auth_state: expired " << db_state.state_timestamp_.in();
|
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_));
|
2018-03-15 11:06:26 +01:00
|
|
|
if (db_state.state_ == State::WaitCode) {
|
|
|
|
send_code_helper_ = std::move(db_state.send_code_helper_);
|
2018-06-07 20:42:17 +02:00
|
|
|
terms_of_service_ = std::move(db_state.terms_of_service_);
|
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_);
|
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
update_state(db_state.state_, false, false);
|
2018-03-14 19:04:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthManager::save_state() {
|
2018-03-15 11:06:26 +01:00
|
|
|
if (state_ != State::WaitCode && state_ != State::WaitPassword) {
|
2018-03-14 19:04:41 +01:00
|
|
|
if (state_ != State::Closing) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("auth_state");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 11:06:26 +01:00
|
|
|
DbState db_state;
|
|
|
|
if (state_ == State::WaitCode) {
|
2018-06-07 20:42:17 +02:00
|
|
|
db_state = DbState::wait_code(api_id_, api_hash_, send_code_helper_, terms_of_service_);
|
2018-03-15 11:06:26 +01:00
|
|
|
} else if (state_ == State::WaitPassword) {
|
|
|
|
db_state = DbState::wait_password(api_id_, api_hash_, wait_password_state_);
|
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
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
|