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
|
|
|
|
|
|
|
|
#include "td/mtproto/AuthData.h"
|
2018-02-12 11:54:27 +01:00
|
|
|
#include "td/mtproto/AuthKey.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/telegram/net/DcId.h"
|
|
|
|
#include "td/telegram/net/PublicRsaKeyShared.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-03-13 14:40:02 +01:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2019-07-23 01:14:34 +02:00
|
|
|
enum class AuthKeyState : int32 { Empty, NoAuth, OK };
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-23 01:14:34 +02:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, AuthKeyState state) {
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (state) {
|
2019-07-23 01:14:34 +02:00
|
|
|
case AuthKeyState::Empty:
|
2018-12-31 20:04:05 +01:00
|
|
|
return sb << "Empty";
|
2019-07-23 01:14:34 +02:00
|
|
|
case AuthKeyState::NoAuth:
|
|
|
|
return sb << "NoAuth";
|
|
|
|
case AuthKeyState::OK:
|
2018-12-31 20:04:05 +01:00
|
|
|
return sb << "OK";
|
|
|
|
default:
|
2019-07-23 01:14:34 +02:00
|
|
|
return sb << "Unknown AuthKeyState";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AuthDataShared {
|
|
|
|
public:
|
|
|
|
virtual ~AuthDataShared() = default;
|
|
|
|
class Listener {
|
|
|
|
public:
|
|
|
|
Listener() = default;
|
|
|
|
Listener(const Listener &) = delete;
|
|
|
|
Listener &operator=(const Listener &) = delete;
|
|
|
|
virtual ~Listener() = default;
|
|
|
|
virtual bool notify() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual DcId dc_id() const = 0;
|
|
|
|
virtual const std::shared_ptr<PublicRsaKeyShared> &public_rsa_key() = 0;
|
|
|
|
virtual mtproto::AuthKey get_auth_key() = 0;
|
2019-07-23 01:14:34 +02:00
|
|
|
virtual std::pair<AuthKeyState, bool> get_auth_key_state() = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void set_auth_key(const mtproto::AuthKey &auth_key) = 0;
|
|
|
|
virtual void update_server_time_difference(double diff) = 0;
|
|
|
|
virtual double get_server_time_difference() = 0;
|
|
|
|
virtual void add_auth_key_listener(unique_ptr<Listener> listener) = 0;
|
|
|
|
|
|
|
|
virtual void set_future_salts(const std::vector<mtproto::ServerSalt> &future_salts) = 0;
|
|
|
|
virtual std::vector<mtproto::ServerSalt> get_future_salts() = 0;
|
|
|
|
|
2019-07-23 01:14:34 +02:00
|
|
|
static AuthKeyState get_auth_key_state(const mtproto::AuthKey &auth_key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (auth_key.empty()) {
|
2019-07-23 01:14:34 +02:00
|
|
|
return AuthKeyState::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (auth_key.auth_flag()) {
|
2019-07-23 01:14:34 +02:00
|
|
|
return AuthKeyState::OK;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-07-23 01:14:34 +02:00
|
|
|
return AuthKeyState::NoAuth;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:40:02 +01:00
|
|
|
static std::shared_ptr<AuthDataShared> create(DcId dc_id, std::shared_ptr<PublicRsaKeyShared> public_rsa_key,
|
|
|
|
std::shared_ptr<Guard> guard);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2018-07-18 03:30:29 +02:00
|
|
|
} // namespace td
|