From 9d895eadbeccc7d04b7342e572f203a8537a3ba6 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 12 Aug 2018 17:45:30 +0300 Subject: [PATCH] Again send credentials only for requested elements. GitOrigin-RevId: afa4ed9c7a9e72f0afbe208088bcf71417a64d4d --- td/telegram/SecureManager.cpp | 57 +++++++++++++++++++++++------------ td/telegram/SecureManager.h | 7 +++-- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/td/telegram/SecureManager.cpp b/td/telegram/SecureManager.cpp index 63720b5e..641dcc17 100644 --- a/td/telegram/SecureManager.cpp +++ b/td/telegram/SecureManager.cpp @@ -619,9 +619,10 @@ class DeleteSecureValue : public NetQueryCallback { class GetPassportAuthorizationForm : public NetQueryCallback { public: - GetPassportAuthorizationForm(ActorShared parent, string password, int32 authorization_form_id, - UserId bot_user_id, string scope, string public_key, - Promise promise) + GetPassportAuthorizationForm( + ActorShared parent, string password, int32 authorization_form_id, UserId bot_user_id, string scope, + string public_key, + Promise, TdApiAuthorizationForm>> promise) : parent_(std::move(parent)) , password_(std::move(password)) , authorization_form_id_(authorization_form_id) @@ -638,7 +639,7 @@ class GetPassportAuthorizationForm : public NetQueryCallback { UserId bot_user_id_; string scope_; string public_key_; - Promise promise_; + Promise, TdApiAuthorizationForm>> promise_; optional secret_; telegram_api::object_ptr authorization_form_; @@ -693,14 +694,14 @@ class GetPassportAuthorizationForm : public NetQueryCallback { auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get(); vector> required_types; - vector all_types; + std::unordered_map all_types; for (auto &type_ptr : authorization_form_->required_types_) { CHECK(type_ptr != nullptr); vector required_type; switch (type_ptr->get_id()) { case telegram_api::secureRequiredType::ID: { auto value = get_suitable_secure_value(move_tl_object_as(type_ptr)); - all_types.push_back(value.type); + all_types.emplace(value.type, value); required_type.push_back(std::move(value)); break; } @@ -709,7 +710,7 @@ class GetPassportAuthorizationForm : public NetQueryCallback { for (auto &type : type_one_of->types_) { if (type->get_id() == telegram_api::secureRequiredType::ID) { auto value = get_suitable_secure_value(move_tl_object_as(type)); - all_types.push_back(value.type); + all_types.emplace(value.type, value); required_type.push_back(std::move(value)); } else { LOG(ERROR) << to_string(type); @@ -721,10 +722,10 @@ class GetPassportAuthorizationForm : public NetQueryCallback { UNREACHABLE(); } } - all_types = unique_secure_value_types(std::move(all_types)); std::vector values; - for (auto type : all_types) { + for (auto suitable_type : all_types) { + auto type = suitable_type.first; for (auto &value : authorization_form_->values_) { if (value == nullptr) { continue; @@ -841,9 +842,11 @@ class GetPassportAuthorizationForm : public NetQueryCallback { message, std::move(source))); } - promise_.set_value(make_tl_object( + auto authorization_form = make_tl_object( authorization_form_id_, get_passport_required_elements_object(required_types), std::move(values), - std::move(errors), authorization_form_->privacy_policy_url_)); + std::move(errors), authorization_form_->privacy_policy_url_); + + promise_.set_value({std::move(all_types), std::move(authorization_form)}); stop(); } }; @@ -1026,9 +1029,10 @@ void SecureManager::get_passport_authorization_form(string password, UserId bot_ refcnt_++; auto authorization_form_id = ++max_authorization_form_id_; authorization_forms_[authorization_form_id] = AuthorizationForm{bot_user_id, scope, public_key, payload, false}; - auto new_promise = - PromiseCreator::lambda([actor_id = actor_id(this), authorization_form_id, promise = std::move(promise)]( - Result r_authorization_form) mutable { + auto new_promise = PromiseCreator::lambda( + [actor_id = actor_id(this), authorization_form_id, promise = std::move(promise)]( + Result, TdApiAuthorizationForm>> + r_authorization_form) mutable { send_closure(actor_id, &SecureManager::on_get_passport_authorization_form, authorization_form_id, std::move(promise), std::move(r_authorization_form)); }); @@ -1038,9 +1042,10 @@ void SecureManager::get_passport_authorization_form(string password, UserId bot_ .release(); } -void SecureManager::on_get_passport_authorization_form(int32 authorization_form_id, - Promise promise, - Result r_authorization_form) { +void SecureManager::on_get_passport_authorization_form( + int32 authorization_form_id, Promise promise, + Result, TdApiAuthorizationForm>> + r_authorization_form) { auto it = authorization_forms_.find(authorization_form_id); CHECK(it != authorization_forms_.end()); CHECK(it->second.is_received == false); @@ -1048,11 +1053,12 @@ void SecureManager::on_get_passport_authorization_form(int32 authorization_form_ authorization_forms_.erase(it); return promise.set_error(r_authorization_form.move_as_error()); } - it->second.is_received = true; auto authorization_form = r_authorization_form.move_as_ok(); - CHECK(authorization_form != nullptr); - promise.set_value(std::move(authorization_form)); + it->second.options = std::move(authorization_form.first); + it->second.is_received = true; + CHECK(authorization_form.second != nullptr); + promise.set_value(std::move(authorization_form.second)); } void SecureManager::send_passport_authorization_form(int32 authorization_form_id, std::vector types, @@ -1082,6 +1088,17 @@ void SecureManager::send_passport_authorization_form(int32 authorization_form_id for (auto &c : credentials) { hashes.push_back(telegram_api::make_object(get_input_secure_value_type(c.type), BufferSlice(c.hash))); + auto options_it = it->second.options.find(c.type); + if (options_it == it->second.options.end()) { + return promise.set_error(Status::Error(400, "Passport Element with the specified type was not requested")); + } + auto &options = options_it->second; + if (!options.is_selfie_required) { + c.selfie = optional(); + } + if (!options.is_translation_required) { + c.translations.clear(); + } } auto r_encrypted_credentials = get_encrypted_credentials(credentials, it->second.payload, it->second.public_key); diff --git a/td/telegram/SecureManager.h b/td/telegram/SecureManager.h index e0296467..01482e8f 100644 --- a/td/telegram/SecureManager.h +++ b/td/telegram/SecureManager.h @@ -60,6 +60,7 @@ class SecureManager : public NetQueryCallback { string public_key; string payload; bool is_received; + std::unordered_map options; }; std::unordered_map authorization_forms_; @@ -69,8 +70,10 @@ class SecureManager : public NetQueryCallback { void hangup_shared() override; void dec_refcnt(); 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 on_get_passport_authorization_form( + int32 authorization_form_id, Promise promise, + Result, TdApiAuthorizationForm>> + r_authorization_form); void on_result(NetQueryPtr query) override; Container> container_;