From 056569f497ac4202c8f8131a6a5ba477c987b488 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 19 Apr 2018 17:31:25 +0300 Subject: [PATCH] Add deletePaasportData implementation. GitOrigin-RevId: 5e5efcd544a7d9cd57abb7a78cd33a4d5c6722f0 --- td/telegram/SecureManager.cpp | 145 ++++++++++++++++++++++++++++++++-- td/telegram/SecureManager.h | 91 +-------------------- td/telegram/SecureValue.cpp | 4 +- td/telegram/SecureValue.h | 2 +- 4 files changed, 143 insertions(+), 99 deletions(-) diff --git a/td/telegram/SecureManager.cpp b/td/telegram/SecureManager.cpp index 827ea361..a83445f0 100644 --- a/td/telegram/SecureManager.cpp +++ b/td/telegram/SecureManager.cpp @@ -23,6 +23,96 @@ namespace td { +class GetSecureValue : public NetQueryCallback { + public: + GetSecureValue(ActorShared<> parent, std::string password, SecureValueType type, + Promise promise); + + private: + ActorShared<> parent_; + string password_; + SecureValueType type_; + Promise promise_; + optional encrypted_secure_value_; + optional secret_; + + void on_error(Status status); + void on_secret(Result r_secret, bool dummy); + void loop() override; + void start_up() override; + + void on_result(NetQueryPtr query) override; +}; + +class GetAllSecureValues : public NetQueryCallback { + public: + GetAllSecureValues(ActorShared<> parent, std::string password, Promise promise); + + private: + ActorShared<> parent_; + string password_; + Promise promise_; + optional> encrypted_secure_values_; + optional secret_; + + void on_error(Status status); + void on_secret(Result r_secret, bool dummy); + void loop() override; + void start_up() override; + + void on_result(NetQueryPtr query) override; +}; + +class SetSecureValue : public NetQueryCallback { + public: + SetSecureValue(ActorShared<> parent, string password, SecureValue secure_value, + Promise promise); + + private: + ActorShared<> parent_; + string password_; + SecureValue secure_value_; + Promise promise_; + optional secret_; + + size_t files_left_to_upload_ = 0; + vector to_upload_; + optional selfie_; + + class UploadCallback; + std::shared_ptr upload_callback_; + + enum class State : int32 { WaitSecret, WaitSetValue } state_ = State::WaitSecret; + + class UploadCallback : public FileManager::UploadCallback { + public: + explicit UploadCallback(ActorId actor_id); + + private: + ActorId actor_id_; + void on_upload_ok(FileId file_id, tl_object_ptr input_file) override; + void on_upload_encrypted_ok(FileId file_id, tl_object_ptr input_file) override; + void on_upload_secure_ok(FileId file_id, tl_object_ptr input_file) override; + void on_upload_error(FileId file_id, Status error) override; + }; + + void on_upload_ok(FileId file_id, tl_object_ptr input_file); + void on_upload_error(FileId file_id, Status error); + + void on_error(Status status); + + void on_secret(Result r_secret, bool x); + + void start_up() override; + void tear_down() override; + + void loop() override; + void on_result(NetQueryPtr query) override; + + void start_upload(FileManager *file_manager, FileId file_id, SecureInputFile &info); + void merge(FileManager *file_manager, FileId file_id, EncryptedSecureFile &encrypted_file); +}; + class SetSecureValueErrorsQuery : public Td::ResultHandler { Promise promise_; @@ -87,10 +177,10 @@ void GetSecureValue::loop() { } void GetSecureValue::start_up() { - std::vector> vec; - vec.push_back(get_secure_value_type_object(type_)); + std::vector> types; + types.push_back(get_input_secure_value_type(type_)); - auto query = G()->net_query_creator().create(create_storer(telegram_api::account_getSecureValue(std::move(vec)))); + auto query = G()->net_query_creator().create(create_storer(telegram_api::account_getSecureValue(std::move(types)))); G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this)); @@ -349,6 +439,36 @@ void SetSecureValue::merge(FileManager *file_manager, FileId file_id, EncryptedS LOG_IF(ERROR, status.is_error()) << status.error(); } +class DeleteSecureValue : public NetQueryCallback { + public: + DeleteSecureValue(ActorShared<> parent, SecureValueType type, Promise promise) + : parent_(std::move(parent)), type_(std::move(type)), promise_(std::move(promise)) { + } + + private: + ActorShared<> parent_; + SecureValueType type_; + Promise promise_; + + void start_up() override { + std::vector> types; + types.push_back(get_input_secure_value_type(type_)); + auto query = + G()->net_query_creator().create(create_storer(telegram_api::account_deleteSecureValue(std::move(types)))); + G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this)); + } + + void on_result(NetQueryPtr query) override { + auto r_result = fetch_result(std::move(query)); + if (r_result.is_error()) { + promise_.set_error(r_result.move_as_error()); + } else { + promise_.set_value(Unit()); + } + stop(); + } +}; + class GetPassportAuthorizationForm : public NetQueryCallback { public: GetPassportAuthorizationForm(ActorShared<> parent, string password, int32 authorization_form_id, UserId bot_user_id, @@ -561,7 +681,20 @@ void SecureManager::set_secure_value(string password, SecureValue secure_value, } void SecureManager::delete_secure_value(SecureValueType type, Promise promise) { - // TODO + refcnt_++; + auto new_promise = PromiseCreator::lambda( + [actor_id = actor_id(this), type, promise = std::move(promise)](Result result) mutable { + send_closure(actor_id, &SecureManager::on_delete_secure_value, type, std::move(promise), std::move(result)); + }); + create_actor("DeleteSecureValue", actor_shared(), type, std::move(new_promise)).release(); +} + +void SecureManager::on_delete_secure_value(SecureValueType type, Promise promise, Result result) { + if (result.is_error()) { + return promise.set_error(result.move_as_error()); + } + + promise.set_value(Unit()); } void SecureManager::set_secure_value_errors(Td *td, tl_object_ptr input_user, @@ -584,7 +717,7 @@ void SecureManager::set_secure_value_errors(Td *td, tl_object_ptrtype_)); + auto type = get_input_secure_value_type(get_secure_value_type_td_api(error->type_)); switch (error->source_->get_id()) { case td_api::inputPassportDataErrorSourceDataField::ID: { auto source = td_api::move_object_as(error->source_); @@ -729,7 +862,7 @@ void SecureManager::do_send_passport_authorization_form(int32 authorization_form } std::vector> hashes; for (auto &c : credentials) { - hashes.push_back(telegram_api::make_object(get_secure_value_type_object(c.type), + hashes.push_back(telegram_api::make_object(get_input_secure_value_type(c.type), BufferSlice(c.hash))); } diff --git a/td/telegram/SecureManager.h b/td/telegram/SecureManager.h index 02168295..628ba602 100644 --- a/td/telegram/SecureManager.h +++ b/td/telegram/SecureManager.h @@ -32,96 +32,6 @@ using TdApiSecureValue = td_api::object_ptr; using TdApiAllSecureValues = td_api::object_ptr; using TdApiAuthorizationForm = td_api::object_ptr; -class GetSecureValue : public NetQueryCallback { - public: - GetSecureValue(ActorShared<> parent, std::string password, SecureValueType type, - Promise promise); - - private: - ActorShared<> parent_; - string password_; - SecureValueType type_; - Promise promise_; - optional encrypted_secure_value_; - optional secret_; - - void on_error(Status status); - void on_secret(Result r_secret, bool dummy); - void loop() override; - void start_up() override; - - void on_result(NetQueryPtr query) override; -}; - -class GetAllSecureValues : public NetQueryCallback { - public: - GetAllSecureValues(ActorShared<> parent, std::string password, Promise promise); - - private: - ActorShared<> parent_; - string password_; - Promise promise_; - optional> encrypted_secure_values_; - optional secret_; - - void on_error(Status status); - void on_secret(Result r_secret, bool dummy); - void loop() override; - void start_up() override; - - void on_result(NetQueryPtr query) override; -}; - -class SetSecureValue : public NetQueryCallback { - public: - SetSecureValue(ActorShared<> parent, string password, SecureValue secure_value, - Promise promise); - - private: - ActorShared<> parent_; - string password_; - SecureValue secure_value_; - Promise promise_; - optional secret_; - - size_t files_left_to_upload_ = 0; - vector to_upload_; - optional selfie_; - - class UploadCallback; - std::shared_ptr upload_callback_; - - enum class State : int32 { WaitSecret, WaitSetValue } state_ = State::WaitSecret; - - class UploadCallback : public FileManager::UploadCallback { - public: - explicit UploadCallback(ActorId actor_id); - - private: - ActorId actor_id_; - void on_upload_ok(FileId file_id, tl_object_ptr input_file) override; - void on_upload_encrypted_ok(FileId file_id, tl_object_ptr input_file) override; - void on_upload_secure_ok(FileId file_id, tl_object_ptr input_file) override; - void on_upload_error(FileId file_id, Status error) override; - }; - - void on_upload_ok(FileId file_id, tl_object_ptr input_file); - void on_upload_error(FileId file_id, Status error); - - void on_error(Status status); - - void on_secret(Result r_secret, bool x); - - void start_up() override; - void tear_down() override; - - void loop() override; - void on_result(NetQueryPtr query) override; - - void start_upload(FileManager *file_manager, FileId file_id, SecureInputFile &info); - void merge(FileManager *file_manager, FileId file_id, EncryptedSecureFile &encrypted_file); -}; - class SecureManager : public NetQueryCallback { public: explicit SecureManager(ActorShared<> parent); @@ -159,6 +69,7 @@ class SecureManager : public NetQueryCallback { void hangup_shared() override; void dec_refcnt(); void do_get_secure_value(std::string password, SecureValueType type, Promise promise); + void on_delete_secure_value(SecureValueType type, Promise promise, Result result); void on_get_passport_authorization_form(int32 authorization_form_id, Promise promise, Result r_authorization_form); void do_send_passport_authorization_form(int32 authorization_form_id, vector credentials, diff --git a/td/telegram/SecureValue.cpp b/td/telegram/SecureValue.cpp index d0d0b58e..44dad72a 100644 --- a/td/telegram/SecureValue.cpp +++ b/td/telegram/SecureValue.cpp @@ -167,7 +167,7 @@ td_api::object_ptr get_passport_data_type_object(Secur } } -td_api::object_ptr get_secure_value_type_object(SecureValueType type) { +td_api::object_ptr get_input_secure_value_type(SecureValueType type) { switch (type) { case SecureValueType::PersonalDetails: return telegram_api::make_object(); @@ -446,7 +446,7 @@ telegram_api::object_ptr get_input_secure_value_ CHECK(selfie); } return telegram_api::make_object( - flags, get_secure_value_type_object(value.type), is_plain ? nullptr : get_secure_data_object(value.data), + flags, get_input_secure_value_type(value.type), is_plain ? nullptr : get_secure_data_object(value.data), get_input_secure_files_object(file_manager, value.files, input_files), std::move(plain_data), has_selfie ? get_input_secure_file_object(file_manager, value.selfie, *selfie) : nullptr); } diff --git a/td/telegram/SecureValue.h b/td/telegram/SecureValue.h index ece0a706..3459b9ae 100644 --- a/td/telegram/SecureValue.h +++ b/td/telegram/SecureValue.h @@ -48,7 +48,7 @@ vector get_secure_value_types_td_api( const vector> &secure_value_types); td_api::object_ptr get_passport_data_type_object(SecureValueType type); -td_api::object_ptr get_secure_value_type_object(SecureValueType type); +td_api::object_ptr get_input_secure_value_type(SecureValueType type); vector> get_passport_data_types_object( const vector &types);