-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
129 lines (114 loc) · 4.92 KB
/
utils.py
File metadata and controls
129 lines (114 loc) · 4.92 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
The file defines some tool functions which will be used in the blueprints.
"""
from authlib.jose import jwt, JoseError
from functools import wraps
from config import SECRET_KEY
from flask import request, jsonify
import re
from models import Employee, Employer
def generateToken(email):
"""
Generate a token for the user with the email.
"""
header = {'alg': 'HS256'}
payload = {'email': email}
token = jwt.encode(header, payload, SECRET_KEY)
tokenStr = str(token, encoding='utf-8')
return tokenStr
def verifyToken(func):
"""
Define a decorator to check the token: employee and employer
"""
def wrapper(self, userType, *args, **kwargs):
tokenStr = request.headers.get('Authorization') # get the token from the header
if tokenStr is None: # no token
return jsonify({'status': 410, 'msg': 'Please log in first!'})
token = tokenStr[9:]
token = bytes(token, encoding="utf8") # convert the token to bytes
if token:
try:
payload = jwt.decode(token, SECRET_KEY) # decode the token
if userType == 'employee': # check the user type
employee = Employee.query.filter_by(email=payload['email']).first()
if employee and employee.logged: # check if the user has logged in
return func(self, userType) # call the function
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
elif userType == 'employer': # check the user type
employer = Employer.query.filter_by(email=payload['email']).first()
if employer and employer.logged: # check if the user has logged in
return func(self, userType) # call the function
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
except JoseError as e:
return jsonify({'status': 409, 'msg': 'Please log in first!'})
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
return wrapper
def verifyEmployeeToken(func):
"""
Define a decorator to check the token: employee.
"""
@wraps(func)
def wrapper(self, *args, **kwargs):
tokenStr = request.headers.get('Authorization') # get the token from the header
if tokenStr is None: # no token
return jsonify({'status': 410, 'msg': 'Please log in first!'})
token = tokenStr[9:]
token = bytes(token, encoding="utf8") # convert the token to bytes
if token:
try:
payload = jwt.decode(token, SECRET_KEY) # decode the token
employee = Employee.query.filter_by(email=payload['email']).first() # get the user
if employee and employee.logged: # check if the user has logged in
return func(self, *args, **kwargs) # call the function
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
except JoseError as e:
return jsonify({'status': 409, 'msg': 'Please log in first!'})
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
return wrapper
def verifyEmployerToken(func):
"""
Define a decorator to check the token: employer.
"""
@wraps(func)
def wrapper(self, *args, **kwargs):
tokenStr = request.headers.get('Authorization') # get the token from the header
if tokenStr is None: # no token
return jsonify({'status': 410, 'msg': 'Please log in first!'})
token = tokenStr[9:]
token = bytes(token, encoding="utf8") # convert the token to bytes
if token:
try:
payload = jwt.decode(token, SECRET_KEY) # decode the token
employer = Employer.query.filter_by(email=payload['email']).first() # get the user
if employer and employer.logged: # check if the user has logged in
return func(self, *args, **kwargs) # call the function
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
except JoseError as e:
return jsonify({'status': 409, 'msg': 'Please log in first!'})
else:
return jsonify({'status': 410, 'msg': 'Please log in first!'})
return wrapper
def emailByTokenStr(tokenStr):
"""
Get the email from the token string.
"""
token = bytes(tokenStr, encoding="utf8")
payload = jwt.decode(token, SECRET_KEY)
email = payload.get('email')
return email
def validTel(telStr):
"""
Check if the telephone number is valid.
"""
if re.match(r'^1[3-9]\d{9}$', telStr):
return True
else:
return False