@7h3/protocol 0.5.0 → 0.5.1
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.md +6 -1317
- package/agentAdapter.d.ts +26 -0
- package/auditLog.d.ts +54 -0
- package/capability.d.ts +67 -0
- package/cborCodec.d.ts +34 -0
- package/conformanceVectors.d.ts +20 -0
- package/encryption.d.ts +85 -0
- package/envelopeCbor.d.ts +33 -0
- package/frameworkAdapters.d.ts +72 -0
- package/gateway.d.ts +53 -0
- package/grpcBinding.d.ts +29 -0
- package/httpBinding.d.ts +50 -0
- package/index.d.ts +39 -0
- package/index.js +3662 -0
- package/keyInfra.d.ts +64 -0
- package/keyRegistry.d.ts +9 -0
- package/keyRotation.d.ts +20 -0
- package/mcpGateway.d.ts +37 -0
- package/mcpTransports.d.ts +62 -0
- package/mcpWrapper.d.ts +83 -0
- package/otel.d.ts +65 -0
- package/package.json +13 -121
- package/policyEnforcer.d.ts +50 -0
- package/policyTelemetryFeedback.d.ts +11 -0
- package/{src/protocol.d.ts → protocol.d.ts} +1 -2
- package/protocolAgent.d.ts +58 -0
- package/protocolBinary.d.ts +8 -0
- package/protocolCapabilities.d.ts +24 -0
- package/protocolReplay.d.ts +35 -0
- package/protocolTransport.d.ts +73 -0
- package/queueBinding.d.ts +43 -0
- package/rateLimiter.d.ts +18 -0
- package/{src/redisClient.ts → redisClient.d.ts} +25 -75
- package/replayStores.d.ts +98 -0
- package/revocation.d.ts +71 -0
- package/routePolicy.d.ts +30 -0
- package/runtimePolicy.d.ts +24 -0
- package/runtimePolicyManager.d.ts +15 -0
- package/runtimePolicyPresets.d.ts +11 -0
- package/signedResponse.d.ts +21 -0
- package/stream.d.ts +59 -0
- package/telemetry.d.ts +79 -0
- package/webhookBinding.d.ts +30 -0
- package/wsBinding.d.ts +52 -0
- package/.dockerignore +0 -19
- package/.github/dependabot.yml +0 -32
- package/.github/workflows/ci.yml +0 -31
- package/.github/workflows/publish.yml +0 -59
- package/.github/workflows/scorecard.yml +0 -37
- package/7h3.example.yaml +0 -125
- package/CHANGELOG.md +0 -152
- package/CONTRIBUTING.md +0 -82
- package/Dockerfile +0 -73
- package/GOVERNANCE.md +0 -62
- package/SECURITY.md +0 -70
- package/bench-results/replay-cache-full-1777891033256.json +0 -10
- package/bench-results/replay-cache-full-1777896317488.json +0 -10
- package/bench-results/replay-cache-full-1777900993184.json +0 -10
- package/bench-results/replay-cache-full-1777901019285.json +0 -10
- package/bench-results/replay-cache-quick-1777870170126.json +0 -10
- package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
- package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
- package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
- package/bench-results/wire-codecs-full-1777891019803.json +0 -93
- package/bench-results/wire-codecs-full-1777896260964.json +0 -93
- package/bench-results/wire-codecs-full-1777901004247.json +0 -93
- package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
- package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
- package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
- package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
- package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
- package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
- package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
- package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
- package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
- package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
- package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
- package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
- package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
- package/bin/7h3.ts +0 -406
- package/conformance/7h3_v0_1.json +0 -77
- package/conformance/7h3_v0_1_binary.json +0 -20
- package/conformance/aip_v0_1_binary.json +0 -20
- package/docker-compose.yaml +0 -77
- package/docs/ADOPTION_PLAN.md +0 -120
- package/docs/AGENTS.md +0 -77
- package/docs/AIP_RFC_v0.1.md +0 -97
- package/docs/AI_DECISION_CARD.md +0 -122
- package/docs/AI_RUNTIME_POLICY.json +0 -126
- package/docs/AI_RUNTIME_POLICY.yaml +0 -110
- package/docs/BACKPRESSURE_TUNING.md +0 -65
- package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
- package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
- package/docs/BINARY_CODEC_BENCH.md +0 -23
- package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
- package/docs/CLOCK_SKEW_POLICY.md +0 -30
- package/docs/DISTRIBUTED_REPLAY.md +0 -142
- package/docs/FUZZ_CAMPAIGN.md +0 -121
- package/docs/GATEWAY.md +0 -195
- package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
- package/docs/KEY_REVOCATION.md +0 -69
- package/docs/MCP_WRAPPER.md +0 -159
- package/docs/MIGRATION_GUIDE.md +0 -40
- package/docs/OPERATORS.md +0 -184
- package/docs/PERF_REGRESSION_POLICY.md +0 -34
- package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
- package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
- package/docs/RELEASE_GATE.md +0 -25
- package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
- package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
- package/docs/TELEMETRY.md +0 -41
- package/docs/THREAT_MODEL.md +0 -89
- package/docs/VERSIONING_POLICY.md +0 -30
- package/docs/assets/banner-github.png +0 -0
- package/docs/assets/banner.png +0 -0
- package/docs/assets/banner.svg +0 -123
- package/eslint.config.js +0 -15
- package/fuzz/ts/harness-decode.ts +0 -136
- package/fuzz/ts/harness-verify.ts +0 -121
- package/fuzz/ts/run.ts +0 -35
- package/mcp-server/README.md +0 -38
- package/mcp-server/package-lock.json +0 -1187
- package/mcp-server/package.json +0 -35
- package/mcp-server/src/index.ts +0 -236
- package/mcp-server/tsconfig.json +0 -14
- package/scripts/aip-framework-quickstart.ts +0 -110
- package/scripts/aip-mcp-gateway.ts +0 -38
- package/scripts/aip-mcp-wrap-demo.ts +0 -72
- package/scripts/aip-quickstart.ts +0 -60
- package/scripts/bench-diff.ts +0 -118
- package/scripts/bench-protocol-e2e.ts +0 -937
- package/scripts/bench-protocol-openloop.ts +0 -1397
- package/scripts/bench-replay-cache.ts +0 -76
- package/scripts/bench-signature-profiles.ts +0 -180
- package/scripts/bench-wire-codecs.ts +0 -161
- package/scripts/build-binary-conformance.ts +0 -36
- package/scripts/build-release-dashboard.ts +0 -175
- package/scripts/canary-rollout.ts +0 -38
- package/scripts/mcpGatewayCli.test.ts +0 -116
- package/scripts/prepare-aip-package.ts +0 -88
- package/scripts/regen-conformance-sigs.ts +0 -18
- package/scripts/release-gate.ts +0 -19
- package/scripts/validate-runtime-policy.ts +0 -18
- package/sdk/browser/index.test.ts +0 -162
- package/sdk/browser/index.ts +0 -257
- package/sdk/browser/package.json +0 -13
- package/sdk/go/cbor.go +0 -551
- package/sdk/go/cbor_test.go +0 -232
- package/sdk/go/encryption.go +0 -280
- package/sdk/go/encryption_test.go +0 -318
- package/sdk/go/go.mod +0 -7
- package/sdk/go/go.sum +0 -4
- package/sdk/go/http.go +0 -135
- package/sdk/go/protocol.go +0 -324
- package/sdk/go/protocol_test.go +0 -334
- package/sdk/go/replay.go +0 -121
- package/sdk/go/replay_test.go +0 -149
- package/sdk/go/webhook.go +0 -136
- package/sdk/pq/package-lock.json +0 -1358
- package/sdk/pq/package.json +0 -42
- package/sdk/pq/src/index.test.ts +0 -143
- package/sdk/pq/src/index.ts +0 -166
- package/sdk/pq/tsconfig.json +0 -14
- package/sdk/pq/vitest.config.ts +0 -7
- package/sdk/python/README.md +0 -18
- package/sdk/python/protocol_7h3/__init__.py +0 -46
- package/sdk/python/protocol_7h3/encryption.py +0 -252
- package/sdk/python/protocol_7h3/http.py +0 -212
- package/sdk/python/protocol_7h3/keys.py +0 -149
- package/sdk/python/protocol_7h3/pq.py +0 -244
- package/sdk/python/protocol_7h3/protocol.py +0 -525
- package/sdk/python/protocol_7h3/queue.py +0 -118
- package/sdk/python/protocol_7h3/replay.py +0 -98
- package/sdk/python/protocol_7h3/webhook.py +0 -116
- package/sdk/python/pyproject.toml +0 -40
- package/sdk/python/tests/test_conformance.py +0 -110
- package/sdk/python/tests/test_encryption.py +0 -206
- package/sdk/python/tests/test_http.py +0 -305
- package/sdk/python/tests/test_keys.py +0 -417
- package/sdk/python/tests/test_queue.py +0 -120
- package/sdk/python/tests/test_webhook.py +0 -345
- package/sdk/rust/Cargo.lock +0 -371
- package/sdk/rust/Cargo.toml +0 -25
- package/sdk/rust/README.md +0 -31
- package/sdk/rust/fuzz/Cargo.toml +0 -29
- package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
- package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
- package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
- package/sdk/rust/src/http.rs +0 -145
- package/sdk/rust/src/keys.rs +0 -161
- package/sdk/rust/src/lib.rs +0 -688
- package/sdk/rust/src/queue.rs +0 -79
- package/sdk/rust/src/webhook.rs +0 -86
- package/sdk/rust/tests/conformance.rs +0 -148
- package/sdk/rust/tests/gateway.rs +0 -130
- package/sdk/rust/tests/http_webhook_queue.rs +0 -201
- package/sdk/rust/tests/keys.rs +0 -189
- package/sdk/threshold/index.d.ts +0 -68
- package/sdk/threshold/index.d.ts.map +0 -1
- package/sdk/threshold/index.js +0 -254
- package/sdk/threshold/package-lock.json +0 -1361
- package/sdk/threshold/package.json +0 -39
- package/sdk/threshold/src/index.d.ts +0 -68
- package/sdk/threshold/src/index.d.ts.map +0 -1
- package/sdk/threshold/src/index.js +0 -254
- package/sdk/threshold/src/index.test.ts +0 -238
- package/sdk/threshold/src/index.ts +0 -355
- package/sdk/threshold/tsconfig.json +0 -19
- package/sdk/threshold/vitest.config.ts +0 -12
- package/src/agentAdapter.test.ts +0 -48
- package/src/agentAdapter.ts +0 -56
- package/src/auditLog.test.ts +0 -145
- package/src/auditLog.ts +0 -147
- package/src/capability.test.ts +0 -504
- package/src/capability.ts +0 -380
- package/src/cborCodec.test.ts +0 -263
- package/src/cborCodec.ts +0 -339
- package/src/conformance.test.ts +0 -136
- package/src/conformanceVectors.ts +0 -99
- package/src/encryption.test.ts +0 -206
- package/src/encryption.ts +0 -245
- package/src/envelopeCbor.ts +0 -140
- package/src/frameworkAdapters.test.ts +0 -290
- package/src/frameworkAdapters.ts +0 -261
- package/src/gateway.test.ts +0 -343
- package/src/gateway.ts +0 -246
- package/src/grpcBinding.test.ts +0 -211
- package/src/grpcBinding.ts +0 -103
- package/src/httpBinding.test.ts +0 -376
- package/src/httpBinding.ts +0 -189
- package/src/index.ts +0 -39
- package/src/keyInfra.test.ts +0 -278
- package/src/keyInfra.ts +0 -228
- package/src/keyRegistry.ts +0 -59
- package/src/keyRotation.test.ts +0 -78
- package/src/keyRotation.ts +0 -72
- package/src/mcpGateway.test.ts +0 -129
- package/src/mcpGateway.ts +0 -250
- package/src/mcpTransports.test.ts +0 -92
- package/src/mcpTransports.ts +0 -169
- package/src/mcpWrapper.test.ts +0 -179
- package/src/mcpWrapper.ts +0 -206
- package/src/otel.ts +0 -136
- package/src/policyEnforcer.test.ts +0 -99
- package/src/policyEnforcer.ts +0 -169
- package/src/policyTelemetryFeedback.test.ts +0 -25
- package/src/policyTelemetryFeedback.ts +0 -38
- package/src/protocol.bench.ts +0 -37
- package/src/protocol.d.ts.map +0 -1
- package/src/protocol.js +0 -294
- package/src/protocol.test.ts +0 -155
- package/src/protocol.ts +0 -414
- package/src/protocolAgent.test.ts +0 -105
- package/src/protocolAgent.ts +0 -169
- package/src/protocolBinary.test.ts +0 -165
- package/src/protocolBinary.ts +0 -312
- package/src/protocolCapabilities.ts +0 -70
- package/src/protocolFuzz.advanced.test.ts +0 -235
- package/src/protocolFuzz.test.ts +0 -111
- package/src/protocolNegative.test.ts +0 -97
- package/src/protocolReplay.test.ts +0 -71
- package/src/protocolReplay.ts +0 -194
- package/src/protocolTransport.test.ts +0 -556
- package/src/protocolTransport.ts +0 -483
- package/src/queueBinding.test.ts +0 -130
- package/src/queueBinding.ts +0 -102
- package/src/rateLimiter.test.ts +0 -96
- package/src/rateLimiter.ts +0 -46
- package/src/redisIntegration.test.ts +0 -134
- package/src/replayStores.test.ts +0 -273
- package/src/replayStores.ts +0 -215
- package/src/revocation.test.ts +0 -98
- package/src/revocation.ts +0 -0
- package/src/routePolicy.test.ts +0 -87
- package/src/routePolicy.ts +0 -72
- package/src/runtimePolicy.test.ts +0 -49
- package/src/runtimePolicy.ts +0 -81
- package/src/runtimePolicyManager.test.ts +0 -29
- package/src/runtimePolicyManager.ts +0 -50
- package/src/runtimePolicyPresets.ts +0 -43
- package/src/signedResponse.test.ts +0 -111
- package/src/signedResponse.ts +0 -83
- package/src/stream.test.ts +0 -254
- package/src/stream.ts +0 -417
- package/src/telemetry.test.ts +0 -251
- package/src/telemetry.ts +0 -299
- package/src/webhookBinding.test.ts +0 -144
- package/src/webhookBinding.ts +0 -115
- package/src/wsBinding.test.ts +0 -221
- package/src/wsBinding.ts +0 -200
- package/tsconfig.json +0 -15
- package/tsconfig.lib.json +0 -23
- package/vite.lib.config.ts +0 -16
- package/vitest.config.ts +0 -11
package/sdk/rust/src/lib.rs
DELETED
|
@@ -1,688 +0,0 @@
|
|
|
1
|
-
pub mod http;
|
|
2
|
-
pub mod keys;
|
|
3
|
-
pub mod queue;
|
|
4
|
-
pub mod webhook;
|
|
5
|
-
|
|
6
|
-
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
|
7
|
-
use base64::Engine;
|
|
8
|
-
use ed25519_dalek::pkcs8::{DecodePrivateKey, DecodePublicKey};
|
|
9
|
-
use ed25519_dalek::{Signature as Ed25519Signature, Signer, SigningKey, Verifier, VerifyingKey};
|
|
10
|
-
use hmac::{Hmac, Mac};
|
|
11
|
-
use serde::{Deserialize, Serialize};
|
|
12
|
-
use serde_json::Value;
|
|
13
|
-
use sha2::Sha256;
|
|
14
|
-
|
|
15
|
-
type HmacSha256 = Hmac<Sha256>;
|
|
16
|
-
|
|
17
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
18
|
-
pub struct ProtocolHeader {
|
|
19
|
-
pub version: String,
|
|
20
|
-
#[serde(rename = "messageId")]
|
|
21
|
-
pub message_id: String,
|
|
22
|
-
#[serde(rename = "timestampMs")]
|
|
23
|
-
pub timestamp_ms: i64,
|
|
24
|
-
#[serde(rename = "ttlMs")]
|
|
25
|
-
pub ttl_ms: i64,
|
|
26
|
-
pub sender: String,
|
|
27
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
28
|
-
pub recipient: Option<String>,
|
|
29
|
-
pub nonce: String,
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
33
|
-
pub struct ProtocolBody {
|
|
34
|
-
pub intent: String,
|
|
35
|
-
pub content: String,
|
|
36
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
37
|
-
pub capability: Option<String>,
|
|
38
|
-
#[serde(rename = "correlationId", skip_serializing_if = "Option::is_none")]
|
|
39
|
-
pub correlation_id: Option<String>,
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
43
|
-
pub struct ProtocolSignature {
|
|
44
|
-
pub alg: String,
|
|
45
|
-
#[serde(rename = "keyId")]
|
|
46
|
-
pub key_id: String,
|
|
47
|
-
pub value: String,
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
51
|
-
pub struct ProtocolEnvelope {
|
|
52
|
-
pub header: ProtocolHeader,
|
|
53
|
-
pub body: ProtocolBody,
|
|
54
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
55
|
-
pub signature: Option<ProtocolSignature>,
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
59
|
-
pub struct ProtocolDiagnostic {
|
|
60
|
-
pub level: String,
|
|
61
|
-
pub message: String,
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
65
|
-
struct CompactSignature {
|
|
66
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
67
|
-
a: Option<String>,
|
|
68
|
-
k: String,
|
|
69
|
-
v: String,
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
73
|
-
struct CompactEnvelope {
|
|
74
|
-
v: String,
|
|
75
|
-
mid: String,
|
|
76
|
-
ts: i64,
|
|
77
|
-
ttl: i64,
|
|
78
|
-
s: String,
|
|
79
|
-
n: String,
|
|
80
|
-
i: String,
|
|
81
|
-
c: String,
|
|
82
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
83
|
-
r: Option<String>,
|
|
84
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
85
|
-
cap: Option<String>,
|
|
86
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
87
|
-
cid: Option<String>,
|
|
88
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
89
|
-
sig: Option<CompactSignature>,
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
fn json_string<T: Serialize>(value: &T) -> String {
|
|
93
|
-
serde_json::to_string(value).expect("json serialization should not fail")
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
pub fn canonicalize_envelope(envelope: &ProtocolEnvelope) -> String {
|
|
97
|
-
let mut body_parts: Vec<String> = Vec::new();
|
|
98
|
-
if let Some(capability) = &envelope.body.capability {
|
|
99
|
-
body_parts.push(format!("\"capability\":{}", json_string(capability)));
|
|
100
|
-
}
|
|
101
|
-
body_parts.push(format!(
|
|
102
|
-
"\"content\":{}",
|
|
103
|
-
json_string(&envelope.body.content)
|
|
104
|
-
));
|
|
105
|
-
if let Some(correlation_id) = &envelope.body.correlation_id {
|
|
106
|
-
body_parts.push(format!("\"correlationId\":{}", json_string(correlation_id)));
|
|
107
|
-
}
|
|
108
|
-
body_parts.push(format!("\"intent\":{}", json_string(&envelope.body.intent)));
|
|
109
|
-
|
|
110
|
-
let mut header_parts: Vec<String> = vec![
|
|
111
|
-
format!("\"messageId\":{}", json_string(&envelope.header.message_id)),
|
|
112
|
-
format!("\"nonce\":{}", json_string(&envelope.header.nonce)),
|
|
113
|
-
];
|
|
114
|
-
if let Some(recipient) = &envelope.header.recipient {
|
|
115
|
-
header_parts.push(format!("\"recipient\":{}", json_string(recipient)));
|
|
116
|
-
}
|
|
117
|
-
header_parts.push(format!(
|
|
118
|
-
"\"sender\":{}",
|
|
119
|
-
json_string(&envelope.header.sender)
|
|
120
|
-
));
|
|
121
|
-
header_parts.push(format!("\"timestampMs\":{}", envelope.header.timestamp_ms));
|
|
122
|
-
header_parts.push(format!("\"ttlMs\":{}", envelope.header.ttl_ms));
|
|
123
|
-
header_parts.push(format!(
|
|
124
|
-
"\"version\":{}",
|
|
125
|
-
json_string(&envelope.header.version)
|
|
126
|
-
));
|
|
127
|
-
|
|
128
|
-
format!(
|
|
129
|
-
"{{\"body\":{{{}}},\"header\":{{{}}}}}",
|
|
130
|
-
body_parts.join(","),
|
|
131
|
-
header_parts.join(",")
|
|
132
|
-
)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
pub fn sign_canonical_payload_hmac(canonical_payload: &str, secret: &str) -> String {
|
|
136
|
-
let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).expect("HMAC key should be valid");
|
|
137
|
-
mac.update(canonical_payload.as_bytes());
|
|
138
|
-
let digest = mac.finalize().into_bytes();
|
|
139
|
-
URL_SAFE_NO_PAD.encode(digest)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
pub fn verify_canonical_payload_hmac(
|
|
143
|
-
canonical_payload: &str,
|
|
144
|
-
signature: &str,
|
|
145
|
-
secret: &str,
|
|
146
|
-
) -> bool {
|
|
147
|
-
let expected = sign_canonical_payload_hmac(canonical_payload, secret);
|
|
148
|
-
expected == signature
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
pub fn sign_canonical_payload_ed25519(
|
|
152
|
-
canonical_payload: &str,
|
|
153
|
-
private_key_pkcs8_base64url: &str,
|
|
154
|
-
) -> Result<String, String> {
|
|
155
|
-
let private_der = URL_SAFE_NO_PAD
|
|
156
|
-
.decode(private_key_pkcs8_base64url)
|
|
157
|
-
.map_err(|_| "Invalid ED25519 private key encoding".to_string())?;
|
|
158
|
-
let signing_key = SigningKey::from_pkcs8_der(&private_der)
|
|
159
|
-
.map_err(|_| "Invalid ED25519 private key".to_string())?;
|
|
160
|
-
let signature = signing_key.sign(canonical_payload.as_bytes());
|
|
161
|
-
Ok(URL_SAFE_NO_PAD.encode(signature.to_bytes()))
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
pub fn verify_canonical_payload_ed25519(
|
|
165
|
-
canonical_payload: &str,
|
|
166
|
-
signature_base64url: &str,
|
|
167
|
-
public_key_spki_base64url: &str,
|
|
168
|
-
) -> Result<bool, String> {
|
|
169
|
-
let public_der = URL_SAFE_NO_PAD
|
|
170
|
-
.decode(public_key_spki_base64url)
|
|
171
|
-
.map_err(|_| "Invalid ED25519 public key encoding".to_string())?;
|
|
172
|
-
let verifying_key = VerifyingKey::from_public_key_der(&public_der)
|
|
173
|
-
.map_err(|_| "Invalid ED25519 public key".to_string())?;
|
|
174
|
-
|
|
175
|
-
let signature_raw = URL_SAFE_NO_PAD
|
|
176
|
-
.decode(signature_base64url)
|
|
177
|
-
.map_err(|_| "Invalid ED25519 signature encoding".to_string())?;
|
|
178
|
-
let signature = Ed25519Signature::from_slice(&signature_raw)
|
|
179
|
-
.map_err(|_| "Invalid ED25519 signature".to_string())?;
|
|
180
|
-
|
|
181
|
-
Ok(verifying_key
|
|
182
|
-
.verify(canonical_payload.as_bytes(), &signature)
|
|
183
|
-
.is_ok())
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
pub fn sign_envelope_hmac(
|
|
187
|
-
envelope: &ProtocolEnvelope,
|
|
188
|
-
secret: &str,
|
|
189
|
-
key_id: &str,
|
|
190
|
-
) -> ProtocolEnvelope {
|
|
191
|
-
let mut unsigned = envelope.clone();
|
|
192
|
-
unsigned.signature = None;
|
|
193
|
-
let canonical = canonicalize_envelope(&unsigned);
|
|
194
|
-
let signature = sign_canonical_payload_hmac(&canonical, secret);
|
|
195
|
-
let mut signed = unsigned;
|
|
196
|
-
signed.signature = Some(ProtocolSignature {
|
|
197
|
-
alg: "HS256".to_string(),
|
|
198
|
-
key_id: key_id.to_string(),
|
|
199
|
-
value: signature,
|
|
200
|
-
});
|
|
201
|
-
signed
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
pub fn verify_envelope_hmac(envelope: &ProtocolEnvelope, secret: &str) -> bool {
|
|
205
|
-
let Some(signature) = &envelope.signature else {
|
|
206
|
-
return false;
|
|
207
|
-
};
|
|
208
|
-
if signature.alg != "HS256" {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
let mut unsigned = envelope.clone();
|
|
212
|
-
unsigned.signature = None;
|
|
213
|
-
let canonical = canonicalize_envelope(&unsigned);
|
|
214
|
-
verify_canonical_payload_hmac(&canonical, &signature.value, secret)
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
pub fn sign_envelope_ed25519(
|
|
218
|
-
envelope: &ProtocolEnvelope,
|
|
219
|
-
private_key_pkcs8_base64url: &str,
|
|
220
|
-
key_id: &str,
|
|
221
|
-
) -> Result<ProtocolEnvelope, String> {
|
|
222
|
-
let mut unsigned = envelope.clone();
|
|
223
|
-
unsigned.signature = None;
|
|
224
|
-
let canonical = canonicalize_envelope(&unsigned);
|
|
225
|
-
let signature = sign_canonical_payload_ed25519(&canonical, private_key_pkcs8_base64url)?;
|
|
226
|
-
let mut signed = unsigned;
|
|
227
|
-
signed.signature = Some(ProtocolSignature {
|
|
228
|
-
alg: "ED25519".to_string(),
|
|
229
|
-
key_id: key_id.to_string(),
|
|
230
|
-
value: signature,
|
|
231
|
-
});
|
|
232
|
-
Ok(signed)
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
pub fn verify_envelope_ed25519(
|
|
236
|
-
envelope: &ProtocolEnvelope,
|
|
237
|
-
public_key_spki_base64url: &str,
|
|
238
|
-
) -> Result<bool, String> {
|
|
239
|
-
let Some(signature) = &envelope.signature else {
|
|
240
|
-
return Ok(false);
|
|
241
|
-
};
|
|
242
|
-
if signature.alg != "ED25519" {
|
|
243
|
-
return Ok(false);
|
|
244
|
-
}
|
|
245
|
-
let mut unsigned = envelope.clone();
|
|
246
|
-
unsigned.signature = None;
|
|
247
|
-
let canonical = canonicalize_envelope(&unsigned);
|
|
248
|
-
verify_canonical_payload_ed25519(&canonical, &signature.value, public_key_spki_base64url)
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
pub fn encode_envelope_compact(envelope: &ProtocolEnvelope) -> String {
|
|
252
|
-
let compact = CompactEnvelope {
|
|
253
|
-
v: envelope.header.version.clone(),
|
|
254
|
-
mid: envelope.header.message_id.clone(),
|
|
255
|
-
ts: envelope.header.timestamp_ms,
|
|
256
|
-
ttl: envelope.header.ttl_ms,
|
|
257
|
-
s: envelope.header.sender.clone(),
|
|
258
|
-
n: envelope.header.nonce.clone(),
|
|
259
|
-
i: envelope.body.intent.clone(),
|
|
260
|
-
c: envelope.body.content.clone(),
|
|
261
|
-
r: envelope.header.recipient.clone(),
|
|
262
|
-
cap: envelope.body.capability.clone(),
|
|
263
|
-
cid: envelope.body.correlation_id.clone(),
|
|
264
|
-
sig: envelope.signature.as_ref().map(|sig| CompactSignature {
|
|
265
|
-
a: Some(sig.alg.clone()),
|
|
266
|
-
k: sig.key_id.clone(),
|
|
267
|
-
v: sig.value.clone(),
|
|
268
|
-
}),
|
|
269
|
-
};
|
|
270
|
-
serde_json::to_string(&compact).expect("compact envelope serialization should not fail")
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
pub fn decode_envelope(raw: &str) -> Result<ProtocolEnvelope, String> {
|
|
274
|
-
let parsed: Value =
|
|
275
|
-
serde_json::from_str(raw).map_err(|_| "Invalid JSON payload".to_string())?;
|
|
276
|
-
|
|
277
|
-
if parsed.get("header").is_some() && parsed.get("body").is_some() {
|
|
278
|
-
return serde_json::from_value(parsed)
|
|
279
|
-
.map_err(|_| "Envelope JSON shape is not recognized".to_string());
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
let compact: CompactEnvelope = serde_json::from_value(parsed)
|
|
283
|
-
.map_err(|_| "Envelope JSON shape is not recognized".to_string())?;
|
|
284
|
-
if compact.v != "7h3/0.1" {
|
|
285
|
-
return Err("Envelope JSON shape is not recognized".to_string());
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
Ok(ProtocolEnvelope {
|
|
289
|
-
header: ProtocolHeader {
|
|
290
|
-
version: compact.v,
|
|
291
|
-
message_id: compact.mid,
|
|
292
|
-
timestamp_ms: compact.ts,
|
|
293
|
-
ttl_ms: compact.ttl,
|
|
294
|
-
sender: compact.s,
|
|
295
|
-
recipient: compact.r,
|
|
296
|
-
nonce: compact.n,
|
|
297
|
-
},
|
|
298
|
-
body: ProtocolBody {
|
|
299
|
-
intent: compact.i,
|
|
300
|
-
content: compact.c,
|
|
301
|
-
capability: compact.cap,
|
|
302
|
-
correlation_id: compact.cid,
|
|
303
|
-
},
|
|
304
|
-
signature: compact.sig.map(|sig| ProtocolSignature {
|
|
305
|
-
alg: sig.a.unwrap_or_else(|| "HS256".to_string()),
|
|
306
|
-
key_id: sig.k,
|
|
307
|
-
value: sig.v,
|
|
308
|
-
}),
|
|
309
|
-
})
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
pub fn validate_envelope(
|
|
313
|
-
envelope: &ProtocolEnvelope,
|
|
314
|
-
now_ms: Option<i64>,
|
|
315
|
-
) -> Vec<ProtocolDiagnostic> {
|
|
316
|
-
let mut diagnostics: Vec<ProtocolDiagnostic> = Vec::new();
|
|
317
|
-
|
|
318
|
-
if envelope.header.version != "7h3/0.1" {
|
|
319
|
-
diagnostics.push(ProtocolDiagnostic {
|
|
320
|
-
level: "error".to_string(),
|
|
321
|
-
message: format!("Unsupported protocol version '{}'", envelope.header.version),
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
if envelope.header.message_id.trim().is_empty() {
|
|
325
|
-
diagnostics.push(ProtocolDiagnostic {
|
|
326
|
-
level: "error".to_string(),
|
|
327
|
-
message: "Missing messageId".to_string(),
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
if envelope.header.sender.trim().is_empty() {
|
|
331
|
-
diagnostics.push(ProtocolDiagnostic {
|
|
332
|
-
level: "error".to_string(),
|
|
333
|
-
message: "Missing sender identity".to_string(),
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
if envelope.header.ttl_ms <= 0 {
|
|
337
|
-
diagnostics.push(ProtocolDiagnostic {
|
|
338
|
-
level: "error".to_string(),
|
|
339
|
-
message: "ttlMs must be greater than zero".to_string(),
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
if let Some(now) = now_ms {
|
|
343
|
-
if envelope.header.timestamp_ms + envelope.header.ttl_ms < now {
|
|
344
|
-
diagnostics.push(ProtocolDiagnostic {
|
|
345
|
-
level: "error".to_string(),
|
|
346
|
-
message: "Message TTL expired".to_string(),
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
if envelope.body.content.trim().is_empty() {
|
|
351
|
-
diagnostics.push(ProtocolDiagnostic {
|
|
352
|
-
level: "warning".to_string(),
|
|
353
|
-
message: "Empty content payload".to_string(),
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
diagnostics
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
361
|
-
#[serde(untagged)]
|
|
362
|
-
pub enum JsonRpcId {
|
|
363
|
-
Null,
|
|
364
|
-
String(String),
|
|
365
|
-
Number(i64),
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
impl JsonRpcId {
|
|
369
|
-
fn as_string(&self) -> String {
|
|
370
|
-
match self {
|
|
371
|
-
JsonRpcId::Null => "null".to_string(),
|
|
372
|
-
JsonRpcId::String(value) => value.clone(),
|
|
373
|
-
JsonRpcId::Number(value) => value.to_string(),
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
379
|
-
pub struct JsonRpcRequestLike {
|
|
380
|
-
pub jsonrpc: String,
|
|
381
|
-
pub id: JsonRpcId,
|
|
382
|
-
pub method: String,
|
|
383
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
384
|
-
pub params: Option<Value>,
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
388
|
-
pub struct JsonRpcError {
|
|
389
|
-
pub code: i64,
|
|
390
|
-
pub message: String,
|
|
391
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
392
|
-
pub data: Option<Value>,
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
396
|
-
pub struct JsonRpcResponseLike {
|
|
397
|
-
pub jsonrpc: String,
|
|
398
|
-
pub id: JsonRpcId,
|
|
399
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
400
|
-
pub result: Option<Value>,
|
|
401
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
402
|
-
pub error: Option<JsonRpcError>,
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
406
|
-
pub struct JsonRpcPolicyContext {
|
|
407
|
-
pub intent: String,
|
|
408
|
-
pub recipient: Option<String>,
|
|
409
|
-
pub capability: String,
|
|
410
|
-
pub correlation_id: String,
|
|
411
|
-
pub created_at_ms: i64,
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
#[derive(Debug, Clone)]
|
|
415
|
-
pub struct JsonRpcBridgeConfig {
|
|
416
|
-
pub recipient: Option<String>,
|
|
417
|
-
pub allowed_methods: Option<Vec<String>>,
|
|
418
|
-
pub capability_prefix: String,
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
impl Default for JsonRpcBridgeConfig {
|
|
422
|
-
fn default() -> Self {
|
|
423
|
-
Self {
|
|
424
|
-
recipient: None,
|
|
425
|
-
allowed_methods: None,
|
|
426
|
-
capability_prefix: "mcp.".to_string(),
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
pub fn create_envelope(
|
|
432
|
-
sender: &str,
|
|
433
|
-
recipient: Option<&str>,
|
|
434
|
-
intent: &str,
|
|
435
|
-
content: &str,
|
|
436
|
-
capability: Option<&str>,
|
|
437
|
-
correlation_id: Option<&str>,
|
|
438
|
-
now_ms: i64,
|
|
439
|
-
ttl_ms: i64,
|
|
440
|
-
) -> ProtocolEnvelope {
|
|
441
|
-
ProtocolEnvelope {
|
|
442
|
-
header: ProtocolHeader {
|
|
443
|
-
version: "7h3/0.1".to_string(),
|
|
444
|
-
message_id: format!("msg-{}-{}", now_ms, sender),
|
|
445
|
-
timestamp_ms: now_ms,
|
|
446
|
-
ttl_ms,
|
|
447
|
-
sender: sender.to_string(),
|
|
448
|
-
recipient: recipient.map(|value| value.to_string()),
|
|
449
|
-
nonce: format!("n-{}", now_ms),
|
|
450
|
-
},
|
|
451
|
-
body: ProtocolBody {
|
|
452
|
-
intent: intent.to_string(),
|
|
453
|
-
content: content.to_string(),
|
|
454
|
-
capability: capability.map(|value| value.to_string()),
|
|
455
|
-
correlation_id: correlation_id.map(|value| value.to_string()),
|
|
456
|
-
},
|
|
457
|
-
signature: None,
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
pub fn create_signed_task_from_jsonrpc<F, G>(
|
|
462
|
-
request: &JsonRpcRequestLike,
|
|
463
|
-
sender: &str,
|
|
464
|
-
secret: &str,
|
|
465
|
-
key_id: &str,
|
|
466
|
-
now_ms: i64,
|
|
467
|
-
config: &JsonRpcBridgeConfig,
|
|
468
|
-
authorize: Option<F>,
|
|
469
|
-
rate_limit: Option<G>,
|
|
470
|
-
) -> Result<ProtocolEnvelope, String>
|
|
471
|
-
where
|
|
472
|
-
F: Fn(&JsonRpcRequestLike, &JsonRpcPolicyContext) -> bool,
|
|
473
|
-
G: Fn(&str, &JsonRpcRequestLike, &JsonRpcPolicyContext) -> bool,
|
|
474
|
-
{
|
|
475
|
-
if request.jsonrpc != "2.0" {
|
|
476
|
-
return Err("JSON-RPC request must have jsonrpc='2.0'".to_string());
|
|
477
|
-
}
|
|
478
|
-
if request.method.trim().is_empty() {
|
|
479
|
-
return Err("JSON-RPC request must include method".to_string());
|
|
480
|
-
}
|
|
481
|
-
if let Some(allowlist) = &config.allowed_methods {
|
|
482
|
-
if !allowlist.iter().any(|method| method == &request.method) {
|
|
483
|
-
return Err(format!(
|
|
484
|
-
"JSON-RPC method '{}' is not allowed",
|
|
485
|
-
request.method
|
|
486
|
-
));
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
let correlation_id = request.id.as_string();
|
|
491
|
-
let capability = format!("{}{}", config.capability_prefix, request.method);
|
|
492
|
-
let context = JsonRpcPolicyContext {
|
|
493
|
-
intent: "TASK".to_string(),
|
|
494
|
-
recipient: config.recipient.clone(),
|
|
495
|
-
capability: capability.clone(),
|
|
496
|
-
correlation_id: correlation_id.clone(),
|
|
497
|
-
created_at_ms: now_ms,
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
if let Some(authorize_fn) = authorize {
|
|
501
|
-
if !authorize_fn(request, &context) {
|
|
502
|
-
return Err(format!(
|
|
503
|
-
"JSON-RPC method '{}' is not authorized",
|
|
504
|
-
request.method
|
|
505
|
-
));
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
let rate_key = request.method.clone();
|
|
510
|
-
if let Some(rate_limit_fn) = rate_limit {
|
|
511
|
-
if !rate_limit_fn(&rate_key, request, &context) {
|
|
512
|
-
return Err(format!(
|
|
513
|
-
"JSON-RPC method '{}' is rate-limited",
|
|
514
|
-
request.method
|
|
515
|
-
));
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
let content = json_string(&serde_json::json!({
|
|
520
|
-
"method": request.method,
|
|
521
|
-
"params": request.params.clone().unwrap_or(Value::Null),
|
|
522
|
-
}));
|
|
523
|
-
let envelope = create_envelope(
|
|
524
|
-
sender,
|
|
525
|
-
config.recipient.as_deref(),
|
|
526
|
-
"TASK",
|
|
527
|
-
&content,
|
|
528
|
-
Some(&capability),
|
|
529
|
-
Some(&correlation_id),
|
|
530
|
-
now_ms,
|
|
531
|
-
60_000,
|
|
532
|
-
);
|
|
533
|
-
Ok(sign_envelope_hmac(&envelope, secret, key_id))
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
pub fn jsonrpc_response_from_envelope(
|
|
537
|
-
envelope: &ProtocolEnvelope,
|
|
538
|
-
id: JsonRpcId,
|
|
539
|
-
) -> JsonRpcResponseLike {
|
|
540
|
-
if envelope.body.intent == "ERROR" {
|
|
541
|
-
return JsonRpcResponseLike {
|
|
542
|
-
jsonrpc: "2.0".to_string(),
|
|
543
|
-
id,
|
|
544
|
-
result: None,
|
|
545
|
-
error: Some(JsonRpcError {
|
|
546
|
-
code: -32000,
|
|
547
|
-
message: envelope.body.content.clone(),
|
|
548
|
-
data: Some(serde_json::json!({
|
|
549
|
-
"capability": envelope.body.capability,
|
|
550
|
-
"correlationId": envelope.body.correlation_id,
|
|
551
|
-
})),
|
|
552
|
-
}),
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
let result = serde_json::from_str::<Value>(&envelope.body.content)
|
|
557
|
-
.unwrap_or_else(|_| Value::String(envelope.body.content.clone()));
|
|
558
|
-
JsonRpcResponseLike {
|
|
559
|
-
jsonrpc: "2.0".to_string(),
|
|
560
|
-
id,
|
|
561
|
-
result: Some(result),
|
|
562
|
-
error: None,
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
#[derive(Debug, Clone)]
|
|
567
|
-
pub struct AipMcpGatewayOptions {
|
|
568
|
-
pub shared_secret: String,
|
|
569
|
-
pub gateway_agent_id: String,
|
|
570
|
-
pub worker_agent_id: String,
|
|
571
|
-
pub allowed_methods: Vec<String>,
|
|
572
|
-
pub capability_prefix: String,
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
impl Default for AipMcpGatewayOptions {
|
|
576
|
-
fn default() -> Self {
|
|
577
|
-
Self {
|
|
578
|
-
shared_secret: "mcp-gateway-secret".to_string(),
|
|
579
|
-
gateway_agent_id: "agent.gateway".to_string(),
|
|
580
|
-
worker_agent_id: "agent.worker".to_string(),
|
|
581
|
-
allowed_methods: vec![
|
|
582
|
-
"tools/call".to_string(),
|
|
583
|
-
"resources/read".to_string(),
|
|
584
|
-
"prompts/get".to_string(),
|
|
585
|
-
],
|
|
586
|
-
capability_prefix: "mcp.".to_string(),
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
#[derive(Debug, Clone)]
|
|
592
|
-
pub struct AipMcpGatewayRuntime {
|
|
593
|
-
options: AipMcpGatewayOptions,
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
pub fn create_aip_mcp_gateway_runtime(options: AipMcpGatewayOptions) -> AipMcpGatewayRuntime {
|
|
597
|
-
AipMcpGatewayRuntime { options }
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
impl AipMcpGatewayRuntime {
|
|
601
|
-
fn error_response(id: Option<JsonRpcId>, code: i64, message: &str) -> String {
|
|
602
|
-
let response = JsonRpcResponseLike {
|
|
603
|
-
jsonrpc: "2.0".to_string(),
|
|
604
|
-
id: id.unwrap_or(JsonRpcId::Null),
|
|
605
|
-
result: None,
|
|
606
|
-
error: Some(JsonRpcError {
|
|
607
|
-
code,
|
|
608
|
-
message: message.to_string(),
|
|
609
|
-
data: None,
|
|
610
|
-
}),
|
|
611
|
-
};
|
|
612
|
-
json_string(&response)
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
pub fn handle_line(&self, line: &str, now_ms: i64) -> Option<String> {
|
|
616
|
-
let raw = line.trim();
|
|
617
|
-
if raw.is_empty() {
|
|
618
|
-
return None;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
let request: JsonRpcRequestLike = match serde_json::from_str(raw) {
|
|
622
|
-
Ok(parsed) => parsed,
|
|
623
|
-
Err(_) => {
|
|
624
|
-
return Some(Self::error_response(None, -32600, "Invalid Request"));
|
|
625
|
-
}
|
|
626
|
-
};
|
|
627
|
-
|
|
628
|
-
let bridge_config = JsonRpcBridgeConfig {
|
|
629
|
-
recipient: Some(self.options.worker_agent_id.clone()),
|
|
630
|
-
allowed_methods: Some(self.options.allowed_methods.clone()),
|
|
631
|
-
capability_prefix: self.options.capability_prefix.clone(),
|
|
632
|
-
};
|
|
633
|
-
|
|
634
|
-
let outbound = match create_signed_task_from_jsonrpc(
|
|
635
|
-
&request,
|
|
636
|
-
&self.options.gateway_agent_id,
|
|
637
|
-
&self.options.shared_secret,
|
|
638
|
-
"gateway-k1",
|
|
639
|
-
now_ms,
|
|
640
|
-
&bridge_config,
|
|
641
|
-
Option::<fn(&JsonRpcRequestLike, &JsonRpcPolicyContext) -> bool>::None,
|
|
642
|
-
Option::<fn(&str, &JsonRpcRequestLike, &JsonRpcPolicyContext) -> bool>::None,
|
|
643
|
-
) {
|
|
644
|
-
Ok(envelope) => envelope,
|
|
645
|
-
Err(message) => {
|
|
646
|
-
let code = if message.contains("not allowed") {
|
|
647
|
-
-32601
|
|
648
|
-
} else if message.contains("not authorized") {
|
|
649
|
-
-32001
|
|
650
|
-
} else if message.contains("rate-limited") {
|
|
651
|
-
-32002
|
|
652
|
-
} else {
|
|
653
|
-
-32603
|
|
654
|
-
};
|
|
655
|
-
return Some(Self::error_response(Some(request.id), code, &message));
|
|
656
|
-
}
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
let worker_payload = serde_json::json!({
|
|
660
|
-
"ok": true,
|
|
661
|
-
"capability": outbound.body.capability,
|
|
662
|
-
"content": outbound.body.content,
|
|
663
|
-
});
|
|
664
|
-
let worker_envelope = create_envelope(
|
|
665
|
-
&self.options.worker_agent_id,
|
|
666
|
-
Some(&self.options.gateway_agent_id),
|
|
667
|
-
"RESULT",
|
|
668
|
-
&json_string(&worker_payload),
|
|
669
|
-
outbound.body.capability.as_deref(),
|
|
670
|
-
outbound.body.correlation_id.as_deref(),
|
|
671
|
-
now_ms,
|
|
672
|
-
60_000,
|
|
673
|
-
);
|
|
674
|
-
let signed_worker =
|
|
675
|
-
sign_envelope_hmac(&worker_envelope, &self.options.shared_secret, "worker-k1");
|
|
676
|
-
|
|
677
|
-
if !verify_envelope_hmac(&signed_worker, &self.options.shared_secret) {
|
|
678
|
-
return Some(Self::error_response(
|
|
679
|
-
Some(request.id),
|
|
680
|
-
-32603,
|
|
681
|
-
"AIP verification failed",
|
|
682
|
-
));
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
let response = jsonrpc_response_from_envelope(&signed_worker, request.id);
|
|
686
|
-
Some(json_string(&response))
|
|
687
|
-
}
|
|
688
|
-
}
|