castlecraft_engineer.authorization
¶
castlecraft_engineer.authorization
¶
Action
¶
Bases: BaseStringEnum
Common actions. Extendable by creating new enums.
Source code in src/castlecraft_engineer/authorization/types.py
AllowAllAuthorizationService
¶
Bases: AuthorizationService
An authorization service that always allows access.
Source code in src/castlecraft_engineer/authorization/default_services.py
check_permission(subject_id, required_permissions, provided_permissions=None, context=None)
async
¶
Always allows the request.
Source code in src/castlecraft_engineer/authorization/default_services.py
AuthorizationService
¶
Bases: ABC
Abstract interface for authorization checks. Implementations connect to engines like Casbin, OPA, SpiceDB, etc.
Source code in src/castlecraft_engineer/authorization/base_service.py
check_permission(subject_id, required_permissions, provided_permissions=None, context=None)
abstractmethod
async
¶
Checks if the subject has the required permissions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subject_id
|
Optional[str]
|
Identifier of the user/service performing the action. Can be None for anonymous checks if supported by the policy. |
required |
required_permissions
|
List[Permission]
|
A list of Permission objects declared by the handler via the @ctx decorator. |
required |
provided_permissions
|
Optional[List[str]]
|
Optional list of permissions the subject possesses. |
None
|
context
|
Optional[Dict[str, Any]]
|
Optional dictionary containing additional data for policy evaluation. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if authorized. |
Raises:
Type | Description |
---|---|
AuthorizationError
|
If the check fails. This is often preferred over returning False to halt execution clearly. |
NotImplementedError
|
If the method is not implemented. |
Source code in src/castlecraft_engineer/authorization/base_service.py
DenyAllAuthorizationService
¶
Bases: AuthorizationService
An authorization service that always denies access.
Source code in src/castlecraft_engineer/authorization/default_services.py
check_permission(subject_id, required_permissions, provided_permissions=None, context=None)
async
¶
Always denies the request by raising an AuthorizationError.
Source code in src/castlecraft_engineer/authorization/default_services.py
Resource
¶
Bases: BaseStringEnum
Common resource types. Extendable by creating new enums.
Source code in src/castlecraft_engineer/authorization/types.py
Scope
¶
ctx(required_permissions)
¶
Decorator to associate required permission context(s) with a handler method.
Injects 'required_permissions' (always as a list) into the keyword arguments passed to the decorated method, allowing the method's implementation to access it and perform authorization checks if needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
required_permissions
|
Union[Permission, List[Permission]]
|
A single Permission object or a list of Permissions. |
required |
Source code in src/castlecraft_engineer/authorization/permission.py
setup_authorization(container)
¶
Sets up the authorization service based on environment configuration and registers it with the provided DI container.