aid-runtime-python 0.1.186__tar.gz → 0.1.188__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.
Files changed (136) hide show
  1. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/PKG-INFO +4 -1
  2. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/pyproject.toml +12 -1
  3. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/__init__.py +1 -1
  4. aid_runtime_python-0.1.188/src/aid_runtime/identity.py +320 -0
  5. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime_python.egg-info/PKG-INFO +4 -1
  6. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime_python.egg-info/SOURCES.txt +2 -0
  7. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime_python.egg-info/requires.txt +4 -0
  8. aid_runtime_python-0.1.188/tests/test_identity.py +445 -0
  9. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/README.md +0 -0
  10. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/setup.cfg +0 -0
  11. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/__init__.py +0 -0
  12. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/chat_model.py +0 -0
  13. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/deep_agents.py +0 -0
  14. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/engine.py +0 -0
  15. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/guardrails.py +0 -0
  16. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/ir.py +0 -0
  17. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/loop.py +0 -0
  18. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/managed.py +0 -0
  19. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/memory.py +0 -0
  20. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/policy.py +0 -0
  21. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/resume.py +0 -0
  22. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/runs.py +0 -0
  23. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/agents/state.py +0 -0
  24. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/cache.py +0 -0
  25. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/concurrency.py +0 -0
  26. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/control.py +0 -0
  27. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/cost.py +0 -0
  28. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/decisions.py +0 -0
  29. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/errors.py +0 -0
  30. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/__init__.py +0 -0
  31. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/agent_runner.py +0 -0
  32. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/dataset.py +0 -0
  33. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/gate.py +0 -0
  34. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/metrics.py +0 -0
  35. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/predicate.py +0 -0
  36. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/runner.py +0 -0
  37. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/evals/trajectory.py +0 -0
  38. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/gateway.py +0 -0
  39. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/local_trace.py +0 -0
  40. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/metrics.py +0 -0
  41. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/pipeline.py +0 -0
  42. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/prompts.py +0 -0
  43. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/__init__.py +0 -0
  44. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/anthropic.py +0 -0
  45. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/base.py +0 -0
  46. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/bedrock.py +0 -0
  47. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/fake.py +0 -0
  48. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/gemini.py +0 -0
  49. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/gemini_wire.py +0 -0
  50. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/http.py +0 -0
  51. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/recorded.py +0 -0
  52. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/providers/vertex.py +0 -0
  53. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/py.typed +0 -0
  54. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/redaction.py +0 -0
  55. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/__init__.py +0 -0
  56. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/chunking.py +0 -0
  57. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/citations.py +0 -0
  58. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/connectors.py +0 -0
  59. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/embedding.py +0 -0
  60. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/filters.py +0 -0
  61. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/gate.py +0 -0
  62. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/hybrid.py +0 -0
  63. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/knowledge_base.py +0 -0
  64. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/metrics.py +0 -0
  65. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/pgvector.py +0 -0
  66. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/rerank.py +0 -0
  67. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/retriever.py +0 -0
  68. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/stores.py +0 -0
  69. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/retrieval/types.py +0 -0
  70. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/steps.py +0 -0
  71. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/structured.py +0 -0
  72. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/telemetry.py +0 -0
  73. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/tools.py +0 -0
  74. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/tracing.py +0 -0
  75. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime/types.py +0 -0
  76. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime_python.egg-info/dependency_links.txt +0 -0
  77. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/src/aid_runtime_python.egg-info/top_level.txt +0 -0
  78. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agent_eval_runner.py +0 -0
  79. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_budget.py +0 -0
  80. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_checkpoint_integrity.py +0 -0
  81. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_deep_agents.py +0 -0
  82. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_hitl.py +0 -0
  83. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_loop.py +0 -0
  84. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_memory.py +0 -0
  85. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_redaction.py +0 -0
  86. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_resume_hardening.py +0 -0
  87. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_agents_runs.py +0 -0
  88. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_anthropic_provider.py +0 -0
  89. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_bedrock_provider.py +0 -0
  90. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_cache.py +0 -0
  91. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_concurrency.py +0 -0
  92. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_control.py +0 -0
  93. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_cost.py +0 -0
  94. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_dataset.py +0 -0
  95. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_decisions.py +0 -0
  96. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_evals.py +0 -0
  97. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_evals_assert_gate.py +0 -0
  98. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_evals_budgets.py +0 -0
  99. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_evals_decision.py +0 -0
  100. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_evals_gate.py +0 -0
  101. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_evals_predicate.py +0 -0
  102. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_evals_trajectory.py +0 -0
  103. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_gateway.py +0 -0
  104. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_gemini_provider.py +0 -0
  105. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_guardrails.py +0 -0
  106. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_guardrails_managed.py +0 -0
  107. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_guardrails_model_armor.py +0 -0
  108. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_http_provider.py +0 -0
  109. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_local_trace.py +0 -0
  110. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_metrics.py +0 -0
  111. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_package.py +0 -0
  112. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_pipeline.py +0 -0
  113. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_prompts.py +0 -0
  114. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_recorded_provider.py +0 -0
  115. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_redaction.py +0 -0
  116. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_chunking.py +0 -0
  117. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_citations.py +0 -0
  118. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_connectors.py +0 -0
  119. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_embedding.py +0 -0
  120. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_filters.py +0 -0
  121. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_gate.py +0 -0
  122. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_hybrid.py +0 -0
  123. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_knowledge_base.py +0 -0
  124. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_metrics.py +0 -0
  125. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_rerank.py +0 -0
  126. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_stores.py +0 -0
  127. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_retrieval_types.py +0 -0
  128. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_steps.py +0 -0
  129. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_streaming.py +0 -0
  130. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_structured.py +0 -0
  131. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_telemetry.py +0 -0
  132. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_tool_permissions.py +0 -0
  133. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_tools.py +0 -0
  134. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_tracing.py +0 -0
  135. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_types.py +0 -0
  136. {aid_runtime_python-0.1.186 → aid_runtime_python-0.1.188}/tests/test_vertex_provider.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.186
3
+ Version: 0.1.188
4
4
  Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
5
5
  Author: The AID Framework authors
6
6
  License-Expression: Apache-2.0
@@ -27,10 +27,13 @@ Provides-Extra: deepagents
27
27
  Requires-Dist: deepagents==0.7.32; extra == "deepagents"
28
28
  Provides-Extra: otlp
29
29
  Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.24; extra == "otlp"
30
+ Provides-Extra: identity
31
+ Requires-Dist: pyjwt[crypto]<3,>=2.8; extra == "identity"
30
32
  Provides-Extra: dev
31
33
  Requires-Dist: mypy<3,>=1.11; extra == "dev"
32
34
  Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.24; extra == "dev"
33
35
  Requires-Dist: opentelemetry-sdk<2,>=1.24; extra == "dev"
36
+ Requires-Dist: pyjwt[crypto]<3,>=2.8; extra == "dev"
34
37
  Requires-Dist: pytest<10,>=8; extra == "dev"
35
38
  Requires-Dist: pytest-asyncio<2,>=0.24; extra == "dev"
36
39
  Requires-Dist: ruff<1,>=0.6; extra == "dev"
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "aid-runtime-python"
7
- version = "0.1.186"
7
+ version = "0.1.188"
8
8
  description = "The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -50,6 +50,13 @@ deepagents = ["deepagents==0.7.32"]
50
50
  # extra in the status it returns when the exporter is absent, so the fix is in
51
51
  # the message rather than in a traceback.
52
52
  otlp = ["opentelemetry-exporter-otlp-proto-http>=1.24,<2"]
53
+ # Token verification for a deployment that declares an identity provider. An extra
54
+ # because `aid_runtime.identity` is imported by one generated module and by nothing on
55
+ # the serving path of a spec that declares no identity: a service that trusts no issuer
56
+ # should not install a JWT library to say so. `[crypto]` is not optional decoration --
57
+ # it is what turns a JWK into a key object, and without it PyJWT verifies HMAC only,
58
+ # which `aid_runtime.identity` refuses to accept.
59
+ identity = ["pyjwt[crypto]>=2.8,<3"]
53
60
  dev = [
54
61
  "mypy>=1.11,<3",
55
62
  # Both the SDK and the OTLP exporter, so the `configured` path is covered by a
@@ -58,6 +65,10 @@ dev = [
58
65
  # extra exists to prevent.
59
66
  "opentelemetry-exporter-otlp-proto-http>=1.24,<2",
60
67
  "opentelemetry-sdk>=1.24,<2",
68
+ # So the verifier is exercised against a real signature implementation rather than a
69
+ # patched one, for the same reason the OTLP exporter is here: the failure mode this
70
+ # guards against is the dependency being absent where it is assumed present.
71
+ "pyjwt[crypto]>=2.8,<3",
61
72
  "pytest>=8,<10",
62
73
  "pytest-asyncio>=0.24,<2",
63
74
  "ruff>=0.6,<1",
@@ -364,7 +364,7 @@ from aid_runtime.types import (
364
364
  )
365
365
 
366
366
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
367
- __version__ = "0.1.186"
367
+ __version__ = "0.1.188"
368
368
 
369
369
  __all__ = [
370
370
  "AGENT_LABEL",
@@ -0,0 +1,320 @@
1
+ """Token verification for a deployment that declares an identity provider.
2
+
3
+ A deployment's ``identity`` block names an issuer and an audience. Until spec 0.35 that was
4
+ a *reference* and nothing more: the generated app seeded :meth:`Principal.unauthenticated`
5
+ and the application was expected to override the ``current_principal`` dependency with its
6
+ own verifier. That is a correct seam and a poor default -- a spec that declared an identity
7
+ provider read as though the generated service trusted it, while the service in fact trusted
8
+ everybody.
9
+
10
+ This module is the check that closes that gap, and it is deliberately narrow:
11
+
12
+ * **One token type.** A signed JWT. A SAML assertion is XML with a different trust model,
13
+ and the IR gate refuses a deployment that asks for one rather than emitting a verifier
14
+ that will never match it (``ir/identity-kind``).
15
+ * **No symmetric algorithm, ever.** :data:`OIDC_ALGORITHMS` holds the asymmetric families
16
+ only, and the token header's ``alg`` is selected from that set by exact match before any
17
+ signature work happens. There is therefore no key-confusion class to get wrong: a public
18
+ key can never be presented as an HMAC secret, and ``alg: none`` is not a member.
19
+ * **Nothing is trusted before it is verified.** The header is decoded to find ``kid`` and
20
+ ``alg`` -- both are peer-supplied -- and both are used only to *choose* a key and an
21
+ algorithm from allow-lists. Every claim check the caller depends on (``iss``, ``aud``,
22
+ ``exp``, and the subject claim) is enforced by the signature-covered decode.
23
+ * **A refusal says nothing.** :class:`TokenRefusedError` carries a ``reason`` for the log and
24
+ the generated route answers one opaque ``401``, following the checkpoint route's
25
+ discipline: a caller holding a bad token learns that it is bad, not which check failed.
26
+
27
+ Keys come from the issuer's JWKS document, cached until its TTL expires and re-fetched
28
+ early -- rate-limited -- when a token names a ``kid`` the cache does not hold, so a key
29
+ rotation does not need a restart and an attacker cannot use unknown ``kid`` values to drive
30
+ unbounded fetches at the identity provider.
31
+
32
+ The verifier is *configuration-complete*: it takes everything it needs at construction and
33
+ reads no environment variable. The generated module supplies the values from the spec, which
34
+ is what keeps the trust decision reviewable in a diff rather than in a deployment's settings.
35
+ """
36
+
37
+ from __future__ import annotations
38
+
39
+ import base64
40
+ import binascii
41
+ import json
42
+ import time
43
+ from collections.abc import Awaitable, Callable, Mapping, Sequence
44
+ from typing import Any
45
+
46
+ from aid_runtime.agents.policy import Principal
47
+
48
+ try: # pragma: no cover - the import is the one thing an absent extra changes
49
+ import jwt
50
+ except ImportError as exc: # pragma: no cover
51
+ raise ImportError(
52
+ "token verification needs PyJWT, which is the `identity` extra: "
53
+ "pip install 'aid-runtime-python[identity]'. The generated verifier names it in "
54
+ "its module docstring too, so the fix is next to the code that needs it."
55
+ ) from exc
56
+
57
+ __all__ = [
58
+ "DEFAULT_ROLES_CLAIM",
59
+ "DEFAULT_SCOPES_CLAIM",
60
+ "DEFAULT_SUBJECT_CLAIM",
61
+ "OIDC_ALGORITHMS",
62
+ "JwksVerifier",
63
+ "TokenRefusedError",
64
+ "bearer_token",
65
+ ]
66
+
67
+ #: The asymmetric JWS families an OIDC provider issues in practice. Deliberately a closed
68
+ #: set: selection is by exact membership, so an algorithm outside it is refused before a
69
+ #: key is looked up. ``none`` and the ``HS*`` family are absent by design, not by omission.
70
+ OIDC_ALGORITHMS: tuple[str, ...] = (
71
+ "RS256",
72
+ "RS384",
73
+ "RS512",
74
+ "PS256",
75
+ "PS384",
76
+ "PS512",
77
+ "ES256",
78
+ "ES384",
79
+ "ES512",
80
+ )
81
+
82
+ DEFAULT_SUBJECT_CLAIM = "sub"
83
+ DEFAULT_ROLES_CLAIM = "roles"
84
+ #: `scp` is what OIDC providers issue; the OAuth2 spelling `scope` is one space-delimited
85
+ #: string, which :func:`_as_claim_set` splits, so both shapes are accepted once named.
86
+ DEFAULT_SCOPES_CLAIM = "scp"
87
+
88
+
89
+ class TokenRefusedError(Exception):
90
+ """A token the verifier would not turn into a principal.
91
+
92
+ ``reason`` is for the log. It names which check failed in the vocabulary an operator
93
+ debugs in -- and it must not reach the caller, who learns only that the token is not
94
+ acceptable.
95
+ """
96
+
97
+ def __init__(self, reason: str) -> None:
98
+ super().__init__(reason)
99
+ self.reason = reason
100
+
101
+
102
+ def _b64url_decode(segment: str) -> bytes:
103
+ padding = "=" * (-len(segment) % 4)
104
+ return base64.urlsafe_b64decode(segment + padding)
105
+
106
+
107
+ def _as_claim_set(value: Any) -> frozenset[str]:
108
+ """A claim as a set of strings, from either shape a provider uses.
109
+
110
+ A list is the common OIDC shape for roles. A single space-delimited string is the
111
+ OAuth2 shape for scopes. Anything else contributes nothing rather than raising: a
112
+ claim of the wrong type is a malformed token, and the caller is refused by the checks
113
+ that matter (signature, issuer, audience, expiry) rather than by a ``TypeError`` here.
114
+ """
115
+
116
+ if isinstance(value, str):
117
+ return frozenset(part for part in value.split() if part)
118
+ if isinstance(value, Sequence):
119
+ return frozenset(str(part) for part in value if isinstance(part, (str, int)))
120
+ return frozenset()
121
+
122
+
123
+ async def _fetch_json(url: str, timeout_seconds: float) -> Any:
124
+ """The default JWKS fetch. Kept module-level so tests can substitute the seam."""
125
+
126
+ import httpx
127
+
128
+ async with httpx.AsyncClient(timeout=timeout_seconds, follow_redirects=False) as client:
129
+ response = await client.get(url, headers={"accept": "application/json"})
130
+ response.raise_for_status()
131
+ return response.json()
132
+
133
+
134
+ def bearer_token(authorization: str | None) -> str:
135
+ """The token from an ``Authorization`` header, or a refusal.
136
+
137
+ The scheme is compared case-insensitively because RFC 7235 makes it case-insensitive
138
+ and providers do vary it. Every other shape -- no header, a different scheme, an empty
139
+ credential -- is refused rather than treated as anonymous: an anonymous fallback on a
140
+ deployment that declared an identity provider would turn a missing header into a
141
+ weaker caller than the spec asks for.
142
+ """
143
+
144
+ if not authorization:
145
+ raise TokenRefusedError("no authorization header")
146
+ scheme, _, credential = authorization.partition(" ")
147
+ if scheme.lower() != "bearer":
148
+ raise TokenRefusedError(f"unsupported authorization scheme {scheme!r}")
149
+ token = credential.strip()
150
+ if not token:
151
+ raise TokenRefusedError("empty bearer credential")
152
+ return token
153
+
154
+
155
+ class JwksVerifier:
156
+ """Verifies a JWKS-signed JWT and projects it onto a runtime :class:`Principal`.
157
+
158
+ Construction is the whole trust decision: the issuer, audience, key source, claim
159
+ names and accepted algorithms all arrive as arguments and none of them is read from
160
+ the environment. The generated module is the only caller, and it passes the spec's
161
+ values, so reviewing a diff shows what the service will trust.
162
+ """
163
+
164
+ def __init__(
165
+ self,
166
+ *,
167
+ issuer: str,
168
+ audience: str,
169
+ jwks_uri: str,
170
+ subject_claim: str = DEFAULT_SUBJECT_CLAIM,
171
+ roles_claim: str = DEFAULT_ROLES_CLAIM,
172
+ scopes_claim: str = DEFAULT_SCOPES_CLAIM,
173
+ algorithms: Sequence[str] = OIDC_ALGORITHMS,
174
+ cache_ttl_seconds: float = 300.0,
175
+ refresh_cooldown_seconds: float = 30.0,
176
+ leeway_seconds: float = 30.0,
177
+ timeout_seconds: float = 5.0,
178
+ fetch: Callable[[str], Awaitable[Any]] | None = None,
179
+ clock: Callable[[], float] = time.monotonic,
180
+ ) -> None:
181
+ accepted = tuple(algorithms)
182
+ outside = [algorithm for algorithm in accepted if algorithm not in OIDC_ALGORITHMS]
183
+ if outside:
184
+ raise ValueError(
185
+ f"refusing to accept {outside!r}: not in the asymmetric families this "
186
+ f"verifier implements ({', '.join(OIDC_ALGORITHMS)})"
187
+ )
188
+ if not accepted:
189
+ raise ValueError("refusing to construct a verifier that accepts no algorithm")
190
+ if leeway_seconds < 0:
191
+ raise ValueError("leeway_seconds must not be negative")
192
+ self._issuer = issuer
193
+ self._audience = audience
194
+ self._jwks_uri = jwks_uri
195
+ self._subject_claim = subject_claim
196
+ self._roles_claim = roles_claim
197
+ self._scopes_claim = scopes_claim
198
+ self._algorithms = accepted
199
+ self._ttl = cache_ttl_seconds
200
+ self._cooldown = refresh_cooldown_seconds
201
+ self._leeway = leeway_seconds
202
+ self._timeout = timeout_seconds
203
+ self._fetch = fetch if fetch is not None else self._default_fetch
204
+ self._clock = clock
205
+ self._keys: dict[str, Any] | None = None
206
+ self._fetched_at = 0.0
207
+
208
+ async def _default_fetch(self, url: str) -> Any:
209
+ return await _fetch_json(url, self._timeout)
210
+
211
+ # -- keys ---------------------------------------------------------------------
212
+
213
+ async def _refresh(self, now: float) -> None:
214
+ document = await self._fetch(self._jwks_uri)
215
+ if not isinstance(document, Mapping):
216
+ raise TokenRefusedError("jwks document is not an object")
217
+ keys = document.get("keys")
218
+ if not isinstance(keys, Sequence) or isinstance(keys, (str, bytes)):
219
+ raise TokenRefusedError("jwks document has no keys array")
220
+ by_kid: dict[str, Any] = {}
221
+ sole: Any = None
222
+ for jwk in keys:
223
+ if not isinstance(jwk, Mapping):
224
+ continue
225
+ # `use` is optional; when present and not `sig` the key is for encryption and
226
+ # must not be used to accept a token.
227
+ if jwk.get("use") not in (None, "sig"):
228
+ continue
229
+ sole = jwk
230
+ kid = jwk.get("kid")
231
+ if isinstance(kid, str) and kid:
232
+ by_kid[kid] = jwk
233
+ if len(by_kid) == 1:
234
+ # A single key needs no `kid` in the token; give it the one key's name so a
235
+ # token that omits `kid` still resolves.
236
+ (only_kid,) = by_kid
237
+ by_kid[""] = by_kid[only_kid]
238
+ elif not by_kid and sole is not None:
239
+ by_kid[""] = sole
240
+ self._keys = by_kid
241
+ self._fetched_at = now
242
+
243
+ async def _key_for(self, kid: str | None, algorithm: str) -> Any:
244
+ now = self._clock()
245
+ stale = self._keys is None or (now - self._fetched_at) >= self._ttl
246
+ if stale:
247
+ await self._refresh(now)
248
+ keys = self._keys or {}
249
+ key_id = kid or ""
250
+ if key_id not in keys:
251
+ # A token naming a key we do not hold is either a rotation we have not seen or
252
+ # a probe. Re-fetch at most once per cooldown, so the first cannot need a
253
+ # restart and the second cannot be used to hammer the identity provider.
254
+ if now - self._fetched_at < self._cooldown:
255
+ raise TokenRefusedError(f"unknown key id {key_id!r} (refresh suppressed)")
256
+ await self._refresh(now)
257
+ keys = self._keys or {}
258
+ if key_id not in keys:
259
+ raise TokenRefusedError(f"unknown key id {key_id!r}")
260
+ jwk = dict(keys[key_id])
261
+ try:
262
+ return jwt.PyJWK(jwk, algorithm=algorithm).key
263
+ except jwt.PyJWTError as exc:
264
+ raise TokenRefusedError(f"unusable key {key_id!r}: {type(exc).__name__}") from None
265
+
266
+ # -- verification -------------------------------------------------------------
267
+
268
+ @staticmethod
269
+ def _header(token: str) -> Mapping[str, Any]:
270
+ """The JOSE header, unverified. Used only to select from allow-lists."""
271
+
272
+ segment = token.split(".", 1)[0]
273
+ try:
274
+ raw = _b64url_decode(segment)
275
+ header = json.loads(raw)
276
+ except (binascii.Error, ValueError, UnicodeDecodeError):
277
+ raise TokenRefusedError("malformed token header") from None
278
+ if not isinstance(header, Mapping):
279
+ raise TokenRefusedError("malformed token header")
280
+ return header
281
+
282
+ async def principal(self, token: str) -> Principal:
283
+ """The principal a token names, or :class:`TokenRefusedError`."""
284
+
285
+ header = self._header(token)
286
+ algorithm = header.get("alg")
287
+ if not isinstance(algorithm, str) or algorithm not in self._algorithms:
288
+ raise TokenRefusedError(f"algorithm {algorithm!r} not accepted")
289
+ kid = header.get("kid")
290
+ if kid is not None and not isinstance(kid, str):
291
+ raise TokenRefusedError("malformed key id")
292
+ key = await self._key_for(kid, algorithm)
293
+ try:
294
+ claims = jwt.decode(
295
+ token,
296
+ key=key,
297
+ algorithms=[algorithm],
298
+ audience=self._audience,
299
+ issuer=self._issuer,
300
+ leeway=self._leeway,
301
+ options={"require": ["exp", "iss", "aud", self._subject_claim]},
302
+ )
303
+ except jwt.InvalidTokenError as exc:
304
+ # The class name is the vocabulary: ExpiredSignatureError, InvalidAudienceError,
305
+ # InvalidIssuerError, InvalidSignatureError, MissingRequiredClaimError.
306
+ raise TokenRefusedError(type(exc).__name__) from None
307
+ subject = claims.get(self._subject_claim)
308
+ if not isinstance(subject, str) or not subject:
309
+ raise TokenRefusedError(f"subject claim {self._subject_claim!r} is not a string")
310
+ return Principal(
311
+ roles=_as_claim_set(claims.get(self._roles_claim)),
312
+ scopes=_as_claim_set(claims.get(self._scopes_claim)),
313
+ authenticated=True,
314
+ subject=subject,
315
+ )
316
+
317
+ async def principal_from_authorization(self, authorization: str | None) -> Principal:
318
+ """The principal an ``Authorization`` header names, or :class:`TokenRefusedError`."""
319
+
320
+ return await self.principal(bearer_token(authorization))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.186
3
+ Version: 0.1.188
4
4
  Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
5
5
  Author: The AID Framework authors
6
6
  License-Expression: Apache-2.0
@@ -27,10 +27,13 @@ Provides-Extra: deepagents
27
27
  Requires-Dist: deepagents==0.7.32; extra == "deepagents"
28
28
  Provides-Extra: otlp
29
29
  Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.24; extra == "otlp"
30
+ Provides-Extra: identity
31
+ Requires-Dist: pyjwt[crypto]<3,>=2.8; extra == "identity"
30
32
  Provides-Extra: dev
31
33
  Requires-Dist: mypy<3,>=1.11; extra == "dev"
32
34
  Requires-Dist: opentelemetry-exporter-otlp-proto-http<2,>=1.24; extra == "dev"
33
35
  Requires-Dist: opentelemetry-sdk<2,>=1.24; extra == "dev"
36
+ Requires-Dist: pyjwt[crypto]<3,>=2.8; extra == "dev"
34
37
  Requires-Dist: pytest<10,>=8; extra == "dev"
35
38
  Requires-Dist: pytest-asyncio<2,>=0.24; extra == "dev"
36
39
  Requires-Dist: ruff<1,>=0.6; extra == "dev"
@@ -8,6 +8,7 @@ src/aid_runtime/cost.py
8
8
  src/aid_runtime/decisions.py
9
9
  src/aid_runtime/errors.py
10
10
  src/aid_runtime/gateway.py
11
+ src/aid_runtime/identity.py
11
12
  src/aid_runtime/local_trace.py
12
13
  src/aid_runtime/metrics.py
13
14
  src/aid_runtime/pipeline.py
@@ -102,6 +103,7 @@ tests/test_guardrails.py
102
103
  tests/test_guardrails_managed.py
103
104
  tests/test_guardrails_model_armor.py
104
105
  tests/test_http_provider.py
106
+ tests/test_identity.py
105
107
  tests/test_local_trace.py
106
108
  tests/test_metrics.py
107
109
  tests/test_package.py
@@ -10,10 +10,14 @@ deepagents==0.7.32
10
10
  mypy<3,>=1.11
11
11
  opentelemetry-exporter-otlp-proto-http<2,>=1.24
12
12
  opentelemetry-sdk<2,>=1.24
13
+ pyjwt[crypto]<3,>=2.8
13
14
  pytest<10,>=8
14
15
  pytest-asyncio<2,>=0.24
15
16
  ruff<1,>=0.6
16
17
 
18
+ [identity]
19
+ pyjwt[crypto]<3,>=2.8
20
+
17
21
  [otlp]
18
22
  opentelemetry-exporter-otlp-proto-http<2,>=1.24
19
23