Miscellaneous fixes

GitOrigin-RevId: cecc8cd20352bbcd947388b4eee44e4378cc2b5f
This commit is contained in:
Arseny Smirnov 2018-04-10 20:10:20 +03:00
parent 16fb0f5969
commit cf015abf64
6 changed files with 59 additions and 67 deletions

View File

@ -363,7 +363,13 @@ void PasswordManager::do_update_password_settings(UpdateSettings update_settings
} }
} }
if (!state.has_password || (update_settings.update_password && update_settings.new_password.empty())) { // Has no password and not setting one.
if (!update_settings.update_password && !state.has_password) {
update_settings.update_secure_secret = false;
}
// Setting an empty password
if (update_settings.update_password && update_settings.new_password.empty()) {
update_settings.update_secure_secret = false; update_settings.update_secure_secret = false;
} }

View File

@ -57,7 +57,7 @@ void GetSecureValue::loop() {
void GetSecureValue::start_up() { void GetSecureValue::start_up() {
std::vector<telegram_api::object_ptr<telegram_api::SecureValueType>> vec; std::vector<telegram_api::object_ptr<telegram_api::SecureValueType>> vec;
vec.push_back(get_secure_value_type_telegram_object(type_)); vec.push_back(get_secure_value_type_object(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(vec))));
@ -534,8 +534,8 @@ void SecureManager::do_send_passport_authorization_form(int32 authorization_form
} }
std::vector<telegram_api::object_ptr<telegram_api::secureValueHash>> hashes; std::vector<telegram_api::object_ptr<telegram_api::secureValueHash>> hashes;
for (auto &c : credentials) { for (auto &c : credentials) {
hashes.push_back(telegram_api::make_object<telegram_api::secureValueHash>( hashes.push_back(telegram_api::make_object<telegram_api::secureValueHash>(get_secure_value_type_object(c.type),
get_secure_value_type_telegram_object(c.type), BufferSlice(c.hash))); BufferSlice(c.hash)));
} }
auto r_encrypted_credentials = encrypted_credentials(credentials, it->second.payload, it->second.public_key); auto r_encrypted_credentials = encrypted_credentials(credentials, it->second.payload, it->second.public_key);

View File

@ -122,7 +122,7 @@ td_api::object_ptr<td_api::PassportDataType> get_passport_data_type_object(Secur
} }
} }
td_api::object_ptr<telegram_api::SecureValueType> get_secure_value_type_telegram_object(SecureValueType type) { td_api::object_ptr<telegram_api::SecureValueType> get_secure_value_type_object(SecureValueType type) {
switch (type) { switch (type) {
case SecureValueType::PersonalDetails: case SecureValueType::PersonalDetails:
return telegram_api::make_object<telegram_api::secureValueTypePersonalDetails>(); return telegram_api::make_object<telegram_api::secureValueTypePersonalDetails>();
@ -223,6 +223,7 @@ telegram_api::object_ptr<telegram_api::InputSecureFile> get_input_secure_file_ob
} }
td_api::object_ptr<td_api::file> get_encrypted_file_object(FileManager *file_manager, const EncryptedSecureFile &file) { td_api::object_ptr<td_api::file> get_encrypted_file_object(FileManager *file_manager, const EncryptedSecureFile &file) {
CHECK(file.file_id.is_valid());
auto file_view = file_manager->get_file_view(file.file_id); auto file_view = file_manager->get_file_view(file.file_id);
auto file_id = file_manager->register_remote( auto file_id = file_manager->register_remote(
FullRemoteFileLocation(FileType::SecureRaw, file_view.remote_location().get_id(), FullRemoteFileLocation(FileType::SecureRaw, file_view.remote_location().get_id(),
@ -245,7 +246,6 @@ vector<telegram_api::object_ptr<telegram_api::InputSecureFile>> get_input_secure
for (size_t i = 0; i < files.size(); i++) { for (size_t i = 0; i < files.size(); i++) {
res[i] = get_input_secure_file_object(file_manager, files[i], input_files[i]); res[i] = get_input_secure_file_object(file_manager, files[i], input_files[i]);
} }
LOG(ERROR) << res.size();
return res; return res;
} }
@ -350,7 +350,7 @@ telegram_api::object_ptr<telegram_api::inputSecureValue> get_input_secure_value_
CHECK(selfie); CHECK(selfie);
} }
return telegram_api::make_object<telegram_api::inputSecureValue>( return telegram_api::make_object<telegram_api::inputSecureValue>(
flags, get_secure_value_type_telegram_object(value.type), is_plain ? nullptr : get_secure_data_object(value.data), flags, get_secure_value_type_object(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), 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); has_selfie ? get_input_secure_file_object(file_manager, value.selfie, *selfie) : nullptr);
} }
@ -413,6 +413,7 @@ Result<SecureValue> get_secure_value(FileManager *file_manager,
} }
return res; return res;
} }
td_api::object_ptr<td_api::passportData> get_passport_data_object(FileManager *file_manager, const SecureValue &value) { td_api::object_ptr<td_api::passportData> get_passport_data_object(FileManager *file_manager, const SecureValue &value) {
std::vector<td_api::object_ptr<td_api::file>> files; std::vector<td_api::object_ptr<td_api::file>> files;
files = transform(value.files, [&](FileId id) { return file_manager->get_file_object(id, true); }); files = transform(value.files, [&](FileId id) { return file_manager->get_file_object(id, true); });
@ -436,7 +437,7 @@ Result<std::pair<FileId, SecureFileCredentials>> decrypt_secure_file(FileManager
const secure_storage::Secret &master_secret, const secure_storage::Secret &master_secret,
const EncryptedSecureFile &secure_file) { const EncryptedSecureFile &secure_file) {
if (!secure_file.file_id.is_valid()) { if (!secure_file.file_id.is_valid()) {
return std::make_pair(secure_file.file_id, SecureFileCredentials()); return std::make_pair(FileId(), SecureFileCredentials());
} }
TRY_RESULT(hash, secure_storage::ValueHash::create(secure_file.file_hash)); TRY_RESULT(hash, secure_storage::ValueHash::create(secure_file.file_hash));
TRY_RESULT(encrypted_secret, secure_storage::EncryptedSecret::create(secure_file.encrypted_secret)); TRY_RESULT(encrypted_secret, secure_storage::EncryptedSecret::create(secure_file.encrypted_secret));
@ -509,9 +510,12 @@ Result<vector<SecureValueWithCredentials>> decrypt_encrypted_secure_values(
vector<SecureValueWithCredentials> result; vector<SecureValueWithCredentials> result;
result.reserve(encrypted_secure_values.size()); result.reserve(encrypted_secure_values.size());
for (auto &encrypted_secure_value : encrypted_secure_values) { for (auto &encrypted_secure_value : encrypted_secure_values) {
TRY_RESULT(secure_value_with_credentials, auto r_secure_value_with_credentials = decrypt_encrypted_secure_value(file_manager, secret, encrypted_secure_value);
decrypt_encrypted_secure_value(file_manager, secret, encrypted_secure_value)); if (r_secure_value_with_credentials.is_ok()) {
result.push_back(std::move(secure_value_with_credentials)); result.push_back(r_secure_value_with_credentials.move_as_ok());
} else {
LOG(ERROR) << "Cannot decrypt secure value: " << r_secure_value_with_credentials.error();
}
} }
return std::move(result); return std::move(result);
} }
@ -584,56 +588,38 @@ EncryptedSecureValue encrypt_secure_value(FileManager *file_manager, const secur
return res; return res;
} }
template <class T> JsonScope &to_json(JsonValueScope &scope, const SecureDataCredentials &credentials) {
class AsJsonable : public Jsonable {
public:
explicit AsJsonable(const T &value) : value_(value) {
}
void store(JsonValueScope *scope) const {
*scope + value_;
}
private:
const T &value_;
};
template <class T>
auto as_jsonable(const T &value) {
return AsJsonable<T>(value);
}
JsonScope &operator+(JsonValueScope &scope, const SecureDataCredentials &credentials) {
auto object = scope.enter_object(); auto object = scope.enter_object();
object << ctie("data_hash", base64_encode(credentials.hash)); object << ctie("data_hash", base64_encode(credentials.hash));
object << ctie("secret", base64_encode(credentials.secret)); object << ctie("secret", base64_encode(credentials.secret));
return scope; return scope;
} }
JsonScope &operator+(JsonValueScope &scope, const SecureFileCredentials &credentials) { JsonScope &to_json(JsonValueScope &scope, const SecureFileCredentials &credentials) {
auto object = scope.enter_object(); auto object = scope.enter_object();
object << ctie("file_hash", base64_encode(credentials.hash)); object << ctie("file_hash", base64_encode(credentials.hash));
object << ctie("secret", base64_encode(credentials.secret)); object << ctie("secret", base64_encode(credentials.secret));
return scope; return scope;
} }
JsonScope &operator+(JsonValueScope &scope, const vector<SecureFileCredentials> &files) { JsonScope &to_json(JsonValueScope &scope, const vector<SecureFileCredentials> &files) {
auto arr = scope.enter_array(); auto arr = scope.enter_array();
for (auto &file : files) { for (auto &file : files) {
arr << as_jsonable(file); arr << ToJson(file);
} }
return scope; return scope;
} }
JsonScope &operator+(JsonValueScope &scope, const SecureValueCredentials &credentials) { JsonScope &to_json(JsonValueScope &scope, const SecureValueCredentials &credentials) {
auto object = scope.enter_object(); auto object = scope.enter_object();
if (credentials.data) { if (credentials.data) {
object << ctie("data", as_jsonable(credentials.data.value())); object << ctie("data", ToJson(credentials.data.value()));
} }
if (!credentials.files.empty()) { if (!credentials.files.empty()) {
object << ctie("files", as_jsonable(credentials.files)); object << ctie("files", ToJson(credentials.files));
} }
if (credentials.selfie) { if (credentials.selfie) {
object << ctie("selfie", as_jsonable(credentials.selfie.value())); object << ctie("selfie", ToJson(credentials.selfie.value()));
} }
return scope; return scope;
} }
@ -667,26 +653,25 @@ Slice secure_value_type_as_slice(SecureValueType type) {
} }
} }
JsonScope &operator+(JsonValueScope &scope, const std::vector<SecureValueCredentials> &credentials) { JsonScope &to_json(JsonValueScope &scope, const std::vector<SecureValueCredentials> &credentials) {
auto object = scope.enter_object(); auto object = scope.enter_object();
for (auto &c : credentials) { for (auto &c : credentials) {
object << ctie(secure_value_type_as_slice(c.type), as_jsonable(c)); object << ctie(secure_value_type_as_slice(c.type), ToJson(c));
} }
return scope; return scope;
} }
JsonScope &operator+(JsonValueScope &scope, JsonScope &to_json(JsonValueScope &scope,
const std::tuple<const std::vector<SecureValueCredentials> &, const Slice &> &credentials) { const std::tuple<const std::vector<SecureValueCredentials> &, const Slice &> &credentials) {
auto object = scope.enter_object(); auto object = scope.enter_object();
object << ctie("secure_data", as_jsonable(std::get<0>(credentials))); object << ctie("secure_data", ToJson(std::get<0>(credentials)));
object << ctie("payload", std::get<1>(credentials)); object << ctie("payload", std::get<1>(credentials));
return scope; return scope;
} }
Result<EncryptedSecureCredentials> encrypted_credentials(std::vector<SecureValueCredentials> &credentials, Result<EncryptedSecureCredentials> encrypted_credentials(std::vector<SecureValueCredentials> &credentials,
Slice payload, Slice public_key) { Slice payload, Slice public_key) {
auto encoded_credentials = json_encode<std::string>(as_jsonable(ctie(credentials, payload))); auto encoded_credentials = json_encode<std::string>(ToJson(ctie(credentials, payload)));
LOG(ERROR) << encoded_credentials;
auto secret = secure_storage::Secret::create_new(); auto secret = secure_storage::Secret::create_new();
auto encrypted_value = secure_storage::encrypt_value(secret, encoded_credentials).move_as_ok(); auto encrypted_value = secure_storage::encrypt_value(secret, encoded_credentials).move_as_ok();

View File

@ -46,7 +46,7 @@ vector<SecureValueType> get_secure_value_types_td_api(
vector<tl_object_ptr<td_api::PassportDataType>> &&secure_value_types); vector<tl_object_ptr<td_api::PassportDataType>> &&secure_value_types);
td_api::object_ptr<td_api::PassportDataType> get_passport_data_type_object(SecureValueType type); td_api::object_ptr<td_api::PassportDataType> get_passport_data_type_object(SecureValueType type);
td_api::object_ptr<telegram_api::SecureValueType> get_secure_value_type_telegram_object(SecureValueType type); td_api::object_ptr<telegram_api::SecureValueType> get_secure_value_type_object(SecureValueType type);
vector<td_api::object_ptr<td_api::PassportDataType>> get_passport_data_types_object( vector<td_api::object_ptr<td_api::PassportDataType>> get_passport_data_types_object(
const vector<SecureValueType> &types); const vector<SecureValueType> &types);

View File

@ -20,28 +20,6 @@
#include <type_traits> #include <type_traits>
namespace td { namespace td {
template <class T>
class ToJsonImpl : public Jsonable {
public:
explicit ToJsonImpl(const T &value) : value_(value) {
}
void store(JsonValueScope *scope) const {
to_json(*scope, value_);
}
private:
const T &value_;
};
template <class T>
auto ToJson(const T &value) {
return ToJsonImpl<T>(value);
}
template <class T>
void to_json(JsonValueScope &jv, const T &value) {
jv << value;
}
struct JsonInt64 { struct JsonInt64 {
int64 value; int64 value;

View File

@ -740,6 +740,29 @@ StrT json_encode(const ValT &val) {
return StrT(slice.begin(), slice.size()); return StrT(slice.begin(), slice.size());
} }
template <class T>
class ToJsonImpl : public Jsonable {
public:
explicit ToJsonImpl(const T &value) : value_(value) {
}
void store(JsonValueScope *scope) const {
to_json(*scope, value_);
}
private:
const T &value_;
};
template <class T>
auto ToJson(const T &value) {
return ToJsonImpl<T>(value);
}
template <class T>
void to_json(JsonValueScope &jv, const T &value) {
jv << value;
}
bool has_json_object_field(JsonObject &object, Slice name); bool has_json_object_field(JsonObject &object, Slice name);
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type, Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type,