Add email address reset infomation to authorizationStateWaitEmailCode.

This commit is contained in:
levlam 2023-03-20 01:40:05 +03:00
parent da2424bc64
commit 61a4259bfe
4 changed files with 46 additions and 18 deletions

View File

@ -119,8 +119,10 @@ authorizationStateWaitEmailAddress allow_apple_id:Bool allow_google_id:Bool = Au
//@allow_apple_id True, if authorization through Apple ID is allowed
//@allow_google_id True, if authorization through Google ID is allowed
//@code_info Information about the sent authentication code
//@next_phone_number_authorization_date Point in time (Unix timestamp) when the user will be able to authorize with a code sent to the user's phone number; 0 if unknown
authorizationStateWaitEmailCode allow_apple_id:Bool allow_google_id:Bool code_info:emailAddressAuthenticationCodeInfo next_phone_number_authorization_date:int32 = AuthorizationState;
//@can_reset_email_address True, if the email address can be reset using resetAuthenticationEmailAddress and the user will be able to authorize with a code sent to the user's phone number
//@reset_period Time required to wait before the email address can be reset using resetAuthenticationEmailAddress if applicable; 0 if the user is subscribed to Telegram Premium
//@reset_in Left time before the email address can be reset, in seconds; 0 if email address reset wasn't requested. updateAuthorizationState is not sent when this field changes
authorizationStateWaitEmailCode allow_apple_id:Bool allow_google_id:Bool code_info:emailAddressAuthenticationCodeInfo can_reset_email_address:Bool reset_period:int32 reset_in:int32 = AuthorizationState;
//@description TDLib needs the user's authentication code to authorize. Call checkAuthenticationCode to check the code @code_info Information about the authorization code that was sent
authorizationStateWaitCode code_info:authenticationCodeInfo = AuthorizationState;

View File

@ -109,10 +109,14 @@ tl_object_ptr<td_api::AuthorizationState> AuthManager::get_authorization_state_o
return make_tl_object<td_api::authorizationStateWaitPhoneNumber>();
case State::WaitEmailAddress:
return make_tl_object<td_api::authorizationStateWaitEmailAddress>(allow_apple_id_, allow_google_id_);
case State::WaitEmailCode:
case State::WaitEmailCode: {
auto can_reset_email_address = reset_available_period_ >= 0;
auto expires_in =
reset_pending_date_ > 0 ? clamp(reset_pending_date_ - G()->unix_time(), 1, reset_available_period_) : 0;
return make_tl_object<td_api::authorizationStateWaitEmailCode>(
allow_apple_id_, allow_google_id_, email_code_info_.get_email_address_authentication_code_info_object(),
next_phone_number_login_date_);
can_reset_email_address, can_reset_email_address ? reset_available_period_ : 0, expires_in);
}
case State::WaitCode:
return send_code_helper_.get_authorization_state_wait_code();
case State::WaitQrCodeConfirmation:
@ -270,7 +274,8 @@ void AuthManager::set_phone_number(uint64 query_id, string phone_number,
allow_google_id_ = false;
email_address_ = {};
email_code_info_ = {};
next_phone_number_login_date_ = 0;
reset_available_period_ = -1;
reset_pending_date_ = 0;
code_ = string();
email_code_ = {};
@ -592,7 +597,15 @@ void AuthManager::on_sent_code(telegram_api::object_ptr<telegram_api::auth_SentC
allow_google_id_ = code_type->google_signin_allowed_;
email_address_.clear();
email_code_info_ = SentEmailCode(std::move(code_type->email_pattern_), code_type->length_);
next_phone_number_login_date_ = 0;
if ((code_type->flags_ & telegram_api::auth_sentCodeTypeEmailCode::RESET_AVAILABLE_PERIOD_MASK) != 0) {
reset_available_period_ = max(code_type->reset_available_period_, 0);
if (reset_available_period_ > 0) {
reset_pending_date_ = code_type->reset_pending_date_;
}
} else {
reset_available_period_ = -1;
reset_pending_date_ = 0;
}
if (email_code_info_.is_empty()) {
email_code_info_ = SentEmailCode("<unknown>", code_type->length_);
CHECK(!email_code_info_.is_empty());
@ -629,7 +642,6 @@ void AuthManager::on_send_email_code_result(NetQueryPtr &result) {
if (email_code_info_.is_empty()) {
return on_query_error(Status::Error(500, "Receive invalid response"));
}
next_phone_number_login_date_ = 0;
update_state(State::WaitEmailCode, true);
on_query_ok();
@ -646,6 +658,8 @@ void AuthManager::on_verify_email_address_result(NetQueryPtr &result) {
if (email_verified->get_id() != telegram_api::account_emailVerifiedLogin::ID) {
return on_query_error(Status::Error(500, "Receive invalid response"));
}
reset_available_period_ = -1;
reset_pending_date_ = 0;
auto verified_login = telegram_api::move_object_as<telegram_api::account_emailVerifiedLogin>(email_verified);
on_sent_code(std::move(verified_login->sent_code_));
@ -1173,7 +1187,8 @@ bool AuthManager::load_state() {
allow_google_id_ = db_state.allow_google_id_;
email_address_ = std::move(db_state.email_address_);
email_code_info_ = std::move(db_state.email_code_info_);
next_phone_number_login_date_ = db_state.next_phone_number_login_date_;
reset_available_period_ = db_state.reset_available_period_;
reset_pending_date_ = db_state.reset_pending_date_;
send_code_helper_ = std::move(db_state.send_code_helper_);
} else if (db_state.state_ == State::WaitCode) {
send_code_helper_ = std::move(db_state.send_code_helper_);
@ -1207,7 +1222,8 @@ void AuthManager::save_state() {
return DbState::wait_email_address(api_id_, api_hash_, allow_apple_id_, allow_google_id_, send_code_helper_);
} else if (state_ == State::WaitEmailCode) {
return DbState::wait_email_code(api_id_, api_hash_, allow_apple_id_, allow_google_id_, email_address_,
email_code_info_, next_phone_number_login_date_, send_code_helper_);
email_code_info_, reset_available_period_, reset_pending_date_,
send_code_helper_);
} else if (state_ == State::WaitCode) {
return DbState::wait_code(api_id_, api_hash_, send_code_helper_);
} else if (state_ == State::WaitQrCodeConfirmation) {

View File

@ -129,7 +129,8 @@ class AuthManager final : public NetActor {
// WaitEmailCode
string email_address_;
SentEmailCode email_code_info_;
int32 next_phone_number_login_date_ = 0;
int32 reset_available_period_ = -1;
int32 reset_pending_date_ = 0;
// WaitEmailAddress, WaitEmailCode, WaitCode and WaitRegistration
SendCodeHelper send_code_helper_;
@ -157,15 +158,16 @@ class AuthManager final : public NetActor {
}
static DbState wait_email_code(int32 api_id, string api_hash, bool allow_apple_id, bool allow_google_id,
string email_address, SentEmailCode email_code_info,
int32 next_phone_number_login_date, SendCodeHelper send_code_helper) {
string email_address, SentEmailCode email_code_info, int32 reset_available_period,
int32 reset_pending_date, SendCodeHelper send_code_helper) {
DbState state(State::WaitEmailCode, api_id, std::move(api_hash));
state.send_code_helper_ = std::move(send_code_helper);
state.allow_apple_id_ = allow_apple_id;
state.allow_google_id_ = allow_google_id;
state.email_address_ = std::move(email_address);
state.email_code_info_ = std::move(email_code_info);
state.next_phone_number_login_date_ = next_phone_number_login_date;
state.reset_available_period_ = reset_available_period;
state.reset_pending_date_ = reset_pending_date;
return state;
}
@ -242,7 +244,8 @@ class AuthManager final : public NetActor {
// State::WaitEmailCode
string email_address_;
SentEmailCode email_code_info_;
int32 next_phone_number_login_date_ = 0;
int32 reset_available_period_ = -1;
int32 reset_pending_date_ = 0;
EmailVerification email_code_;
// State::WaitCode

View File

@ -58,6 +58,7 @@ void AuthManager::DbState::store(StorerT &storer) const {
bool is_wait_registration_stores_phone_number = true;
bool is_wait_qr_code_confirmation_supported = true;
bool is_time_store_supported = true;
bool is_reset_email_address_supported = true;
BEGIN_STORE_FLAGS();
STORE_FLAG(has_terms_of_service);
STORE_FLAG(is_pbkdf2_supported);
@ -68,6 +69,7 @@ void AuthManager::DbState::store(StorerT &storer) const {
STORE_FLAG(allow_apple_id_);
STORE_FLAG(allow_google_id_);
STORE_FLAG(is_time_store_supported);
STORE_FLAG(is_reset_email_address_supported);
END_STORE_FLAGS();
store(state_, storer);
store(api_id_, storer);
@ -84,7 +86,8 @@ void AuthManager::DbState::store(StorerT &storer) const {
store(send_code_helper_, storer);
store(email_address_, storer);
store(email_code_info_, storer);
store(next_phone_number_login_date_, storer);
store(reset_available_period_, storer);
store(reset_pending_date_, storer);
} else if (state_ == State::WaitCode) {
store(send_code_helper_, storer);
} else if (state_ == State::WaitQrCodeConfirmation) {
@ -110,6 +113,7 @@ void AuthManager::DbState::parse(ParserT &parser) {
bool is_wait_registration_stores_phone_number = false;
bool is_wait_qr_code_confirmation_supported = false;
bool is_time_store_supported = false;
bool is_reset_email_address_supported = false;
if (parser.version() >= static_cast<int32>(Version::AddTermsOfService)) {
BEGIN_PARSE_FLAGS();
PARSE_FLAG(has_terms_of_service);
@ -121,16 +125,18 @@ void AuthManager::DbState::parse(ParserT &parser) {
PARSE_FLAG(allow_apple_id_);
PARSE_FLAG(allow_google_id_);
PARSE_FLAG(is_time_store_supported);
PARSE_FLAG(is_reset_email_address_supported);
END_PARSE_FLAGS();
}
if (!is_time_store_supported) {
return parser.set_error("Have no time store support");
if (!is_reset_email_address_supported) {
return parser.set_error("Have no reset email address support");
}
CHECK(is_pbkdf2_supported);
CHECK(is_srp_supported);
CHECK(is_wait_registration_supported);
CHECK(is_wait_registration_stores_phone_number);
CHECK(is_wait_qr_code_confirmation_supported);
CHECK(is_time_store_supported);
parse(state_, parser);
parse(api_id_, parser);
@ -147,7 +153,8 @@ void AuthManager::DbState::parse(ParserT &parser) {
parse(send_code_helper_, parser);
parse(email_address_, parser);
parse(email_code_info_, parser);
parse(next_phone_number_login_date_, parser);
parse(reset_available_period_, parser);
parse(reset_pending_date_, parser);
} else if (state_ == State::WaitCode) {
parse(send_code_helper_, parser);
} else if (state_ == State::WaitQrCodeConfirmation) {