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
| @APP.route('/auth', methods=['POST'])
def auth():
"""
Create JWT token based on email.
"""
request_data = request.get_json()
if request_data is None:
print("request returned None")
else:
print("doing somethings")
email = request_data.get('email')
password = request_data.get('password')
if not email:
LOG.error("No email provided")
return jsonify({"message": "Missing parameter: email"}, 400)
if not password:
LOG.error("No password provided")
return jsonify({"message": "Missing parameter: password"}, 400)
body = {'email': email, 'password': password}
user_data = body
print('user_data : ',user_data)
#return jsonify(token=_get_jwt(user_data).decode('utf-8'))
return jsonify(token=_get_jwt(user_data))
@APP.route('/contents', methods=['GET'])
def decode_jwt():
"""
Check user token and return non-secret data
"""
if not 'Authorization' in request.headers:
abort(401)
data = request.headers['Authorization']
token = str.replace(str(data), 'Bearer ', '')
try:
data = jwt.decode(token, JWT_SECRET, algorithms=['HS256'])
except: # pylint: disable=bare-except
abort(401)
response = {'email': data['email'],
'exp': data['exp'],
'nbf': data['nbf'] }
return jsonify(**response)
def _get_jwt(user_data):
exp_time = datetime.datetime.utcnow() + datetime.timedelta(weeks=2)
payload = {'exp': exp_time,
'nbf': datetime.datetime.utcnow(),
'email': user_data['email']}
return jwt.encode(payload, JWT_SECRET, algorithm='HS256')
if __name__ == '__main__':
APP.run(host='127.0.0.1', port=8080, debug=True) |
Partager