policy
Authorization port for permission checks
Policy is a platform primitive that abstracts authorization. Components inject a Policy port to check if actors can perform actions on resources. Implementations include AllowAllPolicy (dev/single-user), OwnerOnlyPolicy (user owns their data), and can be extended for roles, groups, or custom authorization logic.
0
Ports
0
Schemas
3
Hooks
3
Events
01
Ports
Adapters Provided
- AllowAllPolicy
- OwnerOnlyPolicy
02
Schemas
Defines
No schemas defined
Uses
No external schemas used
03
Hooks
- before_check
- on_grant
- on_deny
04
Events
- PermissionGranted v1
- PermissionDenied v1
- PermissionRequired v1
05
Examples
class CompleteTaskUseCase:
def __init__(self, repo: TaskRepository, policy: Policy) -> None:
self._repo = repo
self._policy = policy
async def execute(self, task_id: UUID, actor_id: str) -> None:
task = await self._repo.get(task_id)
if not self._policy.can(actor_id, Action.COMPLETE, task.owner_id, "Task"):
raise NotAuthorizedError("Cannot complete this task")
task.complete()
await self._repo.update(task)
Use policy.can() to check before performing actions.
async def execute(self, task_id: UUID, actor_id: str) -> None:
task = await self._repo.get(task_id)
# Raises PermissionDeniedError if not allowed
self._policy.require(actor_id, Action.DELETE, task.owner_id, "Task")
await self._repo.delete(task_id)
Raises PermissionDeniedError automatically.
from psp.platform.policy import AllowAllPolicy
def test_complete_task():
policy = AllowAllPolicy() # Allows everything
use_case = CompleteTaskUseCase(repo=InMemoryRepo(), policy=policy)
# No authorization error
use_case.execute(task_id=task.id, actor_id="any-user")
Bypass authorization in tests.
from psp.platform.policy import OwnerOnlyPolicy, Action
policy = OwnerOnlyPolicy()
# Owner can access
policy.can("user-123", Action.READ, owner_id="user-123", "Task") # True
# Others cannot
policy.can("user-456", Action.READ, owner_id="user-123", "Task") # False
# System can access everything
policy.can("system", Action.READ, owner_id="user-123", "Task") # True
Users can only access their own resources.
API Reference
This component mounts routes under /v1/policy.
View OpenAPI specification