-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecure.py
36 lines (31 loc) · 1.05 KB
/
secure.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
import jwt
from settings import JWT_SECRET
class UserTokens:
@classmethod
def create_token(self, username, room, anonymous=True):
"""
create json web token with username and room for rooms
Params:
username {string} - chosen name of user
room {string} - chosen room of user
anonymous - True if the user is not authenticated. False if they have authenticated. Default: True
Returns
token {string} - string representing payload on server side
"""
encoded_jwt = jwt.encode(
{
'username': username,
'room': room,
'anonymous': anonymous
},
JWT_SECRET,
algorithm='HS256'
)
return encoded_jwt.decode('utf-8')
@classmethod
def read_token(self, encoded_jwt):
"""
read json web token string and return decoded payload
"""
decoded_jwt = jwt.decode(encoded_jwt, JWT_SECRET, algorithms=['HS256'])
return decoded_jwt