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
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2023-05-04 14:44:39 +02:00
|
|
|
#include "td/utils/port/Clocks.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
2018-11-27 03:06:34 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class AuthKey {
|
|
|
|
public:
|
|
|
|
AuthKey() = default;
|
|
|
|
AuthKey(uint64 auth_key_id, string &&auth_key) : auth_key_id_(auth_key_id), auth_key_(auth_key) {
|
|
|
|
}
|
2020-01-07 13:42:04 +01:00
|
|
|
void break_key() {
|
|
|
|
auth_key_id_++;
|
|
|
|
auth_key_[0]++;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return auth_key_.empty();
|
|
|
|
}
|
|
|
|
const string &key() const {
|
|
|
|
return auth_key_;
|
|
|
|
}
|
|
|
|
uint64 id() const {
|
|
|
|
return auth_key_id_;
|
|
|
|
}
|
|
|
|
bool auth_flag() const {
|
|
|
|
return auth_flag_;
|
|
|
|
}
|
|
|
|
void set_auth_flag(bool new_auth_flag) {
|
|
|
|
auth_flag_ = new_auth_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_header() const {
|
2022-01-06 19:19:07 +01:00
|
|
|
return have_header_ || Time::now() < header_expires_at_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-01-06 19:19:07 +01:00
|
|
|
void remove_header() {
|
2022-02-07 20:05:01 +01:00
|
|
|
if (auth_flag_ && have_header_) {
|
2022-01-06 19:19:07 +01:00
|
|
|
have_header_ = false;
|
|
|
|
header_expires_at_ = Time::now() + 3;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-02-07 20:05:01 +01:00
|
|
|
void restore_header() {
|
|
|
|
have_header_ = true;
|
|
|
|
}
|
2022-01-06 19:19:07 +01:00
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
double expires_at() const {
|
|
|
|
return expires_at_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-07 13:42:04 +01:00
|
|
|
double created_at() const {
|
|
|
|
return created_at_;
|
|
|
|
}
|
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
void set_expires_at(double expires_at) {
|
|
|
|
expires_at_ = expires_at;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-07 13:42:04 +01:00
|
|
|
void set_created_at(double created_at) {
|
|
|
|
created_at_ = created_at;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
void clear() {
|
|
|
|
auth_key_.clear();
|
|
|
|
}
|
|
|
|
|
2020-06-15 03:52:56 +02:00
|
|
|
static constexpr int32 AUTH_FLAG = 1;
|
|
|
|
static constexpr int32 HAS_CREATED_AT = 4;
|
2023-05-04 14:44:39 +02:00
|
|
|
static constexpr int32 HAS_EXPIRES_AT = 8;
|
2019-07-09 04:01:12 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
storer.store_binary(auth_key_id_);
|
2020-01-07 13:42:04 +01:00
|
|
|
bool has_created_at = created_at_ != 0;
|
2023-05-04 14:44:39 +02:00
|
|
|
bool has_expires_at = expires_at_ != 0;
|
|
|
|
int32 flags = 0;
|
|
|
|
if (auth_flag_) {
|
|
|
|
flags |= AUTH_FLAG;
|
|
|
|
}
|
|
|
|
if (has_created_at) {
|
|
|
|
flags |= HAS_CREATED_AT;
|
|
|
|
}
|
|
|
|
if (has_expires_at) {
|
|
|
|
flags |= HAS_EXPIRES_AT;
|
|
|
|
}
|
|
|
|
storer.store_binary(flags);
|
2018-12-31 20:04:05 +01:00
|
|
|
storer.store_string(auth_key_);
|
2020-01-07 13:42:04 +01:00
|
|
|
if (has_created_at) {
|
|
|
|
storer.store_binary(created_at_);
|
|
|
|
}
|
2023-05-04 14:44:39 +02:00
|
|
|
if (has_expires_at) {
|
|
|
|
double time_left = max(expires_at_ - Time::now(), 0.0);
|
|
|
|
storer.store_binary(time_left);
|
|
|
|
storer.store_binary(Clocks::system());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
auth_key_id_ = parser.fetch_long();
|
|
|
|
auto flags = parser.fetch_int();
|
|
|
|
auth_flag_ = (flags & AUTH_FLAG) != 0;
|
|
|
|
auth_key_ = parser.template fetch_string<string>();
|
2020-01-07 13:42:04 +01:00
|
|
|
if ((flags & HAS_CREATED_AT) != 0) {
|
|
|
|
created_at_ = parser.fetch_double();
|
|
|
|
}
|
2023-05-04 14:44:39 +02:00
|
|
|
if ((flags & HAS_EXPIRES_AT) != 0) {
|
|
|
|
double time_left = parser.fetch_double();
|
|
|
|
double old_server_time = parser.fetch_double();
|
|
|
|
double passed_server_time = max(Clocks::system() - old_server_time, 0.0);
|
|
|
|
time_left = max(time_left - passed_server_time, 0.0);
|
|
|
|
expires_at_ = Time::now() + time_left;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
// just in case
|
2022-01-06 19:19:07 +01:00
|
|
|
have_header_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-01-07 13:42:04 +01:00
|
|
|
uint64 auth_key_id_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
string auth_key_;
|
2020-01-07 13:42:04 +01:00
|
|
|
bool auth_flag_{false};
|
2022-01-06 19:19:07 +01:00
|
|
|
bool have_header_{true};
|
|
|
|
double header_expires_at_{0};
|
2020-01-07 13:42:04 +01:00
|
|
|
double expires_at_{0};
|
|
|
|
double created_at_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|