Skip to main content

An authorization middleware for Flask that supports ACL, RBAC, ABAC, based on Casbin

Project description

flask-authz

Gitter

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

Installation

pip install flask-authz

Simple Example

This repo is just a working Flask app that shows the usage of flask-authz. To use it in your existing Flask app, you need:

from authz.middleware import CasbinMiddleware
import casbin
from flask import Flask

app = Flask(__name__)

# Initialize the Casbin enforcer, load the casbin model and policy from files.
# Change the 2nd arg to use a database.
enforcer = casbin.Enforcer("authz_model.conf", "authz_policy.csv")

app.wsgi_app = CasbinMiddleware(app.wsgi_app, enforcer)


@app.route("/")
def hello_world():
    return "Hello World!"


if __name__ == '__main__':
    app.run()
  • The default policy authz_policy.csv is:
p, anonymous, /, GET
p, admin, *, *
g, alice, admin

It means anonymous user can only access homepage /. Admin users like alice can access any pages. Currently all accesses are regarded as anonymous. Add your authentication to let a user log in.

How are subject, object, action defined?

In middleware.py:

def check_permission(self, request):
    # change the user, path, method as you need.
    user = request.remote_user # subject
    if user is None:
        user = 'anonymous'
    path = request.path # object
    method = request.method # action
    return self.enforcer.enforce(user, path, method)

You may need to copy the middleware.py code to your project and modify it directly if you have other definitions for subject, object, action.

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.

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-authz-0.0.1.tar.gz (3.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_authz-0.0.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file flask-authz-0.0.1.tar.gz.

File metadata

  • Download URL: flask-authz-0.0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for flask-authz-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0f181060e7214dad0db96d91ba19985c7a6d9f1e513b5de8c644d11ed0f4a0f0
MD5 10dba3526ce0adba566070f027900a83
BLAKE2b-256 989c57713744cbc6f5d9bda04356bc841780b9bb9325140b279595339cd2b92e

See more details on using hashes here.

File details

Details for the file flask_authz-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: flask_authz-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for flask_authz-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d6d39d165cdfe81009c148250f9bde110850ddf86a6dc00a14e791028d4899c4
MD5 9f2e7d3e0a99f1bff098c041fb108788
BLAKE2b-256 34d8f9364a03b99467ef0f1b6b7f86da3d2ab001326bf1598669038bba8842f7

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