Improve the error message when the service id is not the right data type.

Improve the error message is the api key is not valid.
This commit is contained in:
Rebecca Law
2016-09-23 11:07:49 +01:00
parent 281323b435
commit f5aac5796c
2 changed files with 29 additions and 22 deletions

View File

@@ -1,4 +1,5 @@
from flask import request, jsonify, _request_ctx_stack, current_app
from flask import request, _request_ctx_stack, current_app
from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
from notifications_python_client.authentication import decode_jwt_token, get_token_issuer
@@ -37,8 +38,10 @@ def requires_auth():
if client == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
return handle_admin_key(auth_token, current_app.config.get('ADMIN_CLIENT_SECRET'))
api_keys = get_model_api_keys(client)
try:
api_keys = get_model_api_keys(client)
except DataError:
raise AuthError("Invalid token: service id is not the right data type", 403)
for api_key in api_keys:
try:
get_decode_errors(auth_token, api_key.unsigned_secret)
@@ -59,19 +62,19 @@ def requires_auth():
if not api_keys:
raise AuthError("Invalid token: service has no API keys", 403)
else:
raise AuthError("Invalid token: signature", 403)
raise AuthError("Invalid token: signature, api token is not valid", 403)
def handle_admin_key(auth_token, secret):
try:
get_decode_errors(auth_token, secret)
return
except TokenDecodeError as e:
except TokenDecodeError:
raise AuthError("Invalid token: signature", 403)
def get_decode_errors(auth_token, unsigned_secret):
try:
decode_jwt_token(auth_token, unsigned_secret)
except TokenExpiredError as e:
except TokenExpiredError:
raise AuthError("Invalid token: expired", 403)