Skip to content

Commit 016f4ab

Browse files
authored
sciter 2fa setting (rustdesk#6951)
not add qr code text line as it's not selectable, and selectable input will steal the focus. Signed-off-by: 21pages <pages21@163.com>
1 parent b1a946e commit 016f4ab

File tree

6 files changed

+112
-12
lines changed

6 files changed

+112
-12
lines changed

Cargo.lock

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ clipboard = { path = "libs/clipboard" }
9797
ctrlc = "3.2"
9898
arboard = { version = "3.2", features = ["wayland-data-control"] }
9999
system_shutdown = "4.0"
100+
qrcode-generator = "4.1"
100101

101102
[target.'cfg(target_os = "windows")'.dependencies]
102103
winapi = { version = "0.3", features = ["winuser", "wincrypt", "shellscalingapi", "pdh", "synchapi", "memoryapi"] }

src/flutter_ffi.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,7 @@ pub fn session_login(
180180
}
181181
}
182182

183-
pub fn session_send2fa(
184-
session_id: SessionID,
185-
code: String,
186-
) {
183+
pub fn session_send2fa(session_id: SessionID, code: String) {
187184
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
188185
session.send2fa(code);
189186
}
@@ -2026,20 +2023,15 @@ pub fn main_supported_input_source() -> SyncReturn<String> {
20262023
}
20272024

20282025
pub fn main_generate2fa() -> String {
2029-
crate::auth_2fa::generate2fa()
2026+
generate2fa()
20302027
}
20312028

20322029
pub fn main_verify2fa(code: String) -> bool {
2033-
let res = crate::auth_2fa::verify2fa(code);
2034-
if res {
2035-
refresh_options();
2036-
}
2037-
res
2030+
verify2fa(code)
20382031
}
20392032

20402033
pub fn main_has_valid_2fa_sync() -> SyncReturn<bool> {
2041-
let raw = get_option("2fa");
2042-
SyncReturn(crate::auth_2fa::get_2fa(Some(raw)).is_some())
2034+
SyncReturn(has_valid_2fa())
20432035
}
20442036

20452037
#[cfg(target_os = "android")]

src/ui.rs

+26
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,28 @@ impl UI {
603603
fn support_remove_wallpaper(&self) -> bool {
604604
support_remove_wallpaper()
605605
}
606+
607+
fn has_valid_2fa(&self) -> bool {
608+
has_valid_2fa()
609+
}
610+
611+
fn generate2fa(&self) -> String {
612+
generate2fa()
613+
}
614+
615+
pub fn verify2fa(&self, code: String) -> bool {
616+
verify2fa(code)
617+
}
618+
619+
fn generate_2fa_img_src(&self, data: String) -> String {
620+
let v = qrcode_generator::to_png_to_vec(data, qrcode_generator::QrCodeEcc::Low, 64)
621+
.unwrap_or_default();
622+
let s = hbb_common::sodiumoxide::base64::encode(
623+
v,
624+
hbb_common::sodiumoxide::base64::Variant::Original,
625+
);
626+
format!("data:image/png;base64,{s}")
627+
}
606628
}
607629

608630
impl sciter::EventHandler for UI {
@@ -690,6 +712,10 @@ impl sciter::EventHandler for UI {
690712
fn handle_relay_id(String);
691713
fn get_login_device_info();
692714
fn support_remove_wallpaper();
715+
fn has_valid_2fa();
716+
fn generate2fa();
717+
fn generate_2fa_img_src(String);
718+
fn verify2fa(String);
693719
}
694720
}
695721

src/ui/index.tis

+31
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,8 @@ class PasswordArea: Reactor.Component {
912912
{ !show_password ? '' : <div .separator /> }
913913
{ !show_password ? '' : <li #set-password disabled={ method == 'use-temporary-password' ? "true" : "false" }>{translate('Set permanent password')}</li> }
914914
{ !show_password ? '' : <TemporaryPasswordLengthMenu /> }
915+
<div .separator />
916+
<li #tfa><span>{svg_checkmark}</span>{translate('enable-2fa-title')}</li>
915917
</menu></popup>;
916918
}
917919

@@ -927,11 +929,14 @@ class PasswordArea: Reactor.Component {
927929
var pwd_id = handler.get_option('verification-method');
928930
if (pwd_id != 'use-temporary-password' && pwd_id != 'use-permanent-password')
929931
pwd_id = 'use-both-passwords';
932+
var has_valid_2fa = handler.has_valid_2fa();
930933
for (var el in this.$$(menu#edit-password-context>li)) {
931934
if (el.id.indexOf("approve-mode-") == 0)
932935
el.attributes.toggleClass("selected", el.id == mode_id);
933936
if (el.id.indexOf("use-") == 0)
934937
el.attributes.toggleClass("selected", el.id == pwd_id);
938+
if (el.id == "tfa")
939+
el.attributes.toggleClass("selected", has_valid_2fa);
935940
}
936941
}
937942

@@ -984,6 +989,32 @@ class PasswordArea: Reactor.Component {
984989
passwordArea.update();
985990
}
986991
}
992+
993+
event click $(li#tfa) {
994+
var me = this;
995+
var has_valid_2fa = handler.has_valid_2fa();
996+
if (has_valid_2fa) {
997+
handler.set_option('2fa', '');
998+
me.update();
999+
} else {
1000+
var new2fa = handler.generate2fa();
1001+
var src = handler.generate_2fa_img_src(new2fa);
1002+
msgbox("custom-2fa-setting", translate('enable-2fa-title'),
1003+
<div .form>
1004+
<div>{translate('enable-2fa-desc')}</div>
1005+
<img src={src} />
1006+
<div .code><input name='code' type='text' .outline-focus /></div>
1007+
</div>
1008+
, "", function(res=null) {
1009+
if (!res) return;
1010+
if (!res.code) return;
1011+
if (!handler.verify2fa(res.code)) {
1012+
return translate('wrong-2fa-code');
1013+
}
1014+
me.update();
1015+
}, 320);
1016+
}
1017+
}
9871018
}
9881019

9891020
var password_cache = ["","","",""];

src/ui_interface.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1301,3 +1301,20 @@ pub fn support_remove_wallpaper() -> bool {
13011301
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
13021302
return false;
13031303
}
1304+
1305+
pub fn has_valid_2fa() -> bool {
1306+
let raw = get_option("2fa");
1307+
crate::auth_2fa::get_2fa(Some(raw)).is_some()
1308+
}
1309+
1310+
pub fn generate2fa() -> String {
1311+
crate::auth_2fa::generate2fa()
1312+
}
1313+
1314+
pub fn verify2fa(code: String) -> bool {
1315+
let res = crate::auth_2fa::verify2fa(code);
1316+
if res {
1317+
refresh_options();
1318+
}
1319+
res
1320+
}

0 commit comments

Comments
 (0)