11import "jquery" ;
2- import utils from "./services/utils.js" ;
2+
33import ko from "knockout" ;
44
5+ import utils from "./services/utils.js" ;
6+
57// TriliumNextTODO: properly make use of below types
68// type SetupModelSetupType = "new-document" | "sync-from-desktop" | "sync-from-server" | "";
79// type SetupModelStep = "sync-in-progress" | "setup-type" | "new-document-in-progress" | "sync-from-desktop";
@@ -16,6 +18,8 @@ class SetupModel {
1618 syncServerHost : ko . Observable < string | undefined > ;
1719 syncProxy : ko . Observable < string | undefined > ;
1820 password : ko . Observable < string | undefined > ;
21+ totpToken : ko . Observable < string | undefined > ;
22+ totpEnabled : ko . Observable < boolean > ;
1923
2024 constructor ( syncInProgress : boolean ) {
2125 this . syncInProgress = syncInProgress ;
@@ -27,6 +31,8 @@ class SetupModel {
2731 this . syncServerHost = ko . observable ( ) ;
2832 this . syncProxy = ko . observable ( ) ;
2933 this . password = ko . observable ( ) ;
34+ this . totpToken = ko . observable ( ) ;
35+ this . totpEnabled = ko . observable ( false ) ;
3036
3137 if ( this . syncInProgress ) {
3238 setInterval ( checkOutstandingSyncs , 1000 ) ;
@@ -40,7 +46,7 @@ class SetupModel {
4046 return ! ! this . setupType ( ) ;
4147 }
4248
43- selectSetupType ( ) {
49+ async selectSetupType ( ) {
4450 if ( this . setupType ( ) === "new-document" ) {
4551 this . step ( "new-document-in-progress" ) ;
4652
@@ -52,6 +58,24 @@ class SetupModel {
5258 }
5359 }
5460
61+ async checkTotpStatus ( ) {
62+ const syncServerHost = this . syncServerHost ( ) ;
63+ if ( ! syncServerHost ) {
64+ this . totpEnabled ( false ) ;
65+ return ;
66+ }
67+
68+ try {
69+ const resp = await $ . post ( "api/setup/check-server-totp" , {
70+ syncServerHost
71+ } ) ;
72+ this . totpEnabled ( ! ! resp . totpEnabled ) ;
73+ } catch {
74+ // If we can't reach the server, don't show TOTP field yet
75+ this . totpEnabled ( false ) ;
76+ }
77+ }
78+
5579 back ( ) {
5680 this . step ( "setup-type" ) ;
5781 this . setupType ( "" ) ;
@@ -72,11 +96,22 @@ class SetupModel {
7296 return ;
7397 }
7498
99+ // Check TOTP status before submitting (in case it wasn't checked yet)
100+ await this . checkTotpStatus ( ) ;
101+
102+ const totpToken = this . totpToken ( ) ;
103+
104+ if ( this . totpEnabled ( ) && ! totpToken ) {
105+ showAlert ( "TOTP token can't be empty when two-factor authentication is enabled" ) ;
106+ return ;
107+ }
108+
75109 // not using server.js because it loads too many dependencies
76110 const resp = await $ . post ( "api/setup/sync-from-server" , {
77- syncServerHost : syncServerHost ,
78- syncProxy : syncProxy ,
79- password : password
111+ syncServerHost,
112+ syncProxy,
113+ password,
114+ totpToken
80115 } ) ;
81116
82117 if ( resp . result === "success" ) {
0 commit comments