-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcasbin_enforcer.py
228 lines (204 loc) · 8.31 KB
/
casbin_enforcer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""flask-casbin: Flask module for using Casbin with flask apps
"""
import casbin
from flask import request, jsonify
from functools import wraps
from abc import ABC
from abc import abstractmethod
import shlex
from flask_authz.utils import authorization_decoder, UnSupportedAuthType
class CasbinEnforcer:
"""
Casbin Enforce decorator
"""
e = None
def __init__(self, app=None, adapter=None, watcher=None):
"""
Args:
app (object): Flask App object to get Casbin Model
adapter (object): Casbin Adapter
"""
self.app = app
self.adapter = adapter
self.e = None
self.watcher = watcher
self._owner_loader = None
self.user_name_headers = None
if self.app is not None:
self.init_app(self.app)
def init_app(self, app):
self.app = app
self.e = casbin.Enforcer(app.config.get("CASBIN_MODEL"), self.adapter)
if self.watcher:
self.e.set_watcher(self.watcher)
self.user_name_headers = app.config.get("CASBIN_USER_NAME_HEADERS", None)
def set_watcher(self, watcher):
"""
Set the watcher to use with the underlying casbin enforcer
Args:
watcher (object):
Returns:
None
"""
self.e.set_watcher(watcher)
def owner_loader(self, callback):
"""
This sets the callback for get owner. The
function return a owner object, or ``None``
:param callback: The callback for retrieving a owner object.
:type callback: callable
"""
self._owner_loader = callback
return callback
def enforcer(self, func, delimiter=","):
@wraps(func)
def wrapper(*args, **kwargs):
if self.e.watcher and self.e.watcher.should_reload():
self.e.watcher.update_callback()
# String used to hold the owners user name for audit logging
owner_audit = ""
# Check sub, obj act against Casbin polices
self.app.logger.debug(
"Enforce Headers Config: %s\nRequest Headers: %s"
% (self.app.config.get("CASBIN_OWNER_HEADERS"), request.headers)
)
# Set resource URI from request
uri = str(request.path)
# Get owner from owner_loader
if self._owner_loader:
self.app.logger.info("Get owner from owner_loader")
for owner in self._owner_loader():
owner = owner.strip('"') if isinstance(owner, str) else owner
if self.e.enforce(owner, uri, request.method):
return func(*args, **kwargs)
for header in map(str.lower, self.app.config.get("CASBIN_OWNER_HEADERS")):
if header in request.headers:
# Make Authorization Header Parser standard
if header == "authorization":
# Get Auth Value then decode and parse for owner
try:
owner = authorization_decoder(
self.app.config, request.headers.get(header)
)
except UnSupportedAuthType:
# Continue if catch unsupported type in the event of
# Other headers needing to be checked
self.app.logger.info(
"Authorization header type requested for "
"decoding is unsupported by flask-casbin at this time"
)
continue
except Exception as e:
self.app.logger.info(e)
continue
if self.user_name_headers and header in map(
str.lower, self.user_name_headers
):
owner_audit = owner
if self.e.enforce(owner, uri, request.method):
self.app.logger.info(
"access granted: method: %s resource: %s%s"
% (
request.method,
uri,
""
if not self.user_name_headers and owner_audit != ""
else " to user: %s" % owner_audit,
)
)
return func(*args, **kwargs)
else:
# Split header by ',' in case of groups when groups are
# sent "group1,group2,group3,..." in the header
for owner in self.sanitize_group_headers(
request.headers.get(header), delimiter
):
self.app.logger.debug(
"Enforce against owner: %s header: %s"
% (owner.strip('"'), header)
)
if self.user_name_headers and header in map(
str.lower, self.user_name_headers
):
owner_audit = owner
if self.e.enforce(owner.strip('"'), uri, request.method):
self.app.logger.info(
"access granted: method: %s resource: %s%s"
% (
request.method,
uri,
""
if not self.user_name_headers
and owner_audit != ""
else " to user: %s" % owner_audit,
)
)
return func(*args, **kwargs)
else:
self.app.logger.error(
"Unauthorized attempt: method: %s resource: %s%s"
% (
request.method,
uri,
""
if not self.user_name_headers and owner_audit != ""
else " by user: %s" % owner_audit,
)
)
return (jsonify({"message": "Unauthorized"}), 401)
return wrapper
@staticmethod
def sanitize_group_headers(headers_str, delimiter=",") -> list:
"""
Sanitizes group header string so that it is easily parsable by enforcer
removes extra spaces, and converts comma delimited or white space
delimited list into a list.
Default delimiter: "," (comma)
Returns:
list
"""
if delimiter == " " and (
(headers_str.startswith("'") and headers_str.endswith("'"))
or (headers_str.startswith('"') and headers_str.endswith('"'))
):
return [
string.strip() for string in shlex.split(headers_str) if string != ""
]
return [
string.strip() for string in headers_str.split(delimiter) if string != ""
]
def manager(self, func):
"""Get the Casbin Enforcer Object to manager Casbin"""
@wraps(func)
def wrapper(*args, **kwargs):
return func(self.e, *args, **kwargs)
return wrapper
class Watcher(ABC):
"""
Watcher interface as it should be implemented for flask-casbin
"""
@abstractmethod
def update(self):
"""
Watcher interface as it should be implemented for flask-casbin
Returns:
None
"""
pass
@abstractmethod
def set_update_callback(self):
"""
Set the update callback to be used when an update is detected
Returns:
None
"""
pass
@abstractmethod
def should_reload(self):
"""
Method which checks if there is an update necessary for the casbin
roles. This is called with each flask request.
Returns:
Bool
"""
pass