Skip to main content

A node based permission engine for python.

Project description

PyPermission

A role-based access control (RBAC) permission library for python.

repository mirror license pipeline status coverage report Documentation Status Code style: black

pkgversion versionsupport Downloads Week Downloads Total

WARNING: ALPHA VERSION

This is a prototype. APIs will be subjects to breaking changes! Existing APIs are not battle tested and might exhibit unexpected behavior!

Features

  • NIST Model for RBAC: https://doi.org/10.1145/344287.344301
    • Level 1: Flat (Missing method)
    • Level 2a: Hierarchical
    • Level 3a: Constrained (TODO)
    • Level 4a: Symmetric (TODO)
  • Permissions with hierarchical ordering
  • Permissions with string payloads
  • Persistency backends
    • SQLAlchemy
    • JSON + YAML save files
  • Subject permission assignment (UBAC oriented)
  • Online Documentation (TODO, is incomplete and incorrect)

Installation

pip

PyPermission can be installed directly from the PyPI repositories.

JSON persistency backend

pip install PyPermission

SQLAlchemy persistency backend

pip install PyPermission[sqlalchemy]

JSON + YAML persistency backend

pip install PyPermission[yaml]

Editable installation for developers

Install PyPermission from the git repository with:

git clone https://gitlab.com/DigonIO/PyPermission.git
cd PyPermission
python -m venv venv  # optional
source ./venv/bin/activate # optional
pip install -e .[dev]

Example: How to RBAC

Import all required objects. Here we will choose the authority with the JSON persistency backend.

from pypermission import PermissionNode
from pypermission.yaml import SerialAuthority

Define an authority with some permission nodes:

class Nodes(PermissionNode):
    CHAT_ = "chat.*"  # parent
    CHAT_GLOBAL = "chat.global"  # leaf
    CHAT_MODERATOR = "chat.moderator"  # leaf
    TICKET_ = "ticket.*"  # parent
    TICKET_OPEN = "ticket.open"  # leaf
    TICKET_CLOSE_ = "ticket.close.*"  # parent
    TICKET_CLOSE_OWN = "ticket.close.own"  # leaf
    TICKET_CLOSE_ALL = "ticket.close.all"  # leaf
    TICKET_ASSIGN = "ticket.assign"  # leaf

auth = SerialAuthority(nodes=Nodes)

The following file save_file.yaml defines a RBAC setup. Alice is a member of the user and moderator role, while Bob is assigned only to the user role:

roles:
  moderator:
    permission_nodes:
      - chat.*
      - ticket.*
    member_subjects:
      - Alice
  user:
    permission_nodes:
      - chat.global
      - ticket.open
      - ticket.close.own
    member_subjects:
      - Alice
      - Bob
subjects:
  Alice: {}
  Bob: {}
auth.load_file(path="save_file.yaml")

Now check if a subject has a desired permission.

>>> auth.subject_has_permission(sid="Bob", node=Nodes.CHAT_GLOBAL)
True

>>> auth.subject_has_permission(sid="Alice", node=Nodes.CHAT_MODERATOR)
True

>>> auth.subject_has_permission(sid="Bob", node=Nodes.TICKET_OPEN)
True

>>> auth.subject_has_permission(sid="Alice", node=Nodes.TICKET_CLOSE_ALL)
True

Documentation

The API documentation can either be viewed online or generated using Sphinx with numpydoc formatting. To build, run:

sphinx-build -b html docs/ docs/_build/html

Testing

Testing is done using pytest. With pytest-cov and coverage a report for the test coverage can be generated:

pytest --cov=src/ tests/
coverage html

To test the examples in the documentation run:

pytest docs/

License

This free and open source software (FOSS) is published under the LGPLv3 license.

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

PyPermission-0.1.1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

PyPermission-0.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: PyPermission-0.1.1.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for PyPermission-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ff128a8d21ec8d85c630eeb13b6008f5b4d7cfcc74e145a9b6db0f3ddfee9626
MD5 4211ff814ceb32b8c98e3a9be9cffdc5
BLAKE2b-256 f25e0d07b30fec91b1e6d99e51a1fb6d2d5d2c3a9f057129d3f840fa67e2a5dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: PyPermission-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for PyPermission-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2aa00e1494cc030a0b040a3259feefa25795e12fc2a65dcd845f548491e511bf
MD5 72f01dd570f9f6fa69a0a03cc21de13c
BLAKE2b-256 7cd787596eba0198b052b853c699514a2bc2e8d5e9f971d0a6e7397a140d3376

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