|
| 1 | +"""Authenticated entitlement and temporary-elevation administration routes.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Awaitable, Callable, Mapping |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from pydantic import BaseModel, ConfigDict, Field |
| 9 | +from starlette.requests import Request |
| 10 | +from starlette.responses import JSONResponse, Response |
| 11 | +from starlette.routing import Route |
| 12 | + |
| 13 | +from agentnet.authorization.elevation import ElevationRequest, ElevationService |
| 14 | +from agentnet.authorization.evidence import IssuanceAuthority, SignedAuthorityCommand |
| 15 | +from agentnet.authorization.policy import HumanEntitlement |
| 16 | +from agentnet.core.app import CommunicationCore |
| 17 | +from agentnet.identity.actors import VerifiedActor |
| 18 | +from agentnet.security.signatures import canonical_digest |
| 19 | + |
| 20 | + |
| 21 | +BodyAndActor = Callable[ |
| 22 | + [Request, CommunicationCore], |
| 23 | + Awaitable[tuple[bytes, VerifiedActor]], |
| 24 | +] |
| 25 | +DecisionIssuer = Callable[..., IssuanceAuthority] |
| 26 | + |
| 27 | + |
| 28 | +class EntitlementIssueBody(BaseModel): |
| 29 | + model_config = ConfigDict(extra="forbid") |
| 30 | + |
| 31 | + entitlement: HumanEntitlement |
| 32 | + command: SignedAuthorityCommand |
| 33 | + |
| 34 | + |
| 35 | +class EntitlementRevokeBody(BaseModel): |
| 36 | + model_config = ConfigDict(extra="forbid") |
| 37 | + |
| 38 | + command: SignedAuthorityCommand |
| 39 | + |
| 40 | + |
| 41 | +class ElevationIssueBody(BaseModel): |
| 42 | + model_config = ConfigDict(extra="forbid") |
| 43 | + |
| 44 | + request: ElevationRequest |
| 45 | + independent_approvals: tuple[dict[str, Any], ...] = Field( |
| 46 | + min_length=1, |
| 47 | + max_length=5, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def create_authority_admin_routes( |
| 52 | + core: CommunicationCore, |
| 53 | + body_and_actor: BodyAndActor, |
| 54 | + elevations: ElevationService, |
| 55 | + issue_decision: DecisionIssuer, |
| 56 | + response_headers: Mapping[str, str], |
| 57 | +) -> list[Route]: |
| 58 | + """Mount only entitlement and elevation administration routes.""" |
| 59 | + |
| 60 | + async def issue_entitlement(request: Request) -> Response: |
| 61 | + body, actor = await body_and_actor(request, core) |
| 62 | + parsed = EntitlementIssueBody.model_validate_json(body) |
| 63 | + resource, mutation = core.policy.entitlement_issuance_binding( |
| 64 | + parsed.entitlement, |
| 65 | + reason=parsed.command.reason, |
| 66 | + ) |
| 67 | + authority = issue_decision( |
| 68 | + core, |
| 69 | + actor=actor, |
| 70 | + action="authorization.entitlement.issue", |
| 71 | + resource=resource, |
| 72 | + request_digest=canonical_digest(mutation), |
| 73 | + ) |
| 74 | + result = core.policy.add_entitlement( |
| 75 | + parsed.entitlement, |
| 76 | + command=parsed.command, |
| 77 | + authority=authority, |
| 78 | + ) |
| 79 | + return JSONResponse( |
| 80 | + result.model_dump(mode="json"), |
| 81 | + status_code=201, |
| 82 | + headers=response_headers, |
| 83 | + ) |
| 84 | + |
| 85 | + async def revoke_entitlement(request: Request) -> Response: |
| 86 | + body, actor = await body_and_actor(request, core) |
| 87 | + parsed = EntitlementRevokeBody.model_validate_json(body) |
| 88 | + entitlement_id = request.path_params["entitlement_id"] |
| 89 | + resource, mutation = core.policy.entitlement_revocation_binding( |
| 90 | + entitlement_id, |
| 91 | + expected_entity_revision=parsed.command.expected_entity_revision, |
| 92 | + reason=parsed.command.reason, |
| 93 | + ) |
| 94 | + authority = issue_decision( |
| 95 | + core, |
| 96 | + actor=actor, |
| 97 | + action="authorization.entitlement.revoke", |
| 98 | + resource=resource, |
| 99 | + request_digest=canonical_digest(mutation), |
| 100 | + ) |
| 101 | + core.policy.revoke_entitlement( |
| 102 | + entitlement_id, |
| 103 | + command=parsed.command, |
| 104 | + authority=authority, |
| 105 | + ) |
| 106 | + return JSONResponse( |
| 107 | + {"entitlement_id": entitlement_id, "revoked": True}, |
| 108 | + headers=response_headers, |
| 109 | + ) |
| 110 | + |
| 111 | + async def issue_elevation(request: Request) -> Response: |
| 112 | + body, actor = await body_and_actor(request, core) |
| 113 | + parsed = ElevationIssueBody.model_validate_json(body) |
| 114 | + resource, mutation = elevations.authority_binding(parsed.request) |
| 115 | + authority = issue_decision( |
| 116 | + core, |
| 117 | + actor=actor, |
| 118 | + action="authorization.elevation.request", |
| 119 | + resource=resource, |
| 120 | + request_digest=mutation["request_digest"], |
| 121 | + ) |
| 122 | + result = elevations.issue( |
| 123 | + parsed.request, |
| 124 | + beneficiary=actor, |
| 125 | + authority=authority, |
| 126 | + approvals=parsed.independent_approvals, |
| 127 | + ) |
| 128 | + return JSONResponse( |
| 129 | + result.model_dump(mode="json"), |
| 130 | + status_code=201, |
| 131 | + headers=response_headers, |
| 132 | + ) |
| 133 | + |
| 134 | + return [ |
| 135 | + Route("/v1/admin/entitlements", issue_entitlement, methods=["POST"]), |
| 136 | + Route( |
| 137 | + "/v1/admin/entitlements/{entitlement_id}/revoke", |
| 138 | + revoke_entitlement, |
| 139 | + methods=["POST"], |
| 140 | + ), |
| 141 | + Route("/v1/admin/elevations", issue_elevation, methods=["POST"]), |
| 142 | + ] |
| 143 | + |
| 144 | + |
| 145 | +__all__ = ["create_authority_admin_routes"] |
0 commit comments