Simplify authorization state timeout handling.

This commit is contained in:
levlam 2022-11-15 11:36:39 +03:00
parent b2b59b02be
commit 2e6ac1f22c
3 changed files with 22 additions and 28 deletions

View File

@ -1121,28 +1121,8 @@ bool AuthManager::load_state() {
LOG(INFO) << "Ignore auth_state: api_id or api_hash changed"; LOG(INFO) << "Ignore auth_state: api_id or api_hash changed";
return false; return false;
} }
if (!db_state.state_timestamp_.is_in_past()) { if (db_state.expires_at_ <= Time::now()) {
LOG(INFO) << "Ignore auth_state: timestamp in the future"; LOG(INFO) << "Ignore auth_state: expired";
return false;
}
auto state_timeout = [state = db_state.state_] {
switch (state) {
case State::WaitPassword:
case State::WaitRegistration:
return 86400;
case State::WaitEmailAddress:
case State::WaitEmailCode:
case State::WaitCode:
case State::WaitQrCodeConfirmation:
return 5 * 60;
default:
UNREACHABLE();
return 0;
}
}();
if (Timestamp::at(db_state.state_timestamp_.at() + state_timeout).is_in_past()) {
LOG(INFO) << "Ignore auth_state: expired " << db_state.state_timestamp_.in();
return false; return false;
} }

View File

@ -117,7 +117,7 @@ class AuthManager final : public NetActor {
State state_; State state_;
int32 api_id_; int32 api_id_;
string api_hash_; string api_hash_;
Timestamp state_timestamp_; double expires_at_;
// WaitEmailAddress and WaitEmailCode // WaitEmailAddress and WaitEmailCode
bool allow_apple_id_ = false; bool allow_apple_id_ = false;
@ -202,7 +202,23 @@ class AuthManager final : public NetActor {
private: private:
DbState(State state, int32 api_id, string &&api_hash) DbState(State state, int32 api_id, string &&api_hash)
: state_(state), api_id_(api_id), api_hash_(std::move(api_hash)), state_timestamp_(Timestamp::now()) { : state_(state), api_id_(api_id), api_hash_(std::move(api_hash)) {
auto state_timeout = [state] {
switch (state) {
case State::WaitPassword:
case State::WaitRegistration:
return 86400;
case State::WaitEmailAddress:
case State::WaitEmailCode:
case State::WaitCode:
case State::WaitQrCodeConfirmation:
return 5 * 60;
default:
UNREACHABLE();
return 0;
}
}();
expires_at_ = Time::now() + state_timeout;
} }
}; };

View File

@ -70,7 +70,7 @@ void AuthManager::DbState::store(StorerT &storer) const {
store(state_, storer); store(state_, storer);
store(api_id_, storer); store(api_id_, storer);
store(api_hash_, storer); store(api_hash_, storer);
store_time(state_timestamp_.at(), storer); store_time(expires_at_, storer);
if (has_terms_of_service) { if (has_terms_of_service) {
store(terms_of_service_, storer); store(terms_of_service_, storer);
@ -133,9 +133,7 @@ void AuthManager::DbState::parse(ParserT &parser) {
parse(state_, parser); parse(state_, parser);
parse(api_id_, parser); parse(api_id_, parser);
parse(api_hash_, parser); parse(api_hash_, parser);
double state_timestamp = 0.0; parse_time(expires_at_, parser);
parse_time(state_timestamp, parser);
state_timestamp_ = Timestamp::at(state_timestamp);
if (has_terms_of_service) { if (has_terms_of_service) {
parse(terms_of_service_, parser); parse(terms_of_service_, parser);