2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2022-09-06 22:29:57 +02:00
|
|
|
#include "td/telegram/EmailVerification.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetActor.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2019-02-16 16:49:39 +01:00
|
|
|
#include "td/telegram/SendCodeHelper.h"
|
2022-09-06 15:08:10 +02:00
|
|
|
#include "td/telegram/SentEmailCode.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2021-03-28 23:50:15 +02:00
|
|
|
#include "td/telegram/TermsOfService.h"
|
|
|
|
#include "td/telegram/UserId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/actor.h"
|
2019-12-17 17:17:57 +01:00
|
|
|
#include "td/actor/Timeout.h"
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-04-11 23:18:03 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class AuthManager final : public NetActor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
AuthManager(int32 api_id, const string &api_hash, ActorShared<> parent);
|
|
|
|
|
2023-11-13 13:07:21 +01:00
|
|
|
bool is_bot() const {
|
|
|
|
return is_bot_ || net_query_type_ == NetQueryType::BotAuthentication;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool is_authorized() const;
|
2020-07-22 20:28:20 +02:00
|
|
|
bool was_authorized() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
void get_state(uint64 query_id);
|
|
|
|
|
2019-05-03 04:44:59 +02:00
|
|
|
void set_phone_number(uint64 query_id, string phone_number,
|
|
|
|
td_api::object_ptr<td_api::phoneNumberAuthenticationSettings> settings);
|
2023-01-19 14:45:47 +01:00
|
|
|
void set_firebase_token(uint64 query_id, string token);
|
2024-04-22 17:39:16 +02:00
|
|
|
void report_missing_code(uint64 query_id, string mobile_network_code);
|
2022-09-06 13:04:39 +02:00
|
|
|
void set_email_address(uint64 query_id, string email_address);
|
2024-05-13 14:55:02 +02:00
|
|
|
void resend_authentication_code(uint64 query_id, td_api::object_ptr<td_api::ResendCodeReason> &&reason);
|
2022-09-06 22:29:57 +02:00
|
|
|
void check_email_code(uint64 query_id, EmailVerification &&code);
|
2023-03-20 13:17:51 +01:00
|
|
|
void reset_email_address(uint64 query_id);
|
2019-07-16 21:08:34 +02:00
|
|
|
void check_code(uint64 query_id, string code);
|
2024-01-29 14:43:42 +01:00
|
|
|
void register_user(uint64 query_id, string first_name, string last_name, bool disable_notification);
|
2021-03-28 23:50:15 +02:00
|
|
|
void request_qr_code_authentication(uint64 query_id, vector<UserId> other_user_ids);
|
2018-12-31 20:04:05 +01:00
|
|
|
void check_bot_token(uint64 query_id, string bot_token);
|
|
|
|
void check_password(uint64 query_id, string password);
|
|
|
|
void request_password_recovery(uint64 query_id);
|
2021-06-28 21:33:08 +02:00
|
|
|
void check_password_recovery_code(uint64 query_id, string code);
|
2021-06-28 18:47:23 +02:00
|
|
|
void recover_password(uint64 query_id, string code, string new_password, string new_hint);
|
2020-07-22 23:19:47 +02:00
|
|
|
void log_out(uint64 query_id);
|
2022-07-01 20:25:34 +02:00
|
|
|
void delete_account(uint64 query_id, string reason, string password);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-17 17:17:57 +01:00
|
|
|
void on_update_login_token();
|
|
|
|
|
2021-12-15 18:30:25 +01:00
|
|
|
void on_authorization_lost(string source);
|
2018-12-26 20:04:46 +01:00
|
|
|
void on_closing(bool destroy_flag);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-20 18:33:41 +02:00
|
|
|
// can return nullptr if state isn't initialized yet
|
|
|
|
tl_object_ptr<td_api::AuthorizationState> get_current_authorization_state_object() const;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
2018-09-17 06:00:19 +02:00
|
|
|
static constexpr size_t MAX_NAME_LENGTH = 64; // server side limit
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-15 11:06:26 +01:00
|
|
|
enum class State : int32 {
|
|
|
|
None,
|
|
|
|
WaitPhoneNumber,
|
|
|
|
WaitCode,
|
2019-12-17 17:17:57 +01:00
|
|
|
WaitQrCodeConfirmation,
|
2018-03-15 11:06:26 +01:00
|
|
|
WaitPassword,
|
2019-07-16 21:08:34 +02:00
|
|
|
WaitRegistration,
|
2022-09-06 11:31:54 +02:00
|
|
|
WaitEmailAddress,
|
2022-09-06 15:08:10 +02:00
|
|
|
WaitEmailCode,
|
2018-03-15 11:06:26 +01:00
|
|
|
Ok,
|
|
|
|
LoggingOut,
|
2017-12-29 21:34:39 +01:00
|
|
|
DestroyingKeys,
|
2018-03-15 11:06:26 +01:00
|
|
|
Closing
|
|
|
|
} state_ = State::None;
|
2023-07-01 17:40:19 +02:00
|
|
|
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class NetQueryType : int32 {
|
2018-12-31 20:04:05 +01:00
|
|
|
None,
|
|
|
|
SignIn,
|
|
|
|
SignUp,
|
|
|
|
SendCode,
|
2022-09-06 13:04:39 +02:00
|
|
|
SendEmailCode,
|
2022-09-06 16:17:39 +02:00
|
|
|
VerifyEmailAddress,
|
2023-03-20 13:17:51 +01:00
|
|
|
ResetEmailAddress,
|
2019-12-17 17:17:57 +01:00
|
|
|
RequestQrCode,
|
|
|
|
ImportQrCode,
|
2018-12-31 20:04:05 +01:00
|
|
|
GetPassword,
|
|
|
|
CheckPassword,
|
|
|
|
RequestPasswordRecovery,
|
2021-06-28 21:33:08 +02:00
|
|
|
CheckPasswordRecoveryCode,
|
2018-12-31 20:04:05 +01:00
|
|
|
RecoverPassword,
|
2023-01-19 14:45:47 +01:00
|
|
|
RequestFirebaseSms,
|
2018-12-31 20:04:05 +01:00
|
|
|
BotAuthentication,
|
|
|
|
Authentication,
|
|
|
|
LogOut,
|
|
|
|
DeleteAccount
|
|
|
|
};
|
|
|
|
|
2018-03-15 11:06:26 +01:00
|
|
|
struct WaitPasswordState {
|
2018-08-04 08:55:49 +02:00
|
|
|
string current_client_salt_;
|
|
|
|
string current_server_salt_;
|
2018-08-10 15:22:48 +02:00
|
|
|
int32 srp_g_ = 0;
|
|
|
|
string srp_p_;
|
|
|
|
string srp_B_;
|
|
|
|
int64 srp_id_ = 0;
|
2018-03-15 11:06:26 +01:00
|
|
|
string hint_;
|
2018-08-04 08:55:49 +02:00
|
|
|
bool has_recovery_ = false;
|
2023-02-22 13:59:36 +01:00
|
|
|
bool has_secure_values_ = false;
|
2018-03-15 11:06:26 +01:00
|
|
|
string email_address_pattern_;
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
2018-03-15 11:06:26 +01:00
|
|
|
};
|
|
|
|
|
2023-07-01 17:40:19 +02:00
|
|
|
struct DbState;
|
2018-03-14 19:04:41 +01:00
|
|
|
|
|
|
|
bool load_state();
|
|
|
|
void save_state();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<> parent_;
|
|
|
|
|
2018-03-14 19:04:41 +01:00
|
|
|
// STATE
|
|
|
|
// from contructor
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 api_id_;
|
|
|
|
string api_hash_;
|
|
|
|
|
2022-09-06 11:31:54 +02:00
|
|
|
// State::WaitEmailAddress
|
|
|
|
bool allow_apple_id_ = false;
|
|
|
|
bool allow_google_id_ = false;
|
2022-09-06 15:08:10 +02:00
|
|
|
|
|
|
|
// State::WaitEmailCode
|
2022-09-06 13:04:39 +02:00
|
|
|
string email_address_;
|
2022-09-06 16:42:19 +02:00
|
|
|
SentEmailCode email_code_info_;
|
2023-03-19 23:40:05 +01:00
|
|
|
int32 reset_available_period_ = -1;
|
2023-03-20 12:45:11 +01:00
|
|
|
int32 reset_pending_date_ = -1;
|
2022-09-06 22:29:57 +02:00
|
|
|
EmailVerification email_code_;
|
2022-09-06 11:31:54 +02:00
|
|
|
|
2018-03-14 19:04:41 +01:00
|
|
|
// State::WaitCode
|
2018-12-31 20:04:05 +01:00
|
|
|
SendCodeHelper send_code_helper_;
|
2018-08-13 00:44:53 +02:00
|
|
|
string code_;
|
2019-07-16 21:08:34 +02:00
|
|
|
|
2019-12-17 17:17:57 +01:00
|
|
|
// State::WaitQrCodeConfirmation
|
2021-03-28 23:50:15 +02:00
|
|
|
vector<UserId> other_user_ids_;
|
2019-12-17 17:17:57 +01:00
|
|
|
string login_token_;
|
|
|
|
double login_token_expires_at_ = 0.0;
|
|
|
|
int32 imported_dc_id_ = -1;
|
|
|
|
|
2019-07-16 21:08:34 +02:00
|
|
|
// State::WaitPassword
|
2018-08-13 11:24:37 +02:00
|
|
|
string password_;
|
2019-07-16 21:08:34 +02:00
|
|
|
|
|
|
|
// State::WaitRegistration
|
2018-06-07 20:42:17 +02:00
|
|
|
TermsOfService terms_of_service_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-14 19:04:41 +01:00
|
|
|
// for bots
|
2018-12-31 20:04:05 +01:00
|
|
|
string bot_token_;
|
2019-12-17 17:17:57 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
uint64 query_id_ = 0;
|
|
|
|
|
2018-03-15 11:06:26 +01:00
|
|
|
WaitPasswordState wait_password_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-06-28 20:36:54 +02:00
|
|
|
string recovery_code_;
|
|
|
|
string new_password_;
|
|
|
|
string new_hint_;
|
|
|
|
|
2019-12-17 17:17:57 +01:00
|
|
|
int32 login_code_retry_delay_ = 0;
|
|
|
|
Timeout poll_export_login_code_timeout_;
|
|
|
|
|
2023-02-04 11:36:01 +01:00
|
|
|
bool checking_password_ = false;
|
2019-12-17 17:17:57 +01:00
|
|
|
bool was_qr_code_request_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool was_check_bot_token_ = false;
|
|
|
|
bool is_bot_ = false;
|
|
|
|
uint64 net_query_id_ = 0;
|
2021-11-11 15:39:09 +01:00
|
|
|
NetQueryType net_query_type_ = NetQueryType::None;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-09 02:17:38 +01:00
|
|
|
vector<uint64> pending_get_authorization_state_requests_;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_new_query(uint64 query_id);
|
2023-09-02 21:11:01 +02:00
|
|
|
void on_current_query_error(Status status);
|
2023-09-02 21:39:26 +02:00
|
|
|
void on_current_query_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
void start_net_query(NetQueryType net_query_type, NetQueryPtr net_query);
|
|
|
|
|
2019-12-17 17:17:57 +01:00
|
|
|
static void on_update_login_token_static(void *td);
|
|
|
|
void send_export_login_token_query();
|
|
|
|
void set_login_token_expires_at(double login_token_expires_at);
|
2020-08-17 16:35:15 +02:00
|
|
|
|
2022-07-01 20:25:34 +02:00
|
|
|
void do_delete_account(uint64 query_id, string reason,
|
|
|
|
Result<tl_object_ptr<telegram_api::InputCheckPasswordSRP>> r_input_password);
|
|
|
|
|
2022-09-06 16:42:19 +02:00
|
|
|
void send_auth_sign_in_query();
|
2020-08-17 16:35:15 +02:00
|
|
|
void send_log_out_query();
|
2017-12-29 21:34:39 +01:00
|
|
|
void destroy_auth_keys();
|
|
|
|
|
2023-09-20 20:28:23 +02:00
|
|
|
void on_account_banned() const;
|
|
|
|
|
2023-01-06 10:42:16 +01:00
|
|
|
void on_sent_code(telegram_api::object_ptr<telegram_api::auth_SentCode> &&sent_code_ptr);
|
2022-09-06 16:17:39 +02:00
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void on_send_code_result(NetQueryPtr &&net_query);
|
|
|
|
void on_send_email_code_result(NetQueryPtr &&net_query);
|
|
|
|
void on_verify_email_address_result(NetQueryPtr &&net_query);
|
|
|
|
void on_reset_email_address_result(NetQueryPtr &&net_query);
|
|
|
|
void on_request_qr_code_result(NetQueryPtr &&net_query, bool is_import);
|
|
|
|
void on_get_password_result(NetQueryPtr &&net_query);
|
|
|
|
void on_request_password_recovery_result(NetQueryPtr &&net_query);
|
|
|
|
void on_check_password_recovery_code_result(NetQueryPtr &&net_query);
|
|
|
|
void on_request_firebase_sms_result(NetQueryPtr &&net_query);
|
|
|
|
void on_authentication_result(NetQueryPtr &&net_query, bool is_from_current_query);
|
|
|
|
void on_log_out_result(NetQueryPtr &&net_query);
|
|
|
|
void on_delete_account_result(NetQueryPtr &&net_query);
|
2019-12-17 17:17:57 +01:00
|
|
|
void on_get_login_token(tl_object_ptr<telegram_api::auth_LoginToken> login_token);
|
|
|
|
void on_get_authorization(tl_object_ptr<telegram_api::auth_Authorization> auth_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void on_result(NetQueryPtr net_query) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-25 15:07:21 +02:00
|
|
|
void update_state(State new_state, bool should_save_state = true);
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<td_api::AuthorizationState> get_authorization_state_object(State authorization_state) const;
|
2021-10-19 17:11:16 +02:00
|
|
|
|
|
|
|
static void send_ok(uint64 query_id);
|
|
|
|
static void on_query_error(uint64 query_id, Status status);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void tear_down() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-02-16 16:49:39 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|