Skip to content

Commit be7a97e

Browse files
authored
Merge pull request #17 from dcplatforms/feat/zero-trust-standardization-6176282475414098330
Zero Trust and Architectural Integrity Refactor
2 parents 7c9199c + 6d16b20 commit be7a97e

4 files changed

Lines changed: 83 additions & 39 deletions

File tree

src/services/agent.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,11 @@ class AgentService {
181181

182182
// Basic policy checks (more complex logic would be here)
183183
if (amount > fromAgent.policy.spendingLimit) {
184-
throw new Error(
185-
`Transfer amount exceeds spending limit for agent ${fromAgentId}`,
186-
);
184+
throw new Error(`Zero Trust Validation Failed: Transfer amount exceeds spending limit for agent ${fromAgentId}`);
187185
}
188-
if (
189-
!fromAgent.policy.authorizedCounterparties.includes(toAgentId) &&
190-
fromAgent.policy.authorizedCounterparties.length > 0
191-
) {
192-
throw new Error(
193-
`Agent ${toAgentId} is not an authorized counterparty for ${fromAgentId}`,
194-
);
186+
if (!fromAgent.policy.authorizedCounterparties.includes(toAgentId) &&
187+
fromAgent.policy.authorizedCounterparties.length > 0) {
188+
throw new Error(`Zero Trust Validation Failed: Agent ${toAgentId} is not an authorized counterparty for ${fromAgentId}`);
195189
}
196190

197191
// Simulate transfer success

src/services/mandate.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,18 @@ class MandateService {
6767
}) {
6868
const decodedIntent = await this.verifyMandate(intentMandate);
6969

70-
if (decodedIntent.type !== "intent_mandate") {
71-
throw new Error(
72-
"Zero Trust Validation Failed: Invalid intent mandate type",
73-
);
70+
if (decodedIntent.type !== 'intent_mandate') {
71+
throw new Error('Zero Trust Validation Failed: Invalid intent mandate type');
7472
}
7573

7674
// Verify budget
7775
if (totalPrice > decodedIntent.max_budget.value) {
78-
throw new Error(
79-
"Zero Trust Validation Failed: Cart total exceeds intent mandate budget",
80-
);
76+
throw new Error('Zero Trust Validation Failed: Cart total exceeds intent mandate budget');
8177
}
8278

8379
// Verify merchant if whitelist exists
84-
if (
85-
decodedIntent.allowed_merchants.length > 0 &&
86-
!decodedIntent.allowed_merchants.includes(merchantDid)
87-
) {
88-
throw new Error(
89-
`Zero Trust Validation Failed: Merchant ${merchantDid} is not authorized by this mandate`,
90-
);
80+
if (decodedIntent.allowed_merchants.length > 0 && !decodedIntent.allowed_merchants.includes(merchantDid)) {
81+
throw new Error(`Zero Trust Validation Failed: Merchant ${merchantDid} is not authorized by this mandate`);
9182
}
9283

9384
// Create cryptographic hash of cart
@@ -120,9 +111,7 @@ class MandateService {
120111
try {
121112
return jwt.verify(token, this.signingKey, { algorithms: ["HS256"] });
122113
} catch (error) {
123-
throw new Error(
124-
`Zero Trust Validation Failed: Mandate verification failed: ${error.message}`,
125-
);
114+
throw new Error(`Zero Trust Validation Failed: Mandate verification failed: ${error.message}`);
126115
}
127116
}
128117

src/services/ucp.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,26 @@ class UCPService {
4242
/**
4343
* Process a UCP Payload
4444
* @param {Object} payload - The raw UCP JSON payload
45+
* @param {string} mandate - Optional signed Mandate (AP2) for Zero Trust validation
4546
*/
46-
async processPayload(payload) {
47+
async processPayload(payload, mandate) {
4748
try {
4849
// 1. Validate the UCP intent against schema
4950
const { error, value } = this.ucpIntentSchema.validate(payload, {
5051
stripUnknown: true,
5152
});
5253
if (error) {
53-
throw new Error(
54-
`Zero Trust Validation Failed: UCP Intent validation failed: ${error.details.map((x) => x.message).join(", ")}`,
55-
);
54+
throw new Error(`Zero Trust Validation Failed: UCP Intent validation failed: ${error.details.map(x => x.message).join(', ')}`);
5655
}
5756

5857
const validatedPayload = value;
5958
const { intent } = validatedPayload;
6059

6160
switch (intent) {
62-
case "transfer":
63-
case "payment":
64-
return this._handleTransfer(validatedPayload);
65-
case "purchase":
61+
case 'transfer':
62+
case 'payment':
63+
return this._handleTransfer(validatedPayload, mandate);
64+
case 'purchase':
6665
// Future implementation: integration with Inventory/Order services
6766
return {
6867
status: "success",
@@ -85,21 +84,22 @@ class UCPService {
8584
* Handle transfer/payment intents via A2AService
8685
* @private
8786
*/
88-
async _handleTransfer(payload) {
87+
async _handleTransfer(payload, mandate) {
8988
const { sender, recipient, amount } = payload;
9089

9190
if (!recipient?.agent_id) {
92-
throw new Error("Missing recipient agent_id for transfer");
91+
throw new Error('Zero Trust Validation Failed: Missing recipient agent_id for transfer');
9392
}
9493
if (!amount?.value) {
95-
throw new Error("Missing amount value");
94+
throw new Error('Zero Trust Validation Failed: Missing amount value');
9695
}
9796

9897
return this.a2aService.executeTransfer({
9998
fromAgentId: sender.agent_id,
10099
toAgentId: recipient.agent_id,
101100
amount: amount.value,
102-
ucpPayload: payload,
101+
mandate,
102+
ucpPayload: payload
103103
});
104104
}
105105

tests/unit/mandate_extra.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const MandateService = require('../../src/services/mandate');
2+
const jwt = require('jsonwebtoken');
3+
4+
describe('MandateService', () => {
5+
let mandateService;
6+
const signingKey = 'test-secret';
7+
8+
beforeEach(() => {
9+
mandateService = new MandateService({ signingKey });
10+
});
11+
12+
describe('issueCartMandate', () => {
13+
it('should throw error for invalid intent mandate type', async () => {
14+
const invalidMandate = jwt.sign({ type: 'not_intent' }, signingKey);
15+
await expect(mandateService.issueCartMandate({
16+
intentMandate: invalidMandate,
17+
cartItems: [],
18+
totalPrice: 100,
19+
merchantDid: 'did:key:m'
20+
})).rejects.toThrow('Zero Trust Validation Failed: Invalid intent mandate type');
21+
});
22+
23+
it('should throw error if cart total exceeds budget', async () => {
24+
const intentMandate = await mandateService.issueIntentMandate({
25+
userDid: 'did:key:u',
26+
agentDid: 'did:key:a',
27+
maxBudget: 50
28+
});
29+
30+
await expect(mandateService.issueCartMandate({
31+
intentMandate,
32+
cartItems: [],
33+
totalPrice: 100,
34+
merchantDid: 'did:key:m'
35+
})).rejects.toThrow('Zero Trust Validation Failed: Cart total exceeds intent mandate budget');
36+
});
37+
38+
it('should throw error if merchant is not authorized', async () => {
39+
const intentMandate = await mandateService.issueIntentMandate({
40+
userDid: 'did:key:u',
41+
agentDid: 'did:key:a',
42+
maxBudget: 500,
43+
allowedMerchants: ['did:key:m1']
44+
});
45+
46+
await expect(mandateService.issueCartMandate({
47+
intentMandate,
48+
cartItems: [],
49+
totalPrice: 100,
50+
merchantDid: 'did:key:m2'
51+
})).rejects.toThrow('Zero Trust Validation Failed: Merchant did:key:m2 is not authorized by this mandate');
52+
});
53+
});
54+
55+
describe('verifyMandate', () => {
56+
it('should throw error for invalid token', async () => {
57+
await expect(mandateService.verifyMandate('invalid-token'))
58+
.rejects.toThrow('Zero Trust Validation Failed: Mandate verification failed: jwt malformed');
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)