-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenTest.py
More file actions
46 lines (35 loc) · 1.19 KB
/
Copy pathtokenTest.py
File metadata and controls
46 lines (35 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
36
37
38
39
40
41
42
43
44
45
46
from flask import Flask, jsonify,request,make_response
import jwt
import datetime
from functools import wraps
app = Flask(__name__)
app.config['SECRET_KEY'] = 'thisisthesecetkey'
def token_required(f):
@wraps(f)
def decorated(*args,**kwargs):
token = request.args.get('token')# get token
if not token:
return(jsonify({'result':'token is missing'}))
try:
data = jwt.decode(token,app.config['SECRET_KEY'])#decode token if false throw error
except:
return(jsonify({'result':'Token is invalid'}))
return(f(*args,**kwargs))
return decorated
@app.route('/unprotected')
def uprotected():
return(jsonify({'result':'unprotected'}))
@app.route('/protected')
@token_required
def protected():
return(jsonify({'result':'protected'}))
@app.route('/login')
def login():
auth = request.authorization
if auth and auth.password == 'passwords':
token = jwt.encode({'user':auth.username,'exp':datetime.datetime.utcnow()+datetime.timedelta(seconds=35)},
app.config['SECRET_KEY'])#create token
return jsonify({'token':token.decode('UTF-8')})
return(make_response('could not varify',401,{'www-Authenticate':'Basic realm="Login-Required"'}))
if __name__ == '__main__':
app.run(debug = True)