Remove separate authorizationStateWaitEncryptionKey.

This commit is contained in:
levlam 2022-09-07 21:27:23 +03:00
parent 20767a318b
commit 287cceb162
12 changed files with 86 additions and 203 deletions

View File

@ -238,82 +238,71 @@ class TdExample {
void on_authorization_state_update() {
authentication_query_id_++;
td_api::downcast_call(
*authorization_state_,
overloaded(
[this](td_api::authorizationStateReady &) {
are_authorized_ = true;
std::cout << "Got authorization" << std::endl;
},
[this](td_api::authorizationStateLoggingOut &) {
are_authorized_ = false;
std::cout << "Logging out" << std::endl;
},
[this](td_api::authorizationStateClosing &) { std::cout << "Closing" << std::endl; },
[this](td_api::authorizationStateClosed &) {
are_authorized_ = false;
need_restart_ = true;
std::cout << "Terminated" << std::endl;
},
[this](td_api::authorizationStateWaitCode &) {
std::cout << "Enter authentication code: " << std::flush;
std::string code;
std::cin >> code;
send_query(td_api::make_object<td_api::checkAuthenticationCode>(code),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitRegistration &) {
std::string first_name;
std::string last_name;
std::cout << "Enter your first name: " << std::flush;
std::cin >> first_name;
std::cout << "Enter your last name: " << std::flush;
std::cin >> last_name;
send_query(td_api::make_object<td_api::registerUser>(first_name, last_name),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitPassword &) {
std::cout << "Enter authentication password: " << std::flush;
std::string password;
std::getline(std::cin, password);
send_query(td_api::make_object<td_api::checkAuthenticationPassword>(password),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitOtherDeviceConfirmation &state) {
std::cout << "Confirm this login link on another device: " << state.link_ << std::endl;
},
[this](td_api::authorizationStateWaitPhoneNumber &) {
std::cout << "Enter phone number: " << std::flush;
std::string phone_number;
std::cin >> phone_number;
send_query(td_api::make_object<td_api::setAuthenticationPhoneNumber>(phone_number, nullptr),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitEncryptionKey &) {
std::cout << "Enter encryption key or DESTROY: " << std::flush;
std::string key;
std::getline(std::cin, key);
if (key == "DESTROY") {
send_query(td_api::make_object<td_api::destroy>(), create_authentication_query_handler());
} else {
send_query(td_api::make_object<td_api::checkDatabaseEncryptionKey>(std::move(key)),
create_authentication_query_handler());
}
},
[this](td_api::authorizationStateWaitTdlibParameters &) {
auto parameters = td_api::make_object<td_api::tdlibParameters>();
parameters->database_directory_ = "tdlib";
parameters->use_message_database_ = true;
parameters->use_secret_chats_ = true;
parameters->api_id_ = 94575;
parameters->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2";
parameters->system_language_code_ = "en";
parameters->device_model_ = "Desktop";
parameters->application_version_ = "1.0";
parameters->enable_storage_optimizer_ = true;
send_query(td_api::make_object<td_api::setTdlibParameters>(std::move(parameters)),
create_authentication_query_handler());
}));
td_api::downcast_call(*authorization_state_,
overloaded(
[this](td_api::authorizationStateReady &) {
are_authorized_ = true;
std::cout << "Got authorization" << std::endl;
},
[this](td_api::authorizationStateLoggingOut &) {
are_authorized_ = false;
std::cout << "Logging out" << std::endl;
},
[this](td_api::authorizationStateClosing &) { std::cout << "Closing" << std::endl; },
[this](td_api::authorizationStateClosed &) {
are_authorized_ = false;
need_restart_ = true;
std::cout << "Terminated" << std::endl;
},
[this](td_api::authorizationStateWaitCode &) {
std::cout << "Enter authentication code: " << std::flush;
std::string code;
std::cin >> code;
send_query(td_api::make_object<td_api::checkAuthenticationCode>(code),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitRegistration &) {
std::string first_name;
std::string last_name;
std::cout << "Enter your first name: " << std::flush;
std::cin >> first_name;
std::cout << "Enter your last name: " << std::flush;
std::cin >> last_name;
send_query(td_api::make_object<td_api::registerUser>(first_name, last_name),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitPassword &) {
std::cout << "Enter authentication password: " << std::flush;
std::string password;
std::getline(std::cin, password);
send_query(td_api::make_object<td_api::checkAuthenticationPassword>(password),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitOtherDeviceConfirmation &state) {
std::cout << "Confirm this login link on another device: " << state.link_ << std::endl;
},
[this](td_api::authorizationStateWaitPhoneNumber &) {
std::cout << "Enter phone number: " << std::flush;
std::string phone_number;
std::cin >> phone_number;
send_query(
td_api::make_object<td_api::setAuthenticationPhoneNumber>(phone_number, nullptr),
create_authentication_query_handler());
},
[this](td_api::authorizationStateWaitTdlibParameters &) {
auto parameters = td_api::make_object<td_api::tdlibParameters>();
parameters->database_directory_ = "tdlib";
parameters->use_message_database_ = true;
parameters->use_secret_chats_ = true;
parameters->api_id_ = 94575;
parameters->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2";
parameters->system_language_code_ = "en";
parameters->device_model_ = "Desktop";
parameters->application_version_ = "1.0";
parameters->enable_storage_optimizer_ = true;
send_query(td_api::make_object<td_api::setTdlibParameters>(std::move(parameters)),
create_authentication_query_handler());
}));
}
void check_authentication_error(Object object) {

View File

@ -80,10 +80,6 @@ namespace TdExample
_client.Send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler());
}
else if (_authorizationState is TdApi.AuthorizationStateWaitEncryptionKey)
{
_client.Send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler());
}
else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber)
{
string phoneNumber = ReadLine("Please enter phone number: ");

View File

@ -114,9 +114,6 @@ public final class Example {
client.send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler());
break;
case TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR:
client.send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler());
break;
case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: {
String phoneNumber = promptString("Please enter phone number: ");
client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, null), new AuthorizationRequestHandler());

View File

@ -105,10 +105,6 @@ while True:
'application_version': '1.0',
'enable_storage_optimizer': True}})
# set an encryption key for database to let know TDLib how to open the database
if auth_state['@type'] == 'authorizationStateWaitEncryptionKey':
td_send({'@type': 'checkDatabaseEncryptionKey', 'encryption_key': ''})
# enter phone number to log in
if auth_state['@type'] == 'authorizationStateWaitPhoneNumber':
phone_number = input('Please enter your phone number: ')

View File

@ -120,9 +120,6 @@ func updateAuthorizationState(authorizationState: Dictionary<String, Any>) {
]
]);
case "authorizationStateWaitEncryptionKey":
client.queryAsync(query: ["@type":"checkDatabaseEncryptionKey", "encryption_key":""])
case "authorizationStateWaitPhoneNumber":
print("Enter your phone number: ")
let phone_number = myReadLine()

View File

@ -46,7 +46,6 @@ namespace TdApp
parameters.DeviceModel = "Desktop";
parameters.ApplicationVersion = "1.0.0";
_client.Send(new TdApi.SetTdlibParameters(parameters), null);
_client.Send(new TdApi.CheckDatabaseEncryptionKey(), null);
}
public void Print(String str)

View File

@ -26,6 +26,7 @@ ok = Ok;
//@use_test_dc If set to true, the Telegram test environment will be used instead of the production environment
//@database_directory The path to the directory for the persistent database; if empty, the current working directory will be used
//@files_directory The path to the directory for storing files; if empty, database_directory will be used
//@database_encryption_key Encryption key for the database
//@use_file_database If set to true, information about downloaded and uploaded files will be saved between application restarts
//@use_chat_info_database If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use_file_database
//@use_message_database If set to true, the library will maintain a cache of chats and messages. Implies use_chat_info_database
@ -38,7 +39,7 @@ ok = Ok;
//@application_version Application version; must be non-empty
//@enable_storage_optimizer If set to true, old files will automatically be deleted
//@ignore_file_names If set to true, original file names will be ignored. Otherwise, downloaded files will be saved under names as close as possible to the original name
tdlibParameters use_test_dc:Bool database_directory:string files_directory:string use_file_database:Bool use_chat_info_database:Bool use_message_database:Bool use_secret_chats:Bool api_id:int32 api_hash:string system_language_code:string device_model:string system_version:string application_version:string enable_storage_optimizer:Bool ignore_file_names:Bool = TdlibParameters;
tdlibParameters use_test_dc:Bool database_directory:string files_directory:string database_encryption_key:bytes use_file_database:Bool use_chat_info_database:Bool use_message_database:Bool use_secret_chats:Bool api_id:int32 api_hash:string system_language_code:string device_model:string system_version:string application_version:string enable_storage_optimizer:Bool ignore_file_names:Bool = TdlibParameters;
//@class AuthenticationCodeType @description Provides information about the method by which an authentication code is delivered to the user
@ -98,9 +99,6 @@ termsOfService text:formattedText min_user_age:int32 show_popup:Bool = TermsOfSe
//@description TDLib needs TdlibParameters for initialization
authorizationStateWaitTdlibParameters = AuthorizationState;
//@description TDLib needs an encryption key to decrypt the local database @is_encrypted True, if the database is currently encrypted
authorizationStateWaitEncryptionKey is_encrypted:Bool = AuthorizationState;
//@description TDLib needs the user's phone number to authorize. Call `setAuthenticationPhoneNumber` to provide the phone number, or use `requestQrCodeAuthentication`, or `checkAuthenticationBotToken` for other authentication options
authorizationStateWaitPhoneNumber = AuthorizationState;
@ -4629,9 +4627,6 @@ getAuthorizationState = AuthorizationState;
//@description Sets the parameters for TDLib initialization. Works only when the current authorization state is authorizationStateWaitTdlibParameters @parameters Parameters for TDLib initialization
setTdlibParameters parameters:tdlibParameters = Ok;
//@description Checks the database encryption key for correctness. Works only when the current authorization state is authorizationStateWaitEncryptionKey @encryption_key Encryption key to check or set up
checkDatabaseEncryptionKey encryption_key:bytes = Ok;
//@description Sets the phone number of the user and sends an authentication code to the user. Works only when the current authorization state is authorizationStateWaitPhoneNumber,
//-or if there is no pending authentication query and the current authorization state is authorizationStateWaitCode, authorizationStateWaitRegistration, or authorizationStateWaitPassword
//@phone_number The phone number of the user, in international format @settings Settings for the authentication of the user's phone number; pass null to use default settings

View File

@ -2777,8 +2777,6 @@ void Td::set_is_bot_online(bool is_bot_online) {
bool Td::is_authentication_request(int32 id) {
switch (id) {
case td_api::setTdlibParameters::ID:
case td_api::checkDatabaseEncryptionKey::ID:
case td_api::setDatabaseEncryptionKey::ID:
case td_api::getAuthorizationState::ID:
case td_api::setAuthenticationPhoneNumber::ID:
case td_api::setAuthenticationEmailAddress::ID:
@ -2902,8 +2900,6 @@ td_api::object_ptr<td_api::AuthorizationState> Td::get_fake_authorization_state_
switch (state_) {
case State::WaitParameters:
return td_api::make_object<td_api::authorizationStateWaitTdlibParameters>();
case State::Decrypt:
return td_api::make_object<td_api::authorizationStateWaitEncryptionKey>(is_database_encrypted_);
case State::Run:
UNREACHABLE();
return nullptr;
@ -2963,10 +2959,6 @@ void Td::run_request(uint64 id, tl_object_ptr<td_api::Function> function) {
pending_set_parameters_requests_.emplace_back(id, std::move(function));
return;
}
if (init_request_id_ > 0) {
pending_init_requests_.emplace_back(id, std::move(function));
return;
}
int32 function_id = function->get_id();
if (state_ != State::Run) {
@ -3016,34 +3008,6 @@ void Td::run_request(uint64 id, tl_object_ptr<td_api::Function> function) {
}
break;
}
case State::Decrypt: {
switch (function_id) {
case td_api::checkDatabaseEncryptionKey::ID: {
auto check_key = move_tl_object_as<td_api::checkDatabaseEncryptionKey>(function);
return start_init(id, std::move(check_key->encryption_key_));
}
case td_api::setDatabaseEncryptionKey::ID: {
auto set_key = move_tl_object_as<td_api::setDatabaseEncryptionKey>(function);
return start_init(id, std::move(set_key->new_encryption_key_));
}
case td_api::destroy::ID:
// need to send response synchronously before actual destroying
send_closure(actor_id(this), &Td::send_result, id, td_api::make_object<td_api::ok>());
send_closure(actor_id(this), &Td::destroy);
return;
default:
if (is_preinitialization_request(function_id)) {
break;
}
if (is_preauthentication_request(function_id)) {
pending_preauthentication_requests_.emplace_back(id, std::move(function));
return;
}
return send_error_impl(
id, make_error(400, "Database encryption key is needed: call checkDatabaseEncryptionKey first"));
}
break;
}
case State::Close:
if (destroy_flag_) {
return send_error_impl(id, make_error(401, "Unauthorized"));
@ -3503,11 +3467,8 @@ void Td::close_impl(bool destroy_flag) {
}
LOG(WARNING) << (destroy_flag ? "Destroy" : "Close") << " Td in state " << static_cast<int32>(state_);
if (state_ == State::WaitParameters || state_ == State::Decrypt) {
if (state_ == State::WaitParameters) {
clear_requests();
if (destroy_flag && state_ == State::Decrypt) {
TdDb::destroy(parameters_).ignore();
}
state_ = State::Close;
close_flag_ = 4;
G()->set_close_flag();
@ -3591,14 +3552,12 @@ void Td::on_parameters_checked(Result<TdDb::CheckedParameters> r_checked_paramet
parameters_.files_directory = std::move(checked_parameters.files_directory);
is_database_encrypted_ = checked_parameters.is_database_encrypted;
state_ = State::Decrypt;
VLOG(td_init) << "Send authorizationStateWaitEncryptionKey";
send_closure(actor_id(this), &Td::send_update,
td_api::make_object<td_api::updateAuthorizationState>(
td_api::make_object<td_api::authorizationStateWaitEncryptionKey>(is_database_encrypted_)));
VLOG(td_init) << "Finish set parameters";
send_closure(actor_id(this), &Td::send_result, set_parameters_request_id_, td_api::make_object<td_api::ok>());
return finish_set_parameters();
VLOG(td_init) << "Begin to init database";
auto promise = PromiseCreator::lambda([actor_id = actor_id(this)](Result<TdDb::OpenedDatabase> r_opened_database) {
send_closure(actor_id, &Td::init, std::move(r_opened_database));
});
TdDb::open(get_database_scheduler_id(), parameters_, as_db_key(std::move(database_encryption_key_)),
std::move(promise));
}
void Td::finish_set_parameters() {
@ -3617,23 +3576,13 @@ void Td::finish_set_parameters() {
CHECK(pending_set_parameters_requests_.size() < requests.size());
}
void Td::start_init(uint64 id, string &&key) {
VLOG(td_init) << "Begin to init database";
init_request_id_ = id;
auto promise = PromiseCreator::lambda([actor_id = actor_id(this)](Result<TdDb::OpenedDatabase> r_opened_database) {
send_closure(actor_id, &Td::init, std::move(r_opened_database));
});
TdDb::open(get_database_scheduler_id(), parameters_, as_db_key(std::move(key)), std::move(promise));
}
void Td::init(Result<TdDb::OpenedDatabase> r_opened_database) {
CHECK(init_request_id_ != 0);
CHECK(set_parameters_request_id_ != 0);
if (r_opened_database.is_error()) {
LOG(WARNING) << "Failed to open database: " << r_opened_database.error();
send_closure(actor_id(this), &Td::send_error, init_request_id_,
send_closure(actor_id(this), &Td::send_error, set_parameters_request_id_,
Status::Error(400, r_opened_database.error().message()));
return finish_init();
return finish_set_parameters();
}
LOG(INFO) << "Successfully inited database in " << tag("database_directory", parameters_.database_directory)
@ -3776,24 +3725,8 @@ void Td::init(Result<TdDb::OpenedDatabase> r_opened_database) {
state_ = State::Run;
send_closure(actor_id(this), &Td::send_result, init_request_id_, td_api::make_object<td_api::ok>());
return finish_init();
}
void Td::finish_init() {
CHECK(init_request_id_ > 0);
init_request_id_ = 0;
if (pending_init_requests_.empty()) {
return;
}
VLOG(td_init) << "Continue to execute " << pending_init_requests_.size() << " pending requests";
auto requests = std::move(pending_init_requests_);
for (auto &request : requests) {
run_request(request.first, std::move(request.second));
}
CHECK(pending_init_requests_.size() < requests.size());
send_closure(actor_id(this), &Td::send_result, set_parameters_request_id_, td_api::make_object<td_api::ok>());
return finish_set_parameters();
}
void Td::init_options_and_network() {
@ -4238,6 +4171,8 @@ Status Td::set_parameters(td_api::object_ptr<td_api::tdlibParameters> parameters
options_.is_emulator = false;
options_.proxy = Proxy();
database_encryption_key_ = std::move(parameters->database_encryption_key_);
return Status::OK();
}
@ -4245,10 +4180,6 @@ void Td::on_request(uint64 id, const td_api::setTdlibParameters &request) {
send_error_raw(id, 400, "Unexpected setTdlibParameters");
}
void Td::on_request(uint64 id, const td_api::checkDatabaseEncryptionKey &request) {
send_error_raw(id, 400, "Unexpected checkDatabaseEncryptionKey");
}
void Td::on_request(uint64 id, td_api::setDatabaseEncryptionKey &request) {
CREATE_OK_REQUEST_PROMISE();
G()->td_db()->get_binlog()->change_key(as_db_key(std::move(request.new_encryption_key_)), std::move(promise));

View File

@ -291,9 +291,9 @@ class Td final : public Actor {
bool destroy_flag_ = false;
int close_flag_ = 0;
enum class State : int32 { WaitParameters, Decrypt, Run, Close } state_ = State::WaitParameters;
uint64 init_request_id_ = 0;
enum class State : int32 { WaitParameters, Run, Close } state_ = State::WaitParameters;
uint64 set_parameters_request_id_ = 0;
string database_encryption_key_;
bool is_database_encrypted_ = false;
FlatHashMap<uint64, std::shared_ptr<ResultHandler>> result_handlers_;
@ -390,10 +390,6 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::setTdlibParameters &request);
void on_request(uint64 id, const td_api::checkDatabaseEncryptionKey &request);
void on_request(uint64 id, td_api::setDatabaseEncryptionKey &request);
void on_request(uint64 id, const td_api::getAuthorizationState &request);
void on_request(uint64 id, td_api::setAuthenticationPhoneNumber &request);
@ -428,6 +424,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::confirmQrCodeAuthentication &request);
void on_request(uint64 id, td_api::setDatabaseEncryptionKey &request);
void on_request(uint64 id, const td_api::getCurrentState &request);
void on_request(uint64 id, td_api::getPasswordState &request);
@ -1470,8 +1468,6 @@ class Td final : public Actor {
void finish_set_parameters();
void start_init(uint64 id, string &&key);
void init(Result<TdDb::OpenedDatabase> r_opened_database);
void init_options_and_network();
@ -1482,8 +1478,6 @@ class Td final : public Actor {
void init_managers();
void finish_init();
void clear();
void close_impl(bool destroy_flag);

View File

@ -415,9 +415,6 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::setTdlibParameters>(std::move(parameters)));
break;
}
case td_api::authorizationStateWaitEncryptionKey::ID:
send_request(td_api::make_object<td_api::checkDatabaseEncryptionKey>());
break;
case td_api::authorizationStateReady::ID:
LOG(INFO) << "Logged in";
break;
@ -1857,9 +1854,7 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::setAuthenticationEmailAddress>(args));
} else if (op == "rac") {
send_request(td_api::make_object<td_api::resendAuthenticationCode>());
} else if (op == "cdek" || op == "CheckDatabaseEncryptionKey") {
send_request(td_api::make_object<td_api::checkDatabaseEncryptionKey>(args));
} else if (op == "sdek" || op == "SetDatabaseEncryptionKey") {
} else if (op == "sdek") {
send_request(td_api::make_object<td_api::setDatabaseEncryptionKey>(args));
} else if (op == "caec") {
send_request(td_api::make_object<td_api::checkAuthenticationEmailCode>(as_email_address_authentication(args)));

View File

@ -230,9 +230,6 @@ class InitTask : public Task {
start_flag_ = true;
td::tl_object_ptr<td::td_api::Function> function;
switch (authorization_state->get_id()) {
case td::td_api::authorizationStateWaitEncryptionKey::ID:
send(td::make_tl_object<td::td_api::checkDatabaseEncryptionKey>());
break;
case td::td_api::authorizationStateReady::ID:
promise_.set_value({});
stop();

View File

@ -211,9 +211,6 @@ class DoAuthentication final : public TestClinetTask {
start_flag_ = true;
td::tl_object_ptr<td::td_api::Function> function;
switch (authorization_state->get_id()) {
case td::td_api::authorizationStateWaitEncryptionKey::ID:
function = td::make_tl_object<td::td_api::checkDatabaseEncryptionKey>();
break;
case td::td_api::authorizationStateWaitPhoneNumber::ID:
function = td::make_tl_object<td::td_api::setAuthenticationPhoneNumber>(phone_, nullptr);
break;