Guards — Aquilia Documentation
Comprehensive guide and documentation for Guards in the Aquilia framework. View API reference, examples, and implementation patterns.
Security & Auth Guards & Decorators Aquilia provides a unified, context-first endpoint protection suite consisting of **Route Decorators** (aquilia/auth/decorators.py) and composable **Stateless Guards** (aquilia/auth/guards.py). Controller Route Decorators Decorators run inside controller endpoints, resolving identities and session structures from active request scopes, injecting resolved parameters into the handler when they are requested. @authenticated Blocks requests lacking active authenticated identities. Can redirect browser clients if a login URL is configured. @roles_required Asserts specific roles (with support for inheritance via PermissionEngine ) on the active identity. @scopes_required Asserts specific OAuth scope capabilities on the identity. @optional_auth Resolves the identity if present but does not block anonymous clients. Injects identity or session into handler. from aquilia.auth.decorators import authenticated, roles_required, scopes_required, optional_auth @authenticated async def get_profile(ctx, user: Identity): # Principal is automatically resolved and injected return @authenticated(login_url="/login", redirect_if_html=True) async def dashboard(ctx, session: Session): # Web browsers get redirected to /login?next=/dashboard return @roles_required("admin") async def delete_user(ctx, identity: Identity): ... @scopes_required("reports:read", require_all=True) async def fetch_reports(ctx): ... Composable Stateless Guards Guards are stateless protocol classes implementing a check(ctx) method. Multiple guards can be composed on any handler or pipeline using the @requires decorator. , , , , ].map((g, i) => ( ))} from aquilia.auth.guards import requires, AuthGuard, RoleGuard, PolicyGuard from aquilia.auth.permissions import PermissionEngine permissions = PermissionEngine() permissions.register_policy("is_owner", lambda identity, resource: identity.id == resource.owner_id) class DocumentController(Controller): @requires(AuthGuard, RoleGuard("editor")) async def edit_document(self, ctx): ... @requires(AuthGuard(), PolicyGuard("is_owner", engine=permissions)) async def delete_document(self, ctx): ... ← Credentials Authorization → )
Go to Homepage