7h3-protocol 0.4.0__tar.gz
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.
- 7h3_protocol-0.4.0/.gitignore +6 -0
- 7h3_protocol-0.4.0/PKG-INFO +47 -0
- 7h3_protocol-0.4.0/README.md +18 -0
- 7h3_protocol-0.4.0/protocol_7h3/__init__.py +46 -0
- 7h3_protocol-0.4.0/protocol_7h3/http.py +212 -0
- 7h3_protocol-0.4.0/protocol_7h3/keys.py +149 -0
- 7h3_protocol-0.4.0/protocol_7h3/protocol.py +525 -0
- 7h3_protocol-0.4.0/protocol_7h3/queue.py +118 -0
- 7h3_protocol-0.4.0/protocol_7h3/webhook.py +116 -0
- 7h3_protocol-0.4.0/pyproject.toml +40 -0
- 7h3_protocol-0.4.0/tests/test_conformance.py +110 -0
- 7h3_protocol-0.4.0/tests/test_http.py +305 -0
- 7h3_protocol-0.4.0/tests/test_keys.py +417 -0
- 7h3_protocol-0.4.0/tests/test_queue.py +120 -0
- 7h3_protocol-0.4.0/tests/test_webhook.py +345 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: 7h3-protocol
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: 7h3 Protocol — Python SDK. Deterministic, signed, replay-safe AI-to-AI messaging. Wire version 7h3/0.1.
|
|
5
|
+
Project-URL: Homepage, https://github.com/IceMasterT/7h3-protocol
|
|
6
|
+
Project-URL: Repository, https://github.com/IceMasterT/7h3-protocol
|
|
7
|
+
Project-URL: Documentation, https://github.com/IceMasterT/7h3-protocol/tree/main/sdk/python
|
|
8
|
+
Project-URL: Changelog, https://github.com/IceMasterT/7h3-protocol/blob/main/CHANGELOG.md
|
|
9
|
+
License: MIT
|
|
10
|
+
Keywords: 7h3,agent,ed25519,mcp,protocol,replay-protection,signing
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Internet
|
|
21
|
+
Classifier: Topic :: Security :: Cryptography
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Provides-Extra: crypto
|
|
25
|
+
Requires-Dist: cryptography>=41.0; extra == 'crypto'
|
|
26
|
+
Provides-Extra: nacl
|
|
27
|
+
Requires-Dist: pynacl>=1.5; extra == 'nacl'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# 7h3 Protocol AIP Python SDK (Skeleton)
|
|
31
|
+
|
|
32
|
+
Minimal reference SDK for `aip/0.1` parity with the TypeScript implementation.
|
|
33
|
+
|
|
34
|
+
## Included
|
|
35
|
+
|
|
36
|
+
- deterministic canonicalization
|
|
37
|
+
- HS256 HMAC signing and verification
|
|
38
|
+
- ED25519 signing and verification (requires `cryptography` package)
|
|
39
|
+
- compact wire encode/decode
|
|
40
|
+
- envelope validation helpers (version, required fields, TTL)
|
|
41
|
+
- conformance tests using shared vectors from `conformance/aip_v0_1.json`
|
|
42
|
+
|
|
43
|
+
## Run conformance tests
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
PYTHONPATH=sdk/python python3 -m unittest discover -s sdk/python/tests -v
|
|
47
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# 7h3 Protocol AIP Python SDK (Skeleton)
|
|
2
|
+
|
|
3
|
+
Minimal reference SDK for `aip/0.1` parity with the TypeScript implementation.
|
|
4
|
+
|
|
5
|
+
## Included
|
|
6
|
+
|
|
7
|
+
- deterministic canonicalization
|
|
8
|
+
- HS256 HMAC signing and verification
|
|
9
|
+
- ED25519 signing and verification (requires `cryptography` package)
|
|
10
|
+
- compact wire encode/decode
|
|
11
|
+
- envelope validation helpers (version, required fields, TTL)
|
|
12
|
+
- conformance tests using shared vectors from `conformance/aip_v0_1.json`
|
|
13
|
+
|
|
14
|
+
## Run conformance tests
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
PYTHONPATH=sdk/python python3 -m unittest discover -s sdk/python/tests -v
|
|
18
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from .protocol import ( # noqa: F401
|
|
2
|
+
canonicalize_envelope,
|
|
3
|
+
decode_envelope,
|
|
4
|
+
encode_envelope_compact,
|
|
5
|
+
sign_canonical_payload_ed25519,
|
|
6
|
+
sign_canonical_payload_hmac,
|
|
7
|
+
sign_envelope_ed25519,
|
|
8
|
+
sign_envelope_hmac,
|
|
9
|
+
validate_envelope,
|
|
10
|
+
verify_canonical_payload_ed25519,
|
|
11
|
+
verify_canonical_payload_hmac,
|
|
12
|
+
verify_envelope_ed25519,
|
|
13
|
+
verify_envelope_hmac,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
from .http import ( # noqa: F401
|
|
18
|
+
DEFAULT_HEADER, KeyRegistry, StaticKeyRegistry,
|
|
19
|
+
verify_http_envelope, sign_http_request, build_signed_request_headers,
|
|
20
|
+
)
|
|
21
|
+
except ImportError:
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
from .webhook import ( # noqa: F401
|
|
26
|
+
WEBHOOK_SIG_HEADER, WEBHOOK_TS_HEADER,
|
|
27
|
+
sign_webhook, sign_webhook_hmac, verify_webhook, verify_webhook_hmac, consume_webhook,
|
|
28
|
+
)
|
|
29
|
+
except ImportError:
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
from .queue import ( # noqa: F401
|
|
34
|
+
sign_queue_message, verify_queue_message, verify_queue_batch,
|
|
35
|
+
)
|
|
36
|
+
except ImportError:
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
from .keys import ( # noqa: F401
|
|
41
|
+
KeyEntry, WellKnownKeysDocument, ManagedKeyPair,
|
|
42
|
+
KeyRotationManager, RevocationRegistry,
|
|
43
|
+
fetch_well_known_keys,
|
|
44
|
+
)
|
|
45
|
+
except ImportError:
|
|
46
|
+
pass
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""HTTP binding for 7h3 Protocol — signs/verifies per-request envelopes."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import time
|
|
6
|
+
import uuid
|
|
7
|
+
import os
|
|
8
|
+
from typing import Any, Callable, Dict, Optional, Tuple
|
|
9
|
+
|
|
10
|
+
from .protocol import (
|
|
11
|
+
verify_envelope_ed25519,
|
|
12
|
+
verify_envelope_hmac,
|
|
13
|
+
sign_envelope_ed25519,
|
|
14
|
+
validate_envelope,
|
|
15
|
+
ProtocolDiagnostic,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
DEFAULT_HEADER = "x-7h3-envelope"
|
|
19
|
+
|
|
20
|
+
VerifyFailReason = str # 'missing-header' | 'malformed-envelope' | 'unknown-sender' | 'invalid-signature' | 'ttl-expired'
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class KeyRegistry:
|
|
24
|
+
"""Abstract key registry — subclass to implement custom lookup."""
|
|
25
|
+
|
|
26
|
+
def get_public_key(self, sender_id: str) -> Optional[str]:
|
|
27
|
+
raise NotImplementedError
|
|
28
|
+
|
|
29
|
+
def get_shared_secret(self, key_id: str) -> Optional[str]:
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class StaticKeyRegistry(KeyRegistry):
|
|
34
|
+
"""In-memory registry backed by a dict {sender_id: public_key_b64url}."""
|
|
35
|
+
|
|
36
|
+
def __init__(self, keys: Dict[str, str]):
|
|
37
|
+
self._keys = keys
|
|
38
|
+
|
|
39
|
+
def get_public_key(self, sender_id: str) -> Optional[str]:
|
|
40
|
+
return self._keys.get(sender_id)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _make_envelope(
|
|
44
|
+
sender: str,
|
|
45
|
+
ttl_ms: int,
|
|
46
|
+
body: Dict[str, Any],
|
|
47
|
+
recipient: Optional[str] = None,
|
|
48
|
+
) -> Dict[str, Any]:
|
|
49
|
+
"""Build an unsigned envelope dict."""
|
|
50
|
+
header: Dict[str, Any] = {
|
|
51
|
+
"version": "7h3/0.1",
|
|
52
|
+
"messageId": str(uuid.uuid4()),
|
|
53
|
+
"timestampMs": int(time.time() * 1000),
|
|
54
|
+
"ttlMs": ttl_ms,
|
|
55
|
+
"sender": sender,
|
|
56
|
+
"nonce": os.urandom(8).hex(),
|
|
57
|
+
}
|
|
58
|
+
if recipient is not None:
|
|
59
|
+
header["recipient"] = recipient
|
|
60
|
+
return {"header": header, "body": body}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def verify_http_envelope(
|
|
64
|
+
headers: Dict[str, str],
|
|
65
|
+
registry: KeyRegistry,
|
|
66
|
+
*,
|
|
67
|
+
header_name: str = DEFAULT_HEADER,
|
|
68
|
+
strict_ttl: bool = True,
|
|
69
|
+
) -> Tuple[bool, Optional[dict], Optional[VerifyFailReason]]:
|
|
70
|
+
"""
|
|
71
|
+
Verify a 7h3 envelope from HTTP headers.
|
|
72
|
+
Returns (ok, envelope_dict, reason).
|
|
73
|
+
reason is None on success, a VerifyFailReason string on failure.
|
|
74
|
+
"""
|
|
75
|
+
# Headers may arrive with mixed case — normalise
|
|
76
|
+
normalised = {k.lower(): v for k, v in headers.items()}
|
|
77
|
+
raw = normalised.get(header_name.lower())
|
|
78
|
+
if not raw:
|
|
79
|
+
return False, None, "missing-header"
|
|
80
|
+
|
|
81
|
+
try:
|
|
82
|
+
envelope = json.loads(raw)
|
|
83
|
+
except Exception:
|
|
84
|
+
return False, None, "malformed-envelope"
|
|
85
|
+
|
|
86
|
+
if not isinstance(envelope, dict) or "header" not in envelope or "signature" not in envelope:
|
|
87
|
+
return False, None, "malformed-envelope"
|
|
88
|
+
|
|
89
|
+
if strict_ttl:
|
|
90
|
+
now_ms = int(time.time() * 1000)
|
|
91
|
+
diags: list[ProtocolDiagnostic] = validate_envelope(envelope, now_ms=now_ms)
|
|
92
|
+
errors = [d for d in diags if d.level == "error"]
|
|
93
|
+
if errors:
|
|
94
|
+
return False, None, "ttl-expired"
|
|
95
|
+
|
|
96
|
+
sender = envelope.get("header", {}).get("sender", "")
|
|
97
|
+
alg = envelope.get("signature", {}).get("alg", "")
|
|
98
|
+
|
|
99
|
+
if alg == "ED25519":
|
|
100
|
+
pub_key = registry.get_public_key(sender)
|
|
101
|
+
if not pub_key:
|
|
102
|
+
return False, None, "unknown-sender"
|
|
103
|
+
valid = verify_envelope_ed25519(envelope, pub_key)
|
|
104
|
+
if not valid:
|
|
105
|
+
return False, None, "invalid-signature"
|
|
106
|
+
elif alg == "HS256":
|
|
107
|
+
key_id = envelope.get("signature", {}).get("keyId", "")
|
|
108
|
+
secret = registry.get_shared_secret(key_id)
|
|
109
|
+
if not secret:
|
|
110
|
+
return False, None, "unknown-sender"
|
|
111
|
+
valid = verify_envelope_hmac(envelope, secret)
|
|
112
|
+
if not valid:
|
|
113
|
+
return False, None, "invalid-signature"
|
|
114
|
+
else:
|
|
115
|
+
return False, None, "malformed-envelope"
|
|
116
|
+
|
|
117
|
+
return True, envelope, None
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def sign_http_request(
|
|
121
|
+
envelope_without_sig: dict,
|
|
122
|
+
private_key: str,
|
|
123
|
+
*,
|
|
124
|
+
header_name: str = DEFAULT_HEADER,
|
|
125
|
+
) -> Dict[str, str]:
|
|
126
|
+
"""Sign an envelope and return HTTP headers dict to add to your request."""
|
|
127
|
+
signed = sign_envelope_ed25519(envelope_without_sig, private_key)
|
|
128
|
+
return {header_name: json.dumps(signed, separators=(",", ":"))}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def build_signed_request_headers(
|
|
132
|
+
sender: str,
|
|
133
|
+
private_key: str,
|
|
134
|
+
*,
|
|
135
|
+
recipient: Optional[str] = None,
|
|
136
|
+
ttl_ms: int = 60_000,
|
|
137
|
+
content: str = "",
|
|
138
|
+
header_name: str = DEFAULT_HEADER,
|
|
139
|
+
) -> Dict[str, str]:
|
|
140
|
+
"""Convenience: build envelope + sign in one call. Returns headers dict."""
|
|
141
|
+
envelope = _make_envelope(
|
|
142
|
+
sender=sender,
|
|
143
|
+
ttl_ms=ttl_ms,
|
|
144
|
+
body={"intent": "TASK", "content": content},
|
|
145
|
+
recipient=recipient,
|
|
146
|
+
)
|
|
147
|
+
return sign_http_request(envelope, private_key, header_name=header_name)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
# --- Framework integrations ---
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def starlette_middleware_factory(registry: KeyRegistry, *, header_name: str = DEFAULT_HEADER):
|
|
154
|
+
"""
|
|
155
|
+
Returns an ASGI middleware class for Starlette/FastAPI.
|
|
156
|
+
|
|
157
|
+
Usage:
|
|
158
|
+
app.add_middleware(starlette_middleware_factory(registry))
|
|
159
|
+
"""
|
|
160
|
+
try:
|
|
161
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
162
|
+
from starlette.requests import Request
|
|
163
|
+
from starlette.responses import JSONResponse
|
|
164
|
+
except ImportError as e:
|
|
165
|
+
raise ImportError("starlette is required: pip install starlette") from e
|
|
166
|
+
|
|
167
|
+
class Protocol7h3Middleware(BaseHTTPMiddleware):
|
|
168
|
+
async def dispatch(self, request: Request, call_next: Callable) -> Any:
|
|
169
|
+
headers = dict(request.headers)
|
|
170
|
+
ok, envelope, reason = verify_http_envelope(
|
|
171
|
+
headers, registry, header_name=header_name
|
|
172
|
+
)
|
|
173
|
+
if not ok:
|
|
174
|
+
return JSONResponse(
|
|
175
|
+
{"error": "7h3: request verification failed", "reason": reason},
|
|
176
|
+
status_code=401,
|
|
177
|
+
)
|
|
178
|
+
request.state.envelope_7h3 = envelope
|
|
179
|
+
return await call_next(request)
|
|
180
|
+
|
|
181
|
+
return Protocol7h3Middleware
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def flask_before_request_factory(registry: KeyRegistry, *, header_name: str = DEFAULT_HEADER):
|
|
185
|
+
"""
|
|
186
|
+
Returns a Flask before_request handler.
|
|
187
|
+
|
|
188
|
+
Usage:
|
|
189
|
+
app.before_request(flask_before_request_factory(registry))
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
def before_request():
|
|
193
|
+
try:
|
|
194
|
+
from flask import request as flask_req, g
|
|
195
|
+
except ImportError as e:
|
|
196
|
+
raise ImportError("flask is required: pip install flask") from e
|
|
197
|
+
|
|
198
|
+
headers = dict(flask_req.headers)
|
|
199
|
+
ok, envelope, reason = verify_http_envelope(headers, registry, header_name=header_name)
|
|
200
|
+
if not ok:
|
|
201
|
+
from flask import make_response
|
|
202
|
+
|
|
203
|
+
resp = make_response(
|
|
204
|
+
json.dumps({"error": "7h3: request verification failed", "reason": reason}),
|
|
205
|
+
401,
|
|
206
|
+
)
|
|
207
|
+
resp.headers["Content-Type"] = "application/json"
|
|
208
|
+
return resp
|
|
209
|
+
g.envelope_7h3 = envelope
|
|
210
|
+
return None # continue
|
|
211
|
+
|
|
212
|
+
return before_request
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Key infrastructure for 7h3 Protocol — discovery, rotation, revocation."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
import json
|
|
4
|
+
import time
|
|
5
|
+
import threading
|
|
6
|
+
from typing import Dict, List, Optional, Any
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
|
|
9
|
+
WELL_KNOWN_PATH = "/.well-known/7h3-keys"
|
|
10
|
+
REVOCATION_PATH = "/.well-known/7h3-revoked"
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class KeyEntry:
|
|
14
|
+
id: str
|
|
15
|
+
algorithm: str # 'Ed25519'
|
|
16
|
+
public_key: str # SPKI base64url
|
|
17
|
+
created: int # Unix ms
|
|
18
|
+
expires: Optional[int] = None
|
|
19
|
+
revoked: bool = False
|
|
20
|
+
revoked_at: Optional[int] = None
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class WellKnownKeysDocument:
|
|
24
|
+
version: str # '7h3/0.1'
|
|
25
|
+
updated: int
|
|
26
|
+
keys: List[KeyEntry]
|
|
27
|
+
|
|
28
|
+
def to_json(self) -> str:
|
|
29
|
+
keys_list = []
|
|
30
|
+
for k in self.keys:
|
|
31
|
+
entry = {
|
|
32
|
+
"id": k.id, "algorithm": k.algorithm, "publicKey": k.public_key,
|
|
33
|
+
"created": k.created,
|
|
34
|
+
}
|
|
35
|
+
if k.expires is not None: entry["expires"] = k.expires
|
|
36
|
+
if k.revoked: entry["revoked"] = True
|
|
37
|
+
if k.revoked_at is not None: entry["revokedAt"] = k.revoked_at
|
|
38
|
+
keys_list.append(entry)
|
|
39
|
+
return json.dumps({"version": self.version, "updated": self.updated, "keys": keys_list}, separators=(",", ":"))
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def from_json(cls, data: str) -> "WellKnownKeysDocument":
|
|
43
|
+
d = json.loads(data)
|
|
44
|
+
keys = []
|
|
45
|
+
for k in d.get("keys", []):
|
|
46
|
+
keys.append(KeyEntry(
|
|
47
|
+
id=k["id"], algorithm=k["algorithm"], public_key=k["publicKey"],
|
|
48
|
+
created=k["created"], expires=k.get("expires"), revoked=k.get("revoked", False),
|
|
49
|
+
revoked_at=k.get("revokedAt"),
|
|
50
|
+
))
|
|
51
|
+
return cls(version=d["version"], updated=d["updated"], keys=keys)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def fetch_well_known_keys(base_url: str, *, timeout: float = 5.0) -> WellKnownKeysDocument:
|
|
55
|
+
"""Fetch the /.well-known/7h3-keys document from a base URL."""
|
|
56
|
+
import urllib.request
|
|
57
|
+
url = base_url.rstrip("/") + WELL_KNOWN_PATH
|
|
58
|
+
with urllib.request.urlopen(url, timeout=timeout) as resp:
|
|
59
|
+
data = resp.read().decode("utf-8")
|
|
60
|
+
return WellKnownKeysDocument.from_json(data)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass
|
|
64
|
+
class ManagedKeyPair:
|
|
65
|
+
id: str
|
|
66
|
+
public_key: str # SPKI base64url
|
|
67
|
+
private_key: str # PKCS8 base64url
|
|
68
|
+
created: int
|
|
69
|
+
expires_at: Optional[int] = None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class KeyRotationManager:
|
|
73
|
+
"""Manages Ed25519 key pairs with automatic rotation."""
|
|
74
|
+
|
|
75
|
+
def __init__(self, max_age_ms: int = 86_400_000, overlap_ms: int = 3_600_000):
|
|
76
|
+
self.max_age_ms = max_age_ms
|
|
77
|
+
self.overlap_ms = overlap_ms
|
|
78
|
+
self._keys: List[ManagedKeyPair] = []
|
|
79
|
+
self._lock = threading.Lock()
|
|
80
|
+
|
|
81
|
+
def add_key(self, pair: ManagedKeyPair) -> None:
|
|
82
|
+
with self._lock:
|
|
83
|
+
self._keys.append(pair)
|
|
84
|
+
|
|
85
|
+
def get_current_key(self) -> Optional[ManagedKeyPair]:
|
|
86
|
+
"""Return the most recently created non-expired key."""
|
|
87
|
+
now = int(time.time() * 1000)
|
|
88
|
+
with self._lock:
|
|
89
|
+
active = [k for k in self._keys if not k.expires_at or k.expires_at > now]
|
|
90
|
+
if not active:
|
|
91
|
+
return None
|
|
92
|
+
return sorted(active, key=lambda k: k.created, reverse=True)[0]
|
|
93
|
+
|
|
94
|
+
def rotate_if_needed(self) -> Optional[ManagedKeyPair]:
|
|
95
|
+
"""Generate a new key if the current one is too old.
|
|
96
|
+
|
|
97
|
+
NOTE: generate_ed25519_keypair does not exist in protocol.py.
|
|
98
|
+
Callers should supply keys externally via add_key() rather than
|
|
99
|
+
relying on this method.
|
|
100
|
+
"""
|
|
101
|
+
raise NotImplementedError(
|
|
102
|
+
"rotate_if_needed requires generate_ed25519_keypair which is not "
|
|
103
|
+
"implemented in protocol.py. Supply new keys externally via add_key()."
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def get_well_known_document(self) -> WellKnownKeysDocument:
|
|
107
|
+
now = int(time.time() * 1000)
|
|
108
|
+
with self._lock:
|
|
109
|
+
entries = []
|
|
110
|
+
for k in self._keys:
|
|
111
|
+
entry = KeyEntry(
|
|
112
|
+
id=k.id, algorithm="Ed25519", public_key=k.public_key,
|
|
113
|
+
created=k.created, expires=k.expires_at,
|
|
114
|
+
revoked=bool(k.expires_at and k.expires_at < now),
|
|
115
|
+
)
|
|
116
|
+
entries.append(entry)
|
|
117
|
+
return WellKnownKeysDocument(version="7h3/0.1", updated=now, keys=entries)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class RevocationRegistry:
|
|
121
|
+
"""Tracks revoked key IDs."""
|
|
122
|
+
|
|
123
|
+
def __init__(self):
|
|
124
|
+
self._revoked: Dict[str, Dict[str, Any]] = {}
|
|
125
|
+
self._lock = threading.Lock()
|
|
126
|
+
|
|
127
|
+
def revoke(self, key_id: str, reason: Optional[str] = None) -> None:
|
|
128
|
+
with self._lock:
|
|
129
|
+
self._revoked[key_id] = {"revokedAt": int(time.time() * 1000), "reason": reason}
|
|
130
|
+
|
|
131
|
+
def is_revoked(self, key_id: str) -> bool:
|
|
132
|
+
with self._lock:
|
|
133
|
+
return key_id in self._revoked
|
|
134
|
+
|
|
135
|
+
def get_list(self) -> dict:
|
|
136
|
+
now = int(time.time() * 1000)
|
|
137
|
+
with self._lock:
|
|
138
|
+
revoked = [
|
|
139
|
+
{"id": kid, "revokedAt": v["revokedAt"], **({"reason": v["reason"]} if v["reason"] else {})}
|
|
140
|
+
for kid, v in self._revoked.items()
|
|
141
|
+
]
|
|
142
|
+
return {"version": "7h3/0.1", "updated": now, "revokedKeys": revoked}
|
|
143
|
+
|
|
144
|
+
def import_list(self, revocation_list: dict) -> None:
|
|
145
|
+
with self._lock:
|
|
146
|
+
for entry in revocation_list.get("revokedKeys", []):
|
|
147
|
+
kid = entry["id"]
|
|
148
|
+
if kid not in self._revoked:
|
|
149
|
+
self._revoked[kid] = {"revokedAt": entry["revokedAt"], "reason": entry.get("reason")}
|