tdesktop/Telegram/SourceFiles/intro/intro_password_check.cpp

407 lines
11 KiB
C++
Raw Normal View History

/*
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
*/
2019-11-27 09:45:23 +00:00
#include "intro/intro_password_check.h"
2019-11-27 09:45:23 +00:00
#include "intro/intro_widget.h"
#include "core/file_utilities.h"
2018-08-03 21:48:00 +00:00
#include "core/core_cloud_password.h"
2021-10-18 21:36:55 +00:00
#include "ui/boxes/confirm_box.h"
#include "boxes/passcode_box.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
2019-11-27 09:45:23 +00:00
#include "intro/intro_signup.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/input_fields.h"
2016-11-24 19:28:23 +00:00
#include "ui/widgets/labels.h"
2019-11-27 08:02:56 +00:00
#include "main/main_account.h"
#include "base/random.h"
2019-11-26 11:10:44 +00:00
#include "styles/style_intro.h"
#include "styles/style_boxes.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
2019-11-26 11:10:44 +00:00
namespace details {
2016-11-24 19:28:23 +00:00
2019-11-27 09:45:23 +00:00
PasswordCheckWidget::PasswordCheckWidget(
2018-08-03 21:48:00 +00:00
QWidget *parent,
2019-07-24 08:46:23 +00:00
not_null<Main::Account*> account,
2019-11-26 11:10:44 +00:00
not_null<Data*> data)
2019-07-24 08:46:23 +00:00
: Step(parent, account, data)
, _passwordState(getData()->pwdState)
, _pwdField(this, st::introPassword, tr::lng_signin_password())
, _pwdHint(this, st::introPasswordHint)
, _codeField(this, st::introPassword, tr::lng_signin_code())
2019-06-19 15:09:03 +00:00
, _toRecover(this, tr::lng_signin_recover(tr::now))
2019-11-27 08:33:18 +00:00
, _toPassword(this, tr::lng_signin_try_password(tr::now)) {
Expects(_passwordState.hasPassword);
2018-08-03 21:48:00 +00:00
2020-09-30 09:11:44 +00:00
Lang::Updated(
) | rpl::start_with_next([=] {
refreshLang();
}, lifetime());
2019-11-27 08:33:18 +00:00
_toRecover->addClickHandler([=] { toRecover(); });
_toPassword->addClickHandler([=] { toPassword(); });
connect(_pwdField, &Ui::PasswordInput::changed, [=] { hideError(); });
connect(_codeField, &Ui::InputField::changed, [=] { hideError(); });
setTitleText(tr::lng_signin_title());
2016-11-24 19:28:23 +00:00
updateDescriptionText();
if (_passwordState.hint.isEmpty()) {
2016-11-24 19:28:23 +00:00
_pwdHint->hide();
} else {
_pwdHint->setText(tr::lng_signin_hint(
tr::now,
lt_password_hint,
_passwordState.hint));
}
_codeField->hide();
_toPassword->hide();
setMouseTracking(true);
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::refreshLang() {
if (_toRecover) {
_toRecover->setText(tr::lng_signin_recover(tr::now));
}
if (_toPassword) {
_toPassword->setText(
tr::lng_signin_try_password(tr::now));
}
if (!_passwordState.hint.isEmpty()) {
_pwdHint->setText(tr::lng_signin_hint(
tr::now,
lt_password_hint,
_passwordState.hint));
}
updateControlsGeometry();
}
2019-11-27 09:45:23 +00:00
int PasswordCheckWidget::errorTop() const {
2019-11-26 14:27:09 +00:00
return contentTop() + st::introErrorBelowLinkTop;
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::resizeEvent(QResizeEvent *e) {
2016-11-24 19:28:23 +00:00
Step::resizeEvent(e);
updateControlsGeometry();
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::updateControlsGeometry() {
2016-11-24 19:28:23 +00:00
_pwdField->moveToLeft(contentLeft(), contentTop() + st::introPasswordTop);
_pwdHint->moveToLeft(contentLeft() + st::buttonRadius, contentTop() + st::introPasswordHintTop);
_codeField->moveToLeft(contentLeft(), contentTop() + st::introStepFieldTop);
auto linkTop = _codeField->y() + _codeField->height() + st::introLinkTop;
_toRecover->moveToLeft(contentLeft() + st::buttonRadius, linkTop);
_toPassword->moveToLeft(contentLeft() + st::buttonRadius, linkTop);
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::setInnerFocus() {
if (_pwdField->isHidden()) {
_codeField->setFocusFast();
} else {
_pwdField->setFocusFast();
}
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::activate() {
2016-11-24 19:28:23 +00:00
if (_pwdField->isHidden() && _codeField->isHidden()) {
Step::activate();
_pwdField->show();
_pwdHint->show();
_toRecover->show();
}
2016-11-24 19:28:23 +00:00
setInnerFocus();
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::cancelled() {
api().request(base::take(_sentRequest)).cancel();
2016-11-24 19:28:23 +00:00
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::pwdSubmitDone(bool recover, const MTPauth_Authorization &result) {
_sentRequest = 0;
if (recover) {
cSetPasswordRecovered(true);
}
2016-11-24 19:28:23 +00:00
auto &d = result.c_auth_authorization();
2019-07-05 13:38:38 +00:00
if (d.vuser().type() != mtpc_user || !d.vuser().c_user().is_self()) { // wtf?
showError(rpl::single(Lang::Hard::ServerError()));
return;
}
2019-07-05 13:38:38 +00:00
finish(d.vuser());
}
2021-03-12 12:48:00 +00:00
void PasswordCheckWidget::pwdSubmitFail(const MTP::Error &error) {
if (MTP::IsFloodError(error)) {
_sentRequest = 0;
showError(tr::lng_flood_error());
2016-11-24 19:28:23 +00:00
_pwdField->showError();
2018-08-10 19:19:46 +00:00
return;
}
_sentRequest = 0;
const auto &type = error.type();
2022-11-26 21:20:17 +00:00
if (type == u"PASSWORD_HASH_INVALID"_q
|| type == u"SRP_PASSWORD_CHANGED"_q) {
showError(tr::lng_signin_bad_password());
_pwdField->selectAll();
2016-11-24 19:28:23 +00:00
_pwdField->showError();
2022-11-26 21:20:17 +00:00
} else if (type == u"PASSWORD_EMPTY"_q
|| type == u"AUTH_KEY_UNREGISTERED"_q) {
2016-11-24 19:28:23 +00:00
goBack();
2022-11-26 21:20:17 +00:00
} else if (type == u"SRP_ID_INVALID"_q) {
2018-08-10 19:19:46 +00:00
handleSrpIdInvalid();
} else {
2018-08-10 19:19:46 +00:00
if (Logs::DebugEnabled()) { // internal server error
showError(rpl::single(type + ": " + error.description()));
2018-08-10 19:19:46 +00:00
} else {
showError(rpl::single(Lang::Hard::ServerError()));
2018-08-10 19:19:46 +00:00
}
_pwdField->setFocus();
}
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::handleSrpIdInvalid() {
const auto now = crl::now();
2018-08-10 19:19:46 +00:00
if (_lastSrpIdInvalidTime > 0
&& now - _lastSrpIdInvalidTime < Core::kHandleSrpIdInvalidTimeout) {
_passwordState.mtp.request.id = 0;
showError(rpl::single(Lang::Hard::ServerError()));
2018-08-10 19:19:46 +00:00
} else {
_lastSrpIdInvalidTime = now;
requestPasswordData();
}
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::checkPasswordHash() {
if (_passwordState.mtp.request.id) {
2018-08-10 19:19:46 +00:00
passwordChecked();
} else {
requestPasswordData();
}
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::requestPasswordData() {
api().request(base::take(_sentRequest)).cancel();
_sentRequest = api().request(
2018-08-10 19:19:46 +00:00
MTPaccount_GetPassword()
).done([=](const MTPaccount_Password &result) {
_sentRequest = 0;
result.match([&](const MTPDaccount_password &data) {
base::RandomAddSeed(bytes::make_span(data.vsecure_random().v));
_passwordState = Core::ParseCloudPasswordState(data);
2018-08-10 19:19:46 +00:00
passwordChecked();
});
}).send();
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::passwordChecked() {
2018-08-10 19:19:46 +00:00
const auto check = Core::ComputeCloudPasswordCheck(
_passwordState.mtp.request,
2018-08-10 19:19:46 +00:00
_passwordHash);
if (!check) {
return serverError();
}
_passwordState.mtp.request.id = 0;
_sentRequest = api().request(
2018-08-10 19:19:46 +00:00
MTPauth_CheckPassword(check.result)
).done([=](const MTPauth_Authorization &result) {
pwdSubmitDone(false, result);
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2018-08-10 19:19:46 +00:00
pwdSubmitFail(error);
2020-06-11 13:07:14 +00:00
}).handleFloodErrors().send();
2018-08-10 19:19:46 +00:00
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::serverError() {
showError(rpl::single(Lang::Hard::ServerError()));
2018-08-10 19:19:46 +00:00
}
void PasswordCheckWidget::codeSubmitDone(
const QString &code,
const MTPBool &result) {
auto fields = PasscodeBox::CloudFields::From(_passwordState);
fields.fromRecoveryCode = code;
fields.hasRecovery = false;
fields.mtp.curRequest = {};
fields.hasPassword = false;
auto box = Box<PasscodeBox>(&api().instance(), nullptr, fields);
const auto boxShared = std::make_shared<QPointer<PasscodeBox>>();
box->newAuthorization(
) | rpl::start_with_next([=](const MTPauth_Authorization &result) {
if (boxShared) {
(*boxShared)->closeBox();
}
pwdSubmitDone(true, result);
}, lifetime());
*boxShared = Ui::show(std::move(box));
}
2021-03-12 12:48:00 +00:00
void PasswordCheckWidget::codeSubmitFail(const MTP::Error &error) {
if (MTP::IsFloodError(error)) {
showError(tr::lng_flood_error());
2016-11-24 19:28:23 +00:00
_codeField->showError();
2018-08-10 19:19:46 +00:00
return;
}
_sentRequest = 0;
const auto &type = error.type();
2022-11-26 21:20:17 +00:00
if (type == u"PASSWORD_EMPTY"_q
|| type == u"AUTH_KEY_UNREGISTERED"_q) {
2016-11-24 19:28:23 +00:00
goBack();
2022-11-26 21:20:17 +00:00
} else if (type == u"PASSWORD_RECOVERY_NA"_q) {
recoverStartFail(error);
2022-11-26 21:20:17 +00:00
} else if (type == u"PASSWORD_RECOVERY_EXPIRED"_q) {
_emailPattern = QString();
2019-11-27 08:33:18 +00:00
toPassword();
2022-11-26 21:20:17 +00:00
} else if (type == u"CODE_INVALID"_q) {
showError(tr::lng_signin_wrong_code());
_codeField->selectAll();
2016-11-24 19:28:23 +00:00
_codeField->showError();
} else {
2018-08-10 19:19:46 +00:00
if (Logs::DebugEnabled()) { // internal server error
showError(rpl::single(type + ": " + error.description()));
2018-08-10 19:19:46 +00:00
} else {
showError(rpl::single(Lang::Hard::ServerError()));
2018-08-10 19:19:46 +00:00
}
_codeField->setFocus();
}
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::recoverStarted(const MTPauth_PasswordRecovery &result) {
2019-07-05 13:38:38 +00:00
_emailPattern = qs(result.c_auth_passwordRecovery().vemail_pattern());
2016-11-24 19:28:23 +00:00
updateDescriptionText();
}
2021-03-12 12:48:00 +00:00
void PasswordCheckWidget::recoverStartFail(const MTP::Error &error) {
_pwdField->show();
2016-11-24 19:28:23 +00:00
_pwdHint->show();
_codeField->hide();
_pwdField->setFocus();
2016-11-24 19:28:23 +00:00
updateDescriptionText();
update();
2016-11-24 19:28:23 +00:00
hideError();
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::toRecover() {
if (_passwordState.hasRecovery) {
if (_sentRequest) {
api().request(base::take(_sentRequest)).cancel();
2015-04-04 20:01:34 +00:00
}
2016-11-24 19:28:23 +00:00
hideError();
_toRecover->hide();
_toPassword->show();
_pwdField->hide();
2016-11-24 19:28:23 +00:00
_pwdHint->hide();
_pwdField->setText(QString());
_codeField->show();
_codeField->setFocus();
2016-11-24 19:28:23 +00:00
updateDescriptionText();
2015-04-04 20:01:34 +00:00
if (_emailPattern.isEmpty()) {
api().request(
2018-08-10 19:19:46 +00:00
MTPauth_RequestPasswordRecovery()
).done([=](const MTPauth_PasswordRecovery &result) {
recoverStarted(result);
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2018-08-10 19:19:46 +00:00
recoverStartFail(error);
}).send();
2015-04-04 20:01:34 +00:00
}
} else {
const auto box = Ui::show(
Ui::MakeInformBox(tr::lng_signin_no_email_forgot()));
box->boxClosing(
) | rpl::start_with_next([=] {
showReset();
}, box->lifetime());
}
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::toPassword() {
const auto box = Ui::show(
Ui::MakeInformBox(tr::lng_signin_cant_email_forgot()));
box->boxClosing(
) | rpl::start_with_next([=] {
showReset();
}, box->lifetime());
2015-04-04 20:01:34 +00:00
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::showReset() {
if (_sentRequest) {
api().request(base::take(_sentRequest)).cancel();
2015-04-04 20:01:34 +00:00
}
_toRecover->show();
_toPassword->hide();
_pwdField->show();
2016-11-24 19:28:23 +00:00
_pwdHint->show();
_codeField->hide();
_codeField->setText(QString());
_pwdField->setFocus();
2016-11-24 19:28:23 +00:00
showResetButton();
updateDescriptionText();
update();
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::updateDescriptionText() {
auto pwdHidden = _pwdField->isHidden();
auto emailPattern = _emailPattern;
setDescriptionText(pwdHidden
? tr::lng_signin_recover_desc(lt_email, rpl::single(emailPattern))
: tr::lng_signin_desc());
2015-04-04 20:01:34 +00:00
}
2019-11-27 09:45:23 +00:00
void PasswordCheckWidget::submit() {
2019-11-27 08:33:18 +00:00
if (_sentRequest) {
return;
}
if (_pwdField->isHidden()) {
2016-11-24 19:28:23 +00:00
auto code = _codeField->getLastText().trimmed();
if (code.isEmpty()) {
2016-11-24 19:28:23 +00:00
_codeField->showError();
return;
}
const auto send = crl::guard(this, [=] {
_sentRequest = api().request(MTPauth_CheckRecoveryPassword(
MTP_string(code)
)).done([=](const MTPBool &result) {
codeSubmitDone(code, result);
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2018-08-10 19:19:46 +00:00
codeSubmitFail(error);
2020-06-11 13:07:14 +00:00
}).handleFloodErrors().send();
});
if (_passwordState.notEmptyPassport) {
const auto confirmed = [=](Fn<void()> &&close) {
send();
close();
};
Ui::show(Ui::MakeConfirmBox({
.text = tr::lng_cloud_password_passport_losing(),
.confirmed = confirmed,
.confirmText = tr::lng_continue(),
}));
} else {
send();
}
} else {
2016-11-24 19:28:23 +00:00
hideError();
2018-08-03 21:48:00 +00:00
const auto password = _pwdField->getLastText().toUtf8();
2018-08-10 19:19:46 +00:00
_passwordHash = Core::ComputeCloudPasswordHash(
_passwordState.mtp.request.algo,
2018-08-03 21:48:00 +00:00
bytes::make_span(password));
2018-08-10 19:19:46 +00:00
checkPasswordHash();
}
}
2019-11-27 09:45:23 +00:00
rpl::producer<QString> PasswordCheckWidget::nextButtonText() const {
return tr::lng_intro_submit();
}
2016-11-24 19:28:23 +00:00
2019-11-26 11:10:44 +00:00
} // namespace details
2016-11-24 19:28:23 +00:00
} // namespace Intro