Respect token auth toggle

This commit is contained in:
lin onetwo 2026-03-07 00:09:02 +08:00
parent 2dc2faa627
commit b3d1425d9c
3 changed files with 52 additions and 2 deletions

View file

@ -83,7 +83,7 @@ function getInfoTiddlerFields(updateInfoTiddlersCallback: (infos: Array<{ text:
}
// Add workspace token for QR code (if available)
if (authToken) {
if (tokenAuth && authToken) {
asyncInfoTiddlerFields.push({ title: '$:/info/tidgi/workspaceToken', text: authToken });
}

View file

@ -0,0 +1,50 @@
import { SupportedStorageServices } from '@services/types';
import { describe, expect, it, vi } from 'vitest';
import { Workspace } from '../index';
import { type IWikiWorkspace, wikiWorkspaceDefaultValues } from '../interface';
vi.mock('../registerMenu', () => ({
registerMenu: vi.fn(),
}));
function createWorkspace(overrides: Partial<IWikiWorkspace>): IWikiWorkspace {
return {
...wikiWorkspaceDefaultValues,
id: 'workspace-1',
name: 'Workspace 1',
wikiFolderLocation: '/tmp/workspace-1',
isSubWiki: false,
mainWikiID: null,
mainWikiToLink: null,
pageType: null,
picturePath: null,
homeUrl: 'tidgi://workspace-1',
gitUrl: null,
storageService: SupportedStorageServices.local,
tagNames: [],
userName: 'tester',
...overrides,
};
}
function createWorkspaceService(workspace: IWikiWorkspace): Workspace {
const service = new Workspace() as Workspace & { workspaces?: Record<string, IWikiWorkspace> };
service.workspaces = { [workspace.id]: workspace };
return service;
}
describe('Workspace token auth', () => {
it('should not expose workspace token when token auth is disabled', async () => {
const service = createWorkspaceService(createWorkspace({ authToken: 'secret-token', tokenAuth: false }));
await expect(service.getWorkspaceToken('workspace-1')).resolves.toBeUndefined();
await expect(service.validateWorkspaceToken('workspace-1', 'secret-token')).resolves.toBe(false);
});
it('should expose workspace token when token auth is enabled', async () => {
const service = createWorkspaceService(createWorkspace({ authToken: 'secret-token', tokenAuth: true }));
await expect(service.getWorkspaceToken('workspace-1')).resolves.toBe('secret-token');
await expect(service.validateWorkspaceToken('workspace-1', 'secret-token')).resolves.toBe(true);
});
});

View file

@ -762,7 +762,7 @@ export class Workspace implements IWorkspaceService {
*/
public async getWorkspaceToken(workspaceId: string): Promise<string | undefined> {
const workspace = this.getSync(workspaceId);
if (!workspace || !isWikiWorkspace(workspace)) {
if (!workspace || !isWikiWorkspace(workspace) || !workspace.tokenAuth) {
return undefined;
}
return workspace.authToken;