2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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
|
|
|
|
|
|
|
|
#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"
|
2018-06-07 20:42:17 +02:00
|
|
|
#include "td/telegram/TermsOfService.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Status.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
#include "td/utils/Time.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-04-11 23:18:03 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class AuthManager : public NetActor {
|
|
|
|
public:
|
|
|
|
AuthManager(int32 api_id, const string &api_hash, ActorShared<> parent);
|
|
|
|
|
|
|
|
bool is_bot() const;
|
2018-03-08 18:35:23 +01:00
|
|
|
void set_is_bot(bool is_bot);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool is_authorized() const;
|
|
|
|
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);
|
2018-12-31 20:04:05 +01:00
|
|
|
void resend_authentication_code(uint64 query_id);
|
2019-07-16 21:08:34 +02:00
|
|
|
void check_code(uint64 query_id, string code);
|
|
|
|
void register_user(uint64 query_id, string first_name, string last_name);
|
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);
|
|
|
|
void recover_password(uint64 query_id, string code);
|
|
|
|
void logout(uint64 query_id);
|
|
|
|
void delete_account(uint64 query_id, const string &reason);
|
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
void on_authorization_lost();
|
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,
|
|
|
|
WaitPassword,
|
2019-07-16 21:08:34 +02:00
|
|
|
WaitRegistration,
|
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;
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class NetQueryType : int32 {
|
2018-12-31 20:04:05 +01:00
|
|
|
None,
|
|
|
|
SignIn,
|
|
|
|
SignUp,
|
|
|
|
SendCode,
|
|
|
|
GetPassword,
|
|
|
|
CheckPassword,
|
|
|
|
RequestPasswordRecovery,
|
|
|
|
RecoverPassword,
|
|
|
|
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;
|
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
|
|
|
};
|
|
|
|
|
2018-03-14 19:04:41 +01:00
|
|
|
struct DbState {
|
|
|
|
State state_;
|
|
|
|
int32 api_id_;
|
|
|
|
string api_hash_;
|
|
|
|
Timestamp state_timestamp_;
|
|
|
|
|
2018-03-15 11:06:26 +01:00
|
|
|
// WaitCode
|
|
|
|
SendCodeHelper send_code_helper_;
|
|
|
|
|
2018-06-07 20:42:17 +02:00
|
|
|
// WaitPassword
|
2018-03-15 11:06:26 +01:00
|
|
|
WaitPasswordState wait_password_state_;
|
|
|
|
|
2019-07-16 21:08:34 +02:00
|
|
|
// WaitRegistration
|
|
|
|
TermsOfService terms_of_service_;
|
|
|
|
|
|
|
|
DbState() = default;
|
2019-11-08 17:23:05 +01:00
|
|
|
static DbState wait_code(int32 api_id, string api_hash, SendCodeHelper send_code_helper) {
|
2019-07-16 21:08:34 +02:00
|
|
|
DbState state(State::WaitCode, api_id, api_hash);
|
2018-03-15 11:06:26 +01:00
|
|
|
state.send_code_helper_ = std::move(send_code_helper);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DbState wait_password(int32 api_id, string api_hash, WaitPasswordState wait_password_state) {
|
2019-07-16 21:08:34 +02:00
|
|
|
DbState state(State::WaitPassword, api_id, api_hash);
|
2018-03-15 11:06:26 +01:00
|
|
|
state.wait_password_state_ = std::move(wait_password_state);
|
2019-07-16 21:08:34 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-11-08 17:23:05 +01:00
|
|
|
static DbState wait_registration(int32 api_id, string api_hash, SendCodeHelper send_code_helper,
|
|
|
|
TermsOfService terms_of_service) {
|
2019-07-16 21:08:34 +02:00
|
|
|
DbState state(State::WaitRegistration, api_id, api_hash);
|
2019-11-08 17:23:05 +01:00
|
|
|
state.send_code_helper_ = std::move(send_code_helper);
|
2019-07-16 21:08:34 +02:00
|
|
|
state.terms_of_service_ = std::move(terms_of_service);
|
2018-03-15 11:06:26 +01:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
2019-07-16 21:08:34 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
DbState(State state, int32 api_id, string api_hash)
|
|
|
|
: state_(state), api_id_(api_id), api_hash_(api_hash), state_timestamp_(Timestamp::now()) {
|
|
|
|
}
|
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_;
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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_;
|
|
|
|
uint64 query_id_ = 0;
|
|
|
|
|
2018-03-15 11:06:26 +01:00
|
|
|
WaitPasswordState wait_password_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool was_check_bot_token_ = false;
|
|
|
|
bool is_bot_ = false;
|
|
|
|
uint64 net_query_id_ = 0;
|
|
|
|
NetQueryType net_query_type_;
|
|
|
|
|
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);
|
|
|
|
void on_query_error(Status status);
|
|
|
|
void on_query_error(uint64 id, Status status);
|
|
|
|
void on_query_ok();
|
|
|
|
void start_net_query(NetQueryType net_query_type, NetQueryPtr net_query);
|
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
void destroy_auth_keys();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_send_code_result(NetQueryPtr &result);
|
|
|
|
void on_get_password_result(NetQueryPtr &result);
|
|
|
|
void on_request_password_recovery_result(NetQueryPtr &result);
|
|
|
|
void on_authentication_result(NetQueryPtr &result, bool expected_flag);
|
|
|
|
void on_log_out_result(NetQueryPtr &result);
|
|
|
|
void on_delete_account_result(NetQueryPtr &result);
|
2019-11-08 17:23:05 +01:00
|
|
|
void on_authorization(tl_object_ptr<telegram_api::auth_Authorization> auth_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void on_result(NetQueryPtr result) override;
|
|
|
|
|
2018-03-14 19:04:41 +01:00
|
|
|
void update_state(State new_state, bool force = false, 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;
|
|
|
|
void send_ok(uint64 query_id);
|
|
|
|
|
|
|
|
void start_up() override;
|
|
|
|
void tear_down() override;
|
|
|
|
};
|
2019-02-16 16:49:39 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|