Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/18923

Vanessa32ed2b879493
diff --git a/app/src/config/tabs/accountAuth.test.ts b/app/src/config/tabs/accountAuth.test.ts
new file mode 100644
index 0000000000..8cdf7fb160
--- /dev/null
+++ b/app/src/config/tabs/accountAuth.test.ts
@@ -0,0 +1,55 @@
+import * as assert from "node:assert/strict";
+import test from "node:test";
+import {bindAccountAuthEnter, isAccountLoginDisabled} from "./accountAuth";
+
+test("login requires agreement and a non-empty password", () => {
+ assert.equal(isAccountLoginDisabled(false, "password"), true);
+ assert.equal(isAccountLoginDisabled(true, ""), true);
+ assert.equal(isAccountLoginDisabled(true, "password"), false);
+});
+
+test("Enter submits account authentication", () => {
+ let listener: (event: KeyboardEvent) => void;
+ const input = {
+ addEventListener: (_type: string, callback: (event: KeyboardEvent) => void) => {
+ listener = callback;
+ },
+ } as unknown as HTMLInputElement;
+ let submitted = 0;
+ bindAccountAuthEnter(input, () => {
+ submitted++;
+ });
+
+ let defaultPrevented = false;
+ listener!({
+ isComposing: false,
+ repeat: false,
+ key: "Enter",
+ preventDefault: () => {
+ defaultPrevented = true;
+ },
+ } as KeyboardEvent);
+
+ assert.equal(submitted, 1);
+ assert.equal(defaultPrevented, true);
+});
+
+test("account authentication ignores other and composing key events", () => {
+ let listener: (event: KeyboardEvent) => void;
+ const input = {
+ addEventListener: (_type: string, callback: (event: KeyboardEvent) => void) => {
+ listener = callback;
+ },
+ } as unknown as HTMLInputElement;
+ let submitted = 0;
+ bindAccountAuthEnter(input, () => {
+ submitted++;
+ });
+
+ const preventDefault = () => assert.fail("ignored events must not be prevented");
+ listener!({isComposing: false, repeat: false, key: "Escape", preventDefault} as unknown as KeyboardEvent);
+ listener!({isComposing: true, repeat: false, key: "Enter", preventDefault} as unknown as KeyboardEvent);
+ listener!({isComposing: false, repeat: true, key: "Enter", preventDefault} as unknown as KeyboardEvent);
+
+ assert.equal(submitted, 0);
+});
diff --git a/app/src/config/tabs/accountAuth.ts b/app/src/config/tabs/accountAuth.ts
new file mode 100644
index 0000000000..0dbd709e9a
--- /dev/null
+++ b/app/src/config/tabs/accountAuth.ts
@@ -0,0 +1,13 @@
+export const isAccountLoginDisabled = (agreed: boolean, password: string) => {
+ return !agreed || password.length === 0;
+};
+
+export const bindAccountAuthEnter = (input: HTMLInputElement, submit: () => void) => {
+ input.addEventListener("keydown", (event) => {
+ if (event.isComposing || event.repeat || event.key !== "Enter") {
+ return;
+ }
+ submit();
+ event.preventDefault();
+ });
+};
diff --git a/app/src/config/tabs/accountUi.ts b/app/src/config/tabs/accountUi.ts
index 450c711301..13a145c1d3 100644
--- a/app/src/config/tabs/accountUi.ts
+++ b/app/src/config/tabs/accountUi.ts
@@ -16,6 +16,7 @@ import md5 from "blueimp-md5";
import type {SettingTabBuilder} from "../setting/builder";
import {patchSyncConfig, refreshSyncCloudSpaceGroup} from "./syncRuntime";
import {escapeAttr, escapeHtml} from "../../util/escape";
+import {bindAccountAuthEnter, isAccountLoginDisabled} from "./accountAuth";
/** 账号节:由 syncTab 注册 */
export const registerAccountGroup = (tab: SettingTabBuilder) => {
@@ -390,7 +391,7 @@ const bindAccountAuthForm = (
if (mode === "login") {
const agreeLoginCheckbox = authFormRoot.querySelector("#agreeLogin") as HTMLInputElement;
const updateLoginButton = () => {
- loginBtn.disabled = !agreeLoginCheckbox.checked || userPasswordInput.value.length === 0;
+ loginBtn.disabled = isAccountLoginDisabled(agreeLoginCheckbox.checked, userPasswordInput.value);
};
agreeLoginCheckbox.addEventListener("change", updateLoginButton);
userPasswordInput.addEventListener("input", updateLoginButton);
@@ -407,19 +408,10 @@ const bindAccountAuthForm = (
refreshCaptchaImg();
});
- const bindEnter = (input: HTMLInputElement, button: HTMLButtonElement) => {
- input.addEventListener("keydown", (event) => {
- if (event.isComposing || event.repeat || event.key !== "Enter") {
- return;
- }
- button.click();
- event.preventDefault();
- });
- };
- bindEnter(userNameInput, loginBtn);
- bindEnter(userPasswordInput, loginBtn);
- bindEnter(captchaInput, loginBtn);
- bindEnter(twofactorAuthCodeInput, login2Btn);
+ bindAccountAuthEnter(userNameInput, () => loginBtn.click());
+ bindAccountAuthEnter(userPasswordInput, () => loginBtn.click());
+ bindAccountAuthEnter(captchaInput, () => loginBtn.click());
+ bindAccountAuthEnter(twofactorAuthCodeInput, () => login2Btn.click());
const completeLogin = (response: IWebSocketData) => {
if (mode === "login") {
diff --git a/kernel/model/cloud_service.go b/kernel/model/cloud_service.go
index de6d7ea22f..1fe4d26de5 100644
--- a/kernel/model/cloud_service.go
+++ b/kernel/model/cloud_service.go
@@ -697,28 +697,53 @@ func Login(userName, password, captcha string, cloudRegion int) (ret *gulu.Resul
func Login2fa(token, code string) (ret *gulu.Result) {
result := map[string]any{}
request := httpclient.NewCloudRequest30s()
- _, err := request.
+ resp, err := request.
SetSuccessResult(&result).
SetBody(map[string]string{"twofactorAuthCode": code}).
SetHeader("token", token).
Post(util.GetCloudServer() + "/apis/siyuan/login/2fa")
if err != nil {
logging.LogErrorf("login 2fa failed: %s", err)
- ret = gulu.Ret.NewResult()
- ret.Code = -1
- ret.Msg = Conf.Language(18)
- return
+ return failedLogin2faResult()
+ }
+ if http.StatusOK != resp.StatusCode {
+ logging.LogErrorf("login 2fa failed: %d", resp.StatusCode)
+ return failedLogin2faResult()
}
- ret = &gulu.Result{
- Code: int(result["code"].(float64)),
- Msg: result["msg"].(string),
- Data: result,
+ ret, ok := parseLogin2faResult(result)
+ if !ok {
+ logging.LogErrorf("login 2fa failed: invalid cloud response")
+ return failedLogin2faResult()
}
+ return
+}
+
+func failedLogin2faResult() *gulu.Result {
+ ret := gulu.Ret.NewResult()
+ ret.Code = -1
+ ret.Msg = Conf.Language(18)
+ return ret
+}
+
+func parseLogin2faResult(result map[string]any) (ret *gulu.Result, ok bool) {
+ code, codeOK := result["code"].(float64)
+ msg, msgOK := result["msg"].(string)
+ if !codeOK || !msgOK {
+ return nil, false
+ }
+ if 0 == code {
+ token, tokenOK := result["token"].(string)
+ if !tokenOK || "" == token {
+ return nil, false
+ }
+ }
+
+ ret = &gulu.Result{Code: int(code), Msg: msg, Data: result}
if -1 == ret.Code {
ret.Code = 1
}
- return
+ return ret, true
}
func LogoutUser() {
diff --git a/kernel/model/cloud_service_login_test.go b/kernel/model/cloud_service_login_test.go
new file mode 100644
index 0000000000..b9ce040f5b
--- /dev/null
+++ b/kernel/model/cloud_service_login_test.go
@@ -0,0 +1,75 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestParseLogin2faResult(t *testing.T) {
+ tests := []struct {
+ name string
+ result map[string]any
+ ok bool
+ expectedCode int
+ }{
+ {
+ name: "success",
+ result: map[string]any{"code": float64(0), "msg": "", "token": "token"},
+ ok: true,
+ expectedCode: 0,
+ },
+ {
+ name: "invalid verification code",
+ result: map[string]any{"code": float64(-1), "msg": "invalid code"},
+ ok: true,
+ expectedCode: 1,
+ },
+ {
+ name: "missing code",
+ result: map[string]any{"msg": "invalid response"},
+ ok: false,
+ },
+ {
+ name: "success without token",
+ result: map[string]any{"code": float64(0), "msg": ""},
+ ok: false,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ result, ok := parseLogin2faResult(test.result)
+ if test.ok != ok {
+ t.Fatalf("unexpected parse status [expected=%t, actual=%t]", test.ok, ok)
+ }
+ if !test.ok {
+ if nil != result {
+ t.Fatalf("unexpected result [%+v]", result)
+ }
+ return
+ }
+ if test.expectedCode != result.Code {
+ t.Fatalf("unexpected code [expected=%d, actual=%d]", test.expectedCode, result.Code)
+ }
+ if !reflect.DeepEqual(result.Data, test.result) {
+ t.Fatalf("response data was not preserved")
+ }
+ })
+ }
+}
diff --git a/kernel/model/sync_config_test.go b/kernel/model/sync_config_test.go
new file mode 100644
index 0000000000..9d787c1528
--- /dev/null
+++ b/kernel/model/sync_config_test.go
@@ -0,0 +1,40 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+)
+
+func TestCheckSyncPreservesEnabledWithoutUser(t *testing.T) {
+ originalConf := Conf
+ Conf = NewAppConf()
+ Conf.Sync = conf.NewSync()
+ Conf.Sync.Enabled = true
+ t.Cleanup(func() {
+ Conf = originalConf
+ })
+
+ if checkSync(false, false, true) {
+ t.Fatal("sync must not start without a user")
+ }
+ if !Conf.Sync.Enabled {
+ t.Fatal("sync configuration must remain enabled without a user")
+ }
+}