I have a Python application :
main.py
import os
import logging
import datetime
import functools
import jwt
# pylint: disable=import-error
from flask import Flask, jsonify, request, abort
"""
A simple app to create a JWT token.
"""
JWT_SECRET = os.environ.get('JWT_SECRET', 'myjwtsecret')
LOG_LEVEL = os.environ.get('LOG_LEVEL', 'DEBUG')
def _logger():
'''
Setup logger format, level, and handler.
RETURNS: log object
'''
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log = logging.getLogger(__name__)
log.setLevel(LOG_LEVEL)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
log.addHandler(stream_handler)
return log
LOG = _logger()
LOG.debug("Starting with log level: %s" % LOG_LEVEL )
APP = Flask(__name__)
def require_jwt(function):
"""
Decorator to check valid jwt is present.
"""
@functools.wraps(function)
def decorated_function(*args, **kws):
if not 'Authorization' in request.headers:
abort(401)
data = request.headers['Authorization']
token = str.replace(str(data), 'Bearer ', '')
try:
jwt.decode(token, JWT_SECRET, algorithms=['HS256'])
except: # pylint: disable=bare-except
abort(401)
return function(*args, **kws)
return decorated_function
@APP.route('/', methods=['POST', 'GET'])
def health():
return jsonify("Healthy")
@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
#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)
executed under a Docker container:
The Docker image is created with:
docker build --tag jwt-api-test .
and executed with:
docker run -p 80:8080 --env-file=.env_file jwt-api-test
.env_file:
JWT_SECRET='myjwtsecret'
LOG_LEVEL=DEBUG
So when I test with Postman:
localhost:80/auth
With Body:
{
"Email": "test@test.com",
"Password": "pass"
}
and Header :
application/json
it returns to me the error:
500 Internal Server Error
And in the terminal window, or it was executed the Docker container, I can read:
File "/app/main.py", line 84, in auth
email = request_data.get('email')
AttributeError: 'NoneType' object has no attribute 'get'
And when I test in a second terminal the following CURL command:
export TOKEN=`curl --data '{"email":"test@test.com","password":"pass"}' --header "Content-Type: application/json" -X POST localhost:80/auth | jq -r '.token'`
I receive the error:
Connection refused
I do not know if it's a python code error or it's a different IP from the Docker container or anothing error ?
I need your help please.
Thanks.