UNPKG

@kya-os/mcp-i

Version:

The TypeScript MCP framework with identity features built-in

137 lines (136 loc) 4.41 kB
"use strict"; /** * Memory Delegation Verifier * * Simple in-memory delegation storage for testing and development. * NOT suitable for production - delegations lost on restart. * * Use for: * - Unit tests * - Local development * - Integration tests * * Related: PHASE_1_XMCP_I_SERVER.md Testing section */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MemoryDelegationVerifier = void 0; const delegation_1 = require("@kya-os/contracts/delegation"); const delegation_verifier_1 = require("./delegation-verifier"); /** * Memory Delegation Verifier * * Simple in-memory storage (for testing only) */ class MemoryDelegationVerifier { delegations = new Map(); agentIndex = new Map(); // agentDid -> Set<delegationId> debug; constructor(config) { this.debug = config.debug || false; } /** * Verify agent delegation */ async verify(agentDid, scopes, _options) { // Validate inputs const validation = delegation_verifier_1.VerifyDelegationInputSchema.safeParse({ agentDid, scopes, options: _options }); if (!validation.success) { return { valid: false, reason: `Invalid request: ${validation.error.errors.map(e => `${e.path.join('.')}: ${e.message}`).join(', ')}`, }; } // Find all delegations for this agent const delegationIds = this.agentIndex.get(agentDid); if (!delegationIds || delegationIds.size === 0) { return { valid: false, reason: 'No delegation found for agent', cached: false, }; } // Check each delegation to find one with matching scopes for (const id of delegationIds) { const delegation = this.delegations.get(id); if (!delegation) continue; // Validate delegation const validation = (0, delegation_verifier_1.validateDelegation)(delegation); if (!validation.valid) continue; // Check scopes const delegationScopes = (0, delegation_verifier_1.extractScopes)(delegation); if ((0, delegation_verifier_1.checkScopes)(delegationScopes, scopes)) { return { valid: true, delegation, cached: false, }; } } // No matching delegation found return { valid: false, reason: 'No delegation with required scopes', cached: false, }; } /** * Get delegation by ID */ async get(delegationId) { return this.delegations.get(delegationId) || null; } /** * Store delegation */ async put(delegation) { // Validate first const parsed = delegation_1.DelegationRecordSchema.safeParse(delegation); if (!parsed.success) { throw new Error(`Invalid delegation record: ${parsed.error.message}`); } const validated = parsed.data; // Store delegation this.delegations.set(validated.id, validated); // Update agent index if (!this.agentIndex.has(validated.subjectDid)) { this.agentIndex.set(validated.subjectDid, new Set()); } this.agentIndex.get(validated.subjectDid).add(validated.id); if (this.debug) { console.log(`[Memory] Stored delegation ${validated.id}`); } } /** * Revoke delegation */ async revoke(delegationId, reason) { const delegation = this.delegations.get(delegationId); if (!delegation) { throw new Error(`Delegation not found: ${delegationId}`); } delegation.status = 'revoked'; delegation.revokedAt = Date.now(); if (reason) { delegation.revokedReason = reason; } if (this.debug) { console.log(`[Memory] Revoked delegation ${delegationId}`); } } /** * Clear all delegations (for testing) */ clear() { this.delegations.clear(); this.agentIndex.clear(); } /** * Get all delegations (for testing/debugging) */ getAll() { return Array.from(this.delegations.values()); } } exports.MemoryDelegationVerifier = MemoryDelegationVerifier;