2018-04-05 13:18:47 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/SecureValue.h"
|
|
|
|
#include "td/telegram/files/FileManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
|
|
|
|
#include "td/utils/optional.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
using TdApiSecureValue = td_api::object_ptr<td_api::passportData>;
|
2018-04-06 15:24:29 +02:00
|
|
|
using TdApiAuthorizationForm = td_api::object_ptr<td_api::passportAuthorizationForm>;
|
2018-04-05 13:18:47 +02:00
|
|
|
class GetSecureValue : public NetQueryCallback {
|
|
|
|
public:
|
2018-04-06 21:37:30 +02:00
|
|
|
GetSecureValue(ActorShared<> parent, std::string password, SecureValueType type,
|
|
|
|
Promise<SecureValueWithCredentials> promise);
|
2018-04-05 13:18:47 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<> parent_;
|
|
|
|
string password_;
|
|
|
|
SecureValueType type_;
|
2018-04-06 21:37:30 +02:00
|
|
|
Promise<SecureValueWithCredentials> promise_;
|
2018-04-05 13:18:47 +02:00
|
|
|
optional<EncryptedSecureValue> encrypted_secure_value_;
|
|
|
|
optional<secure_storage::Secret> secret_;
|
|
|
|
|
|
|
|
void on_error(Status status);
|
|
|
|
void on_secret(Result<secure_storage::Secret> r_secret, bool dummy);
|
|
|
|
void loop() override;
|
|
|
|
void start_up() override;
|
|
|
|
|
|
|
|
void on_result(NetQueryPtr query) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SetSecureValue : public NetQueryCallback {
|
|
|
|
public:
|
2018-04-06 21:37:30 +02:00
|
|
|
SetSecureValue(ActorShared<> parent, string password, SecureValue secure_value,
|
|
|
|
Promise<SecureValueWithCredentials> promise);
|
2018-04-05 13:18:47 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<> 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
|
|
|
optional<secure_storage::Secret> secret_;
|
|
|
|
|
|
|
|
size_t files_left_to_upload_ = 0;
|
|
|
|
vector<SecureInputFile> to_upload_;
|
2018-04-05 15:06:03 +02:00
|
|
|
optional<SecureInputFile> selfie_;
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
class UploadCallback;
|
|
|
|
std::shared_ptr<UploadCallback> upload_callback_;
|
|
|
|
|
|
|
|
enum class State { WaitSecret, WaitSetValue } state_ = State::WaitSecret;
|
|
|
|
|
|
|
|
class UploadCallback : public FileManager::UploadCallback {
|
|
|
|
public:
|
|
|
|
explicit UploadCallback(ActorId<SetSecureValue> actor_id);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<SetSecureValue> actor_id_;
|
|
|
|
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) override;
|
|
|
|
void on_upload_encrypted_ok(FileId file_id, tl_object_ptr<telegram_api::InputEncryptedFile> input_file) override;
|
|
|
|
void on_upload_secure_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file) override;
|
|
|
|
void on_upload_error(FileId file_id, Status error) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file);
|
|
|
|
void on_upload_error(FileId file_id, Status error);
|
|
|
|
|
|
|
|
void on_error(Status status);
|
|
|
|
|
|
|
|
void on_secret(Result<secure_storage::Secret> r_secret, bool x);
|
|
|
|
|
|
|
|
void start_up() override;
|
2018-04-05 15:06:03 +02:00
|
|
|
void tear_down() override;
|
2018-04-05 13:18:47 +02:00
|
|
|
|
|
|
|
void loop() override;
|
|
|
|
void on_result(NetQueryPtr query) override;
|
2018-04-05 15:06:03 +02:00
|
|
|
|
|
|
|
void start_upload(FileManager *file_manager, FileId file_id, SecureInputFile &info);
|
2018-04-06 15:41:38 +02:00
|
|
|
void merge(FileManager *file_manager, FileId file_id, EncryptedSecureFile &encrypted_file);
|
2018-04-05 13:18:47 +02:00
|
|
|
};
|
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
class SecureManager : public NetQueryCallback {
|
2018-04-05 13:18:47 +02:00
|
|
|
public:
|
|
|
|
SecureManager(ActorShared<> parent);
|
|
|
|
|
|
|
|
void get_secure_value(std::string password, SecureValueType type, Promise<TdApiSecureValue> promise);
|
|
|
|
void set_secure_value(string password, SecureValue secure_value, Promise<TdApiSecureValue> promise);
|
|
|
|
|
2018-04-06 21:37:30 +02:00
|
|
|
void get_passport_authorization_form(string password, int32 bot_id, string scope, string public_key, string payload,
|
2018-04-06 15:24:29 +02:00
|
|
|
Promise<TdApiAuthorizationForm> promise);
|
|
|
|
void send_passport_authorization_form(string password, int32 authorization_form_id,
|
|
|
|
std::vector<SecureValueType> types, Promise<> promise);
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
private:
|
|
|
|
ActorShared<> parent_;
|
|
|
|
int32 refcnt_{1};
|
|
|
|
std::map<SecureValueType, ActorOwn<>> set_secure_value_queries_;
|
|
|
|
|
2018-04-06 15:24:29 +02:00
|
|
|
struct AuthorizationForm {
|
|
|
|
int32 bot_id;
|
2018-04-06 21:37:30 +02:00
|
|
|
string scope;
|
2018-04-06 15:24:29 +02:00
|
|
|
string public_key;
|
2018-04-06 21:37:30 +02:00
|
|
|
string payload;
|
2018-04-06 15:24:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::map<int32, AuthorizationForm> authorization_forms_;
|
|
|
|
int32 authorization_form_id_{0};
|
|
|
|
|
2018-04-05 13:18:47 +02:00
|
|
|
void hangup() override;
|
|
|
|
void hangup_shared() override;
|
|
|
|
void dec_refcnt();
|
2018-04-06 21:37:30 +02:00
|
|
|
void do_get_secure_value(std::string password, SecureValueType type, Promise<SecureValueWithCredentials> promise);
|
|
|
|
void do_send_passport_authorization_form(int32 authorization_form_id, vector<SecureValueCredentials> credentials,
|
|
|
|
Promise<> promise);
|
|
|
|
|
|
|
|
void on_result(NetQueryPtr query) override;
|
|
|
|
Container<Promise<NetQueryPtr>> container_;
|
|
|
|
void send_with_promise(NetQueryPtr query, Promise<NetQueryPtr> promise);
|
2018-04-05 13:18:47 +02:00
|
|
|
};
|
|
|
|
} // namespace td
|