Skip to content

Authorization

Authorization is a crucial security feature in Ellar that determines what resources authenticated users can access. Ellar provides a flexible and powerful authorization system through policies, roles, and claims.

Table of Contents

  1. Basic Authorization
  2. Policies
  3. Role-Based Authorization
  4. Claims-Based Authorization
  5. Custom Policies with Requirements
  6. Combining Policies

Basic Authorization

To use authorization in your Ellar application, you need to:

  1. Decorate your controllers or routes with @Authorize()
  2. Apply specific policies using @CheckPolicies()
  3. Ensure users are authenticated using @AuthenticationRequired()

Here's a basic example:

from ellar.auth import AuthenticationRequired, Authorize, CheckPolicies
from ellar.common import Controller, get

@Controller("/articles")
@Authorize()  # Enable authorization for all routes
@AuthenticationRequired()  # Require authentication
class ArticleController:
    @get("/admin")
    @CheckPolicies(RolePolicy("admin"))  # Only allow admins
    async def admin_dashboard(self):
        return "Admin Dashboard"

    @get("/public")
    async def public_articles(self):  # Accessible to any authenticated user
        return "Public Articles"

For detailed information about specific authorization features, please refer to the respective sections in the documentation.