Skip to content

Commit be63bcc

Browse files
committed
2 parents 468b40b + 845e8ce commit be63bcc

30 files changed

+1445
-248
lines changed

.bumpversion.cfg

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[bumpversion]
2+
current_version = 0.1.0
3+
commit = True
4+
tag = True
5+
6+
[bumpversion:file:setup.py]

.flake8

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[flake8]
2+
# Recommend matching the black line length (default 88),
3+
# rather than using the flake8 default of 79:
4+
max-line-length = 88
5+
extend-ignore =
6+
# See https://github.com/PyCQA/pycodestyle/issues/373
7+
E203,

.gitignore

+26-1
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,30 @@ venv.bak/
103103
# mypy
104104
.mypy_cache/
105105

106+
# pycharm
106107
.idea/
107-
*.iml
108+
*.iml
109+
110+
# Packages
111+
*.egg-info
112+
build
113+
eggs
114+
parts
115+
bin
116+
var
117+
sdist
118+
develop-eggs
119+
lib
120+
lib64
121+
122+
.tox
123+
124+
# Complexity
125+
output/*.html
126+
output/*/index.html
127+
128+
# Sphinx
129+
docs/_build
130+
131+
# Cookiecutter
132+
output/

.pre-commit-config.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
default_stages: [commit, push]
2+
fail_fast: true
3+
repos:
4+
- repo: https://github.com/ambv/black
5+
rev: stable
6+
hooks:
7+
- id: black
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v2.1.0
10+
hooks:
11+
- id: trailing-whitespace
12+
- repo: https://github.com/pre-commit/pre-commit-hooks
13+
rev: v2.1.0
14+
hooks:
15+
- id: flake8
16+
- repo: https://github.com/szebenyib/pre-commit-pytest
17+
rev: master
18+
hooks:
19+
- id: pytest

LICENSE.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2020 ScienceLogic, Inc
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

MANIFEST.in

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include README.md
2+
include requirements.txt

README.md

+66-35
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,59 @@ flask-authz is an authorization middleware for [Flask](http://flask.pocoo.org/),
1515
```
1616
pip install flask-authz
1717
```
18+
Or clone the repo:
19+
```
20+
$ git clone https://github.com/pycasbin/flask-authz.git
21+
$ python setup.py install
22+
```
1823

19-
## Simple Example
20-
21-
This repo is just a working Flask app that shows the usage of flask-authz (see: https://github.com/pycasbin/flask-authz/blob/master/app.py). To use it in your existing Flask app, you need:
22-
24+
Module Usage:
2325
```python
24-
from authz.middleware import CasbinMiddleware
25-
import casbin
2626
from flask import Flask
27+
from flask_authz import CasbinEnforcer
28+
from casbin.persist.adapters import FileAdapter
2729

2830
app = Flask(__name__)
31+
# Set up Casbin model config
32+
app.config['CASBIN_MODEL'] = 'casbinmodel.conf'
33+
# Set headers where owner for enforcement policy should be located
34+
app.config['CASBIN_OWNER_HEADERS'] = {'X-User', 'X-Group'}
35+
# Set up Casbin Adapter
36+
adapter = FileAdapter('rbac_policy.csv')
37+
casbin_enforcer = CasbinEnforcer(app, adapter)
38+
39+
@app.route('/', methods=['GET'])
40+
@casbin_enforcer.enforcer
41+
def get_root():
42+
return jsonify({'message': 'If you see this you have access'})
43+
44+
@app.route('/manager', methods=['POST'])
45+
@casbin_enforcer.enforcer
46+
@casbin_enforcer.manager
47+
def make_casbin_change(manager):
48+
# Manager is an casbin.enforcer.Enforcer object to make changes to Casbin
49+
return jsonify({'message': 'If you see this you have access'})
50+
```
51+
Example Config
52+
This example file can be found in `tests/casbin_files`
53+
```ini
54+
[request_definition]
55+
r = sub, obj, act
2956

30-
# Initialize the Casbin enforcer, load the casbin model and policy from files.
31-
# Change the 2nd arg to use a database.
32-
enforcer = casbin.Enforcer("authz_model.conf", "authz_policy.csv")
33-
34-
app.wsgi_app = CasbinMiddleware(app.wsgi_app, enforcer)
35-
57+
[policy_definition]
58+
p = sub, obj, act
3659

37-
@app.route("/")
38-
def hello_world():
39-
return "Hello World!"
60+
[role_definition]
61+
g = _, _
4062

63+
[policy_effect]
64+
e = some(where (p.eft == allow))
4165

42-
if __name__ == '__main__':
43-
app.run()
66+
[matchers]
67+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
4468
```
45-
46-
- The default policy ``authz_policy.csv`` is:
47-
69+
Example Policy
70+
This example file can be found in `tests/casbin_files`
4871
```csv
4972
p, alice, /dataset1/*, GET
5073
p, alice, /dataset1/resource1, POST
@@ -59,24 +82,31 @@ p, anonymous, /, GET
5982
g, cathy, dataset1_admin
6083
```
6184

62-
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.
63-
64-
## How are subject, object, action defined?
65-
66-
In ``middleware.py``:
67-
85+
Development
86+
------------
87+
1. Fork
88+
2. Install Dev ENV
6889
```python
69-
def check_permission(self, request):
70-
# change the user, path, method as you need.
71-
user = request.remote_user # subject
72-
if user is None:
73-
user = 'anonymous'
74-
path = request.path # object
75-
method = request.method # action
76-
return self.enforcer.enforce(user, path, method)
90+
# Install Flask-Casbin with Dev packages
91+
pip install -r dev_requirements.txt
92+
pip install -r requirements.txt
93+
pip install -e .
94+
# Install Pre-commits
95+
pre-commit install
96+
# Create feature branch
97+
git checkout -b feature-more-cool-stuff
98+
# Code stuff
7799
```
100+
Then push your changes and create a PR
78101

79-
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.
102+
#### Manually Bump Version
103+
```
104+
bumpversion major # major release
105+
or
106+
bumpversion minor # minor release
107+
or
108+
bumpversion patch # hotfix release
109+
```
80110

81111
## Documentation
82112

@@ -95,3 +125,4 @@ For how to write authorization policy and other details, please refer to [the Ca
95125
## License
96126

97127
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.
128+

app.py

-27
This file was deleted.

authz/__init__.py

Whitespace-only changes.

authz/middleware.py

-43
This file was deleted.

dev_requirements.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pre-commit
2+
black
3+
sphinx
4+
sphinx_rtd_theme
5+
pytest
6+
pytest-cov
7+
casbin_sqlalchemy_adapter
8+
coverage
9+
pypi-publisher
10+
bumpversion

0 commit comments

Comments
 (0)