2018-04-05 13:18:47 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-04-05 13:18:47 +02: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/SecureManager.h"
|
|
|
|
|
2018-04-11 19:42:06 +02:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2018-04-28 10:56:10 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
2018-04-28 20:05:04 +02:00
|
|
|
#include "td/telegram/files/FileId.h"
|
2018-04-07 00:29:36 +02:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2018-04-05 13:18:47 +02:00
|
|
|
#include "td/telegram/Global.h"
|
2018-04-17 17:04:09 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2018-04-05 13:18:47 +02:00
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2018-04-07 00:29:36 +02:00
|
|
|
#include "td/telegram/PasswordManager.h"
|
|
|
|
#include "td/telegram/Td.h"
|
2018-04-05 13:18:47 +02:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-04-07 01:38:28 +02:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2018-04-28 10:56:10 +02:00
|
|
|
#include "td/utils/optional.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-04-07 01:38:28 +02:00
|
|
|
|
2022-02-09 22:59:52 +01:00
|
|
|
#include <limits>
|
2018-09-27 15:37:15 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
namespace td {
|
2018-04-07 00:29:36 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetSecureValue final : public NetQueryCallback {
|
2018-04-19 16:31:25 +02:00
|
|
|
public:
|
2018-08-12 14:44:24 +02:00
|
|
|
GetSecureValue(ActorShared<SecureManager> parent, std::string password, SecureValueType type,
|
2018-04-19 16:31:25 +02:00
|
|
|
Promise<SecureValueWithCredentials> promise);
|
|
|
|
|
|
|
|
private:
|
2018-08-12 14:44:24 +02:00
|
|
|
ActorShared<SecureManager> parent_;
|
2018-04-19 16:31:25 +02:00
|
|
|
string password_;
|
|
|
|
SecureValueType type_;
|
|
|
|
Promise<SecureValueWithCredentials> promise_;
|
|
|
|
optional<EncryptedSecureValue> encrypted_secure_value_;
|
|
|
|
optional<secure_storage::Secret> secret_;
|
|
|
|
|
2018-05-24 00:30:47 +02:00
|
|
|
void on_error(Status error);
|
2018-04-19 16:31:25 +02:00
|
|
|
void on_secret(Result<secure_storage::Secret> r_secret, bool dummy);
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
|
|
|
void start_up() final;
|
2018-04-19 16:31:25 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final;
|
2018-04-19 16:31:25 +02:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetAllSecureValues final : public NetQueryCallback {
|
2018-04-19 16:31:25 +02:00
|
|
|
public:
|
2018-08-12 14:44:24 +02:00
|
|
|
GetAllSecureValues(ActorShared<SecureManager> parent, std::string password, Promise<TdApiSecureValues> promise);
|
2018-04-19 16:31:25 +02:00
|
|
|
|
|
|
|
private:
|
2018-08-12 14:44:24 +02:00
|
|
|
ActorShared<SecureManager> parent_;
|
2018-04-19 16:31:25 +02:00
|
|
|
string password_;
|
2018-08-01 15:52:07 +02:00
|
|
|
Promise<TdApiSecureValues> promise_;
|
2018-04-19 16:31:25 +02:00
|
|
|
optional<vector<EncryptedSecureValue>> encrypted_secure_values_;
|
|
|
|
optional<secure_storage::Secret> secret_;
|
|
|
|
|
2018-05-24 00:30:47 +02:00
|
|
|
void on_error(Status error);
|
2018-04-19 16:31:25 +02:00
|
|
|
void on_secret(Result<secure_storage::Secret> r_secret, bool dummy);
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
|
|
|
void start_up() final;
|
2018-04-19 16:31:25 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final;
|
2018-04-19 16:31:25 +02:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SetSecureValue final : public NetQueryCallback {
|
2018-04-19 16:31:25 +02:00
|
|
|
public:
|
2018-08-12 14:44:24 +02:00
|
|
|
SetSecureValue(ActorShared<SecureManager> parent, string password, SecureValue secure_value,
|
2018-04-19 16:31:25 +02:00
|
|
|
Promise<SecureValueWithCredentials> promise);
|
|
|
|
|
|
|
|
private:
|
2018-08-12 14:44:24 +02:00
|
|
|
ActorShared<SecureManager> parent_;
|
2018-04-19 16:31:25 +02:00
|
|
|
string password_;
|
|
|
|
SecureValue secure_value_;
|
|
|
|
Promise<SecureValueWithCredentials> promise_;
|
|
|
|
optional<secure_storage::Secret> secret_;
|
|
|
|
|
|
|
|
size_t files_left_to_upload_ = 0;
|
2019-08-01 17:26:43 +02:00
|
|
|
uint32 upload_generation_{0};
|
2018-08-10 18:39:48 +02:00
|
|
|
vector<SecureInputFile> files_to_upload_;
|
|
|
|
vector<SecureInputFile> translations_to_upload_;
|
2018-04-27 14:38:02 +02:00
|
|
|
optional<SecureInputFile> front_side_;
|
|
|
|
optional<SecureInputFile> reverse_side_;
|
2018-04-19 16:31:25 +02:00
|
|
|
optional<SecureInputFile> selfie_;
|
|
|
|
|
|
|
|
class UploadCallback;
|
|
|
|
std::shared_ptr<UploadCallback> upload_callback_;
|
|
|
|
|
|
|
|
enum class State : int32 { WaitSecret, WaitSetValue } state_ = State::WaitSecret;
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class UploadCallback final : public FileManager::UploadCallback {
|
2018-04-19 16:31:25 +02:00
|
|
|
public:
|
2019-08-01 17:26:43 +02:00
|
|
|
UploadCallback(ActorId<SetSecureValue> actor_id, uint32 upload_generation);
|
2018-04-19 16:31:25 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<SetSecureValue> actor_id_;
|
2019-08-01 17:26:43 +02:00
|
|
|
uint32 upload_generation_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) final;
|
|
|
|
void on_upload_encrypted_ok(FileId file_id, tl_object_ptr<telegram_api::InputEncryptedFile> input_file) final;
|
|
|
|
void on_upload_secure_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file) final;
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_upload_error(FileId file_id, Status error) final;
|
2018-04-19 16:31:25 +02:00
|
|
|
};
|
|
|
|
|
2019-08-01 17:26:43 +02:00
|
|
|
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file, uint32 upload_generation);
|
2021-10-19 17:11:16 +02:00
|
|
|
void on_upload_error(FileId file_id, Status error, uint32 upload_generation);
|
2018-04-19 16:31:25 +02:00
|
|
|
|
2018-05-24 00:30:47 +02:00
|
|
|
void on_error(Status error);
|
2018-04-19 16:31:25 +02:00
|
|
|
|
|
|
|
void on_secret(Result<secure_storage::Secret> r_secret, bool x);
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void hangup() final;
|
|
|
|
void tear_down() final;
|
2018-04-19 16:31:25 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
|
|
|
void on_result(NetQueryPtr query) final;
|
2018-04-19 16:31:25 +02:00
|
|
|
|
2019-08-02 11:01:55 +02:00
|
|
|
void load_secret();
|
2019-08-01 17:26:43 +02:00
|
|
|
void cancel_upload();
|
|
|
|
void start_upload_all();
|
2018-04-26 22:41:34 +02:00
|
|
|
void start_upload(FileManager *file_manager, FileId &file_id, SecureInputFile &info);
|
2021-10-19 17:11:16 +02:00
|
|
|
static void merge(FileManager *file_manager, FileId file_id, EncryptedSecureFile &encrypted_file);
|
2018-04-19 16:31:25 +02:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SetSecureValueErrorsQuery final : public Td::ResultHandler {
|
2018-04-17 17:04:09 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SetSecureValueErrorsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(tl_object_ptr<telegram_api::InputUser> input_user,
|
|
|
|
vector<tl_object_ptr<telegram_api::SecureValueError>> input_errors) {
|
|
|
|
send_query(G()->net_query_creator().create(
|
2020-03-15 22:17:11 +01:00
|
|
|
telegram_api::users_setSecureValueErrors(std::move(input_user), std::move(input_errors))));
|
2018-04-17 17:04:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-04-18 16:28:48 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::users_setSecureValueErrors>(packet);
|
2018-04-17 17:04:09 +02:00
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-04-17 17:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ptr = result_ptr.move_as_ok();
|
2020-09-24 15:08:04 +02:00
|
|
|
LOG(DEBUG) << "Receive result for SetSecureValueErrorsQuery: " << ptr;
|
2018-04-17 17:04:09 +02:00
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-04-23 23:01:05 +02:00
|
|
|
if (status.code() != 0) {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
} else {
|
|
|
|
promise_.set_error(Status::Error(400, status.message()));
|
|
|
|
}
|
2018-04-17 17:04:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-12 14:44:24 +02:00
|
|
|
GetSecureValue::GetSecureValue(ActorShared<SecureManager> parent, std::string password, SecureValueType type,
|
2018-04-06 21:37:30 +02:00
|
|
|
Promise<SecureValueWithCredentials> promise)
|
2018-04-05 13:18:47 +02:00
|
|
|
: parent_(std::move(parent)), password_(std::move(password)), type_(type), promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2018-05-24 00:30:47 +02:00
|
|
|
void GetSecureValue::on_error(Status error) {
|
2019-08-02 11:01:55 +02:00
|
|
|
if (error.message() == "SECURE_SECRET_REQUIRED") {
|
|
|
|
send_closure(G()->password_manager(), &PasswordManager::drop_cached_secret);
|
|
|
|
}
|
2018-09-25 04:12:41 +02:00
|
|
|
if (error.code() > 0) {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(std::move(error));
|
2018-04-23 23:01:05 +02:00
|
|
|
} else {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(Status::Error(400, error.message()));
|
2018-04-23 23:01:05 +02:00
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetSecureValue::on_secret(Result<secure_storage::Secret> r_secret, bool dummy) {
|
|
|
|
if (r_secret.is_error()) {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(r_secret.error())) {
|
2018-04-19 17:39:30 +02:00
|
|
|
LOG(ERROR) << "Receive error instead of secret: " << r_secret.error();
|
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
return on_error(r_secret.move_as_error());
|
|
|
|
}
|
|
|
|
secret_ = r_secret.move_as_ok();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetSecureValue::loop() {
|
|
|
|
if (!encrypted_secure_value_ || !secret_) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
|
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-04-19 15:43:09 +02:00
|
|
|
auto r_secure_value = decrypt_secure_value(file_manager, *secret_, *encrypted_secure_value_);
|
2018-04-05 13:18:47 +02:00
|
|
|
if (r_secure_value.is_error()) {
|
|
|
|
return on_error(r_secure_value.move_as_error());
|
|
|
|
}
|
2018-08-12 14:44:24 +02:00
|
|
|
|
|
|
|
send_closure(parent_, &SecureManager::on_get_secure_value, r_secure_value.ok());
|
|
|
|
|
|
|
|
promise_.set_value(r_secure_value.move_as_ok());
|
2018-04-05 13:18:47 +02:00
|
|
|
stop();
|
|
|
|
}
|
2018-04-05 15:06:03 +02:00
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void GetSecureValue::start_up() {
|
2018-04-19 16:31:25 +02:00
|
|
|
std::vector<telegram_api::object_ptr<telegram_api::SecureValueType>> types;
|
|
|
|
types.push_back(get_input_secure_value_type(type_));
|
2018-04-05 13:18:47 +02:00
|
|
|
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(telegram_api::account_getSecureValue(std::move(types)));
|
2018-04-05 13:18:47 +02:00
|
|
|
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this));
|
|
|
|
|
2018-08-14 01:49:32 +02:00
|
|
|
send_closure(G()->password_manager(), &PasswordManager::get_secure_secret, password_,
|
2018-04-05 13:18:47 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<secure_storage::Secret> r_secret) {
|
|
|
|
send_closure(actor_id, &GetSecureValue::on_secret, std::move(r_secret), true);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetSecureValue::on_result(NetQueryPtr query) {
|
|
|
|
auto r_result = fetch_result<telegram_api::account_getSecureValue>(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
return on_error(r_result.move_as_error());
|
|
|
|
}
|
|
|
|
auto result = r_result.move_as_ok();
|
2018-04-07 00:29:36 +02:00
|
|
|
if (result.empty()) {
|
|
|
|
return on_error(Status::Error(404, "Not Found"));
|
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
if (result.size() != 1) {
|
|
|
|
return on_error(Status::Error(PSLICE() << "Expected vector of size 1 got " << result.size()));
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
encrypted_secure_value_ =
|
|
|
|
get_encrypted_secure_value(G()->td().get_actor_unsafe()->file_manager_.get(), std::move(result[0]));
|
2018-04-27 14:38:02 +02:00
|
|
|
if (encrypted_secure_value_.value().type == SecureValueType::None) {
|
|
|
|
return on_error(Status::Error(404, "Not Found"));
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:44:24 +02:00
|
|
|
GetAllSecureValues::GetAllSecureValues(ActorShared<SecureManager> parent, std::string password,
|
|
|
|
Promise<TdApiSecureValues> promise)
|
2018-04-07 00:29:36 +02:00
|
|
|
: parent_(std::move(parent)), password_(std::move(password)), promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2018-05-24 00:30:47 +02:00
|
|
|
void GetAllSecureValues::on_error(Status error) {
|
2019-08-02 11:01:55 +02:00
|
|
|
if (error.message() == "SECURE_SECRET_REQUIRED") {
|
|
|
|
send_closure(G()->password_manager(), &PasswordManager::drop_cached_secret);
|
|
|
|
}
|
2018-09-25 04:12:41 +02:00
|
|
|
if (error.code() > 0) {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(std::move(error));
|
2018-04-23 23:01:05 +02:00
|
|
|
} else {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(Status::Error(400, error.message()));
|
2018-04-23 23:01:05 +02:00
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllSecureValues::on_secret(Result<secure_storage::Secret> r_secret, bool dummy) {
|
|
|
|
if (r_secret.is_error()) {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(r_secret.error())) {
|
2018-04-19 17:39:30 +02:00
|
|
|
LOG(ERROR) << "Receive error instead of secret: " << r_secret.error();
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
return on_error(r_secret.move_as_error());
|
|
|
|
}
|
|
|
|
secret_ = r_secret.move_as_ok();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllSecureValues::loop() {
|
|
|
|
if (!encrypted_secure_values_ || !secret_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-04-19 15:43:09 +02:00
|
|
|
auto r_secure_values = decrypt_secure_values(file_manager, *secret_, *encrypted_secure_values_);
|
2018-04-07 00:29:36 +02:00
|
|
|
if (r_secure_values.is_error()) {
|
|
|
|
return on_error(r_secure_values.move_as_error());
|
|
|
|
}
|
2018-08-12 14:44:24 +02:00
|
|
|
|
|
|
|
for (auto &secure_value : r_secure_values.ok()) {
|
|
|
|
send_closure(parent_, &SecureManager::on_get_secure_value, secure_value);
|
|
|
|
}
|
|
|
|
|
2018-04-07 00:29:36 +02:00
|
|
|
auto secure_values = transform(r_secure_values.move_as_ok(),
|
|
|
|
[](SecureValueWithCredentials &&value) { return std::move(value.value); });
|
2021-10-19 17:11:16 +02:00
|
|
|
promise_.set_value(get_passport_elements_object(file_manager, secure_values));
|
2018-04-07 00:29:36 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllSecureValues::start_up() {
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(telegram_api::account_getAllSecureValues());
|
2018-04-07 00:29:36 +02:00
|
|
|
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this));
|
|
|
|
|
2018-08-14 01:49:32 +02:00
|
|
|
send_closure(G()->password_manager(), &PasswordManager::get_secure_secret, password_,
|
2018-04-07 00:29:36 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<secure_storage::Secret> r_secret) {
|
|
|
|
send_closure(actor_id, &GetAllSecureValues::on_secret, std::move(r_secret), true);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllSecureValues::on_result(NetQueryPtr query) {
|
|
|
|
auto r_result = fetch_result<telegram_api::account_getAllSecureValues>(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
return on_error(r_result.move_as_error());
|
|
|
|
}
|
|
|
|
encrypted_secure_values_ =
|
|
|
|
get_encrypted_secure_values(G()->td().get_actor_unsafe()->file_manager_.get(), r_result.move_as_ok());
|
2018-04-05 13:18:47 +02:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:44:24 +02:00
|
|
|
SetSecureValue::SetSecureValue(ActorShared<SecureManager> parent, string password, SecureValue secure_value,
|
2018-04-06 21:37:30 +02:00
|
|
|
Promise<SecureValueWithCredentials> promise)
|
2018-04-05 13:18:47 +02:00
|
|
|
: parent_(std::move(parent))
|
|
|
|
, password_(std::move(password))
|
|
|
|
, secure_value_(std::move(secure_value))
|
|
|
|
, promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-08-01 17:26:43 +02:00
|
|
|
SetSecureValue::UploadCallback::UploadCallback(ActorId<SetSecureValue> actor_id, uint32 upload_generation)
|
|
|
|
: actor_id_(actor_id), upload_generation_(upload_generation) {
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetSecureValue::UploadCallback::on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) {
|
2018-04-24 19:10:12 +02:00
|
|
|
CHECK(input_file == nullptr);
|
2019-08-01 17:26:43 +02:00
|
|
|
send_closure_later(actor_id_, &SetSecureValue::on_upload_ok, file_id, nullptr, upload_generation_);
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
2018-05-24 00:30:47 +02:00
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SetSecureValue::UploadCallback::on_upload_encrypted_ok(
|
|
|
|
FileId file_id, tl_object_ptr<telegram_api::InputEncryptedFile> input_file) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2018-05-24 00:30:47 +02:00
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SetSecureValue::UploadCallback::on_upload_secure_ok(FileId file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputSecureFile> input_file) {
|
2019-08-01 17:26:43 +02:00
|
|
|
send_closure_later(actor_id_, &SetSecureValue::on_upload_ok, file_id, std::move(input_file), upload_generation_);
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
2018-05-24 00:30:47 +02:00
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SetSecureValue::UploadCallback::on_upload_error(FileId file_id, Status error) {
|
2019-08-01 17:26:43 +02:00
|
|
|
send_closure_later(actor_id_, &SetSecureValue::on_upload_error, file_id, std::move(error), upload_generation_);
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 17:26:43 +02:00
|
|
|
void SetSecureValue::on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file,
|
|
|
|
uint32 upload_generation) {
|
|
|
|
if (upload_generation_ != upload_generation) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-05 15:06:03 +02:00
|
|
|
SecureInputFile *info_ptr = nullptr;
|
2018-08-10 18:39:48 +02:00
|
|
|
for (auto &info : files_to_upload_) {
|
|
|
|
if (info.file_id == file_id) {
|
|
|
|
info_ptr = &info;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &info : translations_to_upload_) {
|
2018-04-27 14:38:02 +02:00
|
|
|
if (info.file_id == file_id) {
|
|
|
|
info_ptr = &info;
|
|
|
|
break;
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
2018-04-27 14:38:02 +02:00
|
|
|
}
|
|
|
|
if (front_side_ && front_side_.value().file_id == file_id) {
|
|
|
|
info_ptr = &front_side_.value();
|
|
|
|
}
|
|
|
|
if (reverse_side_ && reverse_side_.value().file_id == file_id) {
|
|
|
|
info_ptr = &reverse_side_.value();
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
2018-04-05 15:06:03 +02:00
|
|
|
if (selfie_ && selfie_.value().file_id == file_id) {
|
|
|
|
info_ptr = &selfie_.value();
|
|
|
|
}
|
|
|
|
CHECK(info_ptr);
|
|
|
|
auto &info = *info_ptr;
|
|
|
|
CHECK(!info.input_file);
|
|
|
|
info.input_file = std::move(input_file);
|
|
|
|
CHECK(files_left_to_upload_ != 0);
|
|
|
|
files_left_to_upload_--;
|
2018-05-24 00:30:47 +02:00
|
|
|
loop();
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
2018-04-24 19:10:12 +02:00
|
|
|
|
2019-08-01 17:26:43 +02:00
|
|
|
void SetSecureValue::on_upload_error(FileId file_id, Status error, uint32 upload_generation) {
|
|
|
|
if (upload_generation_ != upload_generation) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-24 00:30:47 +02:00
|
|
|
on_error(std::move(error));
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 00:30:47 +02:00
|
|
|
void SetSecureValue::on_error(Status error) {
|
2018-09-25 04:12:41 +02:00
|
|
|
if (error.code() > 0) {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(std::move(error));
|
2018-04-23 23:01:05 +02:00
|
|
|
} else {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(Status::Error(400, error.message()));
|
2018-04-23 23:01:05 +02:00
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSecureValue::on_secret(Result<secure_storage::Secret> r_secret, bool x) {
|
|
|
|
if (r_secret.is_error()) {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(r_secret.error())) {
|
2018-04-19 17:39:30 +02:00
|
|
|
LOG(ERROR) << "Receive error instead of secret: " << r_secret.error();
|
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
return on_error(r_secret.move_as_error());
|
|
|
|
}
|
|
|
|
secret_ = r_secret.move_as_ok();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSecureValue::start_up() {
|
2019-08-02 11:01:55 +02:00
|
|
|
load_secret();
|
2018-04-07 01:38:28 +02:00
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-04-05 13:18:47 +02:00
|
|
|
|
2018-04-20 16:48:17 +02:00
|
|
|
// Remove duplicate files
|
2018-04-27 14:38:02 +02:00
|
|
|
FileId front_side_file_id;
|
|
|
|
if (secure_value_.front_side.file_id.is_valid()) {
|
2022-07-03 11:01:08 +02:00
|
|
|
front_side_file_id = file_manager->get_file_view(secure_value_.front_side.file_id).get_main_file_id();
|
2018-04-27 14:38:02 +02:00
|
|
|
front_side_ = SecureInputFile();
|
|
|
|
}
|
|
|
|
FileId reverse_side_file_id;
|
|
|
|
if (secure_value_.reverse_side.file_id.is_valid()) {
|
2022-07-03 11:01:08 +02:00
|
|
|
reverse_side_file_id = file_manager->get_file_view(secure_value_.reverse_side.file_id).get_main_file_id();
|
2018-04-27 14:38:02 +02:00
|
|
|
reverse_side_ = SecureInputFile();
|
|
|
|
if (front_side_file_id == reverse_side_file_id) {
|
|
|
|
return on_error(Status::Error(400, "Front side and reverse side must be different"));
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 16:48:17 +02:00
|
|
|
FileId selfie_file_id;
|
|
|
|
if (secure_value_.selfie.file_id.is_valid()) {
|
2022-07-03 11:01:08 +02:00
|
|
|
selfie_file_id = file_manager->get_file_view(secure_value_.selfie.file_id).get_main_file_id();
|
2018-04-24 19:10:12 +02:00
|
|
|
selfie_ = SecureInputFile();
|
2018-04-27 14:38:02 +02:00
|
|
|
if (front_side_file_id == selfie_file_id) {
|
|
|
|
return on_error(Status::Error(400, "Front side and selfie must be different"));
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
2018-04-27 14:38:02 +02:00
|
|
|
if (reverse_side_file_id == selfie_file_id) {
|
|
|
|
return on_error(Status::Error(400, "Reverse side and selfie must be different"));
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 18:39:48 +02:00
|
|
|
|
2018-04-27 14:38:02 +02:00
|
|
|
if (!secure_value_.files.empty()) {
|
|
|
|
CHECK(!front_side_file_id.is_valid());
|
|
|
|
CHECK(!reverse_side_file_id.is_valid());
|
|
|
|
CHECK(!selfie_file_id.is_valid());
|
|
|
|
for (auto it = secure_value_.files.begin(); it != secure_value_.files.end();) {
|
2022-07-03 11:01:08 +02:00
|
|
|
auto file_id = file_manager->get_file_view(it->file_id).get_main_file_id();
|
2018-04-27 14:38:02 +02:00
|
|
|
bool is_duplicate = false;
|
2021-08-04 02:43:48 +02:00
|
|
|
for (auto other_it = secure_value_.files.begin(); other_it != it; ++other_it) {
|
2022-07-03 11:01:08 +02:00
|
|
|
if (file_id == file_manager->get_file_view(other_it->file_id).get_main_file_id()) {
|
2018-04-27 14:38:02 +02:00
|
|
|
is_duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_duplicate) {
|
|
|
|
it = secure_value_.files.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 19:10:12 +02:00
|
|
|
}
|
2018-08-10 18:39:48 +02:00
|
|
|
if (!secure_value_.translations.empty()) {
|
|
|
|
for (auto it = secure_value_.translations.begin(); it != secure_value_.translations.end();) {
|
2022-07-03 11:01:08 +02:00
|
|
|
auto file_id = file_manager->get_file_view(it->file_id).get_main_file_id();
|
2018-08-10 18:39:48 +02:00
|
|
|
bool is_duplicate = file_id == front_side_file_id || file_id == reverse_side_file_id || file_id == selfie_file_id;
|
2021-08-04 02:43:48 +02:00
|
|
|
for (auto other_it = secure_value_.translations.begin(); other_it != it; ++other_it) {
|
2022-07-03 11:01:08 +02:00
|
|
|
if (file_id == file_manager->get_file_view(other_it->file_id).get_main_file_id()) {
|
2018-08-10 18:39:48 +02:00
|
|
|
is_duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 15:17:47 +02:00
|
|
|
for (auto &dated_file : secure_value_.files) {
|
2022-07-03 11:01:08 +02:00
|
|
|
if (file_id == file_manager->get_file_view(dated_file.file_id).get_main_file_id()) {
|
2018-08-14 15:17:47 +02:00
|
|
|
is_duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 18:39:48 +02:00
|
|
|
if (is_duplicate) {
|
|
|
|
it = secure_value_.translations.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
|
2019-08-01 17:26:43 +02:00
|
|
|
start_upload_all();
|
|
|
|
}
|
|
|
|
|
2019-08-02 11:01:55 +02:00
|
|
|
void SetSecureValue::load_secret() {
|
|
|
|
secret_ = {};
|
|
|
|
send_closure(G()->password_manager(), &PasswordManager::get_secure_secret, password_,
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<secure_storage::Secret> r_secret) {
|
|
|
|
send_closure(actor_id, &SetSecureValue::on_secret, std::move(r_secret), true);
|
|
|
|
}));
|
|
|
|
}
|
2022-08-09 16:00:36 +02:00
|
|
|
|
2019-08-01 17:26:43 +02:00
|
|
|
void SetSecureValue::cancel_upload() {
|
|
|
|
upload_generation_++;
|
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
|
|
|
if (file_manager == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto &file_info : files_to_upload_) {
|
|
|
|
file_manager->cancel_upload(file_info.file_id);
|
|
|
|
}
|
|
|
|
for (auto &file_info : translations_to_upload_) {
|
|
|
|
file_manager->cancel_upload(file_info.file_id);
|
|
|
|
}
|
|
|
|
if (front_side_) {
|
|
|
|
file_manager->cancel_upload(front_side_.value().file_id);
|
|
|
|
}
|
|
|
|
if (reverse_side_) {
|
|
|
|
file_manager->cancel_upload(reverse_side_.value().file_id);
|
|
|
|
}
|
|
|
|
if (selfie_) {
|
|
|
|
file_manager->cancel_upload(selfie_.value().file_id);
|
|
|
|
}
|
|
|
|
files_left_to_upload_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSecureValue::start_upload_all() {
|
|
|
|
if (files_left_to_upload_ != 0) {
|
|
|
|
cancel_upload();
|
|
|
|
}
|
|
|
|
upload_generation_++;
|
|
|
|
upload_callback_ = std::make_shared<UploadCallback>(actor_id(this), upload_generation_);
|
2018-08-10 18:39:48 +02:00
|
|
|
|
2019-08-01 17:26:43 +02:00
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-08-10 18:39:48 +02:00
|
|
|
files_to_upload_.resize(secure_value_.files.size());
|
|
|
|
for (size_t i = 0; i < files_to_upload_.size(); i++) {
|
|
|
|
start_upload(file_manager, secure_value_.files[i].file_id, files_to_upload_[i]);
|
|
|
|
}
|
|
|
|
translations_to_upload_.resize(secure_value_.translations.size());
|
|
|
|
for (size_t i = 0; i < translations_to_upload_.size(); i++) {
|
|
|
|
start_upload(file_manager, secure_value_.translations[i].file_id, translations_to_upload_[i]);
|
2018-04-05 15:06:03 +02:00
|
|
|
}
|
2018-04-27 14:38:02 +02:00
|
|
|
if (front_side_) {
|
|
|
|
start_upload(file_manager, secure_value_.front_side.file_id, front_side_.value());
|
|
|
|
}
|
|
|
|
if (reverse_side_) {
|
|
|
|
start_upload(file_manager, secure_value_.reverse_side.file_id, reverse_side_.value());
|
|
|
|
}
|
2018-04-24 19:10:12 +02:00
|
|
|
if (selfie_) {
|
2018-04-20 16:48:17 +02:00
|
|
|
start_upload(file_manager, secure_value_.selfie.file_id, selfie_.value());
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 22:41:34 +02:00
|
|
|
void SetSecureValue::start_upload(FileManager *file_manager, FileId &file_id, SecureInputFile &info) {
|
|
|
|
auto file_view = file_manager->get_file_view(file_id);
|
2019-08-01 17:26:43 +02:00
|
|
|
bool force = false;
|
|
|
|
if (info.file_id.empty()) {
|
|
|
|
if (!file_view.is_encrypted_secure()) {
|
2022-11-21 09:40:43 +01:00
|
|
|
auto download_file_id = file_manager->dup_file_id(file_id, "SetSecureValue");
|
2019-08-01 17:26:43 +02:00
|
|
|
file_id =
|
|
|
|
file_manager
|
2022-04-25 18:00:40 +02:00
|
|
|
->register_generate(FileType::SecureEncrypted, FileLocationSource::FromServer, file_view.suggested_path(),
|
2019-08-01 17:26:43 +02:00
|
|
|
PSTRING() << "#file_id#" << download_file_id.get(), DialogId(), file_view.size())
|
|
|
|
.ok();
|
|
|
|
}
|
2018-04-26 22:41:34 +02:00
|
|
|
|
2022-11-21 09:40:43 +01:00
|
|
|
info.file_id = file_manager->dup_file_id(file_id, "SetSecureValue");
|
2019-08-01 17:26:43 +02:00
|
|
|
} else {
|
|
|
|
force = true;
|
|
|
|
}
|
|
|
|
file_manager->resume_upload(info.file_id, {}, upload_callback_, 1, 0, force);
|
2018-04-05 15:06:03 +02:00
|
|
|
files_left_to_upload_++;
|
|
|
|
}
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SetSecureValue::loop() {
|
|
|
|
if (state_ == State::WaitSecret) {
|
|
|
|
if (!secret_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (files_left_to_upload_ != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-04-27 14:38:02 +02:00
|
|
|
auto input_secure_value =
|
|
|
|
get_input_secure_value_object(file_manager, encrypt_secure_value(file_manager, *secret_, secure_value_),
|
2018-08-10 18:39:48 +02:00
|
|
|
files_to_upload_, front_side_, reverse_side_, selfie_, translations_to_upload_);
|
2018-04-05 13:18:47 +02:00
|
|
|
auto save_secure_value =
|
|
|
|
telegram_api::account_saveSecureValue(std::move(input_secure_value), secret_.value().get_hash());
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(save_secure_value);
|
2018-04-05 13:18:47 +02:00
|
|
|
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this));
|
|
|
|
state_ = State::WaitSetValue;
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 15:06:03 +02:00
|
|
|
|
2018-04-24 19:10:12 +02:00
|
|
|
void SetSecureValue::hangup() {
|
2021-10-07 15:36:21 +02:00
|
|
|
on_error(Status::Error(406, "Request canceled"));
|
2018-04-24 19:10:12 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 15:06:03 +02:00
|
|
|
void SetSecureValue::tear_down() {
|
2019-08-01 17:26:43 +02:00
|
|
|
cancel_upload();
|
2018-04-05 15:06:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SetSecureValue::on_result(NetQueryPtr query) {
|
|
|
|
auto r_result = fetch_result<telegram_api::account_saveSecureValue>(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
2019-08-02 11:01:55 +02:00
|
|
|
if (r_result.error().message() == "SECURE_SECRET_REQUIRED") {
|
|
|
|
state_ = State::WaitSecret;
|
|
|
|
send_closure(G()->password_manager(), &PasswordManager::drop_cached_secret);
|
|
|
|
load_secret();
|
|
|
|
return loop();
|
|
|
|
}
|
2019-08-01 17:26:43 +02:00
|
|
|
if (r_result.error().message() == "SECURE_SECRET_INVALID") {
|
|
|
|
state_ = State::WaitSecret;
|
2019-08-02 11:01:55 +02:00
|
|
|
start_upload_all();
|
|
|
|
return loop();
|
2019-08-01 17:26:43 +02:00
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
return on_error(r_result.move_as_error());
|
|
|
|
}
|
|
|
|
auto result = r_result.move_as_ok();
|
2018-04-07 00:29:36 +02:00
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-04-05 13:18:47 +02:00
|
|
|
auto encrypted_secure_value = get_encrypted_secure_value(file_manager, std::move(result));
|
2018-04-27 14:38:02 +02:00
|
|
|
if (encrypted_secure_value.type == SecureValueType::None) {
|
2018-08-01 15:52:07 +02:00
|
|
|
return on_error(Status::Error(500, "Receive invalid Telegram Passport element"));
|
2018-04-27 14:38:02 +02:00
|
|
|
}
|
2018-04-05 13:18:47 +02:00
|
|
|
if (secure_value_.files.size() != encrypted_secure_value.files.size()) {
|
2018-04-26 22:41:34 +02:00
|
|
|
return on_error(Status::Error(500, "Different file count"));
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < secure_value_.files.size(); i++) {
|
2018-04-20 16:48:17 +02:00
|
|
|
merge(file_manager, secure_value_.files[i].file_id, encrypted_secure_value.files[i]);
|
2018-04-05 15:06:03 +02:00
|
|
|
}
|
2018-04-27 14:38:02 +02:00
|
|
|
if (secure_value_.front_side.file_id.is_valid() && encrypted_secure_value.front_side.file.file_id.is_valid()) {
|
|
|
|
merge(file_manager, secure_value_.front_side.file_id, encrypted_secure_value.front_side);
|
|
|
|
}
|
|
|
|
if (secure_value_.reverse_side.file_id.is_valid() && encrypted_secure_value.reverse_side.file.file_id.is_valid()) {
|
|
|
|
merge(file_manager, secure_value_.reverse_side.file_id, encrypted_secure_value.reverse_side);
|
|
|
|
}
|
2018-04-26 22:41:34 +02:00
|
|
|
if (secure_value_.selfie.file_id.is_valid() && encrypted_secure_value.selfie.file.file_id.is_valid()) {
|
2018-04-20 16:48:17 +02:00
|
|
|
merge(file_manager, secure_value_.selfie.file_id, encrypted_secure_value.selfie);
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
2018-08-10 18:39:48 +02:00
|
|
|
for (size_t i = 0; i < secure_value_.translations.size(); i++) {
|
|
|
|
merge(file_manager, secure_value_.translations[i].file_id, encrypted_secure_value.translations[i]);
|
|
|
|
}
|
2018-04-19 15:43:09 +02:00
|
|
|
auto r_secure_value = decrypt_secure_value(file_manager, *secret_, encrypted_secure_value);
|
2018-04-05 13:18:47 +02:00
|
|
|
if (r_secure_value.is_error()) {
|
|
|
|
return on_error(r_secure_value.move_as_error());
|
|
|
|
}
|
2018-08-12 14:44:24 +02:00
|
|
|
|
|
|
|
send_closure(parent_, &SecureManager::on_get_secure_value, r_secure_value.ok());
|
|
|
|
|
|
|
|
promise_.set_value(r_secure_value.move_as_ok());
|
2018-04-05 13:18:47 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:41:38 +02:00
|
|
|
void SetSecureValue::merge(FileManager *file_manager, FileId file_id, EncryptedSecureFile &encrypted_file) {
|
2018-04-05 15:06:03 +02:00
|
|
|
auto file_view = file_manager->get_file_view(file_id);
|
|
|
|
CHECK(!file_view.empty());
|
|
|
|
CHECK(file_view.encryption_key().has_value_hash());
|
|
|
|
if (file_view.encryption_key().value_hash().as_slice() != encrypted_file.file_hash) {
|
2018-04-19 17:39:30 +02:00
|
|
|
LOG(ERROR) << "Hash mismatch";
|
2018-04-05 15:06:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-11-12 07:47:33 +01:00
|
|
|
LOG_STATUS(file_manager->merge(encrypted_file.file.file_id, file_id));
|
2018-04-05 15:06:03 +02:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class DeleteSecureValue final : public NetQueryCallback {
|
2018-04-19 16:31:25 +02:00
|
|
|
public:
|
2018-08-12 14:44:24 +02:00
|
|
|
DeleteSecureValue(ActorShared<SecureManager> parent, SecureValueType type, Promise<Unit> promise)
|
2018-04-19 16:31:25 +02:00
|
|
|
: parent_(std::move(parent)), type_(std::move(type)), promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-12 14:44:24 +02:00
|
|
|
ActorShared<SecureManager> parent_;
|
2018-04-19 16:31:25 +02:00
|
|
|
SecureValueType type_;
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-04-19 16:31:25 +02:00
|
|
|
std::vector<telegram_api::object_ptr<telegram_api::SecureValueType>> types;
|
|
|
|
types.push_back(get_input_secure_value_type(type_));
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(telegram_api::account_deleteSecureValue(std::move(types)));
|
2018-04-19 16:31:25 +02:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final {
|
2018-04-19 16:31:25 +02:00
|
|
|
auto r_result = fetch_result<telegram_api::account_deleteSecureValue>(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
promise_.set_error(r_result.move_as_error());
|
|
|
|
} else {
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetPassportAuthorizationForm final : public NetQueryCallback {
|
2018-04-06 15:24:29 +02:00
|
|
|
public:
|
2018-09-25 04:12:41 +02:00
|
|
|
GetPassportAuthorizationForm(ActorShared<SecureManager> parent, UserId bot_user_id, string scope, string public_key,
|
|
|
|
Promise<telegram_api::object_ptr<telegram_api::account_authorizationForm>> promise)
|
2018-04-06 15:24:29 +02:00
|
|
|
: parent_(std::move(parent))
|
2018-04-07 00:29:36 +02:00
|
|
|
, bot_user_id_(bot_user_id)
|
2018-04-06 15:24:29 +02:00
|
|
|
, scope_(std::move(scope))
|
|
|
|
, public_key_(std::move(public_key))
|
|
|
|
, promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-12 14:44:24 +02:00
|
|
|
ActorShared<SecureManager> parent_;
|
2018-04-07 00:29:36 +02:00
|
|
|
UserId bot_user_id_;
|
2018-04-06 15:24:29 +02:00
|
|
|
string scope_;
|
|
|
|
string public_key_;
|
2018-09-25 04:12:41 +02:00
|
|
|
Promise<telegram_api::object_ptr<telegram_api::account_authorizationForm>> promise_;
|
2018-04-06 15:24:29 +02:00
|
|
|
|
2018-05-24 00:30:47 +02:00
|
|
|
void on_error(Status error) {
|
2018-09-25 04:12:41 +02:00
|
|
|
if (error.code() > 0) {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(std::move(error));
|
2018-04-23 23:01:05 +02:00
|
|
|
} else {
|
2018-05-24 00:30:47 +02:00
|
|
|
promise_.set_error(Status::Error(400, error.message()));
|
2018-04-23 23:01:05 +02:00
|
|
|
}
|
2018-04-06 15:24:29 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-04-06 15:24:29 +02:00
|
|
|
auto account_get_authorization_form =
|
2018-04-07 00:29:36 +02:00
|
|
|
telegram_api::account_getAuthorizationForm(bot_user_id_.get(), std::move(scope_), std::move(public_key_));
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(account_get_authorization_form);
|
2018-04-06 15:24:29 +02:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final {
|
2018-04-06 15:24:29 +02:00
|
|
|
auto r_result = fetch_result<telegram_api::account_getAuthorizationForm>(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
return on_error(r_result.move_as_error());
|
|
|
|
}
|
2018-09-25 04:12:41 +02:00
|
|
|
promise_.set_value(r_result.move_as_ok());
|
2018-04-06 15:24:29 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
SecureManager::SecureManager(ActorShared<> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecureManager::get_secure_value(std::string password, SecureValueType type, Promise<TdApiSecureValue> promise) {
|
2018-04-06 21:37:30 +02:00
|
|
|
auto new_promise =
|
|
|
|
PromiseCreator::lambda([promise = std::move(promise)](Result<SecureValueWithCredentials> r_secure_value) mutable {
|
|
|
|
if (r_secure_value.is_error()) {
|
|
|
|
return promise.set_error(r_secure_value.move_as_error());
|
|
|
|
}
|
2018-08-12 14:44:24 +02:00
|
|
|
|
2018-04-07 00:29:36 +02:00
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-08-12 14:44:24 +02:00
|
|
|
if (file_manager == nullptr) {
|
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
auto r_passport_element = get_passport_element_object(file_manager, r_secure_value.move_as_ok().value);
|
|
|
|
if (r_passport_element.is_error()) {
|
|
|
|
LOG(ERROR) << "Failed to get passport element object: " << r_passport_element.error();
|
2018-04-11 16:21:24 +02:00
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
promise.set_value(r_passport_element.move_as_ok());
|
2018-04-06 21:37:30 +02:00
|
|
|
});
|
2018-08-12 14:44:24 +02:00
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
refcnt_++;
|
2018-08-12 15:46:05 +02:00
|
|
|
create_actor<GetSecureValue>("GetSecureValue", actor_shared(this), std::move(password), type, std::move(new_promise))
|
2018-04-05 13:18:47 +02:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetPassportConfig final : public NetQueryCallback {
|
2018-08-24 00:02:59 +02:00
|
|
|
public:
|
|
|
|
GetPassportConfig(ActorShared<SecureManager> parent, string country_code,
|
|
|
|
Promise<td_api::object_ptr<td_api::text>> promise)
|
|
|
|
: parent_(std::move(parent)), country_code_(std::move(country_code)), promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<SecureManager> parent_;
|
|
|
|
string country_code_;
|
|
|
|
Promise<td_api::object_ptr<td_api::text>> promise_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(telegram_api::help_getPassportConfig(0));
|
2018-08-24 00:02:59 +02:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final {
|
2018-08-24 00:02:59 +02:00
|
|
|
auto r_result = fetch_result<telegram_api::help_getPassportConfig>(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
promise_.set_error(r_result.move_as_error());
|
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto config = r_result.move_as_ok();
|
|
|
|
switch (config->get_id()) {
|
|
|
|
case telegram_api::help_passportConfigNotModified::ID:
|
|
|
|
promise_.set_error(Status::Error(500, "Wrong server response"));
|
|
|
|
break;
|
|
|
|
case telegram_api::help_passportConfig::ID: {
|
|
|
|
const string &data =
|
|
|
|
static_cast<const telegram_api::help_passportConfig *>(config.get())->countries_langs_->data_;
|
|
|
|
auto begin_pos = data.find((PSLICE() << '"' << country_code_ << "\":\"").c_str());
|
|
|
|
if (begin_pos == string::npos) {
|
|
|
|
promise_.set_value(nullptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
begin_pos += 4 + country_code_.size();
|
|
|
|
auto end_pos = data.find('"', begin_pos);
|
|
|
|
if (end_pos == string::npos) {
|
|
|
|
return promise_.set_error(Status::Error(500, "Wrong server response"));
|
|
|
|
}
|
|
|
|
promise_.set_value(td_api::make_object<td_api::text>(data.substr(begin_pos, end_pos - begin_pos)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-12 14:44:24 +02:00
|
|
|
void SecureManager::on_get_secure_value(SecureValueWithCredentials value) {
|
|
|
|
auto type = value.value.type;
|
|
|
|
secure_value_cache_[type] = std::move(value);
|
|
|
|
}
|
|
|
|
|
2018-08-01 15:52:07 +02:00
|
|
|
void SecureManager::get_all_secure_values(std::string password, Promise<TdApiSecureValues> promise) {
|
2018-04-07 00:29:36 +02:00
|
|
|
refcnt_++;
|
2018-08-12 14:44:24 +02:00
|
|
|
create_actor<GetAllSecureValues>("GetAllSecureValues", actor_shared(this), std::move(password), std::move(promise))
|
2018-04-07 00:29:36 +02:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SecureManager::set_secure_value(string password, SecureValue secure_value, Promise<TdApiSecureValue> promise) {
|
|
|
|
refcnt_++;
|
|
|
|
auto type = secure_value.type;
|
2018-04-06 21:37:30 +02:00
|
|
|
auto new_promise =
|
|
|
|
PromiseCreator::lambda([promise = std::move(promise)](Result<SecureValueWithCredentials> r_secure_value) mutable {
|
|
|
|
if (r_secure_value.is_error()) {
|
|
|
|
return promise.set_error(r_secure_value.move_as_error());
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
2018-08-01 15:52:07 +02:00
|
|
|
auto r_passport_element = get_passport_element_object(file_manager, r_secure_value.move_as_ok().value);
|
|
|
|
if (r_passport_element.is_error()) {
|
|
|
|
LOG(ERROR) << "Failed to get passport element object: " << r_passport_element.error();
|
|
|
|
return promise.set_error(Status::Error(500, "Failed to get passport element object"));
|
2018-04-11 16:21:24 +02:00
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
promise.set_value(r_passport_element.move_as_ok());
|
2018-04-06 21:37:30 +02:00
|
|
|
});
|
2018-08-12 14:44:24 +02:00
|
|
|
set_secure_value_queries_[type] = create_actor<SetSecureValue>(
|
|
|
|
"SetSecureValue", actor_shared(this), std::move(password), std::move(secure_value), std::move(new_promise));
|
2018-04-05 13:18:47 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 19:42:06 +02:00
|
|
|
void SecureManager::delete_secure_value(SecureValueType type, Promise<Unit> promise) {
|
2018-04-19 16:31:25 +02:00
|
|
|
refcnt_++;
|
|
|
|
auto new_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), type, promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
send_closure(actor_id, &SecureManager::on_delete_secure_value, type, std::move(promise), std::move(result));
|
|
|
|
});
|
2018-08-12 14:44:24 +02:00
|
|
|
create_actor<DeleteSecureValue>("DeleteSecureValue", actor_shared(this), type, std::move(new_promise)).release();
|
2018-04-19 16:31:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SecureManager::on_delete_secure_value(SecureValueType type, Promise<Unit> promise, Result<Unit> result) {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:44:24 +02:00
|
|
|
secure_value_cache_.erase(type);
|
2018-04-19 16:31:25 +02:00
|
|
|
promise.set_value(Unit());
|
2018-04-11 19:42:06 +02:00
|
|
|
}
|
2018-04-17 17:04:09 +02:00
|
|
|
|
|
|
|
void SecureManager::set_secure_value_errors(Td *td, tl_object_ptr<telegram_api::InputUser> input_user,
|
2018-08-01 15:52:07 +02:00
|
|
|
vector<tl_object_ptr<td_api::inputPassportElementError>> errors,
|
2018-04-17 17:04:09 +02:00
|
|
|
Promise<Unit> promise) {
|
|
|
|
CHECK(td != nullptr);
|
|
|
|
CHECK(input_user != nullptr);
|
|
|
|
vector<tl_object_ptr<telegram_api::SecureValueError>> input_errors;
|
2018-04-18 13:19:47 +02:00
|
|
|
for (auto &error : errors) {
|
|
|
|
if (error == nullptr) {
|
2018-04-17 17:04:09 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Error must be non-empty"));
|
|
|
|
}
|
2018-04-18 13:19:47 +02:00
|
|
|
if (error->type_ == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Type must be non-empty"));
|
|
|
|
}
|
|
|
|
if (!clean_input_string(error->message_)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Error message must be encoded in UTF-8"));
|
|
|
|
}
|
|
|
|
if (error->source_ == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Error source must be non-empty"));
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:31:25 +02:00
|
|
|
auto type = get_input_secure_value_type(get_secure_value_type_td_api(error->type_));
|
2018-04-18 13:19:47 +02:00
|
|
|
switch (error->source_->get_id()) {
|
2018-08-10 18:39:48 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceUnspecified::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceUnspecified>(error->source_);
|
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueError>(
|
|
|
|
std::move(type), BufferSlice(source->element_hash_), error->message_));
|
|
|
|
break;
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceDataField::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceDataField>(error->source_);
|
2018-04-18 13:19:47 +02:00
|
|
|
if (!clean_input_string(source->field_name_)) {
|
2018-04-17 17:04:09 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Field name must be encoded in UTF-8"));
|
|
|
|
}
|
|
|
|
|
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorData>(
|
2018-04-18 13:19:47 +02:00
|
|
|
std::move(type), BufferSlice(source->data_hash_), source->field_name_, error->message_));
|
2018-04-17 19:53:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceFrontSide::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceFrontSide>(error->source_);
|
2018-04-27 14:38:02 +02:00
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorFrontSide>(
|
|
|
|
std::move(type), BufferSlice(source->file_hash_), error->message_));
|
|
|
|
break;
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceReverseSide::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceReverseSide>(error->source_);
|
2018-04-27 14:38:02 +02:00
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorReverseSide>(
|
|
|
|
std::move(type), BufferSlice(source->file_hash_), error->message_));
|
|
|
|
break;
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceSelfie::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceSelfie>(error->source_);
|
2018-04-27 14:38:02 +02:00
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorSelfie>(
|
|
|
|
std::move(type), BufferSlice(source->file_hash_), error->message_));
|
|
|
|
break;
|
|
|
|
}
|
2018-08-10 23:08:13 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceTranslationFile::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceTranslationFile>(error->source_);
|
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorTranslationFile>(
|
2018-08-10 18:39:48 +02:00
|
|
|
std::move(type), BufferSlice(source->file_hash_), error->message_));
|
|
|
|
break;
|
|
|
|
}
|
2018-08-10 23:08:13 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceTranslationFiles::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceTranslationFiles>(error->source_);
|
2018-08-10 18:39:48 +02:00
|
|
|
if (source->file_hashes_.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "File hashes must be non-empty"));
|
|
|
|
}
|
|
|
|
auto file_hashes = transform(source->file_hashes_, [](Slice hash) { return BufferSlice(hash); });
|
2018-08-10 23:08:13 +02:00
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorTranslationFiles>(
|
2018-08-10 18:39:48 +02:00
|
|
|
std::move(type), std::move(file_hashes), error->message_));
|
|
|
|
break;
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceFile::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceFile>(error->source_);
|
2018-04-17 19:53:25 +02:00
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorFile>(
|
2018-04-18 13:19:47 +02:00
|
|
|
std::move(type), BufferSlice(source->file_hash_), error->message_));
|
2018-04-17 17:04:09 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-01 15:52:07 +02:00
|
|
|
case td_api::inputPassportElementErrorSourceFiles::ID: {
|
|
|
|
auto source = td_api::move_object_as<td_api::inputPassportElementErrorSourceFiles>(error->source_);
|
2018-04-18 13:19:47 +02:00
|
|
|
if (source->file_hashes_.empty()) {
|
2018-08-10 18:39:48 +02:00
|
|
|
return promise.set_error(Status::Error(400, "File hashes must be non-empty"));
|
2018-04-17 17:04:09 +02:00
|
|
|
}
|
2018-04-18 13:19:47 +02:00
|
|
|
auto file_hashes = transform(source->file_hashes_, [](Slice hash) { return BufferSlice(hash); });
|
2018-04-17 19:53:25 +02:00
|
|
|
input_errors.push_back(make_tl_object<telegram_api::secureValueErrorFiles>(
|
|
|
|
std::move(type), std::move(file_hashes), error->message_));
|
2018-04-17 17:04:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
td->create_handler<SetSecureValueErrorsQuery>(std::move(promise))
|
|
|
|
->send(std::move(input_user), std::move(input_errors));
|
|
|
|
}
|
2018-04-11 19:42:06 +02:00
|
|
|
|
2018-09-25 04:12:41 +02:00
|
|
|
void SecureManager::get_passport_authorization_form(UserId bot_user_id, string scope, string public_key, string nonce,
|
2018-04-07 00:29:36 +02:00
|
|
|
Promise<TdApiAuthorizationForm> promise) {
|
2018-04-06 15:24:29 +02:00
|
|
|
refcnt_++;
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(max_authorization_form_id_ < std::numeric_limits<int32>::max());
|
2018-08-12 14:44:24 +02:00
|
|
|
auto authorization_form_id = ++max_authorization_form_id_;
|
2022-02-11 17:27:32 +01:00
|
|
|
auto &form_ptr = authorization_forms_[authorization_form_id];
|
|
|
|
if (form_ptr == nullptr) {
|
|
|
|
form_ptr = make_unique<AuthorizationForm>();
|
|
|
|
}
|
|
|
|
auto &form = *form_ptr;
|
2018-08-13 12:00:44 +02:00
|
|
|
form.bot_user_id = bot_user_id;
|
|
|
|
form.scope = scope;
|
|
|
|
form.public_key = public_key;
|
2021-10-19 17:11:16 +02:00
|
|
|
form.nonce = std::move(nonce);
|
2018-08-12 16:45:30 +02:00
|
|
|
auto new_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), authorization_form_id, promise = std::move(promise)](
|
2018-09-25 04:12:41 +02:00
|
|
|
Result<telegram_api::object_ptr<telegram_api::account_authorizationForm>> r_authorization_form) mutable {
|
2018-04-17 14:39:23 +02:00
|
|
|
send_closure(actor_id, &SecureManager::on_get_passport_authorization_form, authorization_form_id,
|
|
|
|
std::move(promise), std::move(r_authorization_form));
|
|
|
|
});
|
2018-09-25 04:12:41 +02:00
|
|
|
create_actor<GetPassportAuthorizationForm>("GetPassportAuthorizationForm", actor_shared(this), bot_user_id,
|
|
|
|
std::move(scope), std::move(public_key), std::move(new_promise))
|
2018-04-06 15:24:29 +02:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:45:30 +02:00
|
|
|
void SecureManager::on_get_passport_authorization_form(
|
|
|
|
int32 authorization_form_id, Promise<TdApiAuthorizationForm> promise,
|
2018-09-25 04:12:41 +02:00
|
|
|
Result<telegram_api::object_ptr<telegram_api::account_authorizationForm>> r_authorization_form) {
|
2018-04-17 14:39:23 +02:00
|
|
|
auto it = authorization_forms_.find(authorization_form_id);
|
|
|
|
CHECK(it != authorization_forms_.end());
|
2022-02-11 17:27:32 +01:00
|
|
|
CHECK(it->second != nullptr);
|
|
|
|
CHECK(!it->second->is_received);
|
2018-04-17 14:39:23 +02:00
|
|
|
if (r_authorization_form.is_error()) {
|
|
|
|
authorization_forms_.erase(it);
|
|
|
|
return promise.set_error(r_authorization_form.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto authorization_form = r_authorization_form.move_as_ok();
|
2018-09-25 04:12:41 +02:00
|
|
|
LOG(INFO) << "Receive " << to_string(authorization_form);
|
2019-01-12 04:48:39 +01:00
|
|
|
G()->td().get_actor_unsafe()->contacts_manager_->on_get_users(std::move(authorization_form->users_),
|
2019-03-24 20:05:06 +01:00
|
|
|
"on_get_passport_authorization_form");
|
2018-09-25 04:12:41 +02:00
|
|
|
|
|
|
|
vector<vector<SuitableSecureValue>> required_types;
|
|
|
|
std::map<SecureValueType, SuitableSecureValue> all_types;
|
|
|
|
for (auto &type_ptr : authorization_form->required_types_) {
|
|
|
|
CHECK(type_ptr != nullptr);
|
|
|
|
vector<SuitableSecureValue> required_type;
|
|
|
|
switch (type_ptr->get_id()) {
|
|
|
|
case telegram_api::secureRequiredType::ID: {
|
|
|
|
auto value = get_suitable_secure_value(move_tl_object_as<telegram_api::secureRequiredType>(type_ptr));
|
|
|
|
all_types.emplace(value.type, value);
|
|
|
|
required_type.push_back(std::move(value));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureRequiredTypeOneOf::ID: {
|
|
|
|
auto type_one_of = move_tl_object_as<telegram_api::secureRequiredTypeOneOf>(type_ptr);
|
|
|
|
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<telegram_api::secureRequiredType>(type));
|
|
|
|
all_types.emplace(value.type, value);
|
|
|
|
required_type.push_back(std::move(value));
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << to_string(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
if (!required_type.empty()) {
|
|
|
|
required_types.push_back(required_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:27:32 +01:00
|
|
|
it->second->options = std::move(all_types);
|
|
|
|
it->second->values = std::move(authorization_form->values_);
|
|
|
|
it->second->errors = std::move(authorization_form->errors_);
|
|
|
|
it->second->is_received = true;
|
2018-09-25 04:12:41 +02:00
|
|
|
|
|
|
|
promise.set_value(td_api::make_object<td_api::passportAuthorizationForm>(
|
|
|
|
authorization_form_id, get_passport_required_elements_object(required_types),
|
|
|
|
authorization_form->privacy_policy_url_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecureManager::get_passport_authorization_form_available_elements(int32 authorization_form_id, string password,
|
|
|
|
Promise<TdApiSecureValuesWithErrors> promise) {
|
|
|
|
auto it = authorization_forms_.find(authorization_form_id);
|
|
|
|
if (it == authorization_forms_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown authorization_form_id"));
|
|
|
|
}
|
2022-02-11 17:27:32 +01:00
|
|
|
CHECK(it->second != nullptr);
|
|
|
|
if (!it->second->is_received) {
|
2018-09-25 04:12:41 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Authorization form isn't received yet"));
|
|
|
|
}
|
|
|
|
|
|
|
|
refcnt_++;
|
|
|
|
send_closure(G()->password_manager(), &PasswordManager::get_secure_secret, password,
|
2022-03-05 12:35:19 +01:00
|
|
|
PromiseCreator::lambda([self = actor_shared(this), authorization_form_id,
|
2018-09-25 04:12:41 +02:00
|
|
|
promise = std::move(promise)](Result<secure_storage::Secret> r_secret) mutable {
|
2022-03-05 12:35:19 +01:00
|
|
|
send_closure(self, &SecureManager::on_get_passport_authorization_form_secret, authorization_form_id,
|
|
|
|
std::move(promise), std::move(r_secret));
|
2018-09-25 04:12:41 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecureManager::on_get_passport_authorization_form_secret(int32 authorization_form_id,
|
|
|
|
Promise<TdApiSecureValuesWithErrors> promise,
|
|
|
|
Result<secure_storage::Secret> r_secret) {
|
|
|
|
auto it = authorization_forms_.find(authorization_form_id);
|
|
|
|
if (it == authorization_forms_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Authorization form has already been sent"));
|
|
|
|
}
|
2022-02-11 17:27:32 +01:00
|
|
|
CHECK(it->second != nullptr);
|
|
|
|
CHECK(it->second->is_received);
|
|
|
|
if (it->second->is_decrypted) {
|
2018-09-25 04:12:41 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Authorization form has already been decrypted"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_secret.is_error()) {
|
|
|
|
auto error = r_secret.move_as_error();
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(error)) {
|
2018-09-25 04:12:41 +02:00
|
|
|
LOG(ERROR) << "Receive error instead of secret: " << error;
|
|
|
|
}
|
|
|
|
if (error.code() <= 0) {
|
|
|
|
error = Status::Error(400, error.message()); // TODO error.set_code(400) ?
|
|
|
|
}
|
|
|
|
return promise.set_error(std::move(error));
|
|
|
|
}
|
|
|
|
auto secret = r_secret.move_as_ok();
|
|
|
|
|
2022-02-11 17:27:32 +01:00
|
|
|
it->second->is_decrypted = true;
|
2018-09-25 04:12:41 +02:00
|
|
|
|
|
|
|
auto *file_manager = G()->td().get_actor_unsafe()->file_manager_.get();
|
|
|
|
std::vector<TdApiSecureValue> values;
|
|
|
|
std::map<SecureValueType, SecureValueCredentials> all_credentials;
|
2022-02-11 17:27:32 +01:00
|
|
|
for (const auto &suitable_type : it->second->options) {
|
2018-09-25 04:12:41 +02:00
|
|
|
auto type = suitable_type.first;
|
2022-02-11 17:27:32 +01:00
|
|
|
for (auto &value : it->second->values) {
|
2018-09-25 04:12:41 +02:00
|
|
|
if (value == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto value_type = get_secure_value_type(value->type_);
|
|
|
|
if (value_type != type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_secure_value =
|
|
|
|
decrypt_secure_value(file_manager, secret, get_encrypted_secure_value(file_manager, std::move(value)));
|
|
|
|
value = nullptr;
|
|
|
|
if (r_secure_value.is_error()) {
|
|
|
|
LOG(ERROR) << "Failed to decrypt secure value: " << r_secure_value.error();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_get_secure_value(r_secure_value.ok());
|
|
|
|
|
|
|
|
auto secure_value = r_secure_value.move_as_ok();
|
2021-10-19 17:11:16 +02:00
|
|
|
auto r_passport_element = get_passport_element_object(file_manager, secure_value.value);
|
2018-09-25 04:12:41 +02:00
|
|
|
if (r_passport_element.is_error()) {
|
|
|
|
LOG(ERROR) << "Failed to get passport element object: " << r_passport_element.error();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
values.push_back(r_passport_element.move_as_ok());
|
|
|
|
all_credentials.emplace(type, std::move(secure_value.credentials));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto get_file_index = [](const vector<SecureFileCredentials> &file_credentials, Slice file_hash) -> int32 {
|
|
|
|
for (size_t i = 0; i < file_credentials.size(); i++) {
|
|
|
|
if (file_credentials[i].hash == file_hash) {
|
|
|
|
return narrow_cast<int32>(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
vector<td_api::object_ptr<td_api::passportElementError>> errors;
|
2022-02-11 17:27:32 +01:00
|
|
|
for (auto &error_ptr : it->second->errors) {
|
2018-09-25 04:12:41 +02:00
|
|
|
CHECK(error_ptr != nullptr);
|
|
|
|
SecureValueType type = SecureValueType::None;
|
|
|
|
td_api::object_ptr<td_api::PassportElementErrorSource> source;
|
|
|
|
string message;
|
|
|
|
switch (error_ptr->get_id()) {
|
|
|
|
case telegram_api::secureValueError::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueError>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceUnspecified>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorData::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorData>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
string field_name = get_secure_value_data_field_name(type, error->field_);
|
|
|
|
if (field_name.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceDataField>(std::move(field_name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorFile::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorFile>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
int32 file_index = get_file_index(all_credentials[type].files, error->file_hash_.as_slice());
|
|
|
|
if (file_index == -1) {
|
|
|
|
LOG(ERROR) << "Can't find file with error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceFile>(file_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorFiles::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorFiles>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceFiles>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorFrontSide::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorFrontSide>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceFrontSide>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorReverseSide::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorReverseSide>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceReverseSide>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorSelfie::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorSelfie>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceSelfie>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorTranslationFile::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorTranslationFile>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
int32 file_index = get_file_index(all_credentials[type].translations, error->file_hash_.as_slice());
|
|
|
|
if (file_index == -1) {
|
|
|
|
LOG(ERROR) << "Can't find translation file with error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceTranslationFile>(file_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::secureValueErrorTranslationFiles::ID: {
|
|
|
|
auto error = move_tl_object_as<telegram_api::secureValueErrorTranslationFiles>(error_ptr);
|
|
|
|
type = get_secure_value_type(error->type_);
|
|
|
|
message = std::move(error->text_);
|
|
|
|
source = td_api::make_object<td_api::passportElementErrorSourceTranslationFiles>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
if (source == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
errors.push_back(td_api::make_object<td_api::passportElementError>(get_passport_element_type_object(type), message,
|
|
|
|
std::move(source)));
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(td_api::make_object<td_api::passportElementsWithErrors>(std::move(values), std::move(errors)));
|
2018-04-17 14:39:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-12 15:46:05 +02:00
|
|
|
void SecureManager::send_passport_authorization_form(int32 authorization_form_id, std::vector<SecureValueType> types,
|
|
|
|
Promise<> promise) {
|
2018-04-06 15:24:29 +02:00
|
|
|
auto it = authorization_forms_.find(authorization_form_id);
|
|
|
|
if (it == authorization_forms_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown authorization_form_id"));
|
|
|
|
}
|
2022-02-11 17:27:32 +01:00
|
|
|
CHECK(it->second != nullptr);
|
|
|
|
if (!it->second->is_received) {
|
2018-04-17 14:39:23 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Authorization form isn't received yet"));
|
|
|
|
}
|
2018-09-25 04:12:41 +02:00
|
|
|
// there is no need to check for is_decrypted
|
2018-04-06 21:37:30 +02:00
|
|
|
if (types.empty()) {
|
2018-04-17 14:39:23 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Types must be non-empty"));
|
2018-04-06 21:37:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-12 15:46:05 +02:00
|
|
|
std::vector<SecureValueCredentials> credentials;
|
|
|
|
credentials.reserve(types.size());
|
2018-04-06 21:37:30 +02:00
|
|
|
for (auto type : types) {
|
2018-08-12 15:46:05 +02:00
|
|
|
auto value_it = secure_value_cache_.find(type);
|
|
|
|
if (value_it == secure_value_cache_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Passport Element with the specified type is not found"));
|
|
|
|
}
|
|
|
|
credentials.push_back(value_it->second.credentials);
|
2018-04-06 21:37:30 +02:00
|
|
|
}
|
2018-08-12 15:46:05 +02:00
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
std::vector<telegram_api::object_ptr<telegram_api::secureValueHash>> hashes;
|
|
|
|
for (auto &c : credentials) {
|
2018-04-19 16:31:25 +02:00
|
|
|
hashes.push_back(telegram_api::make_object<telegram_api::secureValueHash>(get_input_secure_value_type(c.type),
|
2018-04-10 19:10:20 +02:00
|
|
|
BufferSlice(c.hash)));
|
2022-02-11 17:27:32 +01:00
|
|
|
auto options_it = it->second->options.find(c.type);
|
|
|
|
if (options_it == it->second->options.end()) {
|
2018-08-12 16:45:30 +02:00
|
|
|
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<SecureFileCredentials>();
|
|
|
|
}
|
|
|
|
if (!options.is_translation_required) {
|
|
|
|
c.translations.clear();
|
|
|
|
}
|
2018-04-06 21:37:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 21:16:55 +02:00
|
|
|
auto r_encrypted_credentials =
|
2022-02-11 17:27:32 +01:00
|
|
|
get_encrypted_credentials(credentials, it->second->nonce, it->second->public_key,
|
|
|
|
it->second->scope[0] == '{' && it->second->scope.back() == '}');
|
2018-04-06 21:37:30 +02:00
|
|
|
if (r_encrypted_credentials.is_error()) {
|
|
|
|
return promise.set_error(r_encrypted_credentials.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto td_query = telegram_api::account_acceptAuthorization(
|
2022-02-11 17:27:32 +01:00
|
|
|
it->second->bot_user_id.get(), it->second->scope, it->second->public_key, std::move(hashes),
|
2018-04-06 21:37:30 +02:00
|
|
|
get_secure_credentials_encrypted_object(r_encrypted_credentials.move_as_ok()));
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(td_query);
|
2018-04-06 21:37:30 +02:00
|
|
|
auto new_promise =
|
|
|
|
PromiseCreator::lambda([promise = std::move(promise)](Result<NetQueryPtr> r_net_query_ptr) mutable {
|
2018-04-09 18:04:21 +02:00
|
|
|
auto r_result = fetch_result<telegram_api::account_acceptAuthorization>(std::move(r_net_query_ptr));
|
2018-04-06 21:37:30 +02:00
|
|
|
if (r_result.is_error()) {
|
|
|
|
return promise.set_error(r_result.move_as_error());
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
|
|
|
});
|
|
|
|
send_with_promise(std::move(query), std::move(new_promise));
|
|
|
|
}
|
|
|
|
|
2020-08-23 22:02:20 +02:00
|
|
|
void SecureManager::get_preferred_country_language(string country_code,
|
|
|
|
Promise<td_api::object_ptr<td_api::text>> promise) {
|
2018-08-24 00:02:59 +02:00
|
|
|
refcnt_++;
|
|
|
|
for (auto &c : country_code) {
|
|
|
|
c = to_upper(c);
|
|
|
|
}
|
|
|
|
create_actor<GetPassportConfig>("GetPassportConfig", actor_shared(this), std::move(country_code), std::move(promise))
|
|
|
|
.release();
|
|
|
|
}
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SecureManager::hangup() {
|
2018-04-19 17:39:30 +02:00
|
|
|
container_.for_each(
|
2021-10-07 15:36:21 +02:00
|
|
|
[](auto id, Promise<NetQueryPtr> &promise) { promise.set_error(Global::request_aborted_error()); });
|
2018-04-05 13:18:47 +02:00
|
|
|
dec_refcnt();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecureManager::hangup_shared() {
|
|
|
|
dec_refcnt();
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void SecureManager::dec_refcnt() {
|
|
|
|
refcnt_--;
|
|
|
|
if (refcnt_ == 0) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
void SecureManager::on_result(NetQueryPtr query) {
|
|
|
|
auto token = get_link_token();
|
|
|
|
container_.extract(token).set_value(std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecureManager::send_with_promise(NetQueryPtr query, Promise<NetQueryPtr> promise) {
|
|
|
|
auto id = container_.create(std::move(promise));
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, id));
|
|
|
|
}
|
2018-04-07 00:29:36 +02:00
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
} // namespace td
|