- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotected.py
More file actions
Latest commit
35 lines (29 loc) · 1.19 KB
/
Copy pathprotected.py
File metadata and controls
35 lines (29 loc) · 1.19 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
fromflaskimportBlueprint, request
fromutilitiesimportcheckTokenValidity
importdatabase
#Protected endpoints that require a valid token to access
protected=Blueprint('protected', __name__)
#Gets the name associated with the session token.
@protected.get('/getName')
defgetToken():
try:
#throws exception if there is no authorization header
tokenheader=request.headers.get('Authorization')
#Authorization is by bearer token
if'Bearer 'intokenheader:
token=tokenheader.split()[1]
ifcheckTokenValidity(token):
#Token is valid will then attempt to get the name
try:
name=database.retrieveName(token)
if(name!=''):
return {"name": name}, 200
exceptExceptionase:
print(e)
return {"error": "Server error, please try again later."}, 500
else:
#Wrong format for authorization header
return {"error": "Check authorization headers"}, 401
except:
return {"error": "Check authorization headers"}, 401
return {"error": "Invalid token"}, 401