-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.py
98 lines (78 loc) · 2.72 KB
/
auth.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
from flask import request, _request_ctx_stack, abort
from functools import wraps
from jose import jwt
import datetime
from models import BlacklistToken
class AuthError(Exception):
def __init__(self, error, status_code):
self.error = error
self.status_code = status_code
def get_token_auth_header():
auth_header = request.headers.get("Authorization", None)
if not auth_header:
raise AuthError({"code": "authorization_header_missing",
"description":
"Authorization header is expected"}, 401)
header_parts = auth_header.split(' ')
if len(header_parts) != 2 or not header_parts:
raise AuthError({
'code': 'invalid_header',
'description': 'Authorization header must be in the format'
' Bearer token'}, 401)
elif header_parts[0].lower() != 'bearer':
raise AuthError({
'code': 'invalid_header',
'description': 'Authorization header must start with Bearer'}, 401)
return header_parts[1]
def check_permissions(permission, payload):
if 'permissions' not in payload:
abort(400)
if permission not in payload['permissions']:
abort(401)
return True
def encode_auth_token(secret_key, permission, user_id):
"""
Generates the Auth Token
:return: string
"""
try:
payload = {
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60),
'iat': datetime.datetime.utcnow(),
'permissions': permission,
'id': user_id
}
return jwt.encode(
payload,
secret_key,
algorithm='HS256'
)
except Exception as e:
return e
def decode_auth_token(secret_key, auth_token):
"""
Validates the auth token
:param auth_token:
:return: integer|string
"""
try:
payload = jwt.decode(auth_token, secret_key)
is_blacklisted_token = BlacklistToken.check_blacklist(auth_token)
if is_blacklisted_token:
return 'Token blacklisted. Please log in again.'
else:
return payload
except jwt.ExpiredSignatureError:
return 'Signature expired. Please log in again.'
except jwt.InvalidTokenError:
return 'Invalid token. Please log in again.'
def requires_auth(permission=''):
def requires_auth_decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
token = get_token_auth_header()
payload = decode_auth_token("random string", auth_token=token)
check_permissions(permission, payload)
return f(payload, *args, **kwargs)
return wrapper
return requires_auth_decorator