67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
"""
|
|
Derives a trusted CallerContext from a request.
|
|
"""
|
|
from typing import Dict, Any
|
|
from contracts.common import CallerContext
|
|
|
|
# This is a placeholder for a future, more robust caller registry (e.g., Firestore).
|
|
# The key would be the hash of the API key or the IAP-verified user email.
|
|
_CALLER_REGISTRY: Dict[str, Dict[str, Any]] = {
|
|
"agent:perplexity": {
|
|
"profile": "operator",
|
|
"owner_id": "sa:perplexity",
|
|
"workspace_id": "ws:vauco",
|
|
"allowed_tool_policy": {
|
|
# This profile can read anything but requires approval for all writes
|
|
"default": "requires_approval",
|
|
"read_only": "read_only",
|
|
"propose_only": "propose_only",
|
|
"forbidden": "forbidden",
|
|
}
|
|
},
|
|
"user:chris.christiansen@vauco.no": {
|
|
"profile": "admin",
|
|
"owner_id": "user:chris.c",
|
|
"workspace_id": "ws:vauco",
|
|
"allowed_tool_policy": {
|
|
# Admin can do safe writes directly, but needs high approval for destructive actions
|
|
"default": "requires_high_approval",
|
|
"read_only": "read_only",
|
|
"requires_approval": "requires_approval", # Can do normal writes
|
|
}
|
|
}
|
|
}
|
|
|
|
_DEFAULT_CALLER_CONTEXT = CallerContext(
|
|
caller_id="anonymous:unknown",
|
|
caller_type="system",
|
|
profile="readonly",
|
|
owner_id="system:public",
|
|
workspace_id="ws:public",
|
|
allowed_tool_policy={"default": "read_only"}
|
|
)
|
|
|
|
def derive_caller_context(auth_identifier: str) -> CallerContext:
|
|
"""
|
|
Derives a CallerContext from a trusted, server-verified identifier.
|
|
|
|
In a real implementation, `auth_identifier` would be the result of
|
|
authenticating a request (e.g., looking up an API key hash or using an
|
|
IAP-provided email address).
|
|
|
|
Args:
|
|
auth_identifier: The trusted identifier for the caller.
|
|
|
|
Returns:
|
|
A CallerContext object with the appropriate policies.
|
|
"""
|
|
caller_data = _CALLER_REGISTRY.get(auth_identifier)
|
|
if not caller_data:
|
|
return _DEFAULT_CALLER_CONTEXT
|
|
|
|
return CallerContext(
|
|
caller_id=auth_identifier,
|
|
caller_type='human' if auth_identifier.startswith('user:') else auth_identifier.split(":")[0], # cheap trick
|
|
**caller_data
|
|
)
|