Support url_auth_domains.

This commit is contained in:
levlam 2021-03-16 01:29:54 +03:00
parent 54b9760935
commit 62c9890bcf
5 changed files with 78 additions and 15 deletions

View File

@ -11,6 +11,7 @@
#include "td/telegram/Global.h" #include "td/telegram/Global.h"
#include "td/telegram/JsonValue.h" #include "td/telegram/JsonValue.h"
#include "td/telegram/logevent/LogEvent.h" #include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/net/AuthDataShared.h" #include "td/telegram/net/AuthDataShared.h"
#include "td/telegram/net/ConnectionCreator.h" #include "td/telegram/net/ConnectionCreator.h"
#include "td/telegram/net/DcId.h" #include "td/telegram/net/DcId.h"
@ -891,6 +892,8 @@ void ConfigManager::start_up() {
autologin_update_time_ = Time::now() - 365 * 86400; autologin_update_time_ = Time::now() - 365 * 86400;
autologin_domains_ = full_split(G()->td_db()->get_binlog_pmc()->get("autologin_domains"), '\xFF'); autologin_domains_ = full_split(G()->td_db()->get_binlog_pmc()->get("autologin_domains"), '\xFF');
url_auth_domains_ = full_split(G()->td_db()->get_binlog_pmc()->get("url_auth_domains"), '\xFF');
} }
ActorShared<> ConfigManager::create_reference() { ActorShared<> ConfigManager::create_reference() {
@ -979,6 +982,10 @@ void ConfigManager::get_external_link_info(string &&link, Promise<td_api::object
} }
if (!td::contains(autologin_domains_, r_url.ok().host_)) { if (!td::contains(autologin_domains_, r_url.ok().host_)) {
if (td::contains(url_auth_domains_, r_url.ok().host_)) {
send_closure(G()->messages_manager(), &MessagesManager::get_link_login_url_info, link, std::move(promise));
return;
}
return promise.set_value(std::move(default_result)); return promise.set_value(std::move(default_result));
} }
@ -1525,6 +1532,9 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
autologin_domains_.clear(); autologin_domains_.clear();
autologin_update_time_ = Time::now(); autologin_update_time_ = Time::now();
auto old_url_auth_domains = std::move(url_auth_domains_);
url_auth_domains_.clear();
vector<tl_object_ptr<telegram_api::jsonObjectValue>> new_values; vector<tl_object_ptr<telegram_api::jsonObjectValue>> new_values;
string ignored_restriction_reasons; string ignored_restriction_reasons;
vector<string> dice_emojis; vector<string> dice_emojis;
@ -1715,6 +1725,22 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
} }
continue; continue;
} }
if (key == "url_auth_domains") {
if (value->get_id() == telegram_api::jsonArray::ID) {
auto domains = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
for (auto &domain : domains) {
CHECK(domain != nullptr);
if (domain->get_id() == telegram_api::jsonString::ID) {
url_auth_domains_.push_back(std::move(static_cast<telegram_api::jsonString *>(domain.get())->value_));
} else {
LOG(ERROR) << "Receive unexpected url auth domain " << to_string(domain);
}
}
} else {
LOG(ERROR) << "Receive unexpected url_auth_domains " << to_string(*value);
}
continue;
}
new_values.push_back(std::move(key_value)); new_values.push_back(std::move(key_value));
} }
@ -1726,6 +1752,9 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
if (autologin_domains_ != old_autologin_domains) { if (autologin_domains_ != old_autologin_domains) {
G()->td_db()->get_binlog_pmc()->set("autologin_domains", implode(autologin_domains_, '\xFF')); G()->td_db()->get_binlog_pmc()->set("autologin_domains", implode(autologin_domains_, '\xFF'));
} }
if (url_auth_domains_ != old_url_auth_domains) {
G()->td_db()->get_binlog_pmc()->set("url_auth_domains", implode(url_auth_domains_, '\xFF'));
}
ConfigShared &shared_config = G()->shared_config(); ConfigShared &shared_config = G()->shared_config();

View File

@ -119,6 +119,7 @@ class ConfigManager : public NetQueryCallback {
string autologin_token_; string autologin_token_;
vector<string> autologin_domains_; vector<string> autologin_domains_;
double autologin_update_time_ = 0.0; double autologin_update_time_ = 0.0;
vector<string> url_auth_domains_;
FloodControlStrict lazy_request_flood_control_; FloodControlStrict lazy_request_flood_control_;

View File

@ -4616,12 +4616,18 @@ class RequestUrlAuthQuery : public Td::ResultHandler {
void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id) { void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id) {
url_ = std::move(url); url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (dialog_id.is_valid()) {
dialog_id_ = dialog_id; dialog_id_ = dialog_id;
auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read); input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr); CHECK(input_peer != nullptr);
int32 flags = telegram_api::messages_requestUrlAuth::PEER_MASK; flags |= telegram_api::messages_requestUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_requestUrlAuth::URL_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_requestUrlAuth( send_query(G()->net_query_creator().create(telegram_api::messages_requestUrlAuth(
flags, std::move(input_peer), message_id.get_server_message_id().get(), button_id, string()))); flags, std::move(input_peer), message_id.get_server_message_id().get(), button_id, url_)));
} }
void on_result(uint64 id, BufferSlice packet) override { void on_result(uint64 id, BufferSlice packet) override {
@ -4659,7 +4665,8 @@ class RequestUrlAuthQuery : public Td::ResultHandler {
} }
void on_error(uint64 id, Status status) override { void on_error(uint64 id, Status status) override {
if (!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "RequestUrlAuthQuery")) { if (!dialog_id_.is_valid() ||
!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "RequestUrlAuthQuery")) {
LOG(INFO) << "RequestUrlAuthQuery returned " << status; LOG(INFO) << "RequestUrlAuthQuery returned " << status;
} }
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false)); promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false));
@ -4677,16 +4684,21 @@ class AcceptUrlAuthQuery : public Td::ResultHandler {
void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access) { void send(string url, DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access) {
url_ = std::move(url); url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (dialog_id.is_valid()) {
dialog_id_ = dialog_id; dialog_id_ = dialog_id;
auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read); input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr); CHECK(input_peer != nullptr);
int32 flags = telegram_api::messages_acceptUrlAuth::PEER_MASK; flags |= telegram_api::messages_acceptUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_acceptUrlAuth::URL_MASK;
}
if (allow_write_access) { if (allow_write_access) {
flags |= telegram_api::messages_acceptUrlAuth::WRITE_ALLOWED_MASK; flags |= telegram_api::messages_acceptUrlAuth::WRITE_ALLOWED_MASK;
} }
send_query(G()->net_query_creator().create( send_query(G()->net_query_creator().create(telegram_api::messages_acceptUrlAuth(
telegram_api::messages_acceptUrlAuth(flags, false /*ignored*/, std::move(input_peer), flags, false /*ignored*/, std::move(input_peer), message_id.get_server_message_id().get(), button_id, url_)));
message_id.get_server_message_id().get(), button_id, string())));
} }
void on_result(uint64 id, BufferSlice packet) override { void on_result(uint64 id, BufferSlice packet) override {
@ -4713,7 +4725,8 @@ class AcceptUrlAuthQuery : public Td::ResultHandler {
} }
void on_error(uint64 id, Status status) override { void on_error(uint64 id, Status status) override {
if (!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "AcceptUrlAuthQuery")) { if (!dialog_id_.is_valid() ||
!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "AcceptUrlAuthQuery")) {
LOG(INFO) << "AcceptUrlAuthQuery returned " << status; LOG(INFO) << "AcceptUrlAuthQuery returned " << status;
} }
promise_.set_error(std::move(status)); promise_.set_error(std::move(status));
@ -8630,6 +8643,21 @@ void MessagesManager::get_login_url(DialogId dialog_id, MessageId message_id, in
->send(r_url.move_as_ok(), dialog_id, message_id, button_id, allow_write_access); ->send(r_url.move_as_ok(), dialog_id, message_id, button_id, allow_write_access);
} }
void MessagesManager::get_link_login_url_info(const string &url,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise) {
if (G()->close_flag()) {
return promise.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url, false));
}
td_->create_handler<RequestUrlAuthQuery>(std::move(promise))->send(url, DialogId(), MessageId(), 0);
}
void MessagesManager::get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) {
td_->create_handler<AcceptUrlAuthQuery>(std::move(promise))
->send(url, DialogId(), MessageId(), 0, allow_write_access);
}
void MessagesManager::load_secret_thumbnail(FileId thumbnail_file_id) { void MessagesManager::load_secret_thumbnail(FileId thumbnail_file_id) {
class Callback : public FileManager::DownloadCallback { class Callback : public FileManager::DownloadCallback {
public: public:

View File

@ -806,6 +806,11 @@ class MessagesManager : public Actor {
void get_login_url(DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access, void get_login_url(DialogId dialog_id, MessageId message_id, int32 button_id, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise); Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
void get_link_login_url_info(const string &url, Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise);
void get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
void on_authorization_success(); void on_authorization_success();
void before_get_difference(); void before_get_difference();

View File

@ -5484,7 +5484,7 @@ void Td::on_request(uint64 id, td_api::getExternalLink &request) {
CHECK_IS_USER(); CHECK_IS_USER();
CLEAN_INPUT_STRING(request.link_); CLEAN_INPUT_STRING(request.link_);
CREATE_REQUEST_PROMISE(); CREATE_REQUEST_PROMISE();
promise.set_value(td_api::make_object<td_api::httpUrl>(request.link_)); messages_manager_->get_link_login_url(request.link_, request.allow_write_access_, std::move(promise));
} }
void Td::on_request(uint64 id, const td_api::getChatHistory &request) { void Td::on_request(uint64 id, const td_api::getChatHistory &request) {