支持单位授权与客户端限制
This commit is contained in:
@@ -192,6 +192,7 @@ export interface ClientRegisterParams {
|
||||
password: string;
|
||||
savedKey?: string;
|
||||
deviceId?: string;
|
||||
name?: string;
|
||||
sandboxConfigValue: SandboxValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ vi.stubGlobal("window", {
|
||||
electronAPI: {
|
||||
app: {
|
||||
getDeviceId: vi.fn(async () => "mock-device-id"),
|
||||
getPrimaryLocalIp: vi.fn(async () => "192.168.1.23"),
|
||||
},
|
||||
settings: {
|
||||
get: mockSettingsGet,
|
||||
@@ -166,6 +167,7 @@ describe("auth - savedKey 认证 (快捷登录)", () => {
|
||||
expect(params.username).toBe("");
|
||||
expect(params.password).toBe("");
|
||||
expect(params.savedKey).toBe(SAVED_KEY);
|
||||
expect(params.name).toBe("ip-192.168.1.23");
|
||||
});
|
||||
|
||||
it("username/password 为 null + 有 savedKey → 应正常同步", async () => {
|
||||
@@ -313,6 +315,9 @@ describe("auth - savedKey 认证 (快捷登录)", () => {
|
||||
expect(store["auth.password"]).toBeUndefined();
|
||||
// savedKey 应保存
|
||||
expect(store["auth.saved_key"]).toBe(CONFIG_KEY_FROM_SERVER);
|
||||
|
||||
const [params] = mockRegisterClient.mock.calls[0];
|
||||
expect(params.name).toBe("ip-192.168.1.23");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -203,6 +203,17 @@ async function getLocalSandboxValue(): Promise<SandboxValue> {
|
||||
};
|
||||
}
|
||||
|
||||
async function getDefaultSandboxName(): Promise<string | undefined> {
|
||||
try {
|
||||
const ip = await window.electronAPI?.app.getPrimaryLocalIp?.();
|
||||
const normalized = typeof ip === "string" ? ip.trim() : "";
|
||||
return normalized ? `ip-${normalized}` : undefined;
|
||||
} catch (error) {
|
||||
logger.warn("Failed to resolve default sandbox name", "Auth", error);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 错误处理 ===
|
||||
/**
|
||||
* 获取友好的错误信息
|
||||
@@ -288,11 +299,13 @@ export async function loginAndRegister(
|
||||
|
||||
// 构建注册参数
|
||||
const deviceId = await window.electronAPI?.app.getDeviceId();
|
||||
const defaultSandboxName = await getDefaultSandboxName();
|
||||
const params: ClientRegisterParams = {
|
||||
username,
|
||||
password,
|
||||
savedKey: savedKey || undefined,
|
||||
deviceId: deviceId || undefined,
|
||||
name: defaultSandboxName,
|
||||
sandboxConfigValue: await getLocalSandboxValue(),
|
||||
};
|
||||
|
||||
@@ -471,11 +484,13 @@ export async function reRegisterClient(): Promise<ClientRegisterResponse | null>
|
||||
logger.info("Re-registering client (using savedKey)...", "Auth");
|
||||
|
||||
const deviceId = await window.electronAPI?.app.getDeviceId();
|
||||
const defaultSandboxName = await getDefaultSandboxName();
|
||||
const params: ClientRegisterParams = {
|
||||
username: username || "",
|
||||
password: "", // 密码不持久化,使用 savedKey 认证
|
||||
savedKey,
|
||||
deviceId: deviceId || undefined,
|
||||
name: defaultSandboxName,
|
||||
sandboxConfigValue: await getLocalSandboxValue(),
|
||||
};
|
||||
|
||||
@@ -575,11 +590,13 @@ export async function syncConfigToServer(options?: {
|
||||
}
|
||||
|
||||
const deviceId = await window.electronAPI?.app.getDeviceId();
|
||||
const defaultSandboxName = await getDefaultSandboxName();
|
||||
const params: ClientRegisterParams = {
|
||||
username: username || "",
|
||||
password: "", // 密码不持久化,使用 savedKey 认证
|
||||
savedKey,
|
||||
deviceId: deviceId || undefined,
|
||||
name: defaultSandboxName,
|
||||
sandboxConfigValue: await getLocalSandboxValue(),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user