2016-02-08 11:10:54 +00:00
|
|
|
from flask import request, jsonify, _request_ctx_stack, current_app
|
2016-02-09 18:48:02 +00:00
|
|
|
from notifications_python_client.authentication import decode_jwt_token, get_token_issuer
|
2016-04-14 18:12:33 +01:00
|
|
|
from notifications_python_client.errors import TokenDecodeError, TokenExpiredError
|
2016-06-29 14:15:32 +01:00
|
|
|
|
2016-06-29 16:33:02 +01:00
|
|
|
from app.dao.api_key_dao import get_model_api_keys
|
2016-01-15 16:22:03 +00:00
|
|
|
|
2015-12-15 10:47:20 +00:00
|
|
|
|
|
|
|
|
def authentication_response(message, code):
|
2016-03-14 15:51:04 +00:00
|
|
|
return jsonify(result='error',
|
2016-06-17 14:22:58 +01:00
|
|
|
message={"token": [message]}
|
2016-03-14 15:51:04 +00:00
|
|
|
), code
|
2015-12-15 10:47:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def requires_auth():
|
|
|
|
|
auth_header = request.headers.get('Authorization', None)
|
|
|
|
|
if not auth_header:
|
|
|
|
|
return authentication_response('Unauthorized, authentication token must be provided', 401)
|
|
|
|
|
|
|
|
|
|
auth_scheme = auth_header[:7]
|
|
|
|
|
|
|
|
|
|
if auth_scheme != 'Bearer ':
|
|
|
|
|
return authentication_response('Unauthorized, authentication bearer scheme must be used', 401)
|
|
|
|
|
|
2016-01-19 18:25:21 +00:00
|
|
|
auth_token = auth_header[7:]
|
2015-12-15 10:47:20 +00:00
|
|
|
try:
|
2016-06-29 16:33:02 +01:00
|
|
|
client = get_token_issuer(auth_token)
|
2015-12-15 10:47:20 +00:00
|
|
|
except TokenDecodeError:
|
|
|
|
|
return authentication_response("Invalid token: signature", 403)
|
2016-02-08 11:10:54 +00:00
|
|
|
|
2016-06-29 16:33:02 +01:00
|
|
|
if client == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
|
|
|
|
errors_resp = get_decode_errors(auth_token, current_app.config.get('ADMIN_CLIENT_SECRET'), expiry_date=None)
|
|
|
|
|
return errors_resp
|
|
|
|
|
|
|
|
|
|
secret_keys = get_model_api_keys(client)
|
|
|
|
|
for api_key in secret_keys:
|
|
|
|
|
errors_resp = get_decode_errors(auth_token, api_key.unsigned_secret, api_key.expiry_date)
|
|
|
|
|
if not errors_resp:
|
|
|
|
|
if api_key.expiry_date:
|
|
|
|
|
return authentication_response("Invalid token: revoked", 403)
|
|
|
|
|
else:
|
|
|
|
|
_request_ctx_stack.top.api_user = api_key
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if not secret_keys:
|
2016-06-17 14:22:58 +01:00
|
|
|
errors_resp = authentication_response("Invalid token: no api keys for service", 403)
|
2016-05-04 16:08:23 +01:00
|
|
|
current_app.logger.info(errors_resp)
|
2016-01-19 18:25:21 +00:00
|
|
|
return errors_resp
|
2015-12-15 10:47:20 +00:00
|
|
|
|
|
|
|
|
|
2016-06-29 16:33:02 +01:00
|
|
|
def get_decode_errors(auth_token, unsigned_secret, expiry_date=None):
|
|
|
|
|
try:
|
|
|
|
|
decode_jwt_token(auth_token, unsigned_secret)
|
|
|
|
|
except TokenExpiredError:
|
|
|
|
|
return authentication_response("Invalid token: expired", 403)
|
|
|
|
|
except TokenDecodeError:
|
|
|
|
|
return authentication_response("Invalid token: signature", 403)
|
2016-01-19 14:01:26 +00:00
|
|
|
else:
|
2016-06-29 16:33:02 +01:00
|
|
|
return None
|