regent-httpsig 0.7.3__py3-none-any.whl → 0.8.0__py3-none-any.whl

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.
regent_httpsig/sfv.py CHANGED
@@ -5,14 +5,17 @@ Contains the pieces the upstream library is missing for the AI-agent profiles:
5
5
  - :class:`DictKeyComponentResolver` — RFC 9421 §2.1.2 ``;key=`` dictionary-member
6
6
  selection (needed for the current Web Bot Auth ``"signature-agent";key="agent2"``
7
7
  covered component; upstream resolves whole header values only).
8
- - :func:`parse_signature_agent` — both wire forms of ``Signature-Agent``:
9
- the draft -05 sf-dictionary AND the legacy bare sf-string OpenAI ships today.
8
+ - :func:`signature_agent_member` — both wire forms of ``Signature-Agent``:
9
+ the sf-dictionary of draft-ietf-webbotauth-httpsig-protocol-01 AND the legacy
10
+ bare sf-string OpenAI ships today; :func:`parse_covered_components` tells
11
+ which member a signature covers.
10
12
  - :class:`Message` / :class:`StaticKeyResolver` — the minimal request shape and
11
13
  key resolution the verifier needs.
12
14
  """
13
15
 
14
16
  from __future__ import annotations
15
17
 
18
+ import importlib
16
19
  from typing import Any
17
20
 
18
21
  from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
@@ -25,10 +28,14 @@ from http_message_signatures.structures import CaseInsensitiveDict
25
28
 
26
29
  # http-message-signatures 2.1.0 (19.10.2026) dropped its vendored `http_sfv` module for the
27
30
  # `http-sf` package, whose `compat` module keeps the same parse()/params/value API.
31
+ # Imported by name so the module is typed Any whichever one is installed: static
32
+ # imports need `type: ignore` comments that are unused on one side or the other,
33
+ # and strict mypy fails on an unused ignore.
34
+ http_sfv: Any
28
35
  try: # 2.1.0 and later
29
- from http_sf import compat as http_sfv # type: ignore[import-not-found,no-redef]
36
+ http_sfv = importlib.import_module("http_sf.compat")
30
37
  except ImportError: # 2.0.x
31
- from http_message_signatures import http_sfv # type: ignore[attr-defined,no-redef]
38
+ http_sfv = importlib.import_module("http_message_signatures.http_sfv")
32
39
 
33
40
  __all__ = [
34
41
  "ED25519",
@@ -41,15 +48,17 @@ __all__ = [
41
48
  "build_aauth_budget_header",
42
49
  "build_aauth_requirement",
43
50
  "build_person_token_requirement",
51
+ "parse_covered_components",
44
52
  "parse_signature_agent",
45
53
  "parse_signature_input",
46
54
  "parse_signature_key",
47
55
  "parse_signature_key_header",
56
+ "signature_agent_member",
48
57
  ]
49
58
 
50
59
  # The library ships no explicit re-exports (strict mypy: attr-defined) — alias once.
51
- SFDictionary = http_sfv.Dictionary # type: ignore[attr-defined]
52
- SFItem = http_sfv.Item # type: ignore[attr-defined]
60
+ SFDictionary = http_sfv.Dictionary
61
+ SFItem = http_sfv.Item
53
62
  ED25519 = algorithms.ED25519 # type: ignore[attr-defined]
54
63
 
55
64
 
@@ -110,8 +119,10 @@ class StaticKeyResolver(HTTPSignatureKeyResolver):
110
119
 
111
120
 
112
121
  def parse_signature_agent(value: str) -> str | None:
113
- """Extract the directory origin from ``Signature-Agent`` — sf-dictionary
114
- (draft -05) or bare sf-string (legacy, what OpenAI sends)."""
122
+ """The first URL in ``Signature-Agent`` — sf-dictionary (draft -05 and later)
123
+ or bare sf-string (legacy, what OpenAI sends). A convenience for logging:
124
+ the verifier itself attributes a signature only to the member that
125
+ signature covers (:func:`signature_agent_member`), never to "the first"."""
115
126
  value = value.strip()
116
127
  if not value:
117
128
  return None
@@ -186,6 +197,61 @@ def parse_signature_input(value: str) -> dict[str, tuple[list[str], dict[str, An
186
197
  return out
187
198
 
188
199
 
200
+ def parse_covered_components(value: str) -> dict[str, list[tuple[str, dict[str, Any]]]]:
201
+ """``Signature-Input`` → ``{label: [(component identifier, its parameters)]}``.
202
+ Unlike :func:`parse_signature_input` the component parameters are kept —
203
+ ``"signature-agent";key="sig1"`` → ``("signature-agent", {"key": "sig1"})`` —
204
+ which is how a verifier learns which ``Signature-Agent`` member a signature
205
+ covers. Malformed header → ``{}``."""
206
+ out: dict[str, list[tuple[str, dict[str, Any]]]] = {}
207
+ try:
208
+ node = SFDictionary()
209
+ node.parse(value.encode())
210
+ for label, member in node.items():
211
+ items = list(member) if hasattr(member, "data") else []
212
+ out[str(label)] = [
213
+ (str(_plain(getattr(item, "value", item))),
214
+ {str(k): _plain(v) for k, v in getattr(item, "params", {}).items()})
215
+ for item in items
216
+ ]
217
+ except Exception: # noqa: BLE001
218
+ return {}
219
+ return out
220
+
221
+
222
+ def signature_agent_member(value: str, key: str | None) -> str | None:
223
+ """The URL a ``Signature-Agent`` field gives for one member, or ``None``.
224
+
225
+ ``key=None`` reads the legacy bare sf-string form (the whole field is one
226
+ String). Otherwise the field is parsed as the -01 sf-dictionary and member
227
+ ``key`` is returned — only if it is a String Item without parameters
228
+ (draft-ietf-webbotauth-httpsig-protocol-01 §5.2.1: a verifier MUST ignore a
229
+ member that carries parameters; Tokens and other types are not URLs)."""
230
+ value = value.strip()
231
+ try:
232
+ if key is None:
233
+ if not value.startswith('"'):
234
+ return None
235
+ item = SFItem()
236
+ item.parse(value.encode())
237
+ node_value, params = item.value, item.params
238
+ else:
239
+ if value.startswith('"'):
240
+ return None
241
+ node = SFDictionary()
242
+ node.parse(value.encode())
243
+ if key not in node:
244
+ return None
245
+ member = node[key]
246
+ node_value, params = getattr(member, "value", None), member.params
247
+ except Exception: # noqa: BLE001 — malformed header = no member
248
+ return None
249
+ # `type(...) is str`: a String Item; Token and DisplayString are str subclasses.
250
+ if type(node_value) is not str or len(params):
251
+ return None
252
+ return node_value
253
+
254
+
189
255
  def build_person_token_requirement() -> str:
190
256
  """``AAuth-Requirement: requirement=person-token`` (-11 §6.4): what a resource
191
257
  answers a revoked or expired auth token with. The header takes no parameters."""