Skip to content

Authorization Port

authorization_port

Inbound port for authorization and permission checking.

Responsibilities
  • Check whether a user has a specific permission.
  • Evaluate resource-level access control.
  • Retrieve effective permissions and roles for a user.
Non-Responsibilities
  • Authenticate users (handled externally).
  • Enforce business rules (handled by validation or domain).

AuthorizationPort

Bases: InboundPort

Inbound port for authorization decisions.

Class Type Parameters:

Name Bound or Constraints Description Default
AuthorizationCheckContext

The application-defined context for authorization checks.

required
Responsibilities
  • Evaluate permissions against an authorization context.
  • Support resource-level and global permission checks.
  • Expose user roles and effective permissions.
Non-Responsibilities
  • Manage user identities or credentials.
  • Define permission hierarchies (delegated to domain or configuration).
Example
class UserContext:
    user_id: str


class AccessControl(AuthorizationPort[UserContext]):
    async def check_permission(self, context: UserContext, permission: Permission) -> bool:
        return Permission.READ in await self.get_user_permissions(context.user_id)

    ...
Source code in src/forging_blocks/application/ports/inbound/authorization_port.py
class AuthorizationPort[AuthorizationCheckContext](InboundPort):
    """Inbound port for authorization decisions.

    Type Args:
        AuthorizationCheckContext: The application-defined context for authorization checks.

    Responsibilities:
        - Evaluate permissions against an authorization context.
        - Support resource-level and global permission checks.
        - Expose user roles and effective permissions.

    Non-Responsibilities:
        - Manage user identities or credentials.
        - Define permission hierarchies (delegated to domain or configuration).

    Example:
        ```python
        class UserContext:
            user_id: str


        class AccessControl(AuthorizationPort[UserContext]):
            async def check_permission(self, context: UserContext, permission: Permission) -> bool:
                return Permission.READ in await self.get_user_permissions(context.user_id)

            ...
        ```
    """

    @abstractmethod
    async def check_permission(
        self,
        context: AuthorizationCheckContext,
        permission: Permission,
    ) -> bool:
        """Evaluate whether *permission* is granted in *context*."""
        ...

    @abstractmethod
    async def check_resource_permission(
        self,
        context: AuthorizationCheckContext,
        resource: str,
        action: str,
    ) -> bool:
        """Evaluate whether *action* on *resource* is permitted in *context*."""
        ...

    @abstractmethod
    async def get_user_permissions(self, user_id: str) -> list[Permission]:
        """Return the effective permissions for *user_id*."""
        ...

    @abstractmethod
    async def get_user_roles(self, user_id: str) -> list[str]:
        """Return the roles assigned to *user_id*."""
        ...

check_permission(context: AuthorizationCheckContext, permission: Permission) -> bool abstractmethod async

Evaluate whether permission is granted in context.

Source code in src/forging_blocks/application/ports/inbound/authorization_port.py
@abstractmethod
async def check_permission(
    self,
    context: AuthorizationCheckContext,
    permission: Permission,
) -> bool:
    """Evaluate whether *permission* is granted in *context*."""
    ...

check_resource_permission(context: AuthorizationCheckContext, resource: str, action: str) -> bool abstractmethod async

Evaluate whether action on resource is permitted in context.

Source code in src/forging_blocks/application/ports/inbound/authorization_port.py
@abstractmethod
async def check_resource_permission(
    self,
    context: AuthorizationCheckContext,
    resource: str,
    action: str,
) -> bool:
    """Evaluate whether *action* on *resource* is permitted in *context*."""
    ...

get_user_permissions(user_id: str) -> list[Permission] abstractmethod async

Return the effective permissions for user_id.

Source code in src/forging_blocks/application/ports/inbound/authorization_port.py
@abstractmethod
async def get_user_permissions(self, user_id: str) -> list[Permission]:
    """Return the effective permissions for *user_id*."""
    ...

get_user_roles(user_id: str) -> list[str] abstractmethod async

Return the roles assigned to user_id.

Source code in src/forging_blocks/application/ports/inbound/authorization_port.py
@abstractmethod
async def get_user_roles(self, user_id: str) -> list[str]:
    """Return the roles assigned to *user_id*."""
    ...