Add authorizationStateWaitEmailAddress.

This commit is contained in:
levlam 2022-09-06 12:31:54 +03:00
parent 4fdac24cc5
commit 6ac839576c
5 changed files with 61 additions and 16 deletions

View File

@ -92,6 +92,10 @@ authorizationStateWaitEncryptionKey is_encrypted:Bool = AuthorizationState;
//@description TDLib needs the user's phone number to authorize. Call `setAuthenticationPhoneNumber` to provide the phone number, or use `requestQrCodeAuthentication`, or `checkAuthenticationBotToken` for other authentication options
authorizationStateWaitPhoneNumber = AuthorizationState;
//@description TDLib needs the user's email address to authorize. Call `setAuthenticationEmailAddress` to provide the email address, or directly call `setAuthenticationEmailCode` with Apple ID/Google ID token if allowed
//@allow_apple_id True, if authorization through Apple ID is allowed @allow_google_id True, if authorization through Google ID is allowed
authorizationStateWaitEmailAddress allow_apple_id:Bool allow_google_id:Bool = AuthorizationState;
//@description TDLib needs the user's authentication code to authorize @code_info Information about the authorization code that was sent
authorizationStateWaitCode code_info:authenticationCodeInfo = AuthorizationState;

View File

@ -107,6 +107,8 @@ tl_object_ptr<td_api::AuthorizationState> AuthManager::get_authorization_state_o
switch (authorization_state) {
case State::WaitPhoneNumber:
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::WaitCode:
return send_code_helper_.get_authorization_state_wait_code();
case State::WaitQrCodeConfirmation:
@ -174,7 +176,8 @@ void AuthManager::check_bot_token(uint64 query_id, string bot_token) {
void AuthManager::request_qr_code_authentication(uint64 query_id, vector<UserId> other_user_ids) {
if (state_ != State::WaitPhoneNumber) {
if ((state_ == State::WaitCode || state_ == State::WaitPassword || state_ == State::WaitRegistration) &&
if ((state_ == State::WaitEmailAddress || state_ == State::WaitCode || state_ == State::WaitPassword ||
state_ == State::WaitRegistration) &&
net_query_id_ == 0) {
// ok
} else {
@ -239,7 +242,8 @@ void AuthManager::on_update_login_token() {
void AuthManager::set_phone_number(uint64 query_id, string phone_number,
td_api::object_ptr<td_api::phoneNumberAuthenticationSettings> settings) {
if (state_ != State::WaitPhoneNumber) {
if ((state_ == State::WaitCode || state_ == State::WaitPassword || state_ == State::WaitRegistration) &&
if ((state_ == State::WaitEmailAddress || state_ == State::WaitCode || state_ == State::WaitPassword ||
state_ == State::WaitRegistration) &&
net_query_id_ == 0) {
// ok
} else {
@ -490,9 +494,15 @@ void AuthManager::on_send_code_result(NetQueryPtr &result) {
LOG(INFO) << "Receive " << to_string(sent_code);
send_code_helper_.on_sent_code(std::move(sent_code));
update_state(State::WaitCode, true);
if (sent_code->type_->get_id() == telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID) {
auto code_type = move_tl_object_as<telegram_api::auth_sentCodeTypeSetUpEmailRequired>(std::move(sent_code->type_));
allow_apple_id_ = code_type->apple_signin_allowed_;
allow_google_id_ = code_type->google_signin_allowed_;
update_state(State::WaitEmailAddress, true);
} else {
send_code_helper_.on_sent_code(std::move(sent_code));
update_state(State::WaitCode, true);
}
on_query_ok();
}
@ -992,6 +1002,7 @@ bool AuthManager::load_state() {
case State::WaitPassword:
case State::WaitRegistration:
return 86400;
case State::WaitEmailAddress:
case State::WaitCode:
case State::WaitQrCodeConfirmation:
return 5 * 60;
@ -1007,7 +1018,11 @@ bool AuthManager::load_state() {
}
LOG(INFO) << "Load auth_state from database: " << tag("state", static_cast<int32>(db_state.state_));
if (db_state.state_ == State::WaitCode) {
if (db_state.state_ == State::WaitEmailAddress) {
allow_apple_id_ = db_state.allow_apple_id_;
allow_google_id_ = db_state.allow_google_id_;
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_);
} else if (db_state.state_ == State::WaitQrCodeConfirmation) {
other_user_ids_ = std::move(db_state.other_user_ids_);
@ -1026,8 +1041,8 @@ bool AuthManager::load_state() {
}
void AuthManager::save_state() {
if (state_ != State::WaitCode && state_ != State::WaitQrCodeConfirmation && state_ != State::WaitPassword &&
state_ != State::WaitRegistration) {
if (state_ != State::WaitEmailAddress && state_ != State::WaitCode && state_ != State::WaitQrCodeConfirmation &&
state_ != State::WaitPassword && state_ != State::WaitRegistration) {
if (state_ != State::Closing) {
G()->td_db()->get_binlog_pmc()->erase("auth_state");
}
@ -1035,7 +1050,9 @@ void AuthManager::save_state() {
}
DbState db_state = [&] {
if (state_ == State::WaitCode) {
if (state_ == State::WaitEmailAddress) {
return DbState::wait_email_address(api_id_, api_hash_, allow_apple_id_, allow_google_id_, send_code_helper_);
} else if (state_ == State::WaitCode) {
return DbState::wait_code(api_id_, api_hash_, send_code_helper_);
} else if (state_ == State::WaitQrCodeConfirmation) {
return DbState::wait_qr_code_confirmation(api_id_, api_hash_, other_user_ids_, login_token_,

View File

@ -65,6 +65,7 @@ class AuthManager final : public NetActor {
WaitQrCodeConfirmation,
WaitPassword,
WaitRegistration,
WaitEmailAddress,
Ok,
LoggingOut,
DestroyingKeys,
@ -111,7 +112,11 @@ class AuthManager final : public NetActor {
string api_hash_;
Timestamp state_timestamp_;
// WaitCode
// WaitEmailAddress
bool allow_apple_id_ = false;
bool allow_google_id_ = false;
// WaitEmailAddress and WaitCode
SendCodeHelper send_code_helper_;
// WaitQrCodeConfirmation
@ -127,6 +132,15 @@ class AuthManager final : public NetActor {
DbState() = default;
static DbState wait_email_address(int32 api_id, string api_hash, bool allow_apple_id, bool allow_google_id,
SendCodeHelper send_code_helper) {
DbState state(State::WaitEmailAddress, 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;
return state;
}
static DbState wait_code(int32 api_id, string api_hash, SendCodeHelper send_code_helper) {
DbState state(State::WaitCode, api_id, std::move(api_hash));
state.send_code_helper_ = std::move(send_code_helper);
@ -177,6 +191,10 @@ class AuthManager final : public NetActor {
int32 api_id_;
string api_hash_;
// State::WaitEmailAddress
bool allow_apple_id_ = false;
bool allow_google_id_ = false;
// State::WaitCode
SendCodeHelper send_code_helper_;
string code_;

View File

@ -62,6 +62,8 @@ void AuthManager::DbState::store(StorerT &storer) const {
STORE_FLAG(is_wait_registration_supported);
STORE_FLAG(is_wait_registration_stores_phone_number);
STORE_FLAG(is_wait_qr_code_confirmation_supported);
STORE_FLAG(allow_apple_id_);
STORE_FLAG(allow_google_id_);
END_STORE_FLAGS();
store(state_, storer);
store(api_id_, storer);
@ -72,7 +74,9 @@ void AuthManager::DbState::store(StorerT &storer) const {
store(terms_of_service_, storer);
}
if (state_ == State::WaitCode) {
if (state_ == State::WaitEmailAddress) {
store(send_code_helper_, storer);
} else if (state_ == State::WaitCode) {
store(send_code_helper_, storer);
} else if (state_ == State::WaitQrCodeConfirmation) {
store(other_user_ids_, storer);
@ -104,6 +108,8 @@ void AuthManager::DbState::parse(ParserT &parser) {
PARSE_FLAG(is_wait_registration_supported);
PARSE_FLAG(is_wait_registration_stores_phone_number);
PARSE_FLAG(is_wait_qr_code_confirmation_supported);
PARSE_FLAG(allow_apple_id_);
PARSE_FLAG(allow_google_id_);
END_PARSE_FLAGS();
}
if (!is_wait_qr_code_confirmation_supported) {
@ -123,7 +129,9 @@ void AuthManager::DbState::parse(ParserT &parser) {
parse(terms_of_service_, parser);
}
if (state_ == State::WaitCode) {
if (state_ == State::WaitEmailAddress) {
parse(send_code_helper_, parser);
} else if (state_ == State::WaitCode) {
parse(send_code_helper_, parser);
} else if (state_ == State::WaitQrCodeConfirmation) {
parse(other_user_ids_, parser);

View File

@ -145,10 +145,8 @@ SendCodeHelper::AuthenticationCodeInfo SendCodeHelper::get_authentication_code_i
auto code_type = move_tl_object_as<telegram_api::auth_sentCodeTypeEmailCode>(sent_code_type_ptr);
return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Message, code_type->length_, ""};
}
case telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID: {
auto code_type = move_tl_object_as<telegram_api::auth_sentCodeTypeSetUpEmailRequired>(sent_code_type_ptr);
return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Message, 0, ""};
}
case telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID:
return AuthenticationCodeInfo();
default:
UNREACHABLE();
return AuthenticationCodeInfo();