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.4.tar.gz (9.6 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.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_rbac_icdc-0.1.4.tar.gz
  • Upload date:
  • Size: 9.6 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.4.tar.gz
Algorithm Hash digest
SHA256 e97bda3f36554a2a0425562b39a556c67619bc1dcc45e68fd07754a4d687dd9e
MD5 c3bb66324a1207ec6f3371f3b8fdb8c2
BLAKE2b-256 0529308a7f79b1cfc90f436451333e1a170d2d7c59102158f88cec279194b024

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_rbac_icdc-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a15043b9a9a038d3ee7778ee53bb8f494f83b6e3dc40f1c8b10f6a606bc55f65
MD5 bd486a5023732aa1386828452f2cb90a
BLAKE2b-256 8aefc787b5d9e938685e62f52af4f444ad400f0d56e35b1237388555511741e9

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