Skip to main content

Flask RBAC library using YAML config file

Project description

RBAC library for Flask

Overview

This library provides role-based access control (RBAC) for Flask applications using a YAML configuration file.

Installation

pip install flask-rbac-icdc

Usage

Configuration

Create a YAML configuration file for RBAC rules. For example, rbac_config.yaml:

roles:
  admin:
    products:
      permissions:
        - list
        - create
        - update
        - delete
      filters: {}
    accounts:
      permissions:
        - list
        - get
      filters:
        id: account_id
  member:
    products:
      permissions:
        - list
      filters:
        account_id: account_id
        owner: owner

Define Account Class

Implement the RbacAccount abstract class:

from flask_sqlalchemy import SQLAlchemy
from flask_rbac_icdc import RbacAccount

db = SQLAlchemy()

class Account(db.Model, RbacAccount):
    __tablename__ = "accounts"
    object_name = "accounts"
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(64), unique=True, nullable=False)
    # ...Other account properties here

    @classmethod
    def get_by_name(cls, account_name: str) -> Optional["Account"]:
        return cls.query.filter_by(name=account_name).first()

    def get_role(self, requested_role: str) -> str:
        operator = is_operator(self.name, requested_role)
        if requested_role == "operator" and not operator:
            raise PermissionException("You are not operator")
        if operator:
            return "operator"
        return requested_role

Initialize RBAC

Initialize the RBAC instance in your Flask application:

from flask_rbac_icdc import RBAC
from app.models.accounts import Account

rbac = RBAC(config_path='rbac_config.yaml', Accounts)

Protect Endpoints

Use the allow decorator to protect your endpoints:

@app.route('/create', methods=['POST'])
@rbac.allow('products.create')
def create_product(subject):
    # Your logic to create a product
    return 'Product created', 201

@app.route('/read', methods=['GET'])
@rbac.allow('products.list')
def list_products(subject):
    # Your logic to list products
    return 'Products data', 200

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

flask_rbac_icdc-0.1.8.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_rbac_icdc-0.1.8-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_rbac_icdc-0.1.8.tar.gz.

File metadata

  • Download URL: flask_rbac_icdc-0.1.8.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for flask_rbac_icdc-0.1.8.tar.gz
Algorithm Hash digest
SHA256 64d2d1c5a6547171d653f206e23e17ea0372430c7f5027aa48d7e05014c7f569
MD5 57e4ea61f739745e51357e2e640820c3
BLAKE2b-256 e00b63d60f1b4d934d8ea2ddd8786cd473a95bd20e5d98f064cfe735cf3e5be1

See more details on using hashes here.

File details

Details for the file flask_rbac_icdc-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_rbac_icdc-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 aa0b7b4c532b624489687f9ee3ba8b7a3958098ea8c9a7052b0959327c3c4c43
MD5 69e8b6b84d2796b84e0a80ccb8df3486
BLAKE2b-256 1203b89c796ff053122e4c72a5cba6705a8a9e7b5a39e2f3a795894809916723

See more details on using hashes here.

Supported by

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