Skip to content

Commit

Permalink
#61: Handle no credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Sep 16, 2019
1 parent ccef03e commit e1cda06
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from flask_restful import reqparse
from sqlalchemy.exc import IntegrityError

from common.exceptions import MissingRecordError, BadFilterError, AuthenticationError, BadRequestError
from common.exceptions import MissingRecordError, BadFilterError, AuthenticationError, BadRequestError, \
MissingCredentialsError
from common.models.db_models import SESSION
from common.session_manager import session_manager

Expand Down Expand Up @@ -38,9 +39,12 @@ def wrapper_requires_session(*args, **kwargs):
log.info(" Could not authenticate consumer, closing DB session")
session.close()
return "Forbidden", 403
except MissingCredentialsError:
return "Unauthorized", 401
except AuthenticationError:
return "Forbidden", 403


return wrapper_requires_session


Expand Down Expand Up @@ -88,7 +92,7 @@ def get_session_id_from_auth_header():
args = parser.parse_args()
auth_header = args["Authorization"].split(" ") if args["Authorization"] is not None else ""
if auth_header == "":
return ""
raise MissingCredentialsError(f"No credentials provided in auth header")
if len(auth_header) != 2 or auth_header[0] != "Bearer":
raise AuthenticationError(f" Could not authenticate consumer with auth header {auth_header}")
return auth_header[1]
Expand Down

0 comments on commit e1cda06

Please sign in to comment.