bailinghub 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +11 -7
- package/README.md +10 -7
- package/docker-compose.images.yml +2 -2
- package/docs/AGENT_AUTH_API.en.md +112 -1
- package/docs/AGENT_AUTH_API.md +99 -1
- package/docs/AGENT_CLIENT_QUICKSTART.en.md +32 -5
- package/docs/AGENT_CLIENT_QUICKSTART.md +27 -4
- package/docs/AGENT_CLIENT_RUNTIME_API.md +4 -1
- package/docs/AGENT_CONVERSATION_AUDIT.en.md +109 -4
- package/docs/AGENT_CONVERSATION_AUDIT.md +84 -3
- package/docs/AGENT_SUBJECT_DISPLAY.en.md +95 -0
- package/docs/AGENT_SUBJECT_DISPLAY.md +95 -0
- package/docs/AGENT_SYSTEM_INFO.en.md +99 -0
- package/docs/AGENT_SYSTEM_INFO.md +97 -0
- package/docs/CHANGELOG.en.md +25 -2
- package/docs/CHANGELOG.md +24 -1
- package/docs/INDEPENDENT_VALIDATION.en.md +4 -4
- package/docs/INDEPENDENT_VALIDATION.md +4 -4
- package/docs/LOCAL_AGENT_SETUP.en.md +116 -0
- package/docs/LOCAL_AGENT_SETUP.md +116 -0
- package/docs/README.en.md +8 -0
- package/docs/README.md +8 -0
- package/docs/RELEASE_NOTES_NEXT.en.md +3 -0
- package/docs/RELEASE_NOTES_NEXT.md +3 -0
- package/docs/RELEASE_NOTES_v0.6.0.en.md +4 -2
- package/docs/RELEASE_NOTES_v0.6.0.md +2 -0
- package/docs/RELEASE_NOTES_v0.6.1.en.md +53 -0
- package/docs/RELEASE_NOTES_v0.6.1.md +53 -0
- package/docs/RELEASE_NOTES_v0.7.0.en.md +116 -0
- package/docs/RELEASE_NOTES_v0.7.0.md +116 -0
- package/docs/SDK.en.md +31 -0
- package/docs/user-guide/conversations.en.md +4 -2
- package/docs/user-guide/conversations.md +4 -2
- package/package.json +1 -1
- package/schemas/config/route.schema.json +15 -2
- package/scripts/check-doc-links.mjs +2 -0
- package/scripts/check-image-tags.sh +1 -1
- package/scripts/check-release-consistency.test.mjs +2 -2
- package/scripts/install.sh +1 -1
- package/scripts/publish-images.sh +1 -1
- package/scripts/release-audit.mjs +2 -0
- package/scripts/sdk-agent-auth-lifecycle-check.mjs +178 -0
- package/scripts/sdk-runtime-test.mjs +5 -2
- package/scripts/verify-github-repo.mjs +2 -0
- package/sdk/php/README.md +29 -0
- package/sdk/php/src/AgentAuth.php +100 -4
- package/sdk/php/src/HubClient.php +2 -1
- package/sdk/php7/README.md +31 -0
- package/sdk/php7/src/AgentAuth.php +99 -4
- package/sdk/php7/src/HubClient.php +2 -1
- package/sql/058_agent_conversation_member_bindings.sql +9 -0
- package/sql/059_agent_subject_display.sql +5 -0
- package/sql/README.en.md +15 -0
- package/sql/README.md +25 -0
- package/src/app/agent-conversation-audit.ts +20 -0
- package/src/app/agent-system-info.ts +41 -0
- package/src/app/agent-tool-invocations.ts +16 -4
- package/src/core/config/agent-setup.ts +37 -0
- package/src/core/config/client-agent-setup.ts +25 -0
- package/src/core/config/config-schema.test.ts +11 -0
- package/src/core/config/route-config.test.ts +28 -0
- package/src/core/config/route-config.ts +43 -4
- package/src/core/contracts/agent-subject-display.test.ts +19 -0
- package/src/core/contracts/agent-subject-display.ts +36 -0
- package/src/core/contracts/types.ts +15 -0
- package/src/core/runtime/agent-conversation-audit.test.ts +19 -1
- package/src/core/runtime/agent-conversation-audit.ts +39 -0
- package/src/infrastructure/config/config-agent-auth-business-sessions.test.ts +120 -0
- package/src/infrastructure/config/config-agent-auth-lifecycle-race.test.ts +152 -0
- package/src/infrastructure/config/config-agent-auth-repository.test.ts +2 -0
- package/src/infrastructure/config/config-agent-auth-repository.ts +208 -18
- package/src/infrastructure/config/config-agent-conversation-audit-cross-binding.test.ts +120 -0
- package/src/infrastructure/config/config-agent-conversation-audit-repository.ts +117 -23
- package/src/infrastructure/config/config-agent-subject-display.test.ts +103 -0
- package/src/infrastructure/config/config-client-repository.ts +21 -0
- package/src/infrastructure/config/config-route-agent-setup.test.ts +59 -0
- package/src/infrastructure/config/config-route-repository.ts +24 -0
- package/src/infrastructure/config/configstore.ts +7 -3
- package/src/infrastructure/schema/core-schema-migrator.test.ts +90 -0
- package/src/routes/admin-access.ts +37 -0
- package/src/routes/admin-agent-clients.ts +11 -0
- package/src/routes/admin-agent-setup.test.ts +130 -0
- package/src/routes/admin-agent-setup.ts +68 -0
- package/src/routes/admin-client-agent-setup.test.ts +93 -0
- package/src/routes/admin-dispatch-config.ts +2 -0
- package/src/routes/agent-api.ts +7 -0
- package/src/routes/agent-auth-lifecycle-http.test.ts +177 -0
- package/src/routes/agent-auth-lifecycle.test.ts +193 -0
- package/src/routes/agent-auth.test.ts +1 -1
- package/src/routes/agent-auth.ts +206 -16
- package/src/routes/agent-conversation-audit-cross-binding-http.test.ts +122 -0
- package/src/routes/agent-conversation-audit.ts +8 -2
- package/src/routes/agent-subject-display-http.test.ts +145 -0
- package/src/routes/agent-system-info.test.ts +132 -0
- package/src/test-support/agent-auth-business-query-fixture.ts +43 -0
- package/src/test-support/agent-auth-lifecycle-fixture.ts +288 -0
- package/src/test-support/agent-conversation-audit-fixture.ts +30 -6
- package/src/test-support/agent-conversation-audit-http-fixture.ts +84 -0
- package/web/connect/bailing-connect-php.tgz +0 -0
- package/web/connect/bailing-connect-php7.tgz +0 -0
- package/web/console/assets/{Accounts-Ca_VnSu9.js → Accounts-ChL9k-XA.js} +1 -1
- package/web/console/assets/AgentClients-Bs7UgVDj.js +1 -0
- package/web/console/assets/AgentClients-DkgPTEbq.css +1 -0
- package/web/console/assets/{Approvals-CMvPWacY.js → Approvals-Bs6FGbp2.js} +1 -1
- package/web/console/assets/{Audit-CIRqOCnL.js → Audit-4WWYhkFM.js} +1 -1
- package/web/console/assets/{BrandLockup-BrytmTTH.js → BrandLockup-D2GhCipL.js} +1 -1
- package/web/console/assets/{Channels-CApxH2TB.js → Channels-CmoGpiCJ.js} +1 -1
- package/web/console/assets/{ChatEntries-CJon3bWJ.js → ChatEntries-BFRMtwta.js} +1 -1
- package/web/console/assets/Clients-CSHIsaWU.css +1 -0
- package/web/console/assets/Clients-DGkkSXyA.js +3 -0
- package/web/console/assets/{Cost-BbhlQFP8.js → Cost-BmGt0MfS.js} +1 -1
- package/web/console/assets/{Credentials-kqr1W0K7.js → Credentials-CR-sYINT.js} +1 -1
- package/web/console/assets/{Diagnostics-Kljt9XTH.js → Diagnostics-DAPbKSJp.js} +1 -1
- package/web/console/assets/{Executors-DnsB-Xz4.js → Executors-CgpiT3o8.js} +1 -1
- package/web/console/assets/{Kb-DLZSAFWP.js → Kb-Cm9dVSQw.js} +1 -1
- package/web/console/assets/{Login-DiTjPWmN.js → Login-CyUcsTZ_.js} +1 -1
- package/web/console/assets/{Projects-CXcVCgCD.js → Projects-DldFPgWg.js} +1 -1
- package/web/console/assets/Routes-DkMIjMQA.css +1 -0
- package/web/console/assets/Routes-DubfjlBD.js +124 -0
- package/web/console/assets/Runs-1hxgkuR4.js +3 -0
- package/web/console/assets/Runs-CvGTV8I-.css +1 -0
- package/web/console/assets/{Setup-FqoCKmfZ.js → Setup-DBLOnI2_.js} +1 -1
- package/web/console/assets/{Shell-BEqpONiy.js → Shell-gJ5Vz_E7.js} +1 -1
- package/web/console/assets/{StorageBuckets-CkORbYXY.js → StorageBuckets-DkDW16tj.js} +1 -1
- package/web/console/assets/{SystemSettings-gTQAazu7.js → SystemSettings-cDkudo2y.js} +1 -1
- package/web/console/assets/{SystemStatus-DAmUUNqO.js → SystemStatus-DZd6s8OX.js} +1 -1
- package/web/console/assets/{Targets-CZmDcjsE.js → Targets-DYNGq1NU.js} +1 -1
- package/web/console/assets/{Tools-CIHInlHL.js → Tools-DpWM59vV.js} +1 -1
- package/web/console/assets/{index-BkLbd7Cc.js → index-DGqs28gI.js} +2 -2
- package/web/console/assets/{request-r7GHVJzw.js → request-B7MLvOqo.js} +1 -1
- package/web/console/assets/{schema-BVn_hB7M.js → schema-CMuwjYdJ.js} +1 -1
- package/web/console/index.html +1 -1
- package/web-admin/scripts/subject-display-ui.mjs +78 -0
- package/web-admin/src/components/AgentSetupPanel.vue +262 -0
- package/web-admin/src/components/ClientConversations.vue +21 -4
- package/web-admin/src/components/conversation-audit.ts +20 -0
- package/web-admin/src/pages/AgentClients.vue +97 -23
- package/web-admin/src/pages/Clients.vue +4 -1
- package/web-admin/src/pages/Routes.vue +10 -1
- package/web-admin/tests/conversation-audit.fixture.mjs +13 -10
- package/web-admin/tests/conversation-audit.test.mjs +63 -1
- package/web/console/assets/AgentClients-BPIvlLYY.js +0 -1
- package/web/console/assets/AgentClients-DsWADpzr.css +0 -1
- package/web/console/assets/Clients-BSA1rlTB.js +0 -3
- package/web/console/assets/Clients-CNIVs7Qn.css +0 -1
- package/web/console/assets/Routes-CXGl9SMM.css +0 -1
- package/web/console/assets/Routes-DN9_d9Po.js +0 -124
- package/web/console/assets/Runs-Cwbe6thL.js +0 -3
- package/web/console/assets/Runs-D8UmjP5G.css +0 -1
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import type { Pool } from 'mysql2/promise';
|
|
4
|
+
import { AgentAuthRepository } from './config-agent-auth-repository';
|
|
5
|
+
import { evaluateBusinessSessionQuery } from '../../test-support/agent-auth-business-query-fixture';
|
|
6
|
+
|
|
7
|
+
type Row = Record<string, any>;
|
|
8
|
+
const APP = 'example-business';
|
|
9
|
+
const OTHER_APP = 'other-business';
|
|
10
|
+
const sessionId = (n: number) => `123e4567-e89b-42d3-a456-${String(n).padStart(12, '0')}`;
|
|
11
|
+
const authorizationId = (n: number) => `223e4567-e89b-42d3-a456-${String(n).padStart(12, '0')}`;
|
|
12
|
+
function session(n: number, overrides: Row = {}): Row {
|
|
13
|
+
return {
|
|
14
|
+
session_id: sessionId(n), client_app_id: APP, device_label: 'Synthetic device',
|
|
15
|
+
principal_json: { id: 'user-7', tenant: 'tenant-2', roles: ['manager'], audience: 'internal', channel: 'browser', private_secret: 'DO_NOT_RETURN_PRINCIPAL_SECRET' },
|
|
16
|
+
on_behalf_of: 'tenant-2:user-7', allowed_routes: ['orders'],
|
|
17
|
+
access_token_hash: 'DO_NOT_RETURN_ACCESS_HASH', refresh_token: 'DO_NOT_RETURN_REFRESH_TOKEN',
|
|
18
|
+
access_expires_at: '2000-01-01 00:00:00', refresh_expires_at: '2099-01-30 00:00:00',
|
|
19
|
+
created_at: '2026-09-09 08:00:00', updated_at: '2026-09-09 08:00:00', last_seen_at: null, revoked_at: null,
|
|
20
|
+
...overrides,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** In-memory evaluator for the narrow generated SELECT surface, not a live DB. */
|
|
25
|
+
function queryFixture(seed: Row[], authorizations: Row[] = []) {
|
|
26
|
+
const sessions = structuredClone(seed);
|
|
27
|
+
const queries: Array<{ sql: string; params: unknown[] }> = [];
|
|
28
|
+
const pool = { async query(sql: string, params: unknown[] = []) {
|
|
29
|
+
queries.push({ sql, params });
|
|
30
|
+
return evaluateBusinessSessionQuery(sessions, authorizations, sql, params);
|
|
31
|
+
} } as unknown as Pool;
|
|
32
|
+
return { repo: new AgentAuthRepository(() => pool), sessions, queries };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test('business Session detail is App-scoped and returns a principal field whitelist without token material', async () => {
|
|
36
|
+
const fx = queryFixture([session(1), session(2, { client_app_id: OTHER_APP })], [
|
|
37
|
+
{ authorization_id: authorizationId(1), session_id: sessionId(1), client_app_id: APP, status: 'consumed' },
|
|
38
|
+
]);
|
|
39
|
+
const detail = (await fx.repo.getBusinessSession(APP, sessionId(1)))!;
|
|
40
|
+
assert.equal(detail.authorization_id, authorizationId(1));
|
|
41
|
+
assert.equal(detail.state, 'active', 'access expiry alone does not end the absolute refresh lifetime');
|
|
42
|
+
assert.deepEqual(detail.principal, { id: 'user-7', tenant: 'tenant-2', roles: ['manager'], audience: 'internal', channel: 'browser' });
|
|
43
|
+
assert.doesNotMatch(JSON.stringify(detail), /DO_NOT_RETURN|private_secret|token_hash|refresh_token|cursor_created_at/);
|
|
44
|
+
assert.equal(await fx.repo.getBusinessSession(APP, sessionId(2)), null);
|
|
45
|
+
assert.equal(await fx.repo.getBusinessSession(OTHER_APP, sessionId(1)), null);
|
|
46
|
+
await assert.rejects(fx.repo.getBusinessSession('', sessionId(1)), /authenticated Client App/);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('business Session selectors combine exact authorization, subject and principal/tenant filters with AND', async () => {
|
|
50
|
+
const fx = queryFixture([
|
|
51
|
+
session(1), session(2, { on_behalf_of: 'tenant-2:User-7' }),
|
|
52
|
+
session(3, { principal_json: { id: 'User-7', tenant: 'tenant-2', roles: [] } }),
|
|
53
|
+
session(4, { principal_json: { id: 'user-7', tenant: 'tenant-3', roles: [] } }),
|
|
54
|
+
session(5, { client_app_id: OTHER_APP }),
|
|
55
|
+
], [1, 2, 3, 4, 5].map((n) => ({ authorization_id: authorizationId(n), session_id: sessionId(n), client_app_id: n === 5 ? OTHER_APP : APP, status: 'consumed' })));
|
|
56
|
+
const input = { clientAppId: APP, authorizationId: authorizationId(1), onBehalfOf: 'tenant-2:user-7', principalId: 'user-7', tenant: 'tenant-2', state: 'active' as const, limit: 20 };
|
|
57
|
+
assert.deepEqual((await fx.repo.listBusinessSessions(input)).list.map((row) => row.session_id), [sessionId(1)]);
|
|
58
|
+
assert.deepEqual((await fx.repo.listBusinessSessions({ ...input, tenant: 'tenant-3' })).list, []);
|
|
59
|
+
assert.deepEqual((await fx.repo.listBusinessSessions({ ...input, authorizationId: authorizationId(5) })).list, []);
|
|
60
|
+
assert.deepEqual((await fx.repo.listBusinessSessions({ clientAppId: APP, onBehalfOf: 'tenant-2:User-7', limit: 20 })).list.map((row) => row.session_id), [sessionId(2)]);
|
|
61
|
+
assert.match(fx.queries[0]!.sql, /matched\.client_app_id=s\.client_app_id/);
|
|
62
|
+
assert.match(fx.queries[0]!.sql, /BINARY s\.on_behalf_of=BINARY \?/);
|
|
63
|
+
assert.doesNotMatch(JSON.stringify((await fx.repo.listBusinessSessions({ clientAppId: APP, limit: 20 })).list), /DO_NOT_RETURN|private_secret/);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('tenant absence is explicit and Session status distinguishes expiry, revocation and refreshable access', async () => {
|
|
67
|
+
const fx = queryFixture([
|
|
68
|
+
session(1, { principal_json: { id: 'user-7', roles: [] } }),
|
|
69
|
+
session(2, { principal_json: { id: 'user-7', tenant: '', roles: [] } }),
|
|
70
|
+
session(3, { principal_json: { id: 'user-7', tenant: null, roles: [] } }),
|
|
71
|
+
session(4), session(5, { refresh_expires_at: '2000-01-01 00:00:00' }),
|
|
72
|
+
session(6, { revoked_at: '2026-09-09 08:00:00' }),
|
|
73
|
+
]);
|
|
74
|
+
assert.deepEqual((await fx.repo.listBusinessSessions({ clientAppId: APP, tenant: '', limit: 20 })).list.map((row) => row.session_id), [3, 2, 1].map(sessionId));
|
|
75
|
+
assert.equal((await fx.repo.listBusinessSessions({ clientAppId: APP, limit: 20 })).list.length, 6);
|
|
76
|
+
assert.deepEqual((await fx.repo.listBusinessSessions({ clientAppId: APP, state: 'expired', limit: 20 })).list.map((row) => row.state), ['expired']);
|
|
77
|
+
assert.deepEqual((await fx.repo.listBusinessSessions({ clientAppId: APP, state: 'revoked', limit: 20 })).list.map((row) => row.state), ['revoked']);
|
|
78
|
+
assert.equal((await fx.repo.listBusinessSessions({ clientAppId: APP, state: 'active', limit: 20 })).list.length, 4);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('keyset pages preserve identical timestamps and remain stable when earlier active Sessions are revoked', async () => {
|
|
82
|
+
const fx = queryFixture([session(1), session(2), session(3), session(4)]);
|
|
83
|
+
const input = { clientAppId: APP, state: 'active' as const, limit: 2 };
|
|
84
|
+
const first = await fx.repo.listBusinessSessions(input);
|
|
85
|
+
assert.deepEqual(first.list.map((row) => row.session_id), [4, 3].map(sessionId));
|
|
86
|
+
assert.deepEqual(first.nextCursor, { createdAt: '2026-09-09T08:00:00.000Z', sessionId: sessionId(3) });
|
|
87
|
+
for (const row of fx.sessions.filter((item) => [sessionId(3), sessionId(4)].includes(item.session_id))) row.revoked_at = '2026-09-09 09:00:00';
|
|
88
|
+
const second = await fx.repo.listBusinessSessions({ ...input, cursor: first.nextCursor });
|
|
89
|
+
assert.deepEqual(second.list.map((row) => row.session_id), [2, 1].map(sessionId));
|
|
90
|
+
assert.equal(second.nextCursor, undefined);
|
|
91
|
+
assert.equal(fx.queries[0]!.params.at(-1), 3, 'the repository fetches limit+1');
|
|
92
|
+
await fx.repo.listBusinessSessions({ clientAppId: APP, limit: 999 });
|
|
93
|
+
assert.equal(fx.queries.at(-1)!.params.at(-1), 101);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('duplicate or cross-App authorization links do not duplicate Session rows or invent an unambiguous mapping', async () => {
|
|
97
|
+
const fx = queryFixture([session(1), session(2)], [
|
|
98
|
+
{ authorization_id: authorizationId(1), session_id: sessionId(1), client_app_id: APP, status: 'consumed' },
|
|
99
|
+
{ authorization_id: authorizationId(2), session_id: sessionId(1), client_app_id: APP, status: 'consumed' },
|
|
100
|
+
{ authorization_id: authorizationId(3), session_id: sessionId(2), client_app_id: OTHER_APP, status: 'consumed' },
|
|
101
|
+
]);
|
|
102
|
+
const page = await fx.repo.listBusinessSessions({ clientAppId: APP, limit: 20 });
|
|
103
|
+
assert.equal(page.list.length, 2);
|
|
104
|
+
assert.ok(page.list.every((row) => !Object.hasOwn(row, 'authorization_id')));
|
|
105
|
+
assert.equal((await fx.repo.listBusinessSessions({ clientAppId: APP, authorizationId: authorizationId(1), limit: 20 })).list.length, 1);
|
|
106
|
+
assert.equal((await fx.repo.listBusinessSessions({ clientAppId: APP, authorizationId: authorizationId(3), limit: 20 })).list.length, 0);
|
|
107
|
+
assert.match(fx.queries[0]!.sql, /CASE WHEN COUNT\(\*\)=1 THEN MIN\(a\.authorization_id\) ELSE NULL END/);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('authorization projections include consumed Session mapping while code/token hashes remain internal', async () => {
|
|
111
|
+
const row = {
|
|
112
|
+
authorization_id: authorizationId(1), client_app_id: APP, redirect_uri: 'http://127.0.0.1:43123/callback', state_value: 'synthetic-state',
|
|
113
|
+
requested_routes: ['orders'], device_label: 'Synthetic device', code_challenge: 'c'.repeat(43),
|
|
114
|
+
status: 'consumed', session_id: sessionId(1), created_at: '2026-09-09 08:00:00', expires_at: '2026-09-09 08:10:00', code_hash: 'DO_NOT_RETURN_CODE_HASH',
|
|
115
|
+
};
|
|
116
|
+
const pool = { query: async () => [[row], []] } as unknown as Pool;
|
|
117
|
+
const result = await new AgentAuthRepository(() => pool).getAuthorization(authorizationId(1));
|
|
118
|
+
assert.equal(result?.session_id, sessionId(1));
|
|
119
|
+
assert.doesNotMatch(JSON.stringify(result), /DO_NOT_RETURN|code_hash/);
|
|
120
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import { LedgerFixture, AUTH, SESSION, OTHER_SESSION } from '../../test-support/agent-auth-lifecycle-fixture';
|
|
4
|
+
|
|
5
|
+
for (const first of ['revoke', 'exchange'] as const) {
|
|
6
|
+
test(`approved authorization: concurrent ${first} obtains the row lock first without a surviving grant`, { timeout: 5000 }, async () => {
|
|
7
|
+
const db = new LedgerFixture();
|
|
8
|
+
await db.approve();
|
|
9
|
+
const gate = db.holdNextLock('authorization', AUTH);
|
|
10
|
+
const waiting = db.waitForBlocked('authorization', AUTH);
|
|
11
|
+
const firstResult = db[first]();
|
|
12
|
+
await gate.arrived;
|
|
13
|
+
const secondResult = first === 'revoke' ? db.exchange() : db.revoke();
|
|
14
|
+
try { await waiting; } finally { gate.release(); }
|
|
15
|
+
const [one, two] = await Promise.all([firstResult, secondResult]);
|
|
16
|
+
const revoked = first === 'revoke' ? one : two;
|
|
17
|
+
const exchanged = first === 'exchange' ? one : two;
|
|
18
|
+
assert.equal(revoked.ok, true);
|
|
19
|
+
assert.equal(exchanged.ok, first === 'exchange');
|
|
20
|
+
assert.equal(db.rows.session.size, first === 'exchange' ? 1 : 0);
|
|
21
|
+
await db.assertNoCredentialsWork();
|
|
22
|
+
assert.equal((await db.revoke()).ok, true, 'retry is idempotent');
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (const first of ['revoke', 'rotate'] as const) {
|
|
27
|
+
test(`consumed authorization: concurrent ${first} obtains the session lock first and revokes the entire refresh family`, { timeout: 5000 }, async () => {
|
|
28
|
+
const db = new LedgerFixture();
|
|
29
|
+
await db.approve();
|
|
30
|
+
assert.equal((await db.exchange()).ok, true);
|
|
31
|
+
const gate = db.holdNextLock('session', SESSION);
|
|
32
|
+
const waiting = db.waitForBlocked('session', SESSION);
|
|
33
|
+
const firstResult = db[first]();
|
|
34
|
+
await gate.arrived;
|
|
35
|
+
const secondResult = first === 'revoke' ? db.rotate() : db.revoke();
|
|
36
|
+
try { await waiting; } finally { gate.release(); }
|
|
37
|
+
const [one, two] = await Promise.all([firstResult, secondResult]);
|
|
38
|
+
assert.equal((first === 'revoke' ? one : two).ok, true);
|
|
39
|
+
assert.equal((first === 'rotate' ? one : two).ok, first === 'rotate');
|
|
40
|
+
await db.assertNoCredentialsWork();
|
|
41
|
+
assert.equal((await db.revoke()).ok, true);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
test('client ownership is enforced before revocation; consumed cross-client links fail without mutating either session', async () => {
|
|
46
|
+
const db = new LedgerFixture();
|
|
47
|
+
await db.approve();
|
|
48
|
+
assert.deepEqual(await db.repository.revokeAuthorization({ authorizationId: AUTH, clientAppId: 'unrelated-app' }), { ok: false, reason: 'wrong_client' });
|
|
49
|
+
assert.equal((await db.exchange()).ok, true);
|
|
50
|
+
const before = structuredClone(db.rows.session.get(SESSION));
|
|
51
|
+
db.rows.session.get(SESSION)!.client_app_id = 'unrelated-app';
|
|
52
|
+
assert.deepEqual(await db.revoke(), { ok: false, reason: 'wrong_client' });
|
|
53
|
+
assert.equal(db.rows.session.get(SESSION)!.revoked_at, null);
|
|
54
|
+
assert.equal(db.rows.token.get('refresh-original')!.status, 'active');
|
|
55
|
+
db.rows.session.set(SESSION, before!);
|
|
56
|
+
assert.equal((await db.revoke()).ok, true);
|
|
57
|
+
await db.assertNoCredentialsWork();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('refresh revalidates token ownership after its unlocked locator read', { timeout: 5000 }, async () => {
|
|
61
|
+
const db = new LedgerFixture();
|
|
62
|
+
await db.approve();
|
|
63
|
+
assert.equal((await db.exchange()).ok, true);
|
|
64
|
+
const gate = db.holdNextLock('session', SESSION);
|
|
65
|
+
const rotation = db.rotate();
|
|
66
|
+
await gate.arrived;
|
|
67
|
+
// Synthetic corrupt/changed storage: the refresh locator no longer owns the
|
|
68
|
+
// session that was just locked. No new credential may escape this mismatch.
|
|
69
|
+
db.rows.token.get('refresh-original')!.session_id = OTHER_SESSION;
|
|
70
|
+
gate.release();
|
|
71
|
+
assert.deepEqual(await rotation, { ok: false, reason: 'invalid_grant' });
|
|
72
|
+
assert.equal(db.rows.token.has('refresh-next'), false);
|
|
73
|
+
assert.equal(db.rows.session.get(SESSION)!.access_token_hash, 'access-original');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('storage failure after staging session revocation rolls back and cannot report success', async () => {
|
|
77
|
+
const db = new LedgerFixture();
|
|
78
|
+
await db.approve();
|
|
79
|
+
assert.equal((await db.exchange()).ok, true);
|
|
80
|
+
db.failNext("UPDATE bz_agent_refresh_tokens SET status='revoked'");
|
|
81
|
+
await assert.rejects(db.revoke(), /synthetic storage unavailable/);
|
|
82
|
+
assert.equal(db.rows.session.get(SESSION)!.revoked_at, null);
|
|
83
|
+
assert.equal(db.rows.token.get('refresh-original')!.status, 'active');
|
|
84
|
+
assert.equal((await db.revoke()).ok, true, 'explicit retry can establish committed success');
|
|
85
|
+
await db.assertNoCredentialsWork();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('commit failure after revoking an approved code leaves the caller without success evidence', async () => {
|
|
89
|
+
const db = new LedgerFixture();
|
|
90
|
+
await db.approve();
|
|
91
|
+
db.failNextCommit();
|
|
92
|
+
await assert.rejects(db.revoke(), /synthetic commit unavailable/);
|
|
93
|
+
assert.equal(db.rows.authorization.get(AUTH)!.status, 'approved');
|
|
94
|
+
assert.equal(db.rows.authorization.get(AUTH)!.code_hash, 'synthetic-code-hash');
|
|
95
|
+
assert.equal((await db.revoke()).ok, true);
|
|
96
|
+
assert.equal((await db.exchange()).ok, false);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('exchange failure after staging credentials rolls them back before a subsequent revocation', async () => {
|
|
100
|
+
const db = new LedgerFixture();
|
|
101
|
+
await db.approve();
|
|
102
|
+
db.failNext("UPDATE bz_agent_authorizations SET status='consumed'");
|
|
103
|
+
await assert.rejects(db.exchange(), /synthetic storage unavailable/);
|
|
104
|
+
assert.equal(db.rows.session.size, 0);
|
|
105
|
+
assert.equal(db.rows.token.size, 0);
|
|
106
|
+
assert.equal((await db.revoke()).ok, true);
|
|
107
|
+
assert.equal((await db.exchange()).ok, false);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
for (const state of ['pending', 'approved', 'revoked', 'denied', 'expired'] as const) {
|
|
111
|
+
test(`${state} authorization can be safely revoked repeatedly without minting a session`, async () => {
|
|
112
|
+
const db = new LedgerFixture();
|
|
113
|
+
if (state === 'approved') await db.approve();
|
|
114
|
+
else db.rows.authorization.get(AUTH)!.status = state;
|
|
115
|
+
assert.equal((await db.revoke()).ok, true);
|
|
116
|
+
assert.equal((await db.revoke()).ok, true);
|
|
117
|
+
assert.equal(db.rows.authorization.get(AUTH)!.code_hash, null);
|
|
118
|
+
assert.equal(db.rows.authorization.get(AUTH)!.code_expires_at, null);
|
|
119
|
+
assert.equal((await db.exchange()).ok, false);
|
|
120
|
+
assert.equal(db.rows.session.size, 0);
|
|
121
|
+
assert.equal(db.rows.token.size, 0);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
test('missing consumed session or unknown authorization state cannot claim committed revocation', async () => {
|
|
126
|
+
const db = new LedgerFixture();
|
|
127
|
+
await db.approve();
|
|
128
|
+
assert.equal((await db.exchange()).ok, true);
|
|
129
|
+
db.rows.session.delete(SESSION);
|
|
130
|
+
assert.deepEqual(await db.revoke(), { ok: false, reason: 'not_found' });
|
|
131
|
+
assert.equal(db.rows.token.get('refresh-original')!.status, 'active', 'failed mutation has no partial changes');
|
|
132
|
+
db.rows.authorization.get(AUTH)!.status = 'unexpected-state';
|
|
133
|
+
await assert.rejects(db.revoke(), /lifecycle state is invalid/);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('concurrent repeated revocations serialize and both acknowledge the same revoked session', { timeout: 5000 }, async () => {
|
|
137
|
+
const db = new LedgerFixture();
|
|
138
|
+
await db.approve();
|
|
139
|
+
assert.equal((await db.exchange()).ok, true);
|
|
140
|
+
const gate = db.holdNextLock('authorization', AUTH);
|
|
141
|
+
const waiting = db.waitForBlocked('authorization', AUTH);
|
|
142
|
+
const first = db.revoke();
|
|
143
|
+
await gate.arrived;
|
|
144
|
+
const second = db.revoke();
|
|
145
|
+
try { await waiting; } finally { gate.release(); }
|
|
146
|
+
const results = await Promise.all([first, second]);
|
|
147
|
+
for (const result of results) {
|
|
148
|
+
assert.equal(result.ok, true);
|
|
149
|
+
if (result.ok) assert.equal(result.sessionId, SESSION);
|
|
150
|
+
}
|
|
151
|
+
await db.assertNoCredentialsWork();
|
|
152
|
+
});
|
|
@@ -33,6 +33,7 @@ test('AgentAuthRepository refresh rotation keeps the original absolute refresh e
|
|
|
33
33
|
rollback: async () => undefined,
|
|
34
34
|
release: () => undefined,
|
|
35
35
|
async query(sql: string, params: unknown[] = []) {
|
|
36
|
+
if (sql.startsWith('SELECT session_id FROM bz_agent_refresh_tokens')) return [[{ session_id: row.session_id }], []];
|
|
36
37
|
if (sql.startsWith('SELECT * FROM bz_agent_refresh_tokens')) {
|
|
37
38
|
return [[{ token_hash: 'r'.repeat(64), session_id: row.session_id, status: 'active', expires_at: originalRefreshExpiry }], []];
|
|
38
39
|
}
|
|
@@ -79,6 +80,7 @@ test('AgentAuthRepository refresh replay revokes the whole session fail closed',
|
|
|
79
80
|
rollback: async () => undefined,
|
|
80
81
|
release: () => undefined,
|
|
81
82
|
async query(sql: string) {
|
|
83
|
+
if (sql.startsWith('SELECT session_id FROM bz_agent_refresh_tokens')) return [[{ session_id: row.session_id }], []];
|
|
82
84
|
if (sql.startsWith('SELECT * FROM bz_agent_refresh_tokens')) {
|
|
83
85
|
return [[{ token_hash: 'r'.repeat(64), session_id: row.session_id, status: 'used', expires_at: row.refresh_expires_at }], []];
|
|
84
86
|
}
|