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, auth_engine_name=None)
¶
Sets up the authorization service based on configuration and registers it with the provided DI container.
The authorization engine can be specified programmatically via auth_engine_name
or through the ENV_AUTHORIZATION_ENGINE
environment variable. The programmatic
parameter takes precedence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container
|
Container
|
The punq DI container. |
required |
auth_engine_name
|
str | None
|
The name of the authorization engine to use. If None, it falls back to the environment variable. |
None
|
Source code in src/castlecraft_engineer/authorization/setup.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|