mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 15:31:15 -05:00
Give specifc error when service doesn’t exist
If you sign a token with a service ID that doesn’t exist (say, for example, that you get service ID and API key mixed up) then you get an error saying that “no API keys exist for the service”. This is wrong because the service doesn’t even exist. This commit adds: - code to check if the service does exist - a specific error message for this case The check does mean an extra database call to look up the service. However this only happens _after_ looping through all the API keys. So it shouldn’t have a performance implication for anyone using a valid API key.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
from flask import request, jsonify, _request_ctx_stack, current_app
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from notifications_python_client.authentication import decode_jwt_token, get_token_issuer
|
||||
from notifications_python_client.errors import TokenDecodeError, TokenExpiredError
|
||||
|
||||
from app.dao.api_key_dao import get_model_api_keys
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
|
||||
|
||||
class AuthError(Exception):
|
||||
@@ -48,6 +51,11 @@ def requires_auth():
|
||||
_request_ctx_stack.top.api_user = api_key
|
||||
return
|
||||
|
||||
try:
|
||||
dao_fetch_service_by_id(client)
|
||||
except NoResultFound:
|
||||
raise AuthError("Invalid token: service not found", 403)
|
||||
|
||||
if not api_keys:
|
||||
raise AuthError("Invalid token: no api keys for service", 403)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user