-
Notifications
You must be signed in to change notification settings - Fork 1
/
attendance.py
36 lines (30 loc) · 1.16 KB
/
attendance.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
from jose import jwt
import datetime
from models import BlacklistToken
# This method will work as jwt encoding to generate JWT token for attendance
def generate_attendance_code(course_id, time_in_minutes, secret_key='None'):
try:
payload = {
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=time_in_minutes),
'iat': datetime.datetime.utcnow(),
'course_id': course_id
}
return jwt.encode(
payload,
secret_key,
algorithm='HS256'
)
except Exception as e:
return e
def verify_attendance_code(secret_key, attendance_token):
try:
payload = jwt.decode(attendance_token, secret_key)
is_blacklisted_token = BlacklistToken.check_blacklist(attendance_token)
if is_blacklisted_token:
return 'Token blacklisted. Please ask teacher to generate new one'
else:
return payload
except jwt.ExpiredSignatureError:
return 'Signature expired. Please ask teacher to generate new one'
except jwt.InvalidTokenError:
return 'Invalid token. Please ask teacher to generate new one'