-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segurity.py
35 lines (31 loc) · 1.17 KB
/
Segurity.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
import datetime
import os
import jwt
import pytz
class Security():
secret=os.getenv('secreto')
tz = pytz.timezone("America/Lima")
@classmethod
def generate_token(cls, authenticated_user,perfil):
payload = {
'iat': datetime.datetime.now(tz=cls.tz),
'exp': datetime.datetime.now(tz=cls.tz) + datetime.timedelta(minutes=5),
'username': authenticated_user,
'roles': perfil
}
return jwt.encode(payload, cls.secret, algorithm="HS256")
@classmethod
def verify_token(cls, headers):
if 'Authorization' in headers.keys():
authorization = headers['Authorization']
encoded_token = authorization.split(" ")[1]
if (len(encoded_token) > 0):
try:
payload = jwt.decode(encoded_token, cls.secret, algorithms=["HS256"])
roles = payload['roles']
if roles:
return roles,True
return False
except (jwt.ExpiredSignatureError, jwt.InvalidSignatureError):
return False
return False