|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Location: ./cpex/framework/extensions/delegation.py |
| 3 | +Copyright 2025 |
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +Authors: Teryl Taylor |
| 6 | +
|
| 7 | +Delegation extension models. |
| 8 | +Carries the delegation chain state through the CMF message for policy |
| 9 | +evaluation. The chain grows monotonically — each hop appends, never |
| 10 | +removes. Scope narrowing is enforced at the framework level. |
| 11 | +
|
| 12 | +See: docs/delegation-hooks-design.md |
| 13 | +""" |
| 14 | + |
| 15 | +# Standard |
| 16 | +from datetime import datetime |
| 17 | + |
| 18 | +# Third-Party |
| 19 | +from pydantic import BaseModel, ConfigDict, Field |
| 20 | + |
| 21 | + |
| 22 | +class DelegationHop(BaseModel): |
| 23 | + """One hop in the delegation chain. |
| 24 | +
|
| 25 | + Each hop represents one step: "entity X delegated to entity Y |
| 26 | + for audience Z with these scopes." Immutable once created. |
| 27 | +
|
| 28 | + Attributes: |
| 29 | + subject_id: Who is acting at this hop. |
| 30 | + subject_type: Entity kind (user, agent, service). |
| 31 | + audience: Target audience for this hop's token. |
| 32 | + scopes_granted: What this hop's token can do. |
| 33 | + timestamp: When this hop was created. |
| 34 | + ttl_seconds: Token lifetime for this hop. |
| 35 | + strategy: How the token was obtained (token_exchange, ucan, etc.). |
| 36 | + from_cache: Whether the token came from cache. |
| 37 | +
|
| 38 | + Examples: |
| 39 | + >>> hop = DelegationHop( |
| 40 | + ... subject_id="alice@corp.com", |
| 41 | + ... subject_type="user", |
| 42 | + ... scopes_granted=("read:compensation",), |
| 43 | + ... timestamp=datetime(2025, 1, 1), |
| 44 | + ... strategy="token_exchange", |
| 45 | + ... ) |
| 46 | + >>> hop.subject_id |
| 47 | + 'alice@corp.com' |
| 48 | + """ |
| 49 | + |
| 50 | + model_config = ConfigDict(frozen=True) |
| 51 | + |
| 52 | + subject_id: str = Field(description="Who is acting at this hop.") |
| 53 | + subject_type: str = Field(description="Entity kind: user, agent, service, system.") |
| 54 | + audience: str | None = Field(default=None, description="Target audience for this hop's token.") |
| 55 | + scopes_granted: tuple[str, ...] = Field(default=(), description="Scopes this hop's token grants.") |
| 56 | + timestamp: datetime = Field(default_factory=datetime.utcnow, description="When this hop was created.") |
| 57 | + ttl_seconds: int | None = Field(default=None, description="Token lifetime in seconds.") |
| 58 | + strategy: str | None = Field(default=None, description="Token strategy: token_exchange, ucan, passthrough, etc.") |
| 59 | + from_cache: bool = Field(default=False, description="Whether the token came from cache.") |
| 60 | + |
| 61 | + |
| 62 | +class DelegationExtension(BaseModel): |
| 63 | + """Delegation chain state carried in the CMF message. |
| 64 | +
|
| 65 | + Mutability tiers: |
| 66 | + - chain: monotonic (grows with each hop, never shrinks) |
| 67 | + - origin_subject_id, delegated: immutable (set at first delegation) |
| 68 | + - actor_subject_id: updates per hop (current actor changes) |
| 69 | +
|
| 70 | + The chain is available to the DSL via the delegation.* namespace: |
| 71 | + delegation.origin, delegation.actor, delegation.depth, |
| 72 | + delegation.age, delegated |
| 73 | +
|
| 74 | + Attributes: |
| 75 | + chain: Ordered list of delegation hops (monotonic growth). |
| 76 | + depth: Number of hops in the chain. |
| 77 | + origin_subject_id: Original caller (immutable once set). |
| 78 | + actor_subject_id: Current actor (latest hop's subject). |
| 79 | + delegated: Whether this request is delegated. |
| 80 | + age_seconds: Seconds since the original delegation. |
| 81 | +
|
| 82 | + Examples: |
| 83 | + >>> ext = DelegationExtension() |
| 84 | + >>> ext.delegated |
| 85 | + False |
| 86 | + >>> ext.depth |
| 87 | + 0 |
| 88 | + """ |
| 89 | + |
| 90 | + model_config = ConfigDict(frozen=True) |
| 91 | + |
| 92 | + chain: tuple[DelegationHop, ...] = Field(default=(), description="Ordered delegation hops.") |
| 93 | + depth: int = Field(default=0, description="Number of hops.") |
| 94 | + origin_subject_id: str | None = Field(default=None, description="Original caller.") |
| 95 | + actor_subject_id: str | None = Field(default=None, description="Current actor.") |
| 96 | + delegated: bool = Field(default=False, description="Whether this is a delegated request.") |
| 97 | + age_seconds: float = Field(default=0.0, description="Seconds since original delegation.") |
| 98 | + |
| 99 | + def with_new_hop(self, hop: DelegationHop) -> "DelegationExtension": |
| 100 | + """Create a new DelegationExtension with an appended hop. |
| 101 | +
|
| 102 | + Returns a new instance — the original is unchanged (immutable). |
| 103 | + The framework enforces scope narrowing before calling this. |
| 104 | +
|
| 105 | + Args: |
| 106 | + hop: The new delegation hop to append. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + New DelegationExtension with the hop appended. |
| 110 | + """ |
| 111 | + new_chain = self.chain + (hop,) |
| 112 | + origin = self.origin_subject_id or hop.subject_id |
| 113 | + age = (datetime.utcnow() - self.chain[0].timestamp).total_seconds() if self.chain else 0.0 |
| 114 | + return DelegationExtension( |
| 115 | + chain=new_chain, |
| 116 | + depth=len(new_chain), |
| 117 | + origin_subject_id=origin, |
| 118 | + actor_subject_id=hop.subject_id, |
| 119 | + delegated=True, |
| 120 | + age_seconds=age, |
| 121 | + ) |
0 commit comments