Auth authorization¶
Resource policy checks.
rn_forge.fastapi.auth.authorization
¶
Object and query checks from a :class:~rn_forge.auth.ResourcePolicy.
PolicyGuard(policy, *, hides_existence=False)
¶
Apply a :class:~rn_forge.auth.ResourcePolicy to queries and objects.
The guard is independent of the query type: QueryT is whatever the policy
narrows, such as a SQLAlchemy Select.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
ResourcePolicy[ResourceT, QueryT]
|
The policy. |
required |
hides_existence
|
bool
|
Report a denied object as 404 instead of 403. |
False
|
Example
rows = guard.scope(principal, "list", select(Order)) narrows a list
query, so its rows, counts and pages hold only permitted rows;
guard.check(principal, "read", order) then returns order or raises.
check(principal, action, resource)
¶
Return resource if principal may action it.
Raises:
| Type | Description |
|---|---|
LookupError
|
The policy denies it and |
PermissionDenied
|
The policy denies it (403). |
scope(principal, action, query)
¶
Return query narrowed to what principal may action.