Skip to main content

👮 Role-Based-Access-Control module for Orwynn framework

Project description

👮 Role-Based-Access-Control module for Orwynn framework

Gives controls to roles and their permissions in your backend application.

Installation

Via Poetry:

poetry add orwynn_rbac

Usage

Define default roles in your application:

DefaultRoles = [
    DefaultRole(
        name="sergeant",
        title="Sergeant",
        description="Flexible policeman",
        permission_names=set(
            "yourcompany.yourproject.citizen.citizen-permission:get",
            "yourcompany.yourproject.tax.tax-permission:create",
            "yourcompany.yourproject.pursue.start-permission:do"
        )
    ),
    ...
]

NOTE: Default roles are initialized only once per fresh database.

In your Boot setup, initialize a RBACBoot class and get a bootscript from it:

from orwynn_rbac import RBACBoot

Boot(
    ...,
    bootscripts=[
        ...,
        RBACBoot(
            default_roles=DefaultRoles
        ).get_bootscript()
    ]
)

In any module, where RBAC functionality is required (e.g. user access checkers), import orwynn_rbac.module:

import orwynn_rbac

your_module = Module(
    ...,
    imports=[
        ...,
        orwynn_rbac.module
    ]
)

Checking access

To check an access to your controller you are free to implement own middleware, retrieve an user id, e.g. from HTTP authorization header, and pass it to our AccessService.check_user() method. A minimal middleware might look like this:

class AccessMiddleware(HttpMiddleware):
    def __init__(
        self,
        covered_routes: list[str],
        service: AccessService,
    ) -> None:
        super().__init__(covered_routes)
        self.service: AccessService = service

    async def process(
        self,
        request: HttpRequest,
        call_next: Callable,
    ) -> HttpResponse:
        user_id: str | None = request.headers.get("user-id", None)
        self.service.check_user(
            user_id, str(request.url.components.path), request.method
        )

        response: HttpResponse = await call_next(request)

        return response

The method AccessService.check_user() will raise a ForbiddenError if an user with given id has no access to the route and method, so you just need to call it with these arguments.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orwynn_rbac-0.1.1.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

orwynn_rbac-0.1.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file orwynn_rbac-0.1.1.tar.gz.

File metadata

  • Download URL: orwynn_rbac-0.1.1.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.6 Linux/6.6.2-zen1-1-zen

File hashes

Hashes for orwynn_rbac-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6bf1cad2cf12a3e4a73e90bbe315bd7b29fc022224a7e341223d71c7cc0a4cdf
MD5 84e4d66da4a6a5d1fcda2d2ec0fd1fbf
BLAKE2b-256 fadbe2b70fc0c37a8138a73c699df68be15524716beffc006827d330b9fc4dfb

See more details on using hashes here.

File details

Details for the file orwynn_rbac-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: orwynn_rbac-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.6 Linux/6.6.2-zen1-1-zen

File hashes

Hashes for orwynn_rbac-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54b17b6f93f4cf440cb5b5b0a572574dd6910b59b937d9be32d515086c2da3b4
MD5 30dac21185e06a1055539c717a178020
BLAKE2b-256 f6f2bc0cecee31ad998dad68637f78becdcb156beca5a6c8800c12c94be3ed20

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page