-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.py
More file actions
35 lines (31 loc) · 1.04 KB
/
Copy pathauthenticate.py
File metadata and controls
35 lines (31 loc) · 1.04 KB
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
from functools import wraps
import jwt
from flask import request
from models.users import Users
from app_conf import SECRET_KEY
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
if "Authorization" in request.headers:
token = request.headers["Authorization"].split(" ")[1]
if not token:
return {
"message": "Authentication Token is missing!",
"error": "Unauthorized"
}, 401
try:
data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
current_user = Users.get_by_id(data["user_id"])
if current_user is None:
return {
"message": "Invalid Authentication token!",
"error": "Unauthorized"
}, 401
except Exception as e:
return {
"message": "Internal Server Error",
"error": str(e)
}, 500
return f(current_user, *args, **kwargs)
return decorated