-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (38 loc) · 1.4 KB
/
app.py
File metadata and controls
55 lines (38 loc) · 1.4 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
from functools import wraps
from flask import Flask, request,make_response,jsonify
import jwt
import datetime
app = Flask(__name__)
app.config.update(
TESTING=True,
SECRET_KEY='U,+Hes3*F_D3*3c:#}?Y$SM:t$G!_G!gV)!A;mFX+c!>EuAF[T'
)
print(app.config['SECRET_KEY'])
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = request.args.get('token')
if not token:
return jsonify(message='token is missing'), 403
try:
data = jwt.decode(token, app.config['SECRET_KEY'],algorithms=["HS256"])
except:
return jsonify(message='token is invalid'), 403
return f(*args, **kwargs)
return decorated
@app.route('/unprotected ')
def unproctected():
return jsonify(message='unprotected')
@app.route('/protected ')
@token_required
def proctected():
return jsonify(message='protected but you have permission')
@app.route('/login')
def login():
auth = request.authorization
if auth and auth.password == 'admin' and auth.username == 'admin' :
token = jwt.encode({'user': auth.username, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30) }, app.config['SECRET_KEY'],algorithm="HS256")
return jsonify(token)
return make_response('Could not verify',401, {'WWW-Authenticate' : 'Basic realm="Login Required'})
if __name__ == '__main__':
app.run(debug=True)