2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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
|
|
|
|
|
2021-06-11 01:48:10 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
#include "td/telegram/SecureStorage.h"
|
2021-06-11 01:48:10 +02:00
|
|
|
#include "td/telegram/SuggestedAction.h"
|
2018-08-04 08:55:49 +02: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"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2018-08-10 20:54:17 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Container.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
#include "td/utils/optional.h"
|
2018-08-04 08:55:49 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
struct TempPasswordState {
|
|
|
|
bool has_temp_password = false;
|
|
|
|
string temp_password;
|
|
|
|
int32 valid_until = 0; // unix_time
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::temporaryPasswordState> get_temporary_password_state_object() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
using ::td::store;
|
|
|
|
CHECK(has_temp_password);
|
|
|
|
store(temp_password, storer);
|
|
|
|
store(valid_until, storer);
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
2018-12-31 20:04:05 +01:00
|
|
|
using ::td::parse;
|
|
|
|
has_temp_password = true;
|
|
|
|
parse(temp_password, parser);
|
|
|
|
parse(valid_until, parser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PasswordManager : public NetQueryCallback {
|
|
|
|
public:
|
|
|
|
using State = tl_object_ptr<td_api::passwordState>;
|
|
|
|
using TempState = tl_object_ptr<td_api::temporaryPasswordState>;
|
|
|
|
|
|
|
|
explicit PasswordManager(ActorShared<> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
2018-07-03 19:28:00 +02:00
|
|
|
|
2018-08-10 15:22:48 +02:00
|
|
|
static tl_object_ptr<telegram_api::InputCheckPasswordSRP> get_input_check_password(Slice password, Slice client_salt,
|
|
|
|
Slice server_salt, int32 g,
|
|
|
|
Slice p, Slice B, int64 id);
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void get_state(Promise<State> promise);
|
|
|
|
void set_password(string current_password, string new_password, string new_hint, bool set_recovery_email_address,
|
|
|
|
string recovery_email_address, Promise<State> promise);
|
|
|
|
void set_recovery_email_address(string password, string new_recovery_email_address, Promise<State> promise);
|
|
|
|
void get_recovery_email_address(string password, Promise<tl_object_ptr<td_api::recoveryEmailAddress>> promise);
|
2019-02-25 19:37:26 +01:00
|
|
|
void check_recovery_email_address_code(string code, Promise<State> promise);
|
|
|
|
void resend_recovery_email_address_code(Promise<State> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-04-09 18:04:21 +02:00
|
|
|
void send_email_address_verification_code(
|
|
|
|
string email, Promise<td_api::object_ptr<td_api::emailAddressAuthenticationCodeInfo>> promise);
|
|
|
|
void resend_email_address_verification_code(
|
|
|
|
Promise<td_api::object_ptr<td_api::emailAddressAuthenticationCodeInfo>> promise);
|
2018-12-28 23:48:32 +01:00
|
|
|
void check_email_address_verification_code(string code, Promise<Unit> promise);
|
2018-04-09 18:04:21 +02:00
|
|
|
|
2018-04-09 20:06:37 +02:00
|
|
|
void request_password_recovery(Promise<td_api::object_ptr<td_api::emailAddressAuthenticationCodeInfo>> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
void recover_password(string code, Promise<State> promise);
|
|
|
|
|
2018-08-14 01:49:32 +02:00
|
|
|
void get_secure_secret(string password, Promise<secure_storage::Secret> promise);
|
2019-10-17 16:05:01 +02:00
|
|
|
void get_input_check_password_srp(string password,
|
|
|
|
Promise<tl_object_ptr<telegram_api::InputCheckPasswordSRP>> &&promise);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void get_temp_password_state(Promise<TempState> promise) /*const*/;
|
|
|
|
void create_temp_password(string password, int32 timeout, Promise<TempState> promise);
|
|
|
|
void drop_temp_password();
|
2019-08-02 11:01:55 +02:00
|
|
|
void drop_cached_secret();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static TempPasswordState get_temp_password_state_sync();
|
|
|
|
|
|
|
|
private:
|
2018-04-06 14:20:20 +02:00
|
|
|
static constexpr size_t MIN_NEW_SALT_SIZE = 8;
|
|
|
|
static constexpr size_t MIN_NEW_SECURE_SALT_SIZE = 8;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<> parent_;
|
|
|
|
|
|
|
|
struct PasswordState {
|
|
|
|
bool has_password = false;
|
|
|
|
string password_hint;
|
|
|
|
bool has_recovery_email_address = false;
|
2018-04-18 16:28:48 +02:00
|
|
|
bool has_secure_values = false;
|
2018-08-04 08:55:49 +02:00
|
|
|
string unconfirmed_recovery_email_address_pattern;
|
2019-02-10 21:16:52 +01:00
|
|
|
int32 code_length = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
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 current_srp_g;
|
|
|
|
string current_srp_p;
|
|
|
|
string current_srp_B;
|
|
|
|
int64 current_srp_id;
|
2018-08-04 08:55:49 +02:00
|
|
|
string new_client_salt;
|
|
|
|
string new_server_salt;
|
2018-08-10 15:22:48 +02:00
|
|
|
int32 new_srp_g;
|
|
|
|
string new_srp_p;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
string new_secure_salt;
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
State get_password_state_object() const {
|
2019-02-10 21:16:52 +01:00
|
|
|
td_api::object_ptr<td_api::emailAddressAuthenticationCodeInfo> code_info;
|
|
|
|
if (!unconfirmed_recovery_email_address_pattern.empty()) {
|
|
|
|
code_info = td_api::make_object<td_api::emailAddressAuthenticationCodeInfo>(
|
|
|
|
unconfirmed_recovery_email_address_pattern, code_length);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return td_api::make_object<td_api::passwordState>(has_password, password_hint, has_recovery_email_address,
|
2019-02-10 21:16:52 +01:00
|
|
|
has_secure_values, std::move(code_info));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
struct PasswordPrivateState {
|
|
|
|
string email;
|
|
|
|
optional<secure_storage::Secret> secret;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PasswordFullState {
|
|
|
|
PasswordState state;
|
|
|
|
PasswordPrivateState private_state;
|
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct UpdateSettings {
|
|
|
|
string current_password;
|
|
|
|
|
|
|
|
bool update_password = false;
|
|
|
|
string new_password;
|
|
|
|
string new_hint;
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
bool update_secure_secret = false;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool update_recovery_email_address = false;
|
|
|
|
string recovery_email_address;
|
|
|
|
};
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
optional<secure_storage::Secret> secret_;
|
2020-11-21 14:34:39 +01:00
|
|
|
double secret_expire_time_ = 0;
|
2018-09-27 00:48:12 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
TempPasswordState temp_password_state_;
|
|
|
|
Promise<TempState> create_temp_password_promise_;
|
|
|
|
|
2018-07-03 19:28:00 +02:00
|
|
|
string last_verified_email_address_;
|
|
|
|
|
2019-02-10 21:16:52 +01:00
|
|
|
int32 last_code_length_ = 0;
|
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
static Result<secure_storage::Secret> decrypt_secure_secret(
|
|
|
|
Slice password, tl_object_ptr<telegram_api::SecurePasswordKdfAlgo> algo_ptr, Slice secret, int64 secret_id);
|
|
|
|
|
2018-08-10 15:22:48 +02:00
|
|
|
static BufferSlice calc_password_hash(Slice password, Slice client_salt, Slice server_salt);
|
|
|
|
|
|
|
|
static Result<BufferSlice> calc_password_srp_hash(Slice password, Slice client_salt, Slice server_salt, int32 g,
|
|
|
|
Slice p);
|
|
|
|
|
2019-10-17 16:05:01 +02:00
|
|
|
static tl_object_ptr<telegram_api::InputCheckPasswordSRP> get_input_check_password(Slice password,
|
|
|
|
const PasswordState &state);
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void update_password_settings(UpdateSettings update_settings, Promise<State> promise);
|
2018-03-27 15:11:15 +02:00
|
|
|
void do_update_password_settings(UpdateSettings update_settings, PasswordFullState full_state, Promise<bool> promise);
|
2018-08-12 20:54:52 +02:00
|
|
|
void do_update_password_settings_impl(UpdateSettings update_settings, PasswordState state,
|
|
|
|
PasswordPrivateState private_state, Promise<bool> promise);
|
2019-02-10 21:16:52 +01:00
|
|
|
void on_get_code_length(int32 code_length);
|
2018-12-31 20:04:05 +01:00
|
|
|
void do_get_state(Promise<PasswordState> promise);
|
2018-03-27 15:11:15 +02:00
|
|
|
void get_full_state(string password, Promise<PasswordFullState> promise);
|
2019-02-10 21:16:52 +01:00
|
|
|
void do_get_secure_secret(bool allow_recursive, string password, Promise<secure_storage::Secret> promise);
|
2018-03-27 15:11:15 +02:00
|
|
|
void do_get_full_state(string password, PasswordState state, Promise<PasswordFullState> promise);
|
|
|
|
void cache_secret(secure_storage::Secret secret);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void do_create_temp_password(string password, int32 timeout, PasswordState &&password_state,
|
|
|
|
Promise<TempPasswordState> promise);
|
|
|
|
void on_finish_create_temp_password(Result<TempPasswordState> result, bool dummy);
|
|
|
|
|
|
|
|
void on_result(NetQueryPtr query) override;
|
|
|
|
|
|
|
|
void start_up() override;
|
2018-09-27 00:48:12 +02:00
|
|
|
void timeout_expired() override;
|
2018-04-19 17:39:30 +02:00
|
|
|
void hangup() override;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Container<Promise<NetQueryPtr>> container_;
|
|
|
|
void send_with_promise(NetQueryPtr query, Promise<NetQueryPtr> promise);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|