Add class SentEmailCode.

This commit is contained in:
levlam 2022-09-06 14:46:11 +03:00
parent 45484ce53b
commit 79be03d071
5 changed files with 110 additions and 35 deletions

View File

@ -429,6 +429,7 @@ set(TDLIB_SOURCE
td/telegram/SecureStorage.cpp
td/telegram/SecureValue.cpp
td/telegram/SendCodeHelper.cpp
td/telegram/SentEmailCode.cpp
td/telegram/SequenceDispatcher.cpp
td/telegram/SpecialStickerSetType.cpp
td/telegram/SponsoredMessageManager.cpp
@ -676,6 +677,7 @@ set(TDLIB_SOURCE
td/telegram/SecureStorage.h
td/telegram/SecureValue.h
td/telegram/SendCodeHelper.h
td/telegram/SentEmailCode.h
td/telegram/SequenceDispatcher.h
td/telegram/ServerMessageId.h
td/telegram/SetWithPosition.h

View File

@ -425,20 +425,18 @@ void PasswordManager::send_email_address_verification_code(
last_verified_email_address_ = email;
auto query = G()->net_query_creator().create(telegram_api::account_sendVerifyEmailCode(
make_tl_object<telegram_api::emailVerifyPurposePassport>(), std::move(email)));
send_with_promise(
std::move(query), PromiseCreator::lambda([promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::account_sendVerifyEmailCode>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
auto result = r_result.move_as_ok();
if (result->length_ < 0 || result->length_ >= 100) {
LOG(ERROR) << "Receive wrong code length " << result->length_;
result->length_ = 0;
}
return promise.set_value(
make_tl_object<td_api::emailAddressAuthenticationCodeInfo>(result->email_pattern_, result->length_));
}));
send_with_promise(std::move(query),
PromiseCreator::lambda([promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::account_sendVerifyEmailCode>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
SentEmailCode sent_code(r_result.move_as_ok());
if (sent_code.is_empty()) {
return promise.set_error(Status::Error(500, "Receive invalid response"));
}
return promise.set_value(sent_code.get_email_address_authentication_code_info_object());
}));
}
void PasswordManager::resend_email_address_verification_code(
@ -469,16 +467,19 @@ void PasswordManager::check_email_address_verification_code(string code, Promise
void PasswordManager::request_password_recovery(
Promise<td_api::object_ptr<td_api::emailAddressAuthenticationCodeInfo>> promise) {
// is called only after authorization
send_with_promise(
G()->net_query_creator().create(telegram_api::auth_requestPasswordRecovery()),
PromiseCreator::lambda([promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::auth_requestPasswordRecovery>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
auto result = r_result.move_as_ok();
return promise.set_value(make_tl_object<td_api::emailAddressAuthenticationCodeInfo>(result->email_pattern_, 0));
}));
send_with_promise(G()->net_query_creator().create(telegram_api::auth_requestPasswordRecovery()),
PromiseCreator::lambda([promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
auto r_result = fetch_result<telegram_api::auth_requestPasswordRecovery>(std::move(r_query));
if (r_result.is_error()) {
return promise.set_error(r_result.move_as_error());
}
auto result = r_result.move_as_ok();
SentEmailCode sent_code(std::move(result->email_pattern_), 0);
if (sent_code.is_empty()) {
return promise.set_error(Status::Error(500, "Receive invalid response"));
}
return promise.set_value(sent_code.get_email_address_authentication_code_info_object());
}));
}
void PasswordManager::check_password_recovery_code(string code, Promise<Unit> promise) {
@ -795,8 +796,7 @@ void PasswordManager::do_get_state(Promise<PasswordState> promise) {
state.has_password = false;
send_closure(actor_id, &PasswordManager::drop_cached_secret);
}
state.unconfirmed_recovery_email_address_pattern = std::move(password->email_unconfirmed_pattern_);
state.code_length = code_length;
state.unconfirmed_recovery_email_code = {std::move(password->email_unconfirmed_pattern_), code_length};
if (password->flags_ & telegram_api::account_password::PENDING_RESET_DATE_MASK) {
state.pending_reset_date = td::max(password->pending_reset_date_, 0);

View File

@ -9,6 +9,7 @@
#include "td/telegram/net/NetQuery.h"
#include "td/telegram/NewPasswordState.h"
#include "td/telegram/SecureStorage.h"
#include "td/telegram/SentEmailCode.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
@ -106,8 +107,7 @@ class PasswordManager final : public NetQueryCallback {
string password_hint;
bool has_recovery_email_address = false;
bool has_secure_values = false;
string unconfirmed_recovery_email_address_pattern;
int32 code_length = 0;
SentEmailCode unconfirmed_recovery_email_code;
int32 pending_reset_date = 0;
string current_client_salt;
@ -120,13 +120,9 @@ class PasswordManager final : public NetQueryCallback {
NewPasswordState new_state;
State get_password_state_object() const {
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);
}
return td_api::make_object<td_api::passwordState>(has_password, password_hint, has_recovery_email_address,
has_secure_values, std::move(code_info), pending_reset_date);
return td_api::make_object<td_api::passwordState>(
has_password, password_hint, has_recovery_email_address, has_secure_values,
unconfirmed_recovery_email_code.get_email_address_authentication_code_info_object(), pending_reset_date);
}
};

View File

@ -0,0 +1,27 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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)
//
#include "td/telegram/SentEmailCode.h"
namespace td {
SentEmailCode::SentEmailCode(telegram_api::object_ptr<telegram_api::account_sentEmailCode> &&email_code)
: email_address_pattern_(std::move(email_code->email_pattern_)), code_length_(email_code->length_) {
if (code_length_ < 0 || code_length_ >= 100) {
LOG(ERROR) << "Receive wrong email code length " << code_length_;
code_length_ = 0;
}
}
td_api::object_ptr<td_api::emailAddressAuthenticationCodeInfo>
SentEmailCode::get_email_address_authentication_code_info_object() const {
if (is_empty()) {
return nullptr;
}
return td_api::make_object<td_api::emailAddressAuthenticationCodeInfo>(email_address_pattern_, code_length_);
}
} // namespace td

View File

@ -0,0 +1,50 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/common.h"
#include "td/utils/tl_helpers.h"
namespace td {
class SentEmailCode {
string email_address_pattern_;
int32 code_length_ = 0;
public:
SentEmailCode() = default;
SentEmailCode(string &&email_address_pattern, int32 code_length)
: email_address_pattern_(std::move(email_address_pattern)), code_length_(code_length) {
}
explicit SentEmailCode(telegram_api::object_ptr<telegram_api::account_sentEmailCode> &&email_code);
td_api::object_ptr<td_api::emailAddressAuthenticationCodeInfo> get_email_address_authentication_code_info_object()
const;
bool is_empty() const {
return email_address_pattern_.empty();
}
template <class StorerT>
void store(StorerT &storer) const {
td::store(email_address_pattern_, storer);
td::store(code_length_, storer);
}
template <class ParserT>
void parse(ParserT &parser) {
td::parse(email_address_pattern_, parser);
td::parse(code_length_, parser);
}
};
} // namespace td