This commit is contained in:
gr
2026-11-04 10:14:23 +08:00
parent f7f323462c
commit b7c3a962dd
759 changed files with 74649 additions and 4421 deletions
@@ -29,6 +29,19 @@ const origin = 'http://127.0.0.1:5666';
const now = 1_000_000;
const code = 'C'.repeat(43);
test('不同访问地址先进入固定客服回调域,再在该域创建PKCE事务', async () => {
assert.equal(sso.groupLoginEntry(config, origin), '/auth/group-login');
assert.equal(sso.groupLoginEntry(config, 'http://127.0.0.1:8766'), config.redirect_uri);
assert.equal(sso.groupLoginEntry(config, 'http://localhost:5666'), config.redirect_uri);
const pending = storage();
await assert.rejects(sso.beginGroupLogin(config, 'http://127.0.0.1:8766', pending, webcrypto, now));
assert.equal(pending.values.size, 0);
assert.throws(() => sso.groupLoginEntry({...config, enabled: false}, origin));
assert.throws(() => sso.groupLoginEntry({...config, redirect_uri: 'https://evil.example/other'}, origin));
assert.throws(() => sso.groupLoginEntry({...config, authorize_url: 'https://evil.example/sso/authorize'}, origin));
assert.throws(() => sso.groupLoginEntry(config, `${origin}/auth/login`));
});
function storage() {
const values = new Map();
return { getItem: key => values.get(key) ?? null, setItem: (key, value) => values.set(key, value),
@@ -152,6 +165,90 @@ test('password login shares authoritative initialization and preserves forced pa
assert.equal(h.events.at(-1), 'route:/profile/password');
});
test('local and ZYT credentials share password API and use only current /me permissions', async () => {
for (const account of [
{ username: 'local_operator', permissions: ['stats:read', 'conversation:read'], home: '/model/stats' },
{ username: 'zyt_new', permissions: [], home: '/group/welcome' },
{ username: 'zyt_authorized', permissions: ['stats:read', 'user:read'], home: '/model/stats' },
]) {
const h = authHarness();
const requests = [];
const requestClient = {
async post(path, body) {
requests.push({ method: 'POST', path, body });
// Even stale/overbroad permissions in the login response are ignored.
return { access_token: `token-${account.username}`, user: { permissions: ['user:write', 'role:write'] } };
},
async get(path) {
requests.push({ method: 'GET', path });
return { id: 12, username: account.username, permissions: account.permissions,
must_change_password: false, role: account.permissions.length ? 'operator' : 'zyt_basic' };
},
};
h.api.loginApi = loadTs('../src/api/core/auth.ts', {
'#/api/request': { requestClient, baseRequestClient: { get() { assert.fail('Password login requested group config'); } } },
}).loginApi;
h.api.getUserInfoApi = loadTs('../src/api/core/user.ts', { '#/api/request': { requestClient } }).getUserInfoApi;
h.api.groupExchangeApi = async () => { assert.fail('Password login used group exchange'); };
await h.store.authLogin({ username: account.username, password: 'Fixture@2026' });
assert.equal(h.access.accessToken, `token-${account.username}`);
assert.deepEqual(h.access.accessCodes, account.permissions);
assert.deepEqual(h.user.userInfo.roles, account.permissions);
assert.equal(h.user.userInfo.username, account.username);
assert.equal(h.access.accessMenus.length, 0);
assert.equal(h.access.accessRoutes.length, 0);
assert.equal(h.access.isAccessChecked, false);
assert.equal(h.persisted.tabs.length, 0);
assert.equal(h.persisted.cachedUser, null);
assert.equal(h.persisted.lock, false);
assert.equal(h.store.loginLoading.value, false);
assert.deepEqual(h.events, ['reset-routes', 'reset-stores', `token:token-${account.username}`, 'route:' + account.home]);
assert.deepEqual(JSON.parse(JSON.stringify(requests)), [
{ method: 'POST', path: '/auth/login', body: { username: account.username, password: 'Fixture@2026', device_name: 'web' } },
{ method: 'GET', path: '/me' },
]);
}
});
test('ordinary password errors release loading and permit retry through the same authLogin', async () => {
const h = authHarness();
let attempts = 0;
h.api.loginApi = async () => {
if (++attempts === 1) throw new Error('账号或密码错误');
return { accessToken: 'retry-basic' };
};
const credentials = { username: 'zyt_new', password: 'Fixture@2026' };
await assert.rejects(h.store.authLogin(credentials), /账号或密码错误/);
assert.equal(h.store.loginLoading.value, false);
assert.deepEqual(h.events, []);
h.setUser({ userId: '8', username: 'zyt_new', roles: [], homePath: '/group/welcome' });
await h.store.authLogin(credentials);
assert.equal(attempts, 2);
assert.equal(h.store.loginLoading.value, false);
assert.equal(h.access.accessToken, 'retry-basic');
assert.deepEqual(h.access.accessCodes, []);
assert.equal(h.events.at(-1), 'route:/group/welcome');
});
test('ordinary password /me failure clears new identity and allows a clean retry', async () => {
const h = authHarness();
h.api.getUserInfoApi = async () => { throw new Error('revoked'); };
const credentials = { username: 'zyt_new', password: 'Fixture@2026' };
await assert.rejects(h.store.authLogin(credentials), /revoked/);
assert.equal(h.store.loginLoading.value, false);
assert.equal(h.access.accessToken, null);
assert.equal(h.access.accessCodes.length, 0);
assert.equal(h.access.accessMenus.length, 0);
assert.equal(h.access.accessRoutes.length, 0);
assert.equal(h.user.userInfo, null);
assert.equal(h.persisted.cachedUser, null);
h.api.getUserInfoApi = async () => ({ userId: '8', username: 'zyt_new', roles: [], homePath: '/group/welcome' });
await h.store.authLogin(credentials);
assert.equal(h.access.accessToken, 'local-user');
assert.equal(h.access.accessCodes.length, 0);
assert.equal(h.events.at(-1), 'route:/group/welcome');
});
test('failure to load current local identity clears the new token and authorization', async () => {
const h = authHarness();
h.api.getUserInfoApi = async () => { throw new Error('revoked'); };