All Projects → pycasbin → flask-authz

pycasbin / flask-authz

Licence: Apache-2.0 license
Use Casbin in Flask, Casbin is a powerful and efficient open-source access control library.

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to flask-authz

Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+55%)
Mutual labels:  acl, rbac, casbin
Chi Authz
chi-authz is an authorization middleware for Chi
Stars: ✭ 248 (+148%)
Mutual labels:  acl, rbac, casbin
Casbin Rs
An authorization library that supports access control models like ACL, RBAC, ABAC in Rust.
Stars: ✭ 375 (+275%)
Mutual labels:  acl, rbac, casbin
dart-casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Dart/Flutter
Stars: ✭ 30 (-70%)
Mutual labels:  acl, rbac, casbin
Jcasbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Java
Stars: ✭ 1,335 (+1235%)
Mutual labels:  acl, rbac, casbin
Casbin Server
Casbin as a Service (CaaS)
Stars: ✭ 171 (+71%)
Mutual labels:  acl, rbac, casbin
Caddy Authz
Caddy-authz is a middleware for Caddy that blocks or allows requests based on access control policies.
Stars: ✭ 221 (+121%)
Mutual labels:  acl, rbac, casbin
sequelize-adapter
Sequelize adapter for Casbin
Stars: ✭ 51 (-49%)
Mutual labels:  acl, rbac, casbin
Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Golang
Stars: ✭ 10,872 (+10772%)
Mutual labels:  acl, rbac, casbin
Openstack Policy Editor
A Casbin Policy Editor for OpenStack
Stars: ✭ 28 (-72%)
Mutual labels:  acl, rbac, casbin
Negroni Authz
negroni-authz is an authorization middleware for Negroni
Stars: ✭ 152 (+52%)
Mutual labels:  acl, rbac, casbin
Node Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Node.js and Browser
Stars: ✭ 1,757 (+1657%)
Mutual labels:  acl, rbac, casbin
Think Casbin
专为ThinkPHP定制的Casbin的扩展包,Casbin是一个功能强大,高效的开源访问控制库。
Stars: ✭ 138 (+38%)
Mutual labels:  acl, rbac, casbin
actix-casbin-auth
Casbin Actix-web access control middleware
Stars: ✭ 40 (-60%)
Mutual labels:  acl, rbac, casbin
Gorm Adapter
Gorm adapter for Casbin
Stars: ✭ 373 (+273%)
Mutual labels:  acl, rbac, casbin
Casbin.net
An authorization library that supports access control models like ACL, RBAC, ABAC in .NET (C#)
Stars: ✭ 535 (+435%)
Mutual labels:  acl, rbac, casbin
casbin-ex
An authorization library that supports access control models like ACL, RBAC, ABAC in Elixir
Stars: ✭ 37 (-63%)
Mutual labels:  acl, rbac, casbin
Casbin4D
An authorization library that supports access control models like ACL, RBAC, ABAC in Delphi
Stars: ✭ 25 (-75%)
Mutual labels:  acl, rbac, casbin
Pycasbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Python
Stars: ✭ 625 (+525%)
Mutual labels:  acl, rbac, casbin
Casbin Cpp
An authorization library that supports access control models like ACL, RBAC, ABAC in C/C++
Stars: ✭ 113 (+13%)
Mutual labels:  acl, rbac, casbin

flask-authz

Build Status Coverage Status Version PyPI - Wheel Pyversions Download Gitter

flask-authz is an authorization middleware for Flask, it's based on PyCasbin.

Installation

pip install flask-authz

Or clone the repo:

$ git clone https://github.com/pycasbin/flask-authz.git
$ python setup.py install

Module Usage:

from flask import Flask
from flask_authz import CasbinEnforcer
from casbin.persist.adapters import FileAdapter

app = Flask(__name__)
# Set up Casbin model config
app.config['CASBIN_MODEL'] = 'casbinmodel.conf'
# Set headers where owner for enforcement policy should be located
app.config['CASBIN_OWNER_HEADERS'] = {'X-User', 'X-Group'}
# Add User Audit Logging with user name associated to log
# i.e. `[2020-11-10 12:55:06,060] ERROR in casbin_enforcer: Unauthorized attempt: method: GET resource: /api/v1/item by user: [email protected]`
app.config['CASBIN_USER_NAME_HEADERS'] = {'X-User'}
# Set up Casbin Adapter
adapter = FileAdapter('rbac_policy.csv')
casbin_enforcer = CasbinEnforcer(app, adapter)

@app.route('/', methods=['GET'])
@casbin_enforcer.enforcer
def get_root():
    return jsonify({'message': 'If you see this you have access'})

@app.route('/manager', methods=['POST'])
@casbin_enforcer.enforcer
@casbin_enforcer.manager
def make_casbin_change(manager):
    # Manager is an casbin.enforcer.Enforcer object to make changes to Casbin
    return jsonify({'message': 'If you see this you have access'})

Example Config This example file can be found in tests/casbin_files

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = (p.sub == "*" || g(r.sub, p.sub)) && r.obj == p.obj && (p.act == "*" || r.act == p.act)

Example Policy This example file can be found in tests/casbin_files

p, alice, /dataset1/*, GET
p, alice, /dataset1/resource1, POST
p, bob, /dataset2/resource1, *
p, bob, /dataset2/resource2, GET
p, bob, /dataset2/folder1/*, POST
p, dataset1_admin, /dataset1/*, *
p, *, /login, *

p, anonymous, /, GET

g, cathy, dataset1_admin

Development

Run unit tests

  1. Fork/Clone repository
  2. Install flask-authz dependencies, and run pytest
pip install -r dev_requirements.txt
pip install -r requirements.txt
pytest

Setup pre-commit checks

pre-commit install

update requirements with pip-tools

# update requirements.txt
pip-compile --no-annotate --no-header --rebuild requirements.in
# sync venv
pip-sync

Manually Bump Version

bumpversion major  # major release
or
bumpversion minor  # minor release
or
bumpversion patch  # hotfix release

Documentation

The authorization determines a request based on {subject, object, action}, which means what subject can perform what action on what object. In this plugin, the meanings are:

  1. subject: the logged-in user name
  2. object: the URL path for the web resource like "dataset1/item1"
  3. action: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", "write-blog"

For how to write authorization policy and other details, please refer to the Casbin's documentation.

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].