Add td_api::setAuthenticationEmailAddress.

This commit is contained in:
levlam 2022-09-06 14:04:39 +03:00
parent 9fc136976c
commit 45484ce53b
8 changed files with 72 additions and 5 deletions

View File

@ -4615,6 +4615,9 @@ checkDatabaseEncryptionKey encryption_key:bytes = Ok;
//@phone_number The phone number of the user, in international format @settings Settings for the authentication of the user's phone number; pass null to use default settings
setAuthenticationPhoneNumber phone_number:string settings:phoneNumberAuthenticationSettings = Ok;
//@description Sets the email address of the user and sends an authentication code to the email address. Works only when the current authorization state is authorizationStateWaitEmailAddress @email_address The email address of the user
setAuthenticationEmailAddress email_address:string = Ok;
//@description Re-sends an authentication code to the user. Works only when the current authorization state is authorizationStateWaitCode, the next_code_type of the result is not null and the server-specified timeout has passed
resendAuthenticationCode = Ok;

View File

@ -255,7 +255,7 @@ void AuthManager::set_phone_number(uint64 query_id, string phone_number,
query_id, Status::Error(400, "Cannot set phone number after bot token was entered. You need to log out first"));
}
if (phone_number.empty()) {
return on_query_error(query_id, Status::Error(400, "Phone number can't be empty"));
return on_query_error(query_id, Status::Error(400, "Phone number must be non-empty"));
}
other_user_ids_.clear();
@ -272,6 +272,22 @@ void AuthManager::set_phone_number(uint64 query_id, string phone_number,
std::move(phone_number), settings, api_id_, api_hash_)));
}
void AuthManager::set_email_address(uint64 query_id, string email_address) {
if (state_ != State::WaitEmailAddress) {
return on_query_error(query_id, Status::Error(400, "Call to setAuthenticationEmailAddress unexpected"));
}
if (email_address.empty()) {
return on_query_error(query_id, Status::Error(400, "Email address must be non-empty"));
}
email_address_ = std::move(email_address);
on_new_query(query_id);
start_net_query(NetQueryType::SendEmailCode,
G()->net_query_creator().create_unauth(send_code_helper_.send_verify_email_code(email_address_)));
}
void AuthManager::resend_authentication_code(uint64 query_id) {
if (state_ != State::WaitCode) {
return on_query_error(query_id, Status::Error(400, "Call to resendAuthenticationCode unexpected"));
@ -308,7 +324,7 @@ void AuthManager::register_user(uint64 query_id, string first_name, string last_
on_new_query(query_id);
first_name = clean_name(first_name, MAX_NAME_LENGTH);
if (first_name.empty()) {
return on_query_error(Status::Error(400, "First name can't be empty"));
return on_query_error(Status::Error(400, "First name must be non-empty"));
}
last_name = clean_name(last_name, MAX_NAME_LENGTH);
@ -496,6 +512,7 @@ void AuthManager::on_send_code_result(NetQueryPtr &result) {
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_));
send_code_helper_.on_phone_code_hash(std::move(sent_code->phone_code_hash_));
allow_apple_id_ = code_type->apple_signin_allowed_;
allow_google_id_ = code_type->google_signin_allowed_;
update_state(State::WaitEmailAddress, true);
@ -506,6 +523,18 @@ void AuthManager::on_send_code_result(NetQueryPtr &result) {
on_query_ok();
}
void AuthManager::on_send_email_code_result(NetQueryPtr &result) {
auto r_sent_code = fetch_result<telegram_api::account_sendVerifyEmailCode>(result->ok());
if (r_sent_code.is_error()) {
return on_query_error(r_sent_code.move_as_error());
}
auto sent_code = r_sent_code.move_as_ok();
LOG(INFO) << "Receive " << to_string(sent_code);
on_query_ok();
}
void AuthManager::on_request_qr_code_result(NetQueryPtr &result, bool is_import) {
Status status;
if (result->is_ok()) {
@ -871,8 +900,8 @@ void AuthManager::on_result(NetQueryPtr result) {
type = net_query_type_;
net_query_type_ = NetQueryType::None;
if (result->is_error()) {
if ((type == NetQueryType::SendCode || type == NetQueryType::SignIn || type == NetQueryType::RequestQrCode ||
type == NetQueryType::ImportQrCode) &&
if ((type == NetQueryType::SendCode || type == NetQueryType::SendEmailCode || type == NetQueryType::SignIn ||
type == NetQueryType::RequestQrCode || type == NetQueryType::ImportQrCode) &&
result->error().code() == 401 && result->error().message() == CSlice("SESSION_PASSWORD_NEEDED")) {
auto dc_id = DcId::main();
if (type == NetQueryType::ImportQrCode) {
@ -927,6 +956,9 @@ void AuthManager::on_result(NetQueryPtr result) {
case NetQueryType::SendCode:
on_send_code_result(result);
break;
case NetQueryType::SendEmailCode:
on_send_email_code_result(result);
break;
case NetQueryType::RequestQrCode:
on_request_qr_code_result(result, false);
break;

View File

@ -35,6 +35,7 @@ class AuthManager final : public NetActor {
void set_phone_number(uint64 query_id, string phone_number,
td_api::object_ptr<td_api::phoneNumberAuthenticationSettings> settings);
void set_email_address(uint64 query_id, string email_address);
void resend_authentication_code(uint64 query_id);
void check_code(uint64 query_id, string code);
void register_user(uint64 query_id, string first_name, string last_name);
@ -76,6 +77,7 @@ class AuthManager final : public NetActor {
SignIn,
SignUp,
SendCode,
SendEmailCode,
RequestQrCode,
ImportQrCode,
GetPassword,
@ -194,6 +196,7 @@ class AuthManager final : public NetActor {
// State::WaitEmailAddress
bool allow_apple_id_ = false;
bool allow_google_id_ = false;
string email_address_;
// State::WaitCode
SendCodeHelper send_code_helper_;
@ -249,6 +252,7 @@ class AuthManager final : public NetActor {
void destroy_auth_keys();
void on_send_code_result(NetQueryPtr &result);
void on_send_email_code_result(NetQueryPtr &result);
void on_request_qr_code_result(NetQueryPtr &result, bool is_import);
void on_get_password_result(NetQueryPtr &result);
void on_request_password_recovery_result(NetQueryPtr &result);

View File

@ -12,12 +12,16 @@
namespace td {
void SendCodeHelper::on_sent_code(telegram_api::object_ptr<telegram_api::auth_sentCode> sent_code) {
phone_code_hash_ = sent_code->phone_code_hash_;
phone_code_hash_ = std::move(sent_code->phone_code_hash_);
sent_code_info_ = get_authentication_code_info(std::move(sent_code->type_));
next_code_info_ = get_authentication_code_info(std::move(sent_code->next_type_));
next_code_timestamp_ = Timestamp::in((sent_code->flags_ & SENT_CODE_FLAG_HAS_TIMEOUT) != 0 ? sent_code->timeout_ : 0);
}
void SendCodeHelper::on_phone_code_hash(string &&phone_code_hash) {
phone_code_hash_ = std::move(phone_code_hash);
}
td_api::object_ptr<td_api::authorizationStateWaitCode> SendCodeHelper::get_authorization_state_wait_code() const {
return make_tl_object<td_api::authorizationStateWaitCode>(get_authentication_code_info_object());
}
@ -76,6 +80,10 @@ telegram_api::auth_sendCode SendCodeHelper::send_code(string phone_number, const
return telegram_api::auth_sendCode(phone_number_, api_id, api_hash, get_input_code_settings(settings));
}
telegram_api::account_sendVerifyEmailCode SendCodeHelper::send_verify_email_code(const string &email_address) {
return telegram_api::account_sendVerifyEmailCode(get_email_verify_purpose_login_setup(), email_address);
}
telegram_api::account_sendChangePhoneCode SendCodeHelper::send_change_phone_code(Slice phone_number,
const Settings &settings) {
phone_number_ = phone_number.str();
@ -175,4 +183,9 @@ td_api::object_ptr<td_api::AuthenticationCodeType> SendCodeHelper::get_authentic
}
}
telegram_api::object_ptr<telegram_api::emailVerifyPurposeLoginSetup>
SendCodeHelper::get_email_verify_purpose_login_setup() const {
return telegram_api::make_object<telegram_api::emailVerifyPurposeLoginSetup>(phone_number_, phone_code_hash_);
}
} // namespace td

View File

@ -20,6 +20,8 @@ class SendCodeHelper {
public:
void on_sent_code(telegram_api::object_ptr<telegram_api::auth_sentCode> sent_code);
void on_phone_code_hash(string &&phone_code_hash);
td_api::object_ptr<td_api::authorizationStateWaitCode> get_authorization_state_wait_code() const;
td_api::object_ptr<td_api::authenticationCodeInfo> get_authentication_code_info_object() const;
@ -31,6 +33,8 @@ class SendCodeHelper {
telegram_api::auth_sendCode send_code(string phone_number, const Settings &settings, int32 api_id,
const string &api_hash);
telegram_api::account_sendVerifyEmailCode send_verify_email_code(const string &email_address);
telegram_api::account_sendChangePhoneCode send_change_phone_code(Slice phone_number, const Settings &settings);
telegram_api::account_sendVerifyPhoneCode send_verify_phone_code(Slice phone_number, const Settings &settings);
@ -88,6 +92,8 @@ class SendCodeHelper {
const AuthenticationCodeInfo &authentication_code_info);
static telegram_api::object_ptr<telegram_api::codeSettings> get_input_code_settings(const Settings &settings);
telegram_api::object_ptr<telegram_api::emailVerifyPurposeLoginSetup> get_email_verify_purpose_login_setup() const;
};
} // namespace td

View File

@ -4261,6 +4261,11 @@ void Td::on_request(uint64 id, td_api::setAuthenticationPhoneNumber &request) {
std::move(request.settings_));
}
void Td::on_request(uint64 id, td_api::setAuthenticationEmailAddress &request) {
CLEAN_INPUT_STRING(request.email_address_);
send_closure(auth_manager_actor_, &AuthManager::set_email_address, id, std::move(request.email_address_));
}
void Td::on_request(uint64 id, const td_api::resendAuthenticationCode &request) {
send_closure(auth_manager_actor_, &AuthManager::resend_authentication_code, id);
}

View File

@ -398,6 +398,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::setAuthenticationPhoneNumber &request);
void on_request(uint64 id, td_api::setAuthenticationEmailAddress &request);
void on_request(uint64 id, const td_api::resendAuthenticationCode &request);
void on_request(uint64 id, td_api::checkAuthenticationCode &request);

View File

@ -1842,6 +1842,8 @@ class CliClient final : public Actor {
} else if (op == "sap" || op == "sapn") {
send_request(
td_api::make_object<td_api::setAuthenticationPhoneNumber>(args, get_phone_number_authentication_settings()));
} else if (op == "sae" || op == "saea") {
send_request(td_api::make_object<td_api::setAuthenticationEmailAddress>(args));
} else if (op == "rac") {
send_request(td_api::make_object<td_api::resendAuthenticationCode>());
} else if (op == "cdek" || op == "CheckDatabaseEncryptionKey") {