diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f755cfb8..218012006 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -330,6 +330,7 @@ set(TDLIB_SOURCE td/telegram/DownloadManager.cpp td/telegram/DownloadManagerCallback.cpp td/telegram/DraftMessage.cpp + td/telegram/EmailVerification.cpp td/telegram/EmojiStatus.cpp td/telegram/FileReferenceManager.cpp td/telegram/files/FileBitmask.cpp @@ -541,6 +542,7 @@ set(TDLIB_SOURCE td/telegram/DownloadManager.h td/telegram/DownloadManagerCallback.h td/telegram/DraftMessage.h + td/telegram/EmailVerification.h td/telegram/EmojiStatus.h td/telegram/EncryptedFile.h td/telegram/FileReferenceManager.h diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index 40b0a8e47..eabd2e4ab 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -271,7 +271,7 @@ void AuthManager::set_phone_number(uint64 query_id, string phone_number, email_code_info_ = {}; next_phone_number_login_date_ = 0; code_ = string(); - email_code_ = nullptr; + email_code_ = {}; if (send_code_helper_.phone_number() != phone_number) { send_code_helper_ = SendCodeHelper(); @@ -320,21 +320,20 @@ void AuthManager::resend_authentication_code(uint64 query_id) { } void AuthManager::send_auth_sign_in_query() { - bool is_email = email_code_ != nullptr; + bool is_email = !email_code_.is_empty(); int32 flags = is_email ? telegram_api::auth_signIn::EMAIL_VERIFICATION_MASK : telegram_api::auth_signIn::PHONE_CODE_MASK; start_net_query(NetQueryType::SignIn, G()->net_query_creator().create_unauth(telegram_api::auth_signIn( flags, send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code_, - is_email ? get_input_email_verification(email_code_) : nullptr))); + is_email ? email_code_.get_input_email_verification() : nullptr))); } -void AuthManager::check_email_code(uint64 query_id, td_api::object_ptr &&code) { - if (code == nullptr) { +void AuthManager::check_email_code(uint64 query_id, EmailVerification &&code) { + if (code.is_empty()) { return on_query_error(query_id, Status::Error(400, "Code must be non-empty")); } - if (state_ != State::WaitEmailCode && - !(state_ == State::WaitEmailAddress && code->get_id() == td_api::emailAddressAuthenticationCode::ID)) { + if (state_ != State::WaitEmailCode && !(state_ == State::WaitEmailAddress && code.is_email_code())) { return on_query_error(query_id, Status::Error(400, "Call to checkAuthenticationEmailCode unexpected")); } @@ -348,7 +347,7 @@ void AuthManager::check_email_code(uint64 query_id, td_api::object_ptrnet_query_creator().create_unauth(telegram_api::account_verifyEmail( - send_code_helper_.get_email_verify_purpose_login_setup(), get_input_email_verification(email_code_)))); + send_code_helper_.get_email_verify_purpose_login_setup(), email_code_.get_input_email_verification()))); } } @@ -358,7 +357,7 @@ void AuthManager::check_code(uint64 query_id, string code) { } code_ = std::move(code); - email_code_ = nullptr; + email_code_ = {}; on_new_query(query_id); send_auth_sign_in_query(); @@ -1200,35 +1199,4 @@ void AuthManager::save_state() { G()->td_db()->get_binlog_pmc()->set("auth_state", log_event_store(db_state).as_slice().str()); } -telegram_api::object_ptr AuthManager::get_input_email_verification( - const td_api::object_ptr &code) { - CHECK(code != nullptr); - switch (code->get_id()) { - case td_api::emailAddressAuthenticationCode::ID: { - auto token = static_cast(code.get())->code_; - if (!clean_input_string(token)) { - token.clear(); - } - return telegram_api::make_object(token); - } - case td_api::emailAddressAuthenticationAppleId::ID: { - auto token = static_cast(code.get())->token_; - if (!clean_input_string(token)) { - token.clear(); - } - return telegram_api::make_object(token); - } - case td_api::emailAddressAuthenticationGoogleId::ID: { - auto token = static_cast(code.get())->token_; - if (!clean_input_string(token)) { - token.clear(); - } - return telegram_api::make_object(token); - } - default: - UNREACHABLE(); - return nullptr; - } -} - } // namespace td diff --git a/td/telegram/AuthManager.h b/td/telegram/AuthManager.h index b218c4def..d6aeef3c3 100644 --- a/td/telegram/AuthManager.h +++ b/td/telegram/AuthManager.h @@ -6,6 +6,7 @@ // #pragma once +#include "td/telegram/EmailVerification.h" #include "td/telegram/net/NetActor.h" #include "td/telegram/net/NetQuery.h" #include "td/telegram/SendCodeHelper.h" @@ -38,7 +39,7 @@ class AuthManager final : public NetActor { td_api::object_ptr settings); void set_email_address(uint64 query_id, string email_address); void resend_authentication_code(uint64 query_id); - void check_email_code(uint64 query_id, td_api::object_ptr &&code); + void check_email_code(uint64 query_id, EmailVerification &&code); void check_code(uint64 query_id, string code); void register_user(uint64 query_id, string first_name, string last_name); void request_qr_code_authentication(uint64 query_id, vector other_user_ids); @@ -223,7 +224,7 @@ class AuthManager final : public NetActor { string email_address_; SentEmailCode email_code_info_; int32 next_phone_number_login_date_ = 0; - td_api::object_ptr email_code_; + EmailVerification email_code_; // State::WaitCode SendCodeHelper send_code_helper_; @@ -302,9 +303,6 @@ class AuthManager final : public NetActor { static void send_ok(uint64 query_id); static void on_query_error(uint64 query_id, Status status); - static telegram_api::object_ptr get_input_email_verification( - const td_api::object_ptr &code); - void start_up() final; void tear_down() final; }; diff --git a/td/telegram/EmailVerification.cpp b/td/telegram/EmailVerification.cpp new file mode 100644 index 000000000..234190a02 --- /dev/null +++ b/td/telegram/EmailVerification.cpp @@ -0,0 +1,53 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/EmailVerification.h" + +#include "td/telegram/misc.h" + +namespace td { + +EmailVerification::EmailVerification(td_api::object_ptr &&code) { + if (code == nullptr) { + return; + } + switch (code->get_id()) { + case td_api::emailAddressAuthenticationCode::ID: + type_ = Type::Code; + code_ = static_cast(code.get())->code_; + break; + case td_api::emailAddressAuthenticationAppleId::ID: + type_ = Type::Apple; + code_ = static_cast(code.get())->token_; + break; + case td_api::emailAddressAuthenticationGoogleId::ID: + type_ = Type::Google; + code_ = static_cast(code.get())->token_; + break; + default: + UNREACHABLE(); + break; + } + if (!clean_input_string(code_)) { + *this = {}; + } +} + +telegram_api::object_ptr EmailVerification::get_input_email_verification() const { + switch (type_) { + case Type::Code: + return telegram_api::make_object(code_); + case Type::Apple: + return telegram_api::make_object(code_); + case Type::Google: + return telegram_api::make_object(code_); + default: + UNREACHABLE(); + return nullptr; + } +} + +} // namespace td diff --git a/td/telegram/EmailVerification.h b/td/telegram/EmailVerification.h new file mode 100644 index 000000000..abd4d8ed3 --- /dev/null +++ b/td/telegram/EmailVerification.h @@ -0,0 +1,37 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/common.h" + +namespace td { + +class EmailVerification { + enum class Type : int32 { None, Code, Apple, Google }; + Type type_ = Type::None; + string code_; + + public: + EmailVerification() = default; + + explicit EmailVerification(td_api::object_ptr &&code); + + telegram_api::object_ptr get_input_email_verification() const; + + bool is_empty() const { + return type_ == Type::None; + } + + bool is_email_code() const { + return type_ == Type::Code; + } +}; + +} // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index f0a59d9a1..88b289db7 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4274,7 +4274,7 @@ void Td::on_request(uint64 id, const td_api::resendAuthenticationCode &request) } void Td::on_request(uint64 id, td_api::checkAuthenticationEmailCode &request) { - send_closure(auth_manager_actor_, &AuthManager::check_email_code, id, std::move(request.code_)); + send_closure(auth_manager_actor_, &AuthManager::check_email_code, id, EmailVerification(std::move(request.code_))); } void Td::on_request(uint64 id, td_api::checkAuthenticationCode &request) {