Skip to content

Commit cab0596

Browse files
committed
function authorization_decoder add parameters "config", implement JWT decoding.
1 parent 22be8b4 commit cab0596

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

flask_authz/casbin_enforcer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def wrapper(*args, **kwargs):
8787
if header == "authorization":
8888
# Get Auth Value then decode and parse for owner
8989
try:
90-
owner = authorization_decoder(request.headers.get(header))
90+
owner = authorization_decoder(self.app.config, request.headers.get(header))
9191
except UnSupportedAuthType:
9292
# Continue if catch unsupported type in the event of
9393
# Other headers needing to be checked
@@ -96,6 +96,9 @@ def wrapper(*args, **kwargs):
9696
"decoding is unsupported by flask-casbin at this time"
9797
)
9898
continue
99+
except Exception as e:
100+
self.app.logger.info(e)
101+
continue
99102

100103
if self.user_name_headers and header in map(str.lower, self.user_name_headers):
101104
owner_audit = owner

flask_authz/utils/auth_decoder.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from base64 import b64decode
22

3+
import jwt
4+
35

46
class UnSupportedAuthType(Exception):
57
status_code = 501
@@ -20,11 +22,12 @@ def to_dict(self):
2022
return rv
2123

2224

23-
def authorization_decoder(auth_str: str):
25+
def authorization_decoder(config, auth_str: str):
2426
"""
2527
Authorization token decoder based on type. This will decode the token and
2628
only return the owner
2729
Args:
30+
config: app.config
2831
auth_str: Authorization string should be in "<type> <token>" format
2932
Returns:
3033
decoded owner from token
@@ -35,6 +38,8 @@ def authorization_decoder(auth_str: str):
3538
"""Basic format <user>:<password> return only the user"""
3639
return b64decode(token).decode().split(":")[0]
3740
elif type == "Bearer":
38-
raise UnSupportedAuthType("Bearer is not implemented yet")
41+
decoded_jwt = jwt.decode(token, config.get("JWT_SECRET_KEY"),
42+
algorithms=config.get('JWT_HASH'))
43+
return decoded_jwt.get("identity", '')
3944
else:
4045
raise UnSupportedAuthType("%s Authorization is not supported" % type)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ flask>=0.12.2,~=1.1.2
44
itsdangerous==1.1.0
55
jinja2==2.11.2
66
markupsafe==1.1.1
7+
pyjwt==2.0.1
78
simpleeval==0.9.10
89
werkzeug==1.0.1

0 commit comments

Comments
 (0)