2018-03-18 12:51:14 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "passport/passport_form_controller.h"
|
|
|
|
|
2018-03-21 08:35:32 +04:00
|
|
|
#include "passport/passport_encryption.h"
|
2018-03-31 05:45:40 +04:00
|
|
|
#include "passport/passport_panel_controller.h"
|
2018-03-18 12:51:14 +04:00
|
|
|
#include "boxes/confirm_box.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2018-03-19 20:22:27 +04:00
|
|
|
#include "base/openssl_help.h"
|
2018-03-18 12:51:14 +04:00
|
|
|
#include "mainwindow.h"
|
2018-03-31 05:45:40 +04:00
|
|
|
#include "window/window_controller.h"
|
2018-03-25 15:37:57 +04:00
|
|
|
#include "auth_session.h"
|
|
|
|
#include "storage/localimageloader.h"
|
2018-03-29 01:01:28 +04:00
|
|
|
#include "storage/localstorage.h"
|
2018-03-25 15:37:57 +04:00
|
|
|
#include "storage/file_upload.h"
|
|
|
|
#include "storage/file_download.h"
|
2018-03-18 12:51:14 +04:00
|
|
|
|
|
|
|
namespace Passport {
|
2018-03-29 00:40:42 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
QImage ReadImage(bytes::const_span buffer) {
|
|
|
|
return App::readImage(QByteArray::fromRawData(
|
|
|
|
reinterpret_cast<const char*>(buffer.data()),
|
|
|
|
buffer.size()));
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
Value::Type ConvertType(const MTPSecureValueType &type) {
|
|
|
|
using Type = Value::Type;
|
|
|
|
switch (type.type()) {
|
|
|
|
case mtpc_secureValueTypePersonalDetails: return Type::PersonalDetails;
|
|
|
|
case mtpc_secureValueTypePassport: return Type::Passport;
|
|
|
|
case mtpc_secureValueTypeDriverLicense: return Type::DriverLicense;
|
|
|
|
case mtpc_secureValueTypeIdentityCard: return Type::IdentityCard;
|
|
|
|
case mtpc_secureValueTypeAddress: return Type::Address;
|
|
|
|
case mtpc_secureValueTypeUtilityBill: return Type::UtilityBill;
|
|
|
|
case mtpc_secureValueTypeBankStatement: return Type::BankStatement;
|
|
|
|
case mtpc_secureValueTypeRentalAgreement: return Type::RentalAgreement;
|
|
|
|
case mtpc_secureValueTypePhone: return Type::Phone;
|
|
|
|
case mtpc_secureValueTypeEmail: return Type::Email;
|
|
|
|
}
|
|
|
|
Unexpected("Type in secureValueType type.");
|
|
|
|
};
|
|
|
|
|
2018-03-29 00:40:42 +04:00
|
|
|
} // namespace
|
2018-03-18 12:51:14 +04:00
|
|
|
|
|
|
|
FormRequest::FormRequest(
|
|
|
|
UserId botId,
|
2018-03-27 17:00:13 +04:00
|
|
|
const QString &scope,
|
2018-03-18 12:51:14 +04:00
|
|
|
const QString &callbackUrl,
|
|
|
|
const QString &publicKey)
|
|
|
|
: botId(botId)
|
|
|
|
, scope(scope)
|
|
|
|
, callbackUrl(callbackUrl)
|
|
|
|
, publicKey(publicKey) {
|
|
|
|
}
|
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
EditFile::EditFile(
|
2018-04-03 22:24:31 +04:00
|
|
|
not_null<const Value*> value,
|
2018-03-25 15:37:57 +04:00
|
|
|
const File &fields,
|
2018-03-29 18:44:34 +04:00
|
|
|
std::unique_ptr<UploadScanData> &&uploadData)
|
2018-04-03 22:24:31 +04:00
|
|
|
: value(value)
|
|
|
|
, fields(std::move(fields))
|
2018-03-29 23:49:31 +04:00
|
|
|
, uploadData(std::move(uploadData))
|
|
|
|
, guard(std::make_shared<bool>(true)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadScanDataPointer::UploadScanDataPointer(
|
|
|
|
std::unique_ptr<UploadScanData> &&value)
|
|
|
|
: _value(std::move(value)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadScanDataPointer::UploadScanDataPointer(
|
|
|
|
UploadScanDataPointer &&other) = default;
|
|
|
|
|
|
|
|
UploadScanDataPointer &UploadScanDataPointer::operator=(
|
|
|
|
UploadScanDataPointer &&other) = default;
|
|
|
|
|
|
|
|
UploadScanDataPointer::~UploadScanDataPointer() {
|
|
|
|
if (const auto value = _value.get()) {
|
|
|
|
if (const auto fullId = value->fullId) {
|
|
|
|
Auth().uploader().cancel(fullId);
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
2018-03-18 12:51:14 +04:00
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
UploadScanData *UploadScanDataPointer::get() const {
|
|
|
|
return _value.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadScanDataPointer::operator UploadScanData*() const {
|
|
|
|
return _value.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadScanDataPointer::operator bool() const {
|
|
|
|
return _value.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadScanData *UploadScanDataPointer::operator->() const {
|
|
|
|
return _value.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Value(Type type) : type(type) {
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
|
2018-03-18 12:51:14 +04:00
|
|
|
FormController::FormController(
|
|
|
|
not_null<Window::Controller*> controller,
|
|
|
|
const FormRequest &request)
|
|
|
|
: _controller(controller)
|
2018-03-31 05:45:40 +04:00
|
|
|
, _request(request)
|
|
|
|
, _view(std::make_unique<PanelController>(this)) {
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::show() {
|
|
|
|
requestForm();
|
|
|
|
requestPassword();
|
|
|
|
}
|
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
UserData *FormController::bot() const {
|
|
|
|
return _bot;
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
QString FormController::privacyPolicyUrl() const {
|
|
|
|
return _form.privacyPolicyUrl;
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:00:13 +04:00
|
|
|
bytes::vector FormController::passwordHashForAuth(
|
|
|
|
bytes::const_span password) const {
|
|
|
|
return openssl::Sha256(bytes::concatenate(
|
|
|
|
_password.salt,
|
|
|
|
password,
|
|
|
|
_password.salt));
|
|
|
|
}
|
|
|
|
|
2018-03-19 20:22:27 +04:00
|
|
|
void FormController::submitPassword(const QString &password) {
|
2018-03-27 17:00:13 +04:00
|
|
|
Expects(!_password.salt.empty());
|
2018-03-19 20:22:27 +04:00
|
|
|
|
|
|
|
if (_passwordCheckRequestId) {
|
|
|
|
return;
|
2018-03-21 08:35:32 +04:00
|
|
|
} else if (password.isEmpty()) {
|
|
|
|
_passwordError.fire(QString());
|
2018-03-19 20:22:27 +04:00
|
|
|
}
|
2018-03-21 08:35:32 +04:00
|
|
|
const auto passwordBytes = password.toUtf8();
|
2018-03-19 20:22:27 +04:00
|
|
|
_passwordCheckRequestId = request(MTPaccount_GetPasswordSettings(
|
2018-03-27 17:00:13 +04:00
|
|
|
MTP_bytes(passwordHashForAuth(bytes::make_span(passwordBytes)))
|
2018-03-19 20:22:27 +04:00
|
|
|
)).handleFloodErrors(
|
|
|
|
).done([=](const MTPaccount_PasswordSettings &result) {
|
|
|
|
Expects(result.type() == mtpc_account_passwordSettings);
|
|
|
|
|
|
|
|
_passwordCheckRequestId = 0;
|
|
|
|
const auto &data = result.c_account_passwordSettings();
|
2018-03-27 17:00:13 +04:00
|
|
|
_password.confirmedEmail = qs(data.vemail);
|
|
|
|
validateSecureSecret(
|
|
|
|
bytes::make_span(data.vsecure_salt.v),
|
2018-03-27 16:16:00 +04:00
|
|
|
bytes::make_span(data.vsecure_secret.v),
|
2018-03-27 17:00:13 +04:00
|
|
|
bytes::make_span(passwordBytes));
|
2018-03-19 20:22:27 +04:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_passwordCheckRequestId = 0;
|
|
|
|
if (MTP::isFloodError(error)) {
|
|
|
|
_passwordError.fire(lang(lng_flood_error));
|
|
|
|
} else if (error.type() == qstr("PASSWORD_HASH_INVALID")) {
|
|
|
|
_passwordError.fire(lang(lng_passport_password_wrong));
|
|
|
|
} else {
|
|
|
|
_passwordError.fire_copy(error.type());
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:00:13 +04:00
|
|
|
void FormController::validateSecureSecret(
|
|
|
|
bytes::const_span salt,
|
|
|
|
bytes::const_span encryptedSecret,
|
|
|
|
bytes::const_span password) {
|
|
|
|
if (!salt.empty() && !encryptedSecret.empty()) {
|
|
|
|
_secret = DecryptSecureSecret(salt, encryptedSecret, password);
|
|
|
|
if (_secret.empty()) {
|
2018-03-29 18:44:34 +04:00
|
|
|
_secretId = 0;
|
2018-03-27 17:00:13 +04:00
|
|
|
LOG(("API Error: Failed to decrypt secure secret. "
|
|
|
|
"Forgetting all files and data :("));
|
2018-04-03 22:24:31 +04:00
|
|
|
for (auto &[type, value] : _form.values) {
|
2018-03-29 18:44:34 +04:00
|
|
|
if (!value.data.original.isEmpty()) {
|
|
|
|
resetValue(value);
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-29 18:44:34 +04:00
|
|
|
_secretId = CountSecureSecretHash(_secret);
|
|
|
|
decryptValues();
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_secret.empty()) {
|
|
|
|
generateSecret(password);
|
|
|
|
}
|
|
|
|
_secretReady.fire({});
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
void FormController::decryptValues() {
|
2018-03-27 17:00:13 +04:00
|
|
|
Expects(!_secret.empty());
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
for (auto &[type, value] : _form.values) {
|
2018-03-29 18:44:34 +04:00
|
|
|
decryptValue(value);
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
void FormController::decryptValue(Value &value) {
|
2018-03-27 17:00:13 +04:00
|
|
|
Expects(!_secret.empty());
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
if (!validateValueSecrets(value)) {
|
|
|
|
resetValue(value);
|
2018-03-27 17:00:13 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-04-06 22:47:29 +04:00
|
|
|
if (!value.data.original.isEmpty()) {
|
|
|
|
value.data.parsed.fields = DeserializeData(DecryptData(
|
|
|
|
bytes::make_span(value.data.original),
|
|
|
|
value.data.hash,
|
|
|
|
value.data.secret));
|
|
|
|
}
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
bool FormController::validateValueSecrets(Value &value) {
|
2018-04-06 22:47:29 +04:00
|
|
|
if (!value.data.original.isEmpty()) {
|
|
|
|
value.data.secret = DecryptValueSecret(
|
|
|
|
value.data.encryptedSecret,
|
|
|
|
_secret,
|
|
|
|
value.data.hash);
|
|
|
|
if (value.data.secret.empty()) {
|
|
|
|
LOG(("API Error: Could not decrypt data secret. "
|
|
|
|
"Forgetting files and data :("));
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
2018-03-29 18:44:34 +04:00
|
|
|
for (auto &file : value.files) {
|
|
|
|
file.secret = DecryptValueSecret(
|
|
|
|
file.encryptedSecret,
|
|
|
|
_secret,
|
|
|
|
file.hash);
|
|
|
|
if (file.secret.empty()) {
|
|
|
|
LOG(("API Error: Could not decrypt file secret. "
|
|
|
|
"Forgetting files and data :("));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:00:52 +04:00
|
|
|
if (value.selfie) {
|
|
|
|
auto &file = *value.selfie;
|
|
|
|
file.secret = DecryptValueSecret(
|
|
|
|
file.encryptedSecret,
|
|
|
|
_secret,
|
|
|
|
file.hash);
|
|
|
|
if (file.secret.empty()) {
|
|
|
|
LOG(("API Error: Could not decrypt selfie secret. "
|
|
|
|
"Forgetting files and data :("));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 17:00:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
void FormController::resetValue(Value &value) {
|
|
|
|
value = Value(value.type);
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
|
|
|
|
2018-03-19 20:22:27 +04:00
|
|
|
rpl::producer<QString> FormController::passwordError() const {
|
|
|
|
return _passwordError.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormController::passwordHint() const {
|
|
|
|
return _password.hint;
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
void FormController::uploadScan(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
QByteArray &&content) {
|
|
|
|
const auto nonconst = findValue(value);
|
|
|
|
auto fileIndex = int(nonconst->filesInEdit.size());
|
|
|
|
nonconst->filesInEdit.emplace_back(
|
|
|
|
nonconst,
|
2018-03-29 00:40:42 +04:00
|
|
|
File(),
|
|
|
|
nullptr);
|
2018-04-03 22:24:31 +04:00
|
|
|
auto &file = nonconst->filesInEdit.back();
|
2018-04-10 23:00:52 +04:00
|
|
|
encryptFile(file, std::move(content), [=](UploadScanData &&result) {
|
|
|
|
Expects(fileIndex >= 0 && fileIndex < nonconst->filesInEdit.size());
|
|
|
|
|
|
|
|
uploadEncryptedFile(
|
|
|
|
nonconst->filesInEdit[fileIndex],
|
|
|
|
std::move(result));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::deleteScan(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
int fileIndex) {
|
|
|
|
scanDeleteRestore(value, fileIndex, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::restoreScan(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
int fileIndex) {
|
|
|
|
scanDeleteRestore(value, fileIndex, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::uploadSelfie(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
QByteArray &&content) {
|
|
|
|
const auto nonconst = findValue(value);
|
|
|
|
nonconst->selfieInEdit = EditFile{ nonconst, File(), nullptr };
|
|
|
|
auto &file = *nonconst->selfieInEdit;
|
|
|
|
encryptFile(file, std::move(content), [=](UploadScanData &&result) {
|
|
|
|
uploadEncryptedFile(
|
|
|
|
*nonconst->selfieInEdit,
|
|
|
|
std::move(result));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::deleteSelfie(not_null<const Value*> value) {
|
|
|
|
selfieDeleteRestore(value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::restoreSelfie(not_null<const Value*> value) {
|
|
|
|
selfieDeleteRestore(value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::prepareFile(
|
|
|
|
EditFile &file,
|
|
|
|
const QByteArray &content) {
|
|
|
|
const auto fileId = rand_value<uint64>();
|
2018-03-29 00:40:42 +04:00
|
|
|
file.fields.size = content.size();
|
|
|
|
file.fields.id = fileId;
|
|
|
|
file.fields.dcId = MTP::maindc();
|
2018-03-29 18:44:34 +04:00
|
|
|
file.fields.secret = GenerateSecretBytes();
|
|
|
|
file.fields.date = unixtime();
|
2018-03-29 00:40:42 +04:00
|
|
|
file.fields.image = ReadImage(bytes::make_span(content));
|
|
|
|
file.fields.downloadOffset = file.fields.size;
|
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(&file);
|
2018-03-29 00:40:42 +04:00
|
|
|
}
|
2018-03-25 15:37:57 +04:00
|
|
|
|
2018-04-10 23:00:52 +04:00
|
|
|
void FormController::encryptFile(
|
|
|
|
EditFile &file,
|
|
|
|
QByteArray &&content,
|
|
|
|
base::lambda<void(UploadScanData &&result)> callback) {
|
|
|
|
prepareFile(file, content);
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
const auto weak = std::weak_ptr<bool>(file.guard);
|
2018-03-25 15:37:57 +04:00
|
|
|
crl::async([
|
|
|
|
=,
|
2018-03-29 23:49:31 +04:00
|
|
|
fileId = file.fields.id,
|
2018-03-25 15:37:57 +04:00
|
|
|
bytes = std::move(content),
|
2018-03-29 23:49:31 +04:00
|
|
|
fileSecret = file.fields.secret
|
2018-03-25 15:37:57 +04:00
|
|
|
] {
|
|
|
|
auto data = EncryptData(
|
2018-03-27 16:16:00 +04:00
|
|
|
bytes::make_span(bytes),
|
2018-03-29 18:44:34 +04:00
|
|
|
fileSecret);
|
|
|
|
auto result = UploadScanData();
|
2018-03-29 00:40:42 +04:00
|
|
|
result.fileId = fileId;
|
2018-03-25 15:37:57 +04:00
|
|
|
result.hash = std::move(data.hash);
|
|
|
|
result.bytes = std::move(data.bytes);
|
|
|
|
result.md5checksum.resize(32);
|
|
|
|
hashMd5Hex(
|
|
|
|
result.bytes.data(),
|
|
|
|
result.bytes.size(),
|
|
|
|
result.md5checksum.data());
|
|
|
|
crl::on_main([=, encrypted = std::move(result)]() mutable {
|
2018-03-29 23:49:31 +04:00
|
|
|
if (weak.lock()) {
|
2018-04-10 23:00:52 +04:00
|
|
|
callback(std::move(encrypted));
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:45:40 +04:00
|
|
|
void FormController::scanDeleteRestore(
|
2018-04-03 22:24:31 +04:00
|
|
|
not_null<const Value*> value,
|
2018-03-31 05:45:40 +04:00
|
|
|
int fileIndex,
|
|
|
|
bool deleted) {
|
2018-04-03 22:24:31 +04:00
|
|
|
Expects(fileIndex >= 0 && fileIndex < value->filesInEdit.size());
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
const auto nonconst = findValue(value);
|
|
|
|
auto &file = nonconst->filesInEdit[fileIndex];
|
2018-03-31 05:45:40 +04:00
|
|
|
file.deleted = deleted;
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(&file);
|
2018-03-29 00:40:42 +04:00
|
|
|
}
|
|
|
|
|
2018-04-10 23:00:52 +04:00
|
|
|
void FormController::selfieDeleteRestore(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
bool deleted) {
|
|
|
|
Expects(value->selfieInEdit.has_value());
|
|
|
|
|
|
|
|
const auto nonconst = findValue(value);
|
|
|
|
auto &file = *nonconst->selfieInEdit;
|
|
|
|
file.deleted = deleted;
|
|
|
|
_scanUpdated.fire(&file);
|
|
|
|
}
|
|
|
|
|
2018-03-25 15:37:57 +04:00
|
|
|
void FormController::subscribeToUploader() {
|
|
|
|
if (_uploaderSubscriptions) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-29 00:40:42 +04:00
|
|
|
|
|
|
|
using namespace Storage;
|
2018-03-29 18:44:34 +04:00
|
|
|
|
2018-03-25 15:37:57 +04:00
|
|
|
Auth().uploader().secureReady(
|
2018-03-29 00:40:42 +04:00
|
|
|
) | rpl::start_with_next([=](const UploadSecureDone &data) {
|
|
|
|
scanUploadDone(data);
|
2018-03-25 15:37:57 +04:00
|
|
|
}, _uploaderSubscriptions);
|
2018-03-29 18:44:34 +04:00
|
|
|
|
2018-03-25 15:37:57 +04:00
|
|
|
Auth().uploader().secureProgress(
|
2018-03-29 00:40:42 +04:00
|
|
|
) | rpl::start_with_next([=](const UploadSecureProgress &data) {
|
|
|
|
scanUploadProgress(data);
|
2018-03-25 15:37:57 +04:00
|
|
|
}, _uploaderSubscriptions);
|
2018-03-29 18:44:34 +04:00
|
|
|
|
2018-03-25 15:37:57 +04:00
|
|
|
Auth().uploader().secureFailed(
|
|
|
|
) | rpl::start_with_next([=](const FullMsgId &fullId) {
|
2018-03-29 00:40:42 +04:00
|
|
|
scanUploadFail(fullId);
|
2018-03-25 15:37:57 +04:00
|
|
|
}, _uploaderSubscriptions);
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:00:52 +04:00
|
|
|
void FormController::uploadEncryptedFile(
|
|
|
|
EditFile &file,
|
2018-03-29 18:44:34 +04:00
|
|
|
UploadScanData &&data) {
|
2018-03-25 15:37:57 +04:00
|
|
|
subscribeToUploader();
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
file.uploadData = std::make_unique<UploadScanData>(std::move(data));
|
2018-03-25 15:37:57 +04:00
|
|
|
|
2018-03-29 01:01:28 +04:00
|
|
|
auto prepared = std::make_shared<FileLoadResult>(
|
2018-03-25 15:37:57 +04:00
|
|
|
TaskId(),
|
2018-03-29 18:44:34 +04:00
|
|
|
file.uploadData->fileId,
|
2018-03-25 15:37:57 +04:00
|
|
|
FileLoadTo(PeerId(0), false, MsgId(0)),
|
|
|
|
TextWithTags(),
|
|
|
|
std::shared_ptr<SendingAlbum>(nullptr));
|
2018-03-29 01:01:28 +04:00
|
|
|
prepared->type = SendMediaType::Secure;
|
|
|
|
prepared->content = QByteArray::fromRawData(
|
2018-03-29 18:44:34 +04:00
|
|
|
reinterpret_cast<char*>(file.uploadData->bytes.data()),
|
|
|
|
file.uploadData->bytes.size());
|
2018-03-29 01:01:28 +04:00
|
|
|
prepared->setFileData(prepared->content);
|
2018-03-29 18:44:34 +04:00
|
|
|
prepared->filemd5 = file.uploadData->md5checksum;
|
2018-03-25 15:37:57 +04:00
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
file.uploadData->fullId = FullMsgId(0, clientMsgId());
|
|
|
|
Auth().uploader().upload(file.uploadData->fullId, std::move(prepared));
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:40:42 +04:00
|
|
|
void FormController::scanUploadDone(const Storage::UploadSecureDone &data) {
|
|
|
|
if (const auto file = findEditFile(data.fullId)) {
|
2018-03-29 18:44:34 +04:00
|
|
|
Assert(file->uploadData != nullptr);
|
|
|
|
Assert(file->uploadData->fileId == data.fileId);
|
2018-03-25 15:37:57 +04:00
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
file->uploadData->partsCount = data.partsCount;
|
|
|
|
file->fields.hash = std::move(file->uploadData->hash);
|
|
|
|
file->fields.encryptedSecret = EncryptValueSecret(
|
|
|
|
file->fields.secret,
|
|
|
|
_secret,
|
|
|
|
file->fields.hash);
|
|
|
|
file->uploadData->fullId = FullMsgId();
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(file);
|
2018-03-29 00:40:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::scanUploadProgress(
|
|
|
|
const Storage::UploadSecureProgress &data) {
|
|
|
|
if (const auto file = findEditFile(data.fullId)) {
|
2018-03-29 18:44:34 +04:00
|
|
|
Assert(file->uploadData != nullptr);
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
file->uploadData->offset = data.offset;
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(file);
|
2018-03-29 00:40:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::scanUploadFail(const FullMsgId &fullId) {
|
|
|
|
if (const auto file = findEditFile(fullId)) {
|
2018-03-29 18:44:34 +04:00
|
|
|
Assert(file->uploadData != nullptr);
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
file->uploadData->offset = -1;
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(file);
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 20:22:27 +04:00
|
|
|
rpl::producer<> FormController::secretReadyEvents() const {
|
|
|
|
return _secretReady.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormController::defaultEmail() const {
|
2018-03-27 17:00:13 +04:00
|
|
|
return _password.confirmedEmail;
|
2018-03-19 20:22:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString FormController::defaultPhoneNumber() const {
|
|
|
|
if (const auto self = App::self()) {
|
|
|
|
return self->phone();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
auto FormController::scanUpdated() const
|
|
|
|
-> rpl::producer<not_null<const EditFile*>> {
|
2018-03-25 15:37:57 +04:00
|
|
|
return _scanUpdated.events();
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
auto FormController::valueSaveFinished() const
|
|
|
|
-> rpl::producer<not_null<const Value*>> {
|
|
|
|
return _valueSaveFinished.events();
|
2018-04-06 22:47:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
auto FormController::verificationNeeded() const
|
2018-04-09 21:56:07 +04:00
|
|
|
-> rpl::producer<not_null<const Value*>> {
|
2018-04-06 22:47:29 +04:00
|
|
|
return _verificationNeeded.events();
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
auto FormController::verificationUpdate() const
|
|
|
|
-> rpl::producer<not_null<const Value*>> {
|
|
|
|
return _verificationUpdate.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::verify(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
const QString &code) {
|
|
|
|
if (value->verification.requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto nonconst = findValue(value);
|
|
|
|
const auto prepared = code.trimmed();
|
|
|
|
Assert(nonconst->verification.codeLength != 0);
|
|
|
|
verificationError(nonconst, QString());
|
|
|
|
if (nonconst->verification.codeLength > 0
|
|
|
|
&& nonconst->verification.codeLength != prepared.size()) {
|
|
|
|
verificationError(nonconst, lang(lng_signin_wrong_code));
|
|
|
|
return;
|
|
|
|
} else if (prepared.isEmpty()) {
|
|
|
|
verificationError(nonconst, lang(lng_signin_wrong_code));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nonconst->verification.requestId = [&] {
|
|
|
|
switch (nonconst->type) {
|
|
|
|
case Value::Type::Phone:
|
|
|
|
return request(MTPaccount_VerifyPhone(
|
|
|
|
MTP_string(getPhoneFromValue(nonconst)),
|
|
|
|
MTP_string(nonconst->verification.phoneCodeHash),
|
|
|
|
MTP_string(prepared)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
savePlainTextValue(nonconst);
|
|
|
|
clearValueVerification(nonconst);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
nonconst->verification.requestId = 0;
|
|
|
|
if (error.type() == qstr("PHONE_CODE_INVALID")) {
|
2018-04-12 14:20:54 +04:00
|
|
|
verificationError(
|
|
|
|
nonconst,
|
|
|
|
lang(lng_signin_wrong_code));
|
2018-04-09 21:56:07 +04:00
|
|
|
} else {
|
|
|
|
verificationError(nonconst, error.type());
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
case Value::Type::Email:
|
|
|
|
return request(MTPaccount_VerifyEmail(
|
|
|
|
MTP_string(getEmailFromValue(nonconst)),
|
|
|
|
MTP_string(prepared)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
savePlainTextValue(nonconst);
|
|
|
|
clearValueVerification(nonconst);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
nonconst->verification.requestId = 0;
|
|
|
|
if (error.type() == qstr("CODE_INVALID")) {
|
2018-04-12 14:20:54 +04:00
|
|
|
verificationError(
|
|
|
|
nonconst,
|
|
|
|
lang(lng_signin_wrong_code));
|
2018-04-09 21:56:07 +04:00
|
|
|
} else {
|
|
|
|
verificationError(nonconst, error.type());
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
Unexpected("Type in FormController::verify().");
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::verificationError(
|
|
|
|
not_null<Value*> value,
|
|
|
|
const QString &text) {
|
|
|
|
value->verification.error = text;
|
|
|
|
_verificationUpdate.fire_copy(value);
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
const Form &FormController::form() const {
|
|
|
|
return _form;
|
2018-03-19 20:22:27 +04:00
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
not_null<Value*> FormController::findValue(not_null<const Value*> value) {
|
|
|
|
const auto i = _form.values.find(value->type);
|
|
|
|
Assert(i != end(_form.values));
|
|
|
|
const auto result = &i->second;
|
2018-03-29 18:44:34 +04:00
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
Ensures(result == value);
|
|
|
|
return result;
|
|
|
|
}
|
2018-03-21 08:35:32 +04:00
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
void FormController::startValueEdit(not_null<const Value*> value) {
|
2018-04-09 21:56:07 +04:00
|
|
|
const auto nonconst = findValue(value);
|
|
|
|
++nonconst->editScreens;
|
|
|
|
if (savingValue(nonconst)) {
|
2018-04-06 22:47:29 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-04-10 23:00:52 +04:00
|
|
|
for (auto &file : nonconst->files) {
|
|
|
|
loadFile(file);
|
|
|
|
}
|
|
|
|
if (nonconst->selfie) {
|
|
|
|
loadFile(*nonconst->selfie);
|
|
|
|
}
|
2018-04-03 22:24:31 +04:00
|
|
|
nonconst->filesInEdit = ranges::view::all(
|
2018-04-09 21:56:07 +04:00
|
|
|
nonconst->files
|
2018-04-03 22:24:31 +04:00
|
|
|
) | ranges::view::transform([=](const File &file) {
|
2018-04-09 21:56:07 +04:00
|
|
|
return EditFile(nonconst, file, nullptr);
|
2018-04-03 22:24:31 +04:00
|
|
|
}) | ranges::to_vector;
|
2018-04-10 23:00:52 +04:00
|
|
|
|
|
|
|
if (nonconst->selfie) {
|
|
|
|
nonconst->selfieInEdit = EditFile(
|
|
|
|
nonconst,
|
|
|
|
*nonconst->selfie,
|
|
|
|
nullptr);
|
|
|
|
} else {
|
|
|
|
nonconst->selfieInEdit = base::none;
|
|
|
|
}
|
|
|
|
|
2018-04-06 22:47:29 +04:00
|
|
|
nonconst->data.parsedInEdit = nonconst->data.parsed;
|
2018-03-21 08:35:32 +04:00
|
|
|
}
|
|
|
|
|
2018-04-10 23:00:52 +04:00
|
|
|
void FormController::loadFile(File &file) {
|
|
|
|
if (!file.image.isNull()) {
|
|
|
|
file.downloadOffset = file.size;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-04-10 23:00:52 +04:00
|
|
|
const auto key = FileKey{ file.id, file.dcId };
|
|
|
|
const auto i = _fileLoaders.find(key);
|
|
|
|
if (i != _fileLoaders.end()) {
|
|
|
|
return;
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
2018-04-10 23:00:52 +04:00
|
|
|
file.downloadOffset = 0;
|
|
|
|
const auto [j, ok] = _fileLoaders.emplace(
|
|
|
|
key,
|
|
|
|
std::make_unique<mtpFileLoader>(
|
|
|
|
file.dcId,
|
|
|
|
file.id,
|
|
|
|
file.accessHash,
|
|
|
|
0,
|
|
|
|
SecureFileLocation,
|
|
|
|
QString(),
|
|
|
|
file.size,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
LoadFromCloudOrLocal,
|
|
|
|
false));
|
|
|
|
const auto loader = j->second.get();
|
|
|
|
loader->connect(loader, &mtpFileLoader::progress, [=] {
|
|
|
|
if (loader->finished()) {
|
|
|
|
fileLoadDone(key, loader->bytes());
|
|
|
|
} else {
|
|
|
|
fileLoadProgress(key, loader->currentOffset());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
loader->connect(loader, &mtpFileLoader::failed, [=] {
|
|
|
|
fileLoadFail(key);
|
|
|
|
});
|
|
|
|
loader->start();
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:40:42 +04:00
|
|
|
void FormController::fileLoadDone(FileKey key, const QByteArray &bytes) {
|
2018-03-29 18:44:34 +04:00
|
|
|
if (const auto [value, file] = findFile(key); file != nullptr) {
|
2018-03-25 15:37:57 +04:00
|
|
|
const auto decrypted = DecryptData(
|
2018-03-27 16:16:00 +04:00
|
|
|
bytes::make_span(bytes),
|
2018-03-29 18:44:34 +04:00
|
|
|
file->hash,
|
|
|
|
file->secret);
|
|
|
|
if (decrypted.empty()) {
|
|
|
|
fileLoadFail(key);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-29 00:40:42 +04:00
|
|
|
file->downloadOffset = file->size;
|
|
|
|
file->image = App::readImage(QByteArray::fromRawData(
|
2018-03-25 15:37:57 +04:00
|
|
|
reinterpret_cast<const char*>(decrypted.data()),
|
|
|
|
decrypted.size()));
|
2018-03-29 00:40:42 +04:00
|
|
|
if (const auto fileInEdit = findEditFile(key)) {
|
|
|
|
fileInEdit->fields.image = file->image;
|
|
|
|
fileInEdit->fields.downloadOffset = file->downloadOffset;
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(fileInEdit);
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 00:40:42 +04:00
|
|
|
void FormController::fileLoadProgress(FileKey key, int offset) {
|
2018-03-29 18:44:34 +04:00
|
|
|
if (const auto [value, file] = findFile(key); file != nullptr) {
|
2018-03-29 00:40:42 +04:00
|
|
|
file->downloadOffset = offset;
|
|
|
|
if (const auto fileInEdit = findEditFile(key)) {
|
|
|
|
fileInEdit->fields.downloadOffset = file->downloadOffset;
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(fileInEdit);
|
2018-03-29 00:40:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::fileLoadFail(FileKey key) {
|
2018-03-29 18:44:34 +04:00
|
|
|
if (const auto [value, file] = findFile(key); file != nullptr) {
|
2018-03-29 00:40:42 +04:00
|
|
|
file->downloadOffset = -1;
|
|
|
|
if (const auto fileInEdit = findEditFile(key)) {
|
|
|
|
fileInEdit->fields.downloadOffset = file->downloadOffset;
|
2018-03-29 23:49:31 +04:00
|
|
|
_scanUpdated.fire(fileInEdit);
|
2018-03-29 00:40:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
bool FormController::savingValue(not_null<const Value*> value) const {
|
|
|
|
return (value->saveRequestId != 0)
|
|
|
|
|| (value->verification.requestId != 0)
|
|
|
|
|| (value->verification.codeLength != 0);
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
void FormController::cancelValueEdit(not_null<const Value*> value) {
|
2018-04-09 21:56:07 +04:00
|
|
|
Expects(value->editScreens > 0);
|
|
|
|
|
|
|
|
const auto nonconst = findValue(value);
|
|
|
|
--nonconst->editScreens;
|
|
|
|
clearValueEdit(nonconst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::valueEditFailed(not_null<Value*> value) {
|
|
|
|
Expects(!savingValue(value));
|
|
|
|
|
|
|
|
if (value->editScreens == 0) {
|
|
|
|
clearValueEdit(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::clearValueEdit(not_null<Value*> value) {
|
|
|
|
if (savingValue(value)) {
|
2018-04-06 22:47:29 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-04-09 21:56:07 +04:00
|
|
|
value->filesInEdit.clear();
|
2018-04-10 23:00:52 +04:00
|
|
|
value->selfieInEdit = base::none;
|
2018-04-09 21:56:07 +04:00
|
|
|
value->data.encryptedSecretInEdit.clear();
|
|
|
|
value->data.hashInEdit.clear();
|
|
|
|
value->data.parsedInEdit = ValueMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::cancelValueVerification(not_null<const Value*> value) {
|
2018-04-03 22:24:31 +04:00
|
|
|
const auto nonconst = findValue(value);
|
2018-04-09 21:56:07 +04:00
|
|
|
clearValueVerification(nonconst);
|
|
|
|
if (!savingValue(nonconst)) {
|
|
|
|
valueEditFailed(nonconst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::clearValueVerification(not_null<Value*> value) {
|
|
|
|
const auto was = (value->verification.codeLength != 0);
|
|
|
|
if (const auto requestId = base::take(value->verification.requestId)) {
|
|
|
|
request(requestId).cancel();
|
|
|
|
}
|
|
|
|
value->verification = Verification();
|
|
|
|
if (was) {
|
|
|
|
_verificationUpdate.fire_copy(value);
|
|
|
|
}
|
2018-03-21 08:35:32 +04:00
|
|
|
}
|
|
|
|
|
2018-03-29 23:49:31 +04:00
|
|
|
bool FormController::isEncryptedValue(Value::Type type) const {
|
2018-04-03 22:24:31 +04:00
|
|
|
return (type != Value::Type::Phone && type != Value::Type::Email);
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
bool FormController::editFileChanged(const EditFile &file) const {
|
|
|
|
if (file.uploadData) {
|
|
|
|
return !file.deleted;
|
|
|
|
}
|
|
|
|
return file.deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FormController::editValueChanged(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
const ValueMap &data) const {
|
|
|
|
auto filesCount = 0;
|
|
|
|
for (const auto &file : value->filesInEdit) {
|
|
|
|
if (editFileChanged(file)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value->selfieInEdit && editFileChanged(*value->selfieInEdit)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto existing = value->data.parsed.fields;
|
|
|
|
for (const auto &[key, value] : data.fields) {
|
|
|
|
const auto i = existing.find(key);
|
|
|
|
if (i != existing.end()) {
|
|
|
|
if (i->second != value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
existing.erase(i);
|
|
|
|
} else if (!value.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !existing.empty();
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
void FormController::saveValueEdit(
|
|
|
|
not_null<const Value*> value,
|
|
|
|
ValueMap &&data) {
|
2018-04-09 21:56:07 +04:00
|
|
|
if (savingValue(value)) {
|
2018-04-06 22:47:29 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-04-09 21:56:07 +04:00
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
const auto nonconst = findValue(value);
|
2018-04-09 21:56:07 +04:00
|
|
|
if (!editValueChanged(nonconst, data)) {
|
2018-04-10 23:34:43 +04:00
|
|
|
nonconst->saveRequestId = -1;
|
|
|
|
crl::on_main(this, [=] {
|
|
|
|
base::take(nonconst->filesInEdit);
|
|
|
|
base::take(nonconst->selfieInEdit);
|
|
|
|
base::take(nonconst->data.encryptedSecretInEdit);
|
|
|
|
base::take(nonconst->data.hashInEdit);
|
|
|
|
base::take(nonconst->data.parsedInEdit);
|
|
|
|
nonconst->saveRequestId = 0;
|
|
|
|
_valueSaveFinished.fire_copy(nonconst);
|
|
|
|
});
|
2018-04-09 21:56:07 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-04-06 22:47:29 +04:00
|
|
|
nonconst->data.parsedInEdit = std::move(data);
|
2018-03-21 08:35:32 +04:00
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
if (isEncryptedValue(nonconst->type)) {
|
|
|
|
saveEncryptedValue(nonconst);
|
2018-03-29 23:49:31 +04:00
|
|
|
} else {
|
2018-04-03 22:24:31 +04:00
|
|
|
savePlainTextValue(nonconst);
|
2018-03-29 23:49:31 +04:00
|
|
|
}
|
2018-03-21 08:35:32 +04:00
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
void FormController::saveEncryptedValue(not_null<Value*> value) {
|
|
|
|
Expects(isEncryptedValue(value->type));
|
2018-03-21 08:35:32 +04:00
|
|
|
|
|
|
|
if (_secret.empty()) {
|
2018-03-27 17:00:13 +04:00
|
|
|
_secretCallbacks.push_back([=] {
|
2018-04-03 22:24:31 +04:00
|
|
|
saveEncryptedValue(value);
|
2018-03-21 08:35:32 +04:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2018-03-25 15:37:57 +04:00
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
const auto inputFile = [](const EditFile &file) {
|
|
|
|
if (const auto uploadData = file.uploadData.get()) {
|
|
|
|
return MTP_inputSecureFileUploaded(
|
2018-03-25 15:37:57 +04:00
|
|
|
MTP_long(file.fields.id),
|
2018-03-29 18:44:34 +04:00
|
|
|
MTP_int(uploadData->partsCount),
|
|
|
|
MTP_bytes(uploadData->md5checksum),
|
|
|
|
MTP_bytes(file.fields.hash),
|
2018-04-03 22:24:31 +04:00
|
|
|
MTP_bytes(file.fields.encryptedSecret));
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
2018-04-03 22:24:31 +04:00
|
|
|
return MTP_inputSecureFile(
|
|
|
|
MTP_long(file.fields.id),
|
|
|
|
MTP_long(file.fields.accessHash));
|
|
|
|
};
|
|
|
|
|
|
|
|
auto inputFiles = QVector<MTPInputSecureFile>();
|
|
|
|
inputFiles.reserve(value->filesInEdit.size());
|
|
|
|
for (const auto &file : value->filesInEdit) {
|
|
|
|
if (file.deleted) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
inputFiles.push_back(inputFile(file));
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
2018-03-29 18:44:34 +04:00
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
if (value->data.secret.empty()) {
|
|
|
|
value->data.secret = GenerateSecretBytes();
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
|
|
|
const auto encryptedData = EncryptData(
|
2018-04-06 22:47:29 +04:00
|
|
|
SerializeData(value->data.parsedInEdit.fields),
|
2018-04-03 22:24:31 +04:00
|
|
|
value->data.secret);
|
2018-04-06 22:47:29 +04:00
|
|
|
value->data.hashInEdit = encryptedData.hash;
|
|
|
|
value->data.encryptedSecretInEdit = EncryptValueSecret(
|
2018-04-03 22:24:31 +04:00
|
|
|
value->data.secret,
|
2018-03-29 18:44:34 +04:00
|
|
|
_secret,
|
2018-04-06 22:47:29 +04:00
|
|
|
value->data.hashInEdit);
|
2018-04-03 22:24:31 +04:00
|
|
|
|
2018-04-12 14:20:54 +04:00
|
|
|
const auto selfie = (value->selfieInEdit
|
|
|
|
&& !value->selfieInEdit->deleted)
|
2018-04-03 22:24:31 +04:00
|
|
|
? inputFile(*value->selfieInEdit)
|
|
|
|
: MTPInputSecureFile();
|
|
|
|
|
|
|
|
const auto type = [&] {
|
|
|
|
switch (value->type) {
|
|
|
|
case Value::Type::PersonalDetails:
|
|
|
|
return MTP_secureValueTypePersonalDetails();
|
|
|
|
case Value::Type::Passport:
|
|
|
|
return MTP_secureValueTypePassport();
|
|
|
|
case Value::Type::DriverLicense:
|
|
|
|
return MTP_secureValueTypeDriverLicense();
|
|
|
|
case Value::Type::IdentityCard:
|
|
|
|
return MTP_secureValueTypeIdentityCard();
|
|
|
|
case Value::Type::Address:
|
|
|
|
return MTP_secureValueTypeAddress();
|
|
|
|
case Value::Type::UtilityBill:
|
|
|
|
return MTP_secureValueTypeUtilityBill();
|
|
|
|
case Value::Type::BankStatement:
|
|
|
|
return MTP_secureValueTypeBankStatement();
|
|
|
|
case Value::Type::RentalAgreement:
|
|
|
|
return MTP_secureValueTypeRentalAgreement();
|
2018-03-29 23:49:31 +04:00
|
|
|
}
|
|
|
|
Unexpected("Value type in saveEncryptedValue().");
|
|
|
|
}();
|
2018-04-03 22:24:31 +04:00
|
|
|
const auto flags = ((value->filesInEdit.empty()
|
2018-04-06 22:47:29 +04:00
|
|
|
&& value->data.parsedInEdit.fields.empty())
|
2018-04-03 22:24:31 +04:00
|
|
|
? MTPDinputSecureValue::Flag(0)
|
|
|
|
: MTPDinputSecureValue::Flag::f_data)
|
|
|
|
| (value->filesInEdit.empty()
|
|
|
|
? MTPDinputSecureValue::Flag(0)
|
|
|
|
: MTPDinputSecureValue::Flag::f_files)
|
2018-04-12 14:20:54 +04:00
|
|
|
| ((value->selfieInEdit && !value->selfieInEdit->deleted)
|
2018-04-03 22:24:31 +04:00
|
|
|
? MTPDinputSecureValue::Flag::f_selfie
|
|
|
|
: MTPDinputSecureValue::Flag(0));
|
2018-04-06 22:47:29 +04:00
|
|
|
Assert(flags != MTPDinputSecureValue::Flags(0));
|
|
|
|
|
|
|
|
sendSaveRequest(value, MTP_inputSecureValue(
|
|
|
|
MTP_flags(flags),
|
|
|
|
type,
|
|
|
|
MTP_secureData(
|
|
|
|
MTP_bytes(encryptedData.bytes),
|
|
|
|
MTP_bytes(value->data.hashInEdit),
|
|
|
|
MTP_bytes(value->data.encryptedSecretInEdit)),
|
|
|
|
MTP_vector<MTPInputSecureFile>(inputFiles),
|
|
|
|
MTPSecurePlainData(),
|
|
|
|
selfie));
|
2018-04-03 22:24:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::savePlainTextValue(not_null<Value*> value) {
|
|
|
|
Expects(!isEncryptedValue(value->type));
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
const auto text = getPlainTextFromValue(value);
|
2018-04-03 22:24:31 +04:00
|
|
|
const auto type = [&] {
|
|
|
|
switch (value->type) {
|
|
|
|
case Value::Type::Phone: return MTP_secureValueTypePhone();
|
|
|
|
case Value::Type::Email: return MTP_secureValueTypeEmail();
|
2018-03-29 23:49:31 +04:00
|
|
|
}
|
|
|
|
Unexpected("Value type in savePlainTextValue().");
|
|
|
|
}();
|
2018-04-03 22:24:31 +04:00
|
|
|
const auto plain = [&] {
|
|
|
|
switch (value->type) {
|
|
|
|
case Value::Type::Phone: return MTP_securePlainPhone;
|
|
|
|
case Value::Type::Email: return MTP_securePlainEmail;
|
|
|
|
}
|
|
|
|
Unexpected("Value type in savePlainTextValue().");
|
|
|
|
}();
|
|
|
|
sendSaveRequest(value, MTP_inputSecureValue(
|
|
|
|
MTP_flags(MTPDinputSecureValue::Flag::f_plain_data),
|
|
|
|
type,
|
|
|
|
MTPSecureData(),
|
|
|
|
MTPVector<MTPInputSecureFile>(),
|
|
|
|
plain(MTP_string(text)),
|
|
|
|
MTPInputSecureFile()));
|
2018-03-29 23:49:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::sendSaveRequest(
|
2018-04-03 22:24:31 +04:00
|
|
|
not_null<Value*> value,
|
|
|
|
const MTPInputSecureValue &data) {
|
2018-04-06 22:47:29 +04:00
|
|
|
Expects(value->saveRequestId == 0);
|
|
|
|
|
|
|
|
value->saveRequestId = request(MTPaccount_SaveSecureValue(
|
2018-04-03 22:24:31 +04:00
|
|
|
data,
|
2018-03-29 23:49:31 +04:00
|
|
|
MTP_long(_secretId)
|
2018-04-03 22:24:31 +04:00
|
|
|
)).done([=](const MTPSecureValue &result) {
|
|
|
|
Expects(result.type() == mtpc_secureValue);
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
value->saveRequestId = 0;
|
|
|
|
|
2018-04-12 14:20:54 +04:00
|
|
|
const auto filesInEdit = base::take(value->filesInEdit);
|
2018-04-10 23:00:52 +04:00
|
|
|
auto selfiesInEdit = std::vector<EditFile>();
|
|
|
|
if (auto selfie = base::take(value->selfieInEdit)) {
|
|
|
|
selfiesInEdit.push_back(std::move(*selfie));
|
|
|
|
}
|
2018-04-12 14:20:54 +04:00
|
|
|
|
|
|
|
const auto &data = result.c_secureValue();
|
|
|
|
value->files = data.has_files()
|
|
|
|
? parseFiles(data.vfiles.v, filesInEdit)
|
|
|
|
: std::vector<File>();
|
2018-04-10 23:00:52 +04:00
|
|
|
value->selfie = data.has_selfie()
|
|
|
|
? parseFile(data.vselfie, selfiesInEdit)
|
|
|
|
: base::none;
|
2018-04-06 22:47:29 +04:00
|
|
|
value->data.encryptedSecret = std::move(
|
|
|
|
value->data.encryptedSecretInEdit);
|
|
|
|
value->data.parsed = std::move(value->data.parsedInEdit);
|
|
|
|
value->data.hash = std::move(value->data.hashInEdit);
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
_valueSaveFinished.fire_copy(value);
|
2018-03-25 15:37:57 +04:00
|
|
|
}).fail([=](const RPCError &error) {
|
2018-04-06 22:47:29 +04:00
|
|
|
value->saveRequestId = 0;
|
|
|
|
if (error.type() == qstr("PHONE_VERIFICATION_NEEDED")) {
|
|
|
|
if (value->type == Value::Type::Phone) {
|
2018-04-09 21:56:07 +04:00
|
|
|
startPhoneVerification(value);
|
2018-04-06 22:47:29 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (error.type() == qstr("EMAIL_VERIFICATION_NEEDED")) {
|
|
|
|
if (value->type == Value::Type::Email) {
|
2018-04-09 21:56:07 +04:00
|
|
|
startEmailVerification(value);
|
2018-04-06 22:47:29 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 21:56:07 +04:00
|
|
|
valueSaveFailed(value, error);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormController::getPhoneFromValue(
|
|
|
|
not_null<const Value*> value) const {
|
|
|
|
Expects(value->type == Value::Type::Phone);
|
|
|
|
|
|
|
|
return getPlainTextFromValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormController::getEmailFromValue(
|
|
|
|
not_null<const Value*> value) const {
|
|
|
|
Expects(value->type == Value::Type::Email);
|
|
|
|
|
|
|
|
return getPlainTextFromValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormController::getPlainTextFromValue(
|
|
|
|
not_null<const Value*> value) const {
|
|
|
|
Expects(value->type == Value::Type::Phone
|
|
|
|
|| value->type == Value::Type::Email);
|
|
|
|
|
|
|
|
const auto i = value->data.parsedInEdit.fields.find("value");
|
|
|
|
Assert(i != end(value->data.parsedInEdit.fields));
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::startPhoneVerification(not_null<Value*> value) {
|
|
|
|
value->verification.requestId = request(MTPaccount_SendVerifyPhoneCode(
|
|
|
|
MTP_flags(MTPaccount_SendVerifyPhoneCode::Flag(0)),
|
|
|
|
MTP_string(getPhoneFromValue(value)),
|
|
|
|
MTPBool()
|
|
|
|
)).done([=](const MTPauth_SentCode &result) {
|
|
|
|
Expects(result.type() == mtpc_auth_sentCode);
|
|
|
|
|
|
|
|
value->verification.requestId = 0;
|
|
|
|
|
|
|
|
const auto &data = result.c_auth_sentCode();
|
|
|
|
value->verification.phoneCodeHash = qs(data.vphone_code_hash);
|
|
|
|
switch (data.vtype.type()) {
|
|
|
|
case mtpc_auth_sentCodeTypeApp:
|
|
|
|
LOG(("API Error: sentCodeTypeApp not expected "
|
|
|
|
"in FormController::startPhoneVerification."));
|
|
|
|
return;
|
|
|
|
case mtpc_auth_sentCodeTypeFlashCall:
|
|
|
|
LOG(("API Error: sentCodeTypeFlashCall not expected "
|
|
|
|
"in FormController::startPhoneVerification."));
|
|
|
|
return;
|
|
|
|
case mtpc_auth_sentCodeTypeCall: {
|
|
|
|
const auto &type = data.vtype.c_auth_sentCodeTypeCall();
|
|
|
|
value->verification.codeLength = (type.vlength.v > 0)
|
|
|
|
? type.vlength.v
|
|
|
|
: -1;
|
|
|
|
value->verification.call = std::make_unique<SentCodeCall>(
|
|
|
|
[=] { requestPhoneCall(value); },
|
|
|
|
[=] { _verificationUpdate.fire_copy(value); });
|
|
|
|
value->verification.call->setStatus(
|
|
|
|
{ SentCodeCall::State::Called, 0 });
|
|
|
|
if (data.has_next_type()) {
|
|
|
|
LOG(("API Error: next_type is not supported for calls."));
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case mtpc_auth_sentCodeTypeSms: {
|
|
|
|
const auto &type = data.vtype.c_auth_sentCodeTypeSms();
|
|
|
|
value->verification.codeLength = (type.vlength.v > 0)
|
|
|
|
? type.vlength.v
|
|
|
|
: -1;
|
|
|
|
const auto &next = data.vnext_type;
|
|
|
|
if (data.has_next_type()
|
|
|
|
&& next.type() == mtpc_auth_codeTypeCall) {
|
|
|
|
value->verification.call = std::make_unique<SentCodeCall>(
|
|
|
|
[=] { requestPhoneCall(value); },
|
|
|
|
[=] { _verificationUpdate.fire_copy(value); });
|
|
|
|
value->verification.call->setStatus({
|
|
|
|
SentCodeCall::State::Waiting,
|
|
|
|
data.has_timeout() ? data.vtimeout.v : 60 });
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
_verificationNeeded.fire_copy(value);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
value->verification.requestId = 0;
|
|
|
|
valueSaveFailed(value, error);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::startEmailVerification(not_null<Value*> value) {
|
|
|
|
value->verification.requestId = request(MTPaccount_SendVerifyEmailCode(
|
|
|
|
MTP_string(getEmailFromValue(value))
|
|
|
|
)).done([=](const MTPaccount_SentEmailCode &result) {
|
|
|
|
Expects(result.type() == mtpc_account_sentEmailCode);
|
|
|
|
|
|
|
|
value->verification.requestId = 0;
|
|
|
|
const auto &data = result.c_account_sentEmailCode();
|
|
|
|
value->verification.codeLength = (data.vlength.v > 0)
|
|
|
|
? data.vlength.v
|
|
|
|
: -1;
|
|
|
|
_verificationNeeded.fire_copy(value);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
valueSaveFailed(value, error);
|
2018-03-21 08:35:32 +04:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
|
|
|
|
void FormController::requestPhoneCall(not_null<Value*> value) {
|
|
|
|
Expects(value->verification.call != nullptr);
|
|
|
|
|
|
|
|
value->verification.call->setStatus(
|
|
|
|
{ SentCodeCall::State::Calling, 0 });
|
|
|
|
request(MTPauth_ResendCode(
|
|
|
|
MTP_string(getPhoneFromValue(value)),
|
|
|
|
MTP_string(value->verification.phoneCodeHash)
|
|
|
|
)).done([=](const MTPauth_SentCode &code) {
|
|
|
|
value->verification.call->callDone();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::valueSaveFailed(
|
|
|
|
not_null<Value*> value,
|
|
|
|
const RPCError &error) {
|
|
|
|
_view->show(Box<InformBox>("Error saving value:\n" + error.type()));
|
|
|
|
valueEditFailed(value);
|
|
|
|
_valueSaveFinished.fire_copy(value);
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:00:13 +04:00
|
|
|
void FormController::generateSecret(bytes::const_span password) {
|
2018-03-21 08:35:32 +04:00
|
|
|
if (_saveSecretRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto secret = GenerateSecretBytes();
|
2018-03-27 17:00:13 +04:00
|
|
|
|
|
|
|
auto randomSaltPart = bytes::vector(8);
|
|
|
|
bytes::set_random(randomSaltPart);
|
|
|
|
auto newSecureSaltFull = bytes::concatenate(
|
|
|
|
_password.newSecureSalt,
|
|
|
|
randomSaltPart);
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
auto secureSecretId = CountSecureSecretHash(secret);
|
|
|
|
auto encryptedSecret = EncryptSecureSecret(
|
2018-03-27 17:00:13 +04:00
|
|
|
newSecureSaltFull,
|
2018-03-29 18:44:34 +04:00
|
|
|
secret,
|
|
|
|
password);
|
|
|
|
|
2018-03-27 17:00:13 +04:00
|
|
|
const auto hashForAuth = openssl::Sha256(bytes::concatenate(
|
|
|
|
_password.salt,
|
|
|
|
password,
|
|
|
|
_password.salt));
|
|
|
|
|
2018-03-21 08:35:32 +04:00
|
|
|
using Flag = MTPDaccount_passwordInputSettings::Flag;
|
|
|
|
_saveSecretRequestId = request(MTPaccount_UpdatePasswordSettings(
|
2018-03-27 17:00:13 +04:00
|
|
|
MTP_bytes(hashForAuth),
|
2018-03-21 08:35:32 +04:00
|
|
|
MTP_account_passwordInputSettings(
|
|
|
|
MTP_flags(Flag::f_new_secure_secret),
|
|
|
|
MTPbytes(), // new_salt
|
|
|
|
MTPbytes(), // new_password_hash
|
|
|
|
MTPstring(), // hint
|
|
|
|
MTPstring(), // email
|
2018-03-27 17:00:13 +04:00
|
|
|
MTP_bytes(newSecureSaltFull),
|
|
|
|
MTP_bytes(encryptedSecret),
|
2018-03-29 18:44:34 +04:00
|
|
|
MTP_long(secureSecretId))
|
2018-03-21 08:35:32 +04:00
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
_saveSecretRequestId = 0;
|
|
|
|
_secret = secret;
|
2018-03-29 18:44:34 +04:00
|
|
|
_secretId = secureSecretId;
|
2018-03-27 17:00:13 +04:00
|
|
|
//_password.salt = newPasswordSaltFull;
|
2018-03-25 15:37:57 +04:00
|
|
|
for (const auto &callback : base::take(_secretCallbacks)) {
|
|
|
|
callback();
|
|
|
|
}
|
2018-03-21 08:35:32 +04:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
// #TODO wrong password hash error?
|
|
|
|
Ui::show(Box<InformBox>("Saving encrypted value failed."));
|
|
|
|
_saveSecretRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-03-18 12:51:14 +04:00
|
|
|
void FormController::requestForm() {
|
|
|
|
auto normalizedKey = _request.publicKey;
|
|
|
|
normalizedKey.replace("\r\n", "\n");
|
2018-03-19 20:22:27 +04:00
|
|
|
_formRequestId = request(MTPaccount_GetAuthorizationForm(
|
2018-03-18 12:51:14 +04:00
|
|
|
MTP_int(_request.botId),
|
2018-03-27 17:00:13 +04:00
|
|
|
MTP_string(_request.scope),
|
|
|
|
MTP_bytes(normalizedKey.toUtf8())
|
2018-03-18 12:51:14 +04:00
|
|
|
)).done([=](const MTPaccount_AuthorizationForm &result) {
|
2018-03-19 20:22:27 +04:00
|
|
|
_formRequestId = 0;
|
2018-03-18 12:51:14 +04:00
|
|
|
formDone(result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
formFail(error);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-03-29 00:40:42 +04:00
|
|
|
auto FormController::parseFiles(
|
|
|
|
const QVector<MTPSecureFile> &data,
|
|
|
|
const std::vector<EditFile> &editData) const
|
|
|
|
-> std::vector<File> {
|
|
|
|
auto result = std::vector<File>();
|
|
|
|
result.reserve(data.size());
|
|
|
|
|
|
|
|
for (const auto &file : data) {
|
2018-04-10 23:00:52 +04:00
|
|
|
if (auto normal = parseFile(file, editData)) {
|
|
|
|
result.push_back(std::move(*normal));
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
|
|
|
}
|
2018-03-29 00:40:42 +04:00
|
|
|
|
2018-03-27 17:00:13 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:00:52 +04:00
|
|
|
auto FormController::parseFile(
|
|
|
|
const MTPSecureFile &data,
|
|
|
|
const std::vector<EditFile> &editData) const
|
|
|
|
-> base::optional<File> {
|
|
|
|
switch (data.type()) {
|
|
|
|
case mtpc_secureFileEmpty:
|
|
|
|
return base::none;
|
|
|
|
|
|
|
|
case mtpc_secureFile: {
|
|
|
|
const auto &fields = data.c_secureFile();
|
|
|
|
auto result = File();
|
|
|
|
result.id = fields.vid.v;
|
|
|
|
result.accessHash = fields.vaccess_hash.v;
|
|
|
|
result.size = fields.vsize.v;
|
|
|
|
result.date = fields.vdate.v;
|
|
|
|
result.dcId = fields.vdc_id.v;
|
|
|
|
result.hash = bytes::make_vector(fields.vfile_hash.v);
|
|
|
|
result.encryptedSecret = bytes::make_vector(fields.vsecret.v);
|
|
|
|
fillDownloadedFile(result, editData);
|
|
|
|
return result;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
Unexpected("Type in FormController::parseFile.");
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:44:34 +04:00
|
|
|
void FormController::fillDownloadedFile(
|
|
|
|
File &destination,
|
|
|
|
const std::vector<EditFile> &source) const {
|
|
|
|
const auto i = ranges::find(
|
|
|
|
source,
|
|
|
|
destination.hash,
|
|
|
|
[](const EditFile &file) { return file.fields.hash; });
|
|
|
|
if (i == source.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
destination.image = i->fields.image;
|
|
|
|
destination.downloadOffset = i->fields.downloadOffset;
|
|
|
|
if (!i->uploadData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Local::writeImage(
|
|
|
|
StorageKey(
|
|
|
|
storageMix32To64(
|
|
|
|
SecureFileLocation,
|
|
|
|
destination.dcId),
|
|
|
|
destination.id),
|
|
|
|
StorageImageSaved(QByteArray::fromRawData(
|
|
|
|
reinterpret_cast<const char*>(
|
|
|
|
i->uploadData->bytes.data()),
|
|
|
|
i->uploadData->bytes.size())));
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
auto FormController::parseValue(
|
|
|
|
const MTPSecureValue &value) const -> Value {
|
|
|
|
Expects(value.type() == mtpc_secureValue);
|
|
|
|
|
|
|
|
const auto &data = value.c_secureValue();
|
|
|
|
const auto type = ConvertType(data.vtype);
|
2018-03-29 18:44:34 +04:00
|
|
|
auto result = Value(type);
|
2018-04-03 22:24:31 +04:00
|
|
|
if (data.has_data()) {
|
|
|
|
Assert(data.vdata.type() == mtpc_secureData);
|
|
|
|
const auto &fields = data.vdata.c_secureData();
|
|
|
|
result.data.original = fields.vdata.v;
|
|
|
|
result.data.hash = bytes::make_vector(fields.vdata_hash.v);
|
|
|
|
result.data.encryptedSecret = bytes::make_vector(fields.vsecret.v);
|
|
|
|
}
|
|
|
|
if (data.has_files()) {
|
|
|
|
result.files = parseFiles(data.vfiles.v);
|
|
|
|
}
|
2018-04-10 23:00:52 +04:00
|
|
|
if (data.has_selfie()) {
|
|
|
|
result.selfie = parseFile(data.vselfie);
|
|
|
|
}
|
2018-04-03 22:24:31 +04:00
|
|
|
if (data.has_plain_data()) {
|
|
|
|
switch (data.vplain_data.type()) {
|
|
|
|
case mtpc_securePlainPhone: {
|
|
|
|
const auto &fields = data.vplain_data.c_securePlainPhone();
|
|
|
|
result.data.parsed.fields["value"] = qs(fields.vphone);
|
|
|
|
} break;
|
|
|
|
case mtpc_securePlainEmail: {
|
|
|
|
const auto &fields = data.vplain_data.c_securePlainEmail();
|
|
|
|
result.data.parsed.fields["value"] = qs(fields.vemail);
|
|
|
|
} break;
|
|
|
|
}
|
2018-03-27 17:00:13 +04:00
|
|
|
}
|
2018-04-03 22:24:31 +04:00
|
|
|
// #TODO passport selfie
|
2018-03-27 17:00:13 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-25 15:37:57 +04:00
|
|
|
auto FormController::findEditFile(const FullMsgId &fullId) -> EditFile* {
|
2018-04-10 23:00:52 +04:00
|
|
|
const auto found = [&](const EditFile &file) {
|
|
|
|
return (file.uploadData && file.uploadData->fullId == fullId);
|
|
|
|
};
|
2018-04-03 22:24:31 +04:00
|
|
|
for (auto &[type, value] : _form.values) {
|
2018-03-29 18:44:34 +04:00
|
|
|
for (auto &file : value.filesInEdit) {
|
2018-04-10 23:00:52 +04:00
|
|
|
if (found(file)) {
|
2018-03-27 17:00:13 +04:00
|
|
|
return &file;
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:00:52 +04:00
|
|
|
if (value.selfieInEdit && found(*value.selfieInEdit)) {
|
|
|
|
return &*value.selfieInEdit;
|
|
|
|
}
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-29 00:40:42 +04:00
|
|
|
auto FormController::findEditFile(const FileKey &key) -> EditFile* {
|
2018-04-10 23:00:52 +04:00
|
|
|
const auto found = [&](const EditFile &file) {
|
|
|
|
return (file.fields.dcId == key.dcId && file.fields.id == key.id);
|
|
|
|
};
|
2018-04-03 22:24:31 +04:00
|
|
|
for (auto &[type, value] : _form.values) {
|
2018-03-29 18:44:34 +04:00
|
|
|
for (auto &file : value.filesInEdit) {
|
2018-04-10 23:00:52 +04:00
|
|
|
if (found(file)) {
|
2018-03-29 00:40:42 +04:00
|
|
|
return &file;
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:00:52 +04:00
|
|
|
if (value.selfieInEdit && found(*value.selfieInEdit)) {
|
|
|
|
return &*value.selfieInEdit;
|
|
|
|
}
|
2018-03-29 00:40:42 +04:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-25 15:37:57 +04:00
|
|
|
auto FormController::findFile(const FileKey &key)
|
2018-03-29 18:44:34 +04:00
|
|
|
-> std::pair<Value*, File*> {
|
2018-04-10 23:00:52 +04:00
|
|
|
const auto found = [&](const File &file) {
|
|
|
|
return (file.dcId == key.dcId) && (file.id == key.id);
|
|
|
|
};
|
2018-04-03 22:24:31 +04:00
|
|
|
for (auto &[type, value] : _form.values) {
|
2018-03-29 18:44:34 +04:00
|
|
|
for (auto &file : value.files) {
|
2018-04-10 23:00:52 +04:00
|
|
|
if (found(file)) {
|
2018-03-29 18:44:34 +04:00
|
|
|
return { &value, &file };
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:00:52 +04:00
|
|
|
if (value.selfie && found(*value.selfie)) {
|
|
|
|
return { &value, &*value.selfie };
|
|
|
|
}
|
2018-03-25 15:37:57 +04:00
|
|
|
}
|
|
|
|
return { nullptr, nullptr };
|
|
|
|
}
|
|
|
|
|
2018-03-18 12:51:14 +04:00
|
|
|
void FormController::formDone(const MTPaccount_AuthorizationForm &result) {
|
|
|
|
parseForm(result);
|
2018-03-19 20:22:27 +04:00
|
|
|
if (!_passwordRequestId) {
|
2018-03-31 05:45:40 +04:00
|
|
|
showForm();
|
2018-03-19 20:22:27 +04:00
|
|
|
}
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::parseForm(const MTPaccount_AuthorizationForm &result) {
|
|
|
|
Expects(result.type() == mtpc_account_authorizationForm);
|
|
|
|
|
|
|
|
const auto &data = result.c_account_authorizationForm();
|
2018-03-27 17:00:13 +04:00
|
|
|
|
|
|
|
App::feedUsers(data.vusers);
|
|
|
|
|
2018-04-03 22:24:31 +04:00
|
|
|
for (const auto &value : data.vvalues.v) {
|
|
|
|
auto parsed = parseValue(value);
|
|
|
|
const auto type = parsed.type;
|
|
|
|
const auto alreadyIt = _form.values.find(type);
|
|
|
|
if (alreadyIt != _form.values.end()) {
|
|
|
|
LOG(("API Error: Two values for type %1 in authorization form"
|
|
|
|
"%1").arg(int(type)));
|
|
|
|
continue;
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
2018-04-03 22:24:31 +04:00
|
|
|
_form.values.emplace(type, std::move(parsed));
|
|
|
|
}
|
|
|
|
_form.identitySelfieRequired = data.is_selfie_required();
|
|
|
|
if (data.has_privacy_policy_url()) {
|
|
|
|
_form.privacyPolicyUrl = qs(data.vprivacy_policy_url);
|
|
|
|
}
|
|
|
|
for (const auto &required : data.vrequired_types.v) {
|
|
|
|
const auto type = ConvertType(required);
|
|
|
|
_form.request.push_back(type);
|
|
|
|
_form.values.emplace(type, Value(type));
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
|
|
|
_bot = App::userLoaded(_request.botId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::formFail(const RPCError &error) {
|
2018-04-03 22:24:31 +04:00
|
|
|
Ui::show(Box<InformBox>(lang(lng_passport_form_error)));
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::requestPassword() {
|
2018-03-19 20:22:27 +04:00
|
|
|
_passwordRequestId = request(MTPaccount_GetPassword(
|
2018-03-18 12:51:14 +04:00
|
|
|
)).done([=](const MTPaccount_Password &result) {
|
2018-03-19 20:22:27 +04:00
|
|
|
_passwordRequestId = 0;
|
2018-03-18 12:51:14 +04:00
|
|
|
passwordDone(result);
|
2018-03-19 20:22:27 +04:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
formFail(error);
|
2018-03-18 12:51:14 +04:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::passwordDone(const MTPaccount_Password &result) {
|
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_account_noPassword:
|
2018-03-19 20:22:27 +04:00
|
|
|
parsePassword(result.c_account_noPassword());
|
2018-03-18 12:51:14 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case mtpc_account_password:
|
2018-03-19 20:22:27 +04:00
|
|
|
parsePassword(result.c_account_password());
|
2018-03-18 12:51:14 +04:00
|
|
|
break;
|
|
|
|
}
|
2018-03-19 20:22:27 +04:00
|
|
|
if (!_formRequestId) {
|
2018-03-31 05:45:40 +04:00
|
|
|
showForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::showForm() {
|
|
|
|
if (!_bot) {
|
|
|
|
Ui::show(Box<InformBox>("Could not get authorization bot."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!_password.salt.empty()) {
|
|
|
|
_view->showAskPassword();
|
|
|
|
} else if (!_password.unconfirmedPattern.isEmpty()) {
|
|
|
|
_view->showPasswordUnconfirmed();
|
|
|
|
} else {
|
|
|
|
_view->showNoPassword();
|
2018-03-19 20:22:27 +04:00
|
|
|
}
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormController::passwordFail(const RPCError &error) {
|
|
|
|
Ui::show(Box<InformBox>("Could not get authorization form."));
|
|
|
|
}
|
|
|
|
|
2018-03-19 20:22:27 +04:00
|
|
|
void FormController::parsePassword(const MTPDaccount_noPassword &result) {
|
|
|
|
_password.unconfirmedPattern = qs(result.vemail_unconfirmed_pattern);
|
2018-03-27 17:00:13 +04:00
|
|
|
_password.newSalt = bytes::make_vector(result.vnew_salt.v);
|
|
|
|
_password.newSecureSalt = bytes::make_vector(result.vnew_secure_salt.v);
|
|
|
|
openssl::AddRandomSeed(bytes::make_span(result.vsecure_random.v));
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
|
|
|
|
2018-03-19 20:22:27 +04:00
|
|
|
void FormController::parsePassword(const MTPDaccount_password &result) {
|
|
|
|
_password.hint = qs(result.vhint);
|
|
|
|
_password.hasRecovery = mtpIsTrue(result.vhas_recovery);
|
2018-03-27 17:00:13 +04:00
|
|
|
_password.salt = bytes::make_vector(result.vcurrent_salt.v);
|
2018-03-19 20:22:27 +04:00
|
|
|
_password.unconfirmedPattern = qs(result.vemail_unconfirmed_pattern);
|
2018-03-27 17:00:13 +04:00
|
|
|
_password.newSalt = bytes::make_vector(result.vnew_salt.v);
|
|
|
|
_password.newSecureSalt = bytes::make_vector(result.vnew_secure_salt.v);
|
|
|
|
openssl::AddRandomSeed(bytes::make_span(result.vsecure_random.v));
|
2018-03-18 12:51:14 +04:00
|
|
|
}
|
|
|
|
|
2018-03-31 05:45:40 +04:00
|
|
|
void FormController::cancel() {
|
|
|
|
if (!_cancelled) {
|
|
|
|
_cancelled = true;
|
|
|
|
crl::on_main(this, [=] {
|
2018-04-06 22:47:29 +04:00
|
|
|
_controller->clearPassportForm();
|
2018-03-31 05:45:40 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::lifetime &FormController::lifetime() {
|
|
|
|
return _lifetime;
|
|
|
|
}
|
|
|
|
|
2018-03-25 15:37:57 +04:00
|
|
|
FormController::~FormController() = default;
|
|
|
|
|
2018-03-18 12:51:14 +04:00
|
|
|
} // namespace Passport
|