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-03-01 13:30:10 +00:00
|
|
|
from werkzeug.exceptions import abort
|
2016-01-19 18:25:21 +00:00
|
|
|
from app.dao.api_key_dao import get_unsigned_secrets
|
2016-03-01 13:30:10 +00:00
|
|
|
from app import api_user
|
|
|
|
|
from functools import wraps
|
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:
|
|
|
|
|
api_client = fetch_client(get_token_issuer(auth_token))
|
|
|
|
|
except TokenDecodeError:
|
|
|
|
|
return authentication_response("Invalid token: signature", 403)
|
2016-02-08 11:10:54 +00:00
|
|
|
|
2016-01-19 18:25:21 +00:00
|
|
|
for secret in api_client['secret']:
|
|
|
|
|
try:
|
|
|
|
|
decode_jwt_token(
|
|
|
|
|
auth_token,
|
2016-05-04 16:08:23 +01:00
|
|
|
secret
|
2016-01-19 18:25:21 +00:00
|
|
|
)
|
|
|
|
|
_request_ctx_stack.top.api_user = api_client
|
|
|
|
|
return
|
|
|
|
|
except TokenExpiredError:
|
|
|
|
|
errors_resp = authentication_response("Invalid token: expired", 403)
|
|
|
|
|
except TokenDecodeError:
|
|
|
|
|
errors_resp = authentication_response("Invalid token: signature", 403)
|
|
|
|
|
|
2016-04-29 09:54:40 +01:00
|
|
|
if not api_client['secret']:
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_client(client):
|
2016-01-19 14:01:26 +00:00
|
|
|
if client == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
|
|
|
|
return {
|
|
|
|
|
"client": client,
|
2016-01-20 10:57:46 +00:00
|
|
|
"secret": [current_app.config.get('ADMIN_CLIENT_SECRET')]
|
2016-01-19 14:01:26 +00:00
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
return {
|
|
|
|
|
"client": client,
|
2016-01-19 18:25:21 +00:00
|
|
|
"secret": get_unsigned_secrets(client)
|
2016-01-19 14:01:26 +00:00
|
|
|
}
|