From c9fb75d5fa7025bf9f5e09d31d14ab3425c76c21 Mon Sep 17 00:00:00 2001 From: AlexeyBarabash Date: Fri, 18 Feb 2022 14:40:15 +0200 Subject: [PATCH] Desktop uses sync words v2 --- .../ui/webui/settings/brave_sync_handler.cc | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/browser/ui/webui/settings/brave_sync_handler.cc b/browser/ui/webui/settings/brave_sync_handler.cc index ec86261bd85a..8f75c38bf90f 100644 --- a/browser/ui/webui/settings/brave_sync_handler.cc +++ b/browser/ui/webui/settings/brave_sync_handler.cc @@ -15,6 +15,7 @@ #include "brave/components/brave_sync/crypto/crypto.h" #include "brave/components/brave_sync/qr_code_data.h" #include "brave/components/brave_sync/sync_service_impl_helper.h" +#include "brave/components/brave_sync/time_limited_words.h" #include "brave/components/sync/driver/brave_sync_service_impl.h" #include "brave/components/sync_device_info/brave_device_info.h" #include "chrome/browser/profiles/profile.h" @@ -91,7 +92,11 @@ void BraveSyncHandler::HandleGetSyncCode(base::Value::ConstListView args) { if (sync_service) sync_code = sync_service->GetOrCreateSyncCode(); - ResolveJavascriptCallback(args[0].Clone(), base::Value(sync_code)); + std::string time_limited_sync_code = + brave_sync::TimeLimitedWords::GenerateForNow(sync_code); + + ResolveJavascriptCallback(args[0].Clone(), + base::Value(time_limited_sync_code)); } void BraveSyncHandler::HandleGetQRCode(base::Value::ConstListView args) { @@ -145,16 +150,28 @@ void BraveSyncHandler::HandleSetSyncCode(base::Value::ConstListView args) { AllowJavascript(); CHECK_EQ(2U, args.size()); CHECK(args[1].is_string()); - const std::string sync_code = args[1].GetString(); - - if (sync_code.empty()) { + const std::string time_limited_sync_code = args[1].GetString(); + if (time_limited_sync_code.empty()) { LOG(ERROR) << "No sync code parameter provided!"; RejectJavascriptCallback(args[0].Clone(), base::Value(false)); return; } + std::string pure_sync_code; + brave_sync::WordsValidationResult validation_result = + brave_sync::TimeLimitedWords::Validate(time_limited_sync_code, + &pure_sync_code); + if (validation_result != brave_sync::WordsValidationResult::kValid) { + LOG(ERROR) << "Could not validate a sync code, validation_result=" + << static_cast(validation_result); + RejectJavascriptCallback(args[0].Clone(), base::Value(false)); + return; + } + + CHECK(!pure_sync_code.empty()); + auto* sync_service = GetSyncService(); - if (!sync_service || !sync_service->SetSyncCode(sync_code)) { + if (!sync_service || !sync_service->SetSyncCode(pure_sync_code)) { RejectJavascriptCallback(args[0].Clone(), base::Value(false)); return; }