@private.me/xbind 1.2.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/AGENTS.md +778 -0
- package/LICENSE.md +27 -0
- package/README.md +400 -0
- package/dist-standalone/_deps/crypto/base64.d.ts +29 -0
- package/dist-standalone/_deps/crypto/base64.js +97 -0
- package/dist-standalone/_deps/crypto/cjs/base64.js +103 -0
- package/dist-standalone/_deps/crypto/cjs/errors.js +119 -0
- package/dist-standalone/_deps/crypto/cjs/hmac.js +71 -0
- package/dist-standalone/_deps/crypto/cjs/index.js +86 -0
- package/dist-standalone/_deps/crypto/cjs/padding.js +57 -0
- package/dist-standalone/_deps/crypto/cjs/share-header.js +68 -0
- package/dist-standalone/_deps/crypto/cjs/shares.js +152 -0
- package/dist-standalone/_deps/crypto/cjs/tlv.js +199 -0
- package/dist-standalone/_deps/crypto/cjs/uuid.js +61 -0
- package/dist-standalone/_deps/crypto/cjs/verify.js +24 -0
- package/dist-standalone/_deps/crypto/cjs/xorida.js +221 -0
- package/dist-standalone/_deps/crypto/errors.d.ts +51 -0
- package/dist-standalone/_deps/crypto/errors.js +109 -0
- package/dist-standalone/_deps/crypto/hmac.d.ts +39 -0
- package/dist-standalone/_deps/crypto/hmac.js +66 -0
- package/dist-standalone/_deps/crypto/index.d.ts +20 -0
- package/dist-standalone/_deps/crypto/index.js +45 -0
- package/dist-standalone/_deps/crypto/padding.d.ts +19 -0
- package/dist-standalone/_deps/crypto/padding.js +53 -0
- package/dist-standalone/_deps/crypto/share-header.d.ts +44 -0
- package/dist-standalone/_deps/crypto/share-header.js +63 -0
- package/dist-standalone/_deps/crypto/shares.d.ts +27 -0
- package/dist-standalone/_deps/crypto/shares.js +148 -0
- package/dist-standalone/_deps/crypto/tlv.d.ts +26 -0
- package/dist-standalone/_deps/crypto/tlv.js +195 -0
- package/dist-standalone/_deps/crypto/uuid.d.ts +22 -0
- package/dist-standalone/_deps/crypto/uuid.js +56 -0
- package/dist-standalone/_deps/crypto/verify.d.ts +15 -0
- package/dist-standalone/_deps/crypto/verify.js +15 -0
- package/dist-standalone/_deps/crypto/xorida.d.ts +44 -0
- package/dist-standalone/_deps/crypto/xorida.js +215 -0
- package/dist-standalone/_deps/mldsa-wasm/LICENSE +24 -0
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -0
- package/dist-standalone/_deps/mldsa-wasm/package.json +46 -0
- package/dist-standalone/_deps/mldsa-wasm/types/mldsa.d.ts +30 -0
- package/dist-standalone/_deps/shared/cjs/errors.js +582 -0
- package/dist-standalone/_deps/shared/cjs/index.js +492 -0
- package/dist-standalone/_deps/shared/cjs/package.json +1 -0
- package/dist-standalone/_deps/shared/cjs/types.js +403 -0
- package/dist-standalone/_deps/shared/errors.d.ts +48 -0
- package/dist-standalone/_deps/shared/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/shared/errors.js +192 -0
- package/dist-standalone/_deps/shared/errors.js.map +1 -0
- package/dist-standalone/_deps/shared/index.d.ts +4 -0
- package/dist-standalone/_deps/shared/index.d.ts.map +1 -0
- package/dist-standalone/_deps/shared/index.js +78 -0
- package/dist-standalone/_deps/shared/index.js.map +1 -0
- package/dist-standalone/_deps/shared/types.d.ts +1097 -0
- package/dist-standalone/_deps/shared/types.d.ts.map +1 -0
- package/dist-standalone/_deps/shared/types.js +89 -0
- package/dist-standalone/_deps/shared/types.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts +115 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts +13 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/package.json +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts +39 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +83 -0
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts +99 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +143 -0
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts +32 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +119 -0
- package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts +109 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +8 -0
- package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/errors.d.ts +115 -0
- package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/errors.js +253 -0
- package/dist-standalone/_deps/ux-helpers/errors.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/index.d.ts +13 -0
- package/dist-standalone/_deps/ux-helpers/index.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/index.js +16 -0
- package/dist-standalone/_deps/ux-helpers/index.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts +39 -0
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/pagination.js +79 -0
- package/dist-standalone/_deps/ux-helpers/pagination.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/progress.d.ts +99 -0
- package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/progress.js +138 -0
- package/dist-standalone/_deps/ux-helpers/progress.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/search.d.ts +32 -0
- package/dist-standalone/_deps/ux-helpers/search.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/search.js +116 -0
- package/dist-standalone/_deps/ux-helpers/search.js.map +1 -0
- package/dist-standalone/_deps/ux-helpers/types.d.ts +109 -0
- package/dist-standalone/_deps/ux-helpers/types.d.ts.map +1 -0
- package/dist-standalone/_deps/ux-helpers/types.js +7 -0
- package/dist-standalone/_deps/ux-helpers/types.js.map +1 -0
- package/dist-standalone/_deps/xchange/auto-accept.d.ts +127 -0
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/package.json +1 -0
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -0
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -0
- package/dist-standalone/_deps/xchange/errors.d.ts +69 -0
- package/dist-standalone/_deps/xchange/errors.js +1 -0
- package/dist-standalone/_deps/xchange/index.d.ts +15 -0
- package/dist-standalone/_deps/xchange/index.js +1 -0
- package/dist-standalone/_deps/xchange/invite-client.d.ts +178 -0
- package/dist-standalone/_deps/xchange/invite-client.js +1 -0
- package/dist-standalone/_deps/xchange/lazy-init.d.ts +176 -0
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -0
- package/dist-standalone/_deps/xchange/trust-integration.d.ts +102 -0
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -0
- package/dist-standalone/_deps/xchange/xchange.d.ts +60 -0
- package/dist-standalone/_deps/xchange/xchange.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/package.json +1 -0
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -0
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -0
- package/dist-standalone/_deps/xregistry/discovery.d.ts +126 -0
- package/dist-standalone/_deps/xregistry/discovery.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/discovery.js +1 -0
- package/dist-standalone/_deps/xregistry/discovery.js.map +1 -0
- package/dist-standalone/_deps/xregistry/errors.d.ts +41 -0
- package/dist-standalone/_deps/xregistry/errors.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/errors.js +1 -0
- package/dist-standalone/_deps/xregistry/errors.js.map +1 -0
- package/dist-standalone/_deps/xregistry/index.d.ts +8 -0
- package/dist-standalone/_deps/xregistry/index.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/index.js +1 -0
- package/dist-standalone/_deps/xregistry/index.js.map +1 -0
- package/dist-standalone/_deps/xregistry/registry.d.ts +85 -0
- package/dist-standalone/_deps/xregistry/registry.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/registry.js +1 -0
- package/dist-standalone/_deps/xregistry/registry.js.map +1 -0
- package/dist-standalone/_deps/xregistry/schema.d.ts +81 -0
- package/dist-standalone/_deps/xregistry/schema.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/schema.js +1 -0
- package/dist-standalone/_deps/xregistry/schema.js.map +1 -0
- package/dist-standalone/_deps/xregistry/types.d.ts +95 -0
- package/dist-standalone/_deps/xregistry/types.d.ts.map +1 -0
- package/dist-standalone/_deps/xregistry/types.js +1 -0
- package/dist-standalone/_deps/xregistry/types.js.map +1 -0
- package/dist-standalone/agent-call.d.ts +286 -0
- package/dist-standalone/agent-call.js +642 -0
- package/dist-standalone/agent-sdk.d.ts +207 -0
- package/dist-standalone/agent-sdk.js +328 -0
- package/dist-standalone/agent.d.ts +670 -0
- package/dist-standalone/agent.js +1529 -0
- package/dist-standalone/approval.d.ts +145 -0
- package/dist-standalone/approval.js +193 -0
- package/dist-standalone/auth.d.ts +75 -0
- package/dist-standalone/auth.js +219 -0
- package/dist-standalone/auto-accept.d.ts +102 -0
- package/dist-standalone/auto-accept.js +229 -0
- package/dist-standalone/backup-config.d.ts +150 -0
- package/dist-standalone/backup-config.js +201 -0
- package/dist-standalone/checkpoint.d.ts +125 -0
- package/dist-standalone/checkpoint.js +186 -0
- package/dist-standalone/cjs/agent-call.js +651 -0
- package/dist-standalone/cjs/agent-sdk.js +332 -0
- package/dist-standalone/cjs/agent.js +1566 -0
- package/dist-standalone/cjs/approval.js +199 -0
- package/dist-standalone/cjs/auth.js +225 -0
- package/dist-standalone/cjs/auto-accept.js +233 -0
- package/dist-standalone/cjs/backup-config.js +207 -0
- package/dist-standalone/cjs/checkpoint.js +193 -0
- package/dist-standalone/cjs/cli/init.js +487 -0
- package/dist-standalone/cjs/connect.js +312 -0
- package/dist-standalone/cjs/did-document.js +101 -0
- package/dist-standalone/cjs/did-privateme.js +130 -0
- package/dist-standalone/cjs/did-web.js +201 -0
- package/dist-standalone/cjs/discovery.js +462 -0
- package/dist-standalone/cjs/dual-mode.js +251 -0
- package/dist-standalone/cjs/email-templates.js +313 -0
- package/dist-standalone/cjs/email-transport.js +239 -0
- package/dist-standalone/cjs/envelope.js +510 -0
- package/dist-standalone/cjs/errors.js +562 -0
- package/dist-standalone/cjs/gateway-state.js +55 -0
- package/dist-standalone/cjs/gateway-transport.js +120 -0
- package/dist-standalone/cjs/guardrails.js +223 -0
- package/dist-standalone/cjs/http-compat.js +272 -0
- package/dist-standalone/cjs/identity.js +541 -0
- package/dist-standalone/cjs/index.js +224 -0
- package/dist-standalone/cjs/invitation.js +421 -0
- package/dist-standalone/cjs/invite.js +328 -0
- package/dist-standalone/cjs/key-agreement.js +246 -0
- package/dist-standalone/cjs/lazy-init.js +300 -0
- package/dist-standalone/cjs/mdns-discovery.js +202 -0
- package/dist-standalone/cjs/nonce-store.js +66 -0
- package/dist-standalone/cjs/package.json +3 -0
- package/dist-standalone/cjs/pairing-manager.js +223 -0
- package/dist-standalone/cjs/policy.js +320 -0
- package/dist-standalone/cjs/redis-nonce-store.js +76 -0
- package/dist-standalone/cjs/registry-middleware.js +50 -0
- package/dist-standalone/cjs/retry-transport.js +102 -0
- package/dist-standalone/cjs/security-policy.js +204 -0
- package/dist-standalone/cjs/split-channel.js +177 -0
- package/dist-standalone/cjs/subscription-proof.js +230 -0
- package/dist-standalone/cjs/succession.js +148 -0
- package/dist-standalone/cjs/transport.js +63 -0
- package/dist-standalone/cjs/trust-registry.js +742 -0
- package/dist-standalone/cjs/verify.js +25 -0
- package/dist-standalone/cjs/xfetch.js +252 -0
- package/dist-standalone/cli/init.d.ts +63 -0
- package/dist-standalone/cli/init.js +450 -0
- package/dist-standalone/connect.d.ts +143 -0
- package/dist-standalone/connect.js +274 -0
- package/dist-standalone/did-document.d.ts +65 -0
- package/dist-standalone/did-document.js +96 -0
- package/dist-standalone/did-privateme.d.ts +70 -0
- package/dist-standalone/did-privateme.js +121 -0
- package/dist-standalone/did-web.d.ts +73 -0
- package/dist-standalone/did-web.js +196 -0
- package/dist-standalone/discovery.d.ts +176 -0
- package/dist-standalone/discovery.js +458 -0
- package/dist-standalone/dual-mode.d.ts +145 -0
- package/dist-standalone/dual-mode.js +247 -0
- package/dist-standalone/email-templates.d.ts +41 -0
- package/dist-standalone/email-templates.js +309 -0
- package/dist-standalone/email-transport.d.ts +139 -0
- package/dist-standalone/email-transport.js +232 -0
- package/dist-standalone/envelope.d.ts +288 -0
- package/dist-standalone/envelope.js +497 -0
- package/dist-standalone/errors.d.ts +74 -0
- package/dist-standalone/errors.js +548 -0
- package/dist-standalone/gateway-state.d.ts +32 -0
- package/dist-standalone/gateway-state.js +51 -0
- package/dist-standalone/gateway-transport.d.ts +59 -0
- package/dist-standalone/gateway-transport.js +116 -0
- package/dist-standalone/guardrails.d.ts +136 -0
- package/dist-standalone/guardrails.js +216 -0
- package/dist-standalone/http-compat.d.ts +150 -0
- package/dist-standalone/http-compat.js +267 -0
- package/dist-standalone/identity.d.ts +176 -0
- package/dist-standalone/identity.js +516 -0
- package/dist-standalone/index.d.ts +83 -0
- package/dist-standalone/index.js +51 -0
- package/dist-standalone/invitation.d.ts +211 -0
- package/dist-standalone/invitation.js +415 -0
- package/dist-standalone/invite.d.ts +192 -0
- package/dist-standalone/invite.js +324 -0
- package/dist-standalone/key-agreement.d.ts +122 -0
- package/dist-standalone/key-agreement.js +236 -0
- package/dist-standalone/lazy-init.d.ts +167 -0
- package/dist-standalone/lazy-init.js +295 -0
- package/dist-standalone/mdns-discovery.d.ts +117 -0
- package/dist-standalone/mdns-discovery.js +195 -0
- package/dist-standalone/nonce-store.d.ts +39 -0
- package/dist-standalone/nonce-store.js +62 -0
- package/dist-standalone/package.json +11 -0
- package/dist-standalone/pairing-manager.d.ts +147 -0
- package/dist-standalone/pairing-manager.js +219 -0
- package/dist-standalone/policy.d.ts +150 -0
- package/dist-standalone/policy.js +315 -0
- package/dist-standalone/redis-nonce-store.d.ts +93 -0
- package/dist-standalone/redis-nonce-store.js +72 -0
- package/dist-standalone/registry-middleware.d.ts +38 -0
- package/dist-standalone/registry-middleware.js +47 -0
- package/dist-standalone/retry-transport.d.ts +76 -0
- package/dist-standalone/retry-transport.js +98 -0
- package/dist-standalone/security-policy.d.ts +146 -0
- package/dist-standalone/security-policy.js +198 -0
- package/dist-standalone/split-channel.d.ts +69 -0
- package/dist-standalone/split-channel.js +171 -0
- package/dist-standalone/subscription-proof.d.ts +103 -0
- package/dist-standalone/subscription-proof.js +224 -0
- package/dist-standalone/succession.d.ts +57 -0
- package/dist-standalone/succession.js +142 -0
- package/dist-standalone/transport.d.ts +50 -0
- package/dist-standalone/transport.js +59 -0
- package/dist-standalone/trust-registry.d.ts +286 -0
- package/dist-standalone/trust-registry.js +702 -0
- package/dist-standalone/verify.d.ts +16 -0
- package/dist-standalone/verify.js +16 -0
- package/dist-standalone/xfetch.d.ts +129 -0
- package/dist-standalone/xfetch.js +247 -0
- package/llms.txt +800 -0
- package/package.json +79 -0
- package/share1.dat +0 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module pairing-manager
|
|
4
|
+
* Security validation for peer-to-peer device pairing via mDNS.
|
|
5
|
+
*
|
|
6
|
+
* Security properties:
|
|
7
|
+
* - 16-byte cryptographically secure nonce (prevents replay attacks)
|
|
8
|
+
* - 60-second timeout (limits attack window)
|
|
9
|
+
* - User confirmation required on BOTH devices
|
|
10
|
+
* - Nonce echo in response (prevents MITM substitution)
|
|
11
|
+
* - Trust registry integration (stores paired DIDs with scopes)
|
|
12
|
+
* - Gold Standard compliant (GS-CONNECT requirement #26, GS-SEC-RNG compliant)
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.PairingManager = void 0;
|
|
16
|
+
const shared_1 = require("../_deps/shared/index.js");
|
|
17
|
+
/**
|
|
18
|
+
* Pairing manager for secure peer-to-peer device pairing.
|
|
19
|
+
*
|
|
20
|
+
* Implements challenge-response protocol with user confirmation.
|
|
21
|
+
* Integrates with trust registry for persistent pairing storage.
|
|
22
|
+
*/
|
|
23
|
+
class PairingManager {
|
|
24
|
+
pendingNonces = new Map(); // nonce → timestamp
|
|
25
|
+
registry;
|
|
26
|
+
deviceDid;
|
|
27
|
+
/**
|
|
28
|
+
* Create pairing manager.
|
|
29
|
+
*
|
|
30
|
+
* @param deviceDid - DID of this device
|
|
31
|
+
* @param registry - Trust registry instance
|
|
32
|
+
*/
|
|
33
|
+
constructor(deviceDid, registry) {
|
|
34
|
+
this.deviceDid = deviceDid;
|
|
35
|
+
this.registry = registry;
|
|
36
|
+
// Clean up expired nonces every 10 seconds
|
|
37
|
+
setInterval(() => {
|
|
38
|
+
const now = Date.now();
|
|
39
|
+
for (const [nonce, timestamp] of this.pendingNonces.entries()) {
|
|
40
|
+
if (now - timestamp > 60_000) {
|
|
41
|
+
this.pendingNonces.delete(nonce);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}, 10_000);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create pairing request.
|
|
48
|
+
*
|
|
49
|
+
* Generates cryptographically secure nonce and stores it for validation.
|
|
50
|
+
* Device A calls this before sending request to Device B.
|
|
51
|
+
*
|
|
52
|
+
* @returns Pairing request to send to remote device
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const manager = new PairingManager('did:key:z6MkA...', registry);
|
|
57
|
+
* const request = manager.createRequest();
|
|
58
|
+
* // Send request to Device B via HTTP POST
|
|
59
|
+
* const response = await fetch(`${deviceB.endpoint}/pair`, {
|
|
60
|
+
* method: 'POST',
|
|
61
|
+
* body: JSON.stringify(request),
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
createRequest() {
|
|
66
|
+
// Generate cryptographically secure nonce (GS-SEC-RNG: NEVER use Math.random)
|
|
67
|
+
const nonceBytes = new Uint8Array(16);
|
|
68
|
+
if (typeof globalThis.crypto !== 'undefined' && globalThis.crypto.getRandomValues) {
|
|
69
|
+
globalThis.crypto.getRandomValues(nonceBytes);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// Node.js environment
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
74
|
+
const crypto = require('node:crypto');
|
|
75
|
+
crypto.getRandomValues(nonceBytes);
|
|
76
|
+
}
|
|
77
|
+
const nonce = Array.from(nonceBytes)
|
|
78
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
79
|
+
.join('');
|
|
80
|
+
const timestamp = Date.now();
|
|
81
|
+
// Store nonce for validation (60-second TTL)
|
|
82
|
+
this.pendingNonces.set(nonce, timestamp);
|
|
83
|
+
return {
|
|
84
|
+
device_a_did: this.deviceDid,
|
|
85
|
+
nonce,
|
|
86
|
+
timestamp,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Validate pairing request.
|
|
91
|
+
*
|
|
92
|
+
* Checks timeout and nonce uniqueness.
|
|
93
|
+
* Device B calls this when receiving request from Device A.
|
|
94
|
+
*
|
|
95
|
+
* @param request - Pairing request from remote device
|
|
96
|
+
* @returns Ok if valid, Err with reason if invalid
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* // Device B receives request
|
|
101
|
+
* const validation = manager.validateRequest(request);
|
|
102
|
+
* if (!validation.ok) {
|
|
103
|
+
* return res.status(400).json({ error: validation.error });
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
validateRequest(request) {
|
|
108
|
+
// Check timeout (60-second window)
|
|
109
|
+
const age = Date.now() - request.timestamp;
|
|
110
|
+
if (age > 60_000) {
|
|
111
|
+
return (0, shared_1.err)(`Request expired (${Math.floor(age / 1000)}s old, max 60s)`);
|
|
112
|
+
}
|
|
113
|
+
if (age < 0) {
|
|
114
|
+
return (0, shared_1.err)('Request timestamp in the future (clock skew detected)');
|
|
115
|
+
}
|
|
116
|
+
// Check required fields
|
|
117
|
+
if (!request.device_a_did || !request.nonce || !request.timestamp) {
|
|
118
|
+
return (0, shared_1.err)('Missing required fields (device_a_did, nonce, timestamp)');
|
|
119
|
+
}
|
|
120
|
+
return (0, shared_1.ok)(undefined);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Accept pairing request.
|
|
124
|
+
*
|
|
125
|
+
* Shows UI prompt for user confirmation, then stores in trust registry.
|
|
126
|
+
* Device B calls this after validating request.
|
|
127
|
+
*
|
|
128
|
+
* @param request - Validated pairing request
|
|
129
|
+
* @param promptCallback - Async function to show UI prompt (returns true if accepted)
|
|
130
|
+
* @returns Pairing response to send back to Device A
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const response = await manager.acceptPairing(request, async () => {
|
|
135
|
+
* // Show modal: "Device {name} wants to pair. Accept?"
|
|
136
|
+
* return await showConfirmationDialog({
|
|
137
|
+
* title: 'Pair with Device?',
|
|
138
|
+
* message: `DID: ${request.device_a_did.slice(0, 20)}...`,
|
|
139
|
+
* });
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* if (response.ok && response.value.accepted) {
|
|
143
|
+
* // Send response back to Device A
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
async acceptPairing(request, promptCallback) {
|
|
148
|
+
// Validate request first
|
|
149
|
+
const validation = this.validateRequest(request);
|
|
150
|
+
if (!validation.ok) {
|
|
151
|
+
return (0, shared_1.err)(validation.error);
|
|
152
|
+
}
|
|
153
|
+
// Prompt user for confirmation
|
|
154
|
+
const accepted = await promptCallback();
|
|
155
|
+
if (accepted) {
|
|
156
|
+
// Store in trust registry (Device A → Device B)
|
|
157
|
+
const registerResult = await this.registry.register(request.device_a_did, new Uint8Array(32), 'Paired Device', ['pairing']);
|
|
158
|
+
if (!registerResult.ok) {
|
|
159
|
+
const errorMsg = typeof registerResult.error === 'string'
|
|
160
|
+
? registerResult.error
|
|
161
|
+
: 'Unknown error';
|
|
162
|
+
return (0, shared_1.err)(`Failed to register pairing: ${errorMsg}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Return response (echo nonce to prevent MITM)
|
|
166
|
+
return (0, shared_1.ok)({
|
|
167
|
+
device_b_did: this.deviceDid,
|
|
168
|
+
accepted,
|
|
169
|
+
nonce: request.nonce,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Finalize pairing.
|
|
174
|
+
*
|
|
175
|
+
* Verifies response nonce and stores remote DID in trust registry.
|
|
176
|
+
* Device A calls this after receiving response from Device B.
|
|
177
|
+
*
|
|
178
|
+
* @param response - Pairing response from Device B
|
|
179
|
+
* @param expectedNonce - Nonce from original request (for verification)
|
|
180
|
+
* @returns Ok if successful, Err if nonce mismatch or storage failed
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* const request = manager.createRequest();
|
|
185
|
+
* const response = await sendPairingRequest(deviceB.endpoint, request);
|
|
186
|
+
*
|
|
187
|
+
* const result = await manager.finalizePairing(response, request.nonce);
|
|
188
|
+
* if (result.ok) {
|
|
189
|
+
* console.log('Pairing successful!');
|
|
190
|
+
* } else {
|
|
191
|
+
* console.error('Pairing failed:', result.error);
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
async finalizePairing(response, expectedNonce) {
|
|
196
|
+
// Verify nonce echo (prevents MITM substitution)
|
|
197
|
+
if (response.nonce !== expectedNonce) {
|
|
198
|
+
return (0, shared_1.err)('Nonce mismatch (possible MITM attack)');
|
|
199
|
+
}
|
|
200
|
+
// Check if pairing was accepted
|
|
201
|
+
if (!response.accepted) {
|
|
202
|
+
return (0, shared_1.err)('Pairing rejected by remote device');
|
|
203
|
+
}
|
|
204
|
+
// Remove nonce from pending set
|
|
205
|
+
this.pendingNonces.delete(expectedNonce);
|
|
206
|
+
// Store in trust registry (Device B → Device A)
|
|
207
|
+
const registerResult = await this.registry.register(response.device_b_did, new Uint8Array(32), 'Paired Device', ['pairing']);
|
|
208
|
+
if (!registerResult.ok) {
|
|
209
|
+
const errorMsg = typeof registerResult.error === 'string'
|
|
210
|
+
? registerResult.error
|
|
211
|
+
: 'Unknown error';
|
|
212
|
+
return (0, shared_1.err)(`Failed to register pairing: ${errorMsg}`);
|
|
213
|
+
}
|
|
214
|
+
return (0, shared_1.ok)(undefined);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Get pending nonce count (for debugging/monitoring).
|
|
218
|
+
*/
|
|
219
|
+
getPendingNonceCount() {
|
|
220
|
+
return this.pendingNonces.size;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
exports.PairingManager = PairingManager;
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module policy
|
|
4
|
+
* PolicyEngine for agent.call() constraint enforcement
|
|
5
|
+
*
|
|
6
|
+
* Enforces spending limits, rate limits, scope restrictions, and data filters
|
|
7
|
+
* on agent tool calls. Separate from SecurityPolicy (risk classification).
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.PolicyEngine = void 0;
|
|
11
|
+
exports.getGlobalPolicyEngine = getGlobalPolicyEngine;
|
|
12
|
+
const shared_1 = require("../_deps/shared/index.js");
|
|
13
|
+
const agent_call_js_1 = require("./agent-call.js");
|
|
14
|
+
/**
|
|
15
|
+
* PolicyEngine - Enforces constraints on agent.call() requests
|
|
16
|
+
*
|
|
17
|
+
* Tracks:
|
|
18
|
+
* - Per-transaction spending limits
|
|
19
|
+
* - Daily spending limits
|
|
20
|
+
* - Rate limits (calls per minute)
|
|
21
|
+
* - Allowed tools/scopes
|
|
22
|
+
*
|
|
23
|
+
* Thread-safe for concurrent requests from the same agent.
|
|
24
|
+
*/
|
|
25
|
+
class PolicyEngine {
|
|
26
|
+
/** Rate limit tracker by agent DID */
|
|
27
|
+
rateLimits = new Map();
|
|
28
|
+
/** Daily spending tracker by agent DID */
|
|
29
|
+
dailySpending = new Map();
|
|
30
|
+
/** Monthly spending tracker by agent DID */
|
|
31
|
+
monthlySpending = new Map();
|
|
32
|
+
/**
|
|
33
|
+
* Evaluate a policy against a request
|
|
34
|
+
*
|
|
35
|
+
* @param agentDID - Agent DID making the request
|
|
36
|
+
* @param tool - Tool being called (e.g., "stripe:createCharge")
|
|
37
|
+
* @param params - Tool parameters
|
|
38
|
+
* @param constraints - Policy constraints to enforce
|
|
39
|
+
* @returns Policy evaluation result
|
|
40
|
+
*/
|
|
41
|
+
evaluate(agentDID, tool, params, constraints) {
|
|
42
|
+
// Check allowed tools
|
|
43
|
+
if (constraints.allowedTools && constraints.allowedTools.length > 0) {
|
|
44
|
+
const [service] = tool.split(':');
|
|
45
|
+
const isAllowed = constraints.allowedTools.some((allowed) => allowed === tool || allowed === `${service}:*` || allowed === '*');
|
|
46
|
+
if (!isAllowed) {
|
|
47
|
+
const details = {
|
|
48
|
+
requested: tool,
|
|
49
|
+
allowed: constraints.allowedTools,
|
|
50
|
+
constraint: 'tool',
|
|
51
|
+
fix: `Update policy.allowedTools to include "${tool}" or "${service}:*" or request approval`,
|
|
52
|
+
};
|
|
53
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Tool "${tool}" is not allowed by policy`, details));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Check scopes
|
|
57
|
+
if (constraints.scopes && constraints.scopes.length > 0) {
|
|
58
|
+
// Extract scope from params if present
|
|
59
|
+
const scope = typeof params === 'object' && params !== null && 'scope' in params
|
|
60
|
+
? params.scope
|
|
61
|
+
: undefined;
|
|
62
|
+
if (scope && !constraints.scopes.includes(scope)) {
|
|
63
|
+
const details = {
|
|
64
|
+
requested: scope,
|
|
65
|
+
allowed: constraints.scopes,
|
|
66
|
+
constraint: 'scope',
|
|
67
|
+
fix: `Update policy.scopes to include "${scope}" or request additional permissions`,
|
|
68
|
+
};
|
|
69
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Scope "${scope}" is not allowed by policy`, details));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Check rate limits
|
|
73
|
+
if (constraints.limits?.callsPerMinute) {
|
|
74
|
+
const rateCheck = this.checkRateLimit(agentDID, constraints.limits.callsPerMinute);
|
|
75
|
+
if (!rateCheck.ok) {
|
|
76
|
+
return rateCheck;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Check spending limits (if amount is in params)
|
|
80
|
+
const amount = this.extractAmount(params);
|
|
81
|
+
if (amount !== null) {
|
|
82
|
+
// Check per-transaction limit
|
|
83
|
+
if (constraints.limits?.amountPerTxn && amount > constraints.limits.amountPerTxn) {
|
|
84
|
+
const details = {
|
|
85
|
+
requested: amount,
|
|
86
|
+
allowed: constraints.limits.amountPerTxn,
|
|
87
|
+
constraint: 'amountPerTxn',
|
|
88
|
+
fix: `Reduce amount to ${constraints.limits.amountPerTxn} or less, or update policy.limits.amountPerTxn`,
|
|
89
|
+
};
|
|
90
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Amount ${amount} exceeds per-transaction limit of ${constraints.limits.amountPerTxn}`, details));
|
|
91
|
+
}
|
|
92
|
+
// Check daily limit
|
|
93
|
+
if (constraints.limits?.dailyAmount) {
|
|
94
|
+
const dailyCheck = this.checkDailyLimit(agentDID, amount, constraints.limits.dailyAmount);
|
|
95
|
+
if (!dailyCheck.ok) {
|
|
96
|
+
return dailyCheck;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// Check monthly limit
|
|
100
|
+
if (constraints.limits?.monthlyAmount) {
|
|
101
|
+
const monthlyCheck = this.checkMonthlyLimit(agentDID, amount, constraints.limits.monthlyAmount);
|
|
102
|
+
if (!monthlyCheck.ok) {
|
|
103
|
+
return monthlyCheck;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// All checks passed
|
|
108
|
+
return (0, shared_1.ok)(undefined);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Record a successful call (for rate limiting)
|
|
112
|
+
*
|
|
113
|
+
* @param agentDID - Agent DID
|
|
114
|
+
*/
|
|
115
|
+
recordCall(agentDID) {
|
|
116
|
+
const now = Date.now();
|
|
117
|
+
const entry = this.rateLimits.get(agentDID) ?? {
|
|
118
|
+
calls: [],
|
|
119
|
+
windowStart: now,
|
|
120
|
+
};
|
|
121
|
+
entry.calls.push(now);
|
|
122
|
+
// Clean up old calls (older than 1 minute)
|
|
123
|
+
const oneMinuteAgo = now - 60000;
|
|
124
|
+
entry.calls = entry.calls.filter((timestamp) => timestamp > oneMinuteAgo);
|
|
125
|
+
this.rateLimits.set(agentDID, entry);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Record successful spending (for daily and monthly limits)
|
|
129
|
+
*
|
|
130
|
+
* @param agentDID - Agent DID
|
|
131
|
+
* @param amount - Amount spent
|
|
132
|
+
*/
|
|
133
|
+
recordSpending(agentDID, amount) {
|
|
134
|
+
const today = this.getCurrentDay();
|
|
135
|
+
const thisMonth = this.getCurrentMonth();
|
|
136
|
+
// Update daily spending
|
|
137
|
+
const dailyEntry = this.dailySpending.get(agentDID);
|
|
138
|
+
if (dailyEntry && dailyEntry.day === today) {
|
|
139
|
+
dailyEntry.amount += amount;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.dailySpending.set(agentDID, {
|
|
143
|
+
amount,
|
|
144
|
+
day: today,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
// Update monthly spending
|
|
148
|
+
const monthlyEntry = this.monthlySpending.get(agentDID);
|
|
149
|
+
if (monthlyEntry && monthlyEntry.month === thisMonth) {
|
|
150
|
+
monthlyEntry.amount += amount;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this.monthlySpending.set(agentDID, {
|
|
154
|
+
amount,
|
|
155
|
+
month: thisMonth,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get current rate limit status for an agent
|
|
161
|
+
*
|
|
162
|
+
* @param agentDID - Agent DID
|
|
163
|
+
* @returns Calls in current minute
|
|
164
|
+
*/
|
|
165
|
+
getCurrentRateLimit(agentDID) {
|
|
166
|
+
const entry = this.rateLimits.get(agentDID);
|
|
167
|
+
if (!entry)
|
|
168
|
+
return 0;
|
|
169
|
+
const now = Date.now();
|
|
170
|
+
const oneMinuteAgo = now - 60000;
|
|
171
|
+
// Count calls in last minute
|
|
172
|
+
return entry.calls.filter((timestamp) => timestamp > oneMinuteAgo).length;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get current daily spending for an agent
|
|
176
|
+
*
|
|
177
|
+
* @param agentDID - Agent DID
|
|
178
|
+
* @returns Amount spent today
|
|
179
|
+
*/
|
|
180
|
+
getDailySpending(agentDID) {
|
|
181
|
+
const today = this.getCurrentDay();
|
|
182
|
+
const entry = this.dailySpending.get(agentDID);
|
|
183
|
+
if (!entry || entry.day !== today)
|
|
184
|
+
return 0;
|
|
185
|
+
return entry.amount;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Get current monthly spending for an agent
|
|
189
|
+
*
|
|
190
|
+
* @param agentDID - Agent DID
|
|
191
|
+
* @returns Amount spent this month
|
|
192
|
+
*/
|
|
193
|
+
getMonthlySpending(agentDID) {
|
|
194
|
+
const thisMonth = this.getCurrentMonth();
|
|
195
|
+
const entry = this.monthlySpending.get(agentDID);
|
|
196
|
+
if (!entry || entry.month !== thisMonth)
|
|
197
|
+
return 0;
|
|
198
|
+
return entry.amount;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Reset all limits for an agent (for testing)
|
|
202
|
+
*
|
|
203
|
+
* @param agentDID - Agent DID
|
|
204
|
+
*/
|
|
205
|
+
reset(agentDID) {
|
|
206
|
+
this.rateLimits.delete(agentDID);
|
|
207
|
+
this.dailySpending.delete(agentDID);
|
|
208
|
+
this.monthlySpending.delete(agentDID);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Check rate limit
|
|
212
|
+
*/
|
|
213
|
+
checkRateLimit(agentDID, limit) {
|
|
214
|
+
const current = this.getCurrentRateLimit(agentDID);
|
|
215
|
+
if (current >= limit) {
|
|
216
|
+
const details = {
|
|
217
|
+
requested: current + 1,
|
|
218
|
+
allowed: limit,
|
|
219
|
+
constraint: 'callsPerMinute',
|
|
220
|
+
current,
|
|
221
|
+
fix: `Wait before making additional calls, or update policy.limits.callsPerMinute to a higher value`,
|
|
222
|
+
};
|
|
223
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Rate limit exceeded: ${current}/${limit} calls per minute`, details));
|
|
224
|
+
}
|
|
225
|
+
return (0, shared_1.ok)(undefined);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Check daily spending limit
|
|
229
|
+
*/
|
|
230
|
+
checkDailyLimit(agentDID, amount, limit) {
|
|
231
|
+
const current = this.getDailySpending(agentDID);
|
|
232
|
+
const newTotal = current + amount;
|
|
233
|
+
if (newTotal > limit) {
|
|
234
|
+
const remaining = limit - current;
|
|
235
|
+
const details = {
|
|
236
|
+
requested: amount,
|
|
237
|
+
allowed: limit,
|
|
238
|
+
constraint: 'dailyAmount',
|
|
239
|
+
current,
|
|
240
|
+
newTotal,
|
|
241
|
+
fix: remaining > 0
|
|
242
|
+
? `Reduce amount to ${remaining} or less (remaining today), or update policy.limits.dailyAmount`
|
|
243
|
+
: `Daily limit already reached. Wait until tomorrow or update policy.limits.dailyAmount`,
|
|
244
|
+
};
|
|
245
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Amount ${amount} would exceed daily limit: ${newTotal}/${limit} (current: ${current})`, details));
|
|
246
|
+
}
|
|
247
|
+
return (0, shared_1.ok)(undefined);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Extract amount from params
|
|
251
|
+
*/
|
|
252
|
+
extractAmount(params) {
|
|
253
|
+
if (typeof params !== 'object' || params === null)
|
|
254
|
+
return null;
|
|
255
|
+
const obj = params;
|
|
256
|
+
// Check common amount field names
|
|
257
|
+
if (typeof obj.amount === 'number')
|
|
258
|
+
return obj.amount;
|
|
259
|
+
if (typeof obj.value === 'number')
|
|
260
|
+
return obj.value;
|
|
261
|
+
if (typeof obj.price === 'number')
|
|
262
|
+
return obj.price;
|
|
263
|
+
if (typeof obj.total === 'number')
|
|
264
|
+
return obj.total;
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Get current day identifier (YYYY-MM-DD in UTC)
|
|
269
|
+
*/
|
|
270
|
+
getCurrentDay() {
|
|
271
|
+
const now = new Date();
|
|
272
|
+
return now.toISOString().split('T')[0] ?? '';
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Get current month identifier (YYYY-MM in UTC)
|
|
276
|
+
*/
|
|
277
|
+
getCurrentMonth() {
|
|
278
|
+
const now = new Date();
|
|
279
|
+
const iso = now.toISOString();
|
|
280
|
+
return iso.substring(0, 7); // YYYY-MM
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Check monthly spending limit
|
|
284
|
+
*/
|
|
285
|
+
checkMonthlyLimit(agentDID, amount, limit) {
|
|
286
|
+
const current = this.getMonthlySpending(agentDID);
|
|
287
|
+
const newTotal = current + amount;
|
|
288
|
+
if (newTotal > limit) {
|
|
289
|
+
const remaining = limit - current;
|
|
290
|
+
const details = {
|
|
291
|
+
requested: amount,
|
|
292
|
+
allowed: limit,
|
|
293
|
+
constraint: 'dailyAmount', // Reuse constraint type (will add monthlyAmount later)
|
|
294
|
+
current,
|
|
295
|
+
newTotal,
|
|
296
|
+
fix: remaining > 0
|
|
297
|
+
? `Reduce amount to ${remaining} or less (remaining this month), or update policy.limits.monthlyAmount`
|
|
298
|
+
: `Monthly limit already reached. Wait until next month or update policy.limits.monthlyAmount`,
|
|
299
|
+
};
|
|
300
|
+
return (0, shared_1.err)(new agent_call_js_1.AgentError(agent_call_js_1.AgentErrorCode.POLICY_VIOLATION, `Amount ${amount} would exceed monthly limit: ${newTotal}/${limit} (current: ${current})`, details));
|
|
301
|
+
}
|
|
302
|
+
return (0, shared_1.ok)(undefined);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
exports.PolicyEngine = PolicyEngine;
|
|
306
|
+
/**
|
|
307
|
+
* Global policy engine instance (singleton)
|
|
308
|
+
*/
|
|
309
|
+
let globalPolicyEngine = null;
|
|
310
|
+
/**
|
|
311
|
+
* Get the global policy engine instance
|
|
312
|
+
*
|
|
313
|
+
* @returns PolicyEngine instance
|
|
314
|
+
*/
|
|
315
|
+
function getGlobalPolicyEngine() {
|
|
316
|
+
if (!globalPolicyEngine) {
|
|
317
|
+
globalPolicyEngine = new PolicyEngine();
|
|
318
|
+
}
|
|
319
|
+
return globalPolicyEngine;
|
|
320
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* -- RedisNonceStore -- Cross-Node Replay Prevention -- */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.RedisNonceStore = void 0;
|
|
5
|
+
/** Default TTL for nonce entries: 600 seconds (10 minutes). */
|
|
6
|
+
const DEFAULT_TTL_SECONDS = 600;
|
|
7
|
+
/** Default key prefix for Redis nonce keys. */
|
|
8
|
+
const DEFAULT_KEY_PREFIX = 'nonce:';
|
|
9
|
+
/**
|
|
10
|
+
* Redis-backed nonce store for multi-node deployments.
|
|
11
|
+
*
|
|
12
|
+
* Uses Redis SET NX with TTL for atomic, distributed nonce deduplication.
|
|
13
|
+
* Nonce expiry is handled by Redis TTL -- no manual cleanup needed.
|
|
14
|
+
* Zero npm dependencies: users provide their own RedisClient implementation.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { RedisNonceStore } from '@private.me/xbind';
|
|
19
|
+
* import Redis from 'ioredis';
|
|
20
|
+
*
|
|
21
|
+
* const redis = new Redis();
|
|
22
|
+
* const store = new RedisNonceStore({
|
|
23
|
+
* client: {
|
|
24
|
+
* setNX: (key, value, ttl) =>
|
|
25
|
+
* redis.set(key, value, 'EX', ttl, 'NX'),
|
|
26
|
+
* del: (key) => redis.del(key),
|
|
27
|
+
* quit: () => redis.quit(),
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
class RedisNonceStore {
|
|
33
|
+
client;
|
|
34
|
+
ttlSeconds;
|
|
35
|
+
keyPrefix;
|
|
36
|
+
constructor(opts) {
|
|
37
|
+
this.client = opts.client;
|
|
38
|
+
this.ttlSeconds = opts.ttlSeconds ?? DEFAULT_TTL_SECONDS;
|
|
39
|
+
this.keyPrefix = opts.keyPrefix ?? DEFAULT_KEY_PREFIX;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if a nonce is fresh and record it atomically via Redis SET NX.
|
|
43
|
+
*
|
|
44
|
+
* Uses Redis SET with NX (set-if-not-exists) and EX (TTL in seconds)
|
|
45
|
+
* for atomic, distributed deduplication. If the key already exists in
|
|
46
|
+
* any Redis-connected node, the nonce is rejected as a duplicate.
|
|
47
|
+
*
|
|
48
|
+
* @param nonce - The nonce string to check.
|
|
49
|
+
* @param senderDid - The DID of the sender.
|
|
50
|
+
* @returns true if the nonce is new (accepted), false if duplicate (rejected).
|
|
51
|
+
*/
|
|
52
|
+
async check(nonce, senderDid) {
|
|
53
|
+
const key = `${this.keyPrefix}${senderDid}:${nonce}`;
|
|
54
|
+
const result = await this.client.setNX(key, '1', this.ttlSeconds);
|
|
55
|
+
return result === 'OK';
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* No-op for Redis store -- TTL-based expiry is handled automatically.
|
|
59
|
+
*
|
|
60
|
+
* This method exists to satisfy the NonceStore interface. Redis keys
|
|
61
|
+
* expire based on the TTL set during setNX, so no manual cleanup is needed.
|
|
62
|
+
*/
|
|
63
|
+
cleanup() {
|
|
64
|
+
// No-op: Redis TTL handles expiry automatically
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Disconnect from Redis gracefully.
|
|
68
|
+
*
|
|
69
|
+
* Calls the underlying RedisClient quit() method to close the connection.
|
|
70
|
+
* After calling dispose(), the store should not be used.
|
|
71
|
+
*/
|
|
72
|
+
dispose() {
|
|
73
|
+
void this.client.quit();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.RedisNonceStore = RedisNonceStore;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Express middleware for protecting trust registry endpoints with bearer auth.
|
|
4
|
+
*
|
|
5
|
+
* GET/HEAD requests pass through (public reads). POST/PUT/DELETE require
|
|
6
|
+
* a valid Bearer token matching the configured admin token.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import express from 'express';
|
|
11
|
+
* import { createRegistryAuthMiddleware } from '@private.me/xbind';
|
|
12
|
+
*
|
|
13
|
+
* const app = express();
|
|
14
|
+
* app.use('/registry', createRegistryAuthMiddleware(process.env.REGISTRY_ADMIN_TOKEN!));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.createRegistryAuthMiddleware = createRegistryAuthMiddleware;
|
|
19
|
+
/**
|
|
20
|
+
* Create an Express-compatible middleware that protects write operations
|
|
21
|
+
* on registry endpoints with bearer token authentication.
|
|
22
|
+
*
|
|
23
|
+
* GET and HEAD requests pass through without authentication.
|
|
24
|
+
* POST, PUT, and DELETE require `Authorization: Bearer <token>`.
|
|
25
|
+
*
|
|
26
|
+
* @param token - The admin token to validate against.
|
|
27
|
+
* @returns Express middleware function.
|
|
28
|
+
*/
|
|
29
|
+
function createRegistryAuthMiddleware(token) {
|
|
30
|
+
return (req, res, next) => {
|
|
31
|
+
const method = req.method.toUpperCase();
|
|
32
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
33
|
+
next();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const authHeader = typeof req.headers['authorization'] === 'string'
|
|
37
|
+
? req.headers['authorization']
|
|
38
|
+
: undefined;
|
|
39
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
40
|
+
res.status(401).json({ error: 'UNAUTHORIZED' });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const provided = authHeader.slice('Bearer '.length);
|
|
44
|
+
if (provided !== token) {
|
|
45
|
+
res.status(401).json({ error: 'UNAUTHORIZED' });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
next();
|
|
49
|
+
};
|
|
50
|
+
}
|