Skip to content

Commit

Permalink
improve missing header error
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Nov 28, 2024
1 parent 1214246 commit a737258
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions app/resources/api_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from functools import wraps
from flask import request
from flask_api import status
from jwt.exceptions import PyJWTError, ExpiredSignatureError
from jwt.exceptions import PyJWTError, ExpiredSignatureError, InvalidTokenError

from resources.connections import JWT_SECRET

Expand All @@ -15,9 +15,13 @@ def wrapper(f):
def decorator_f(*args, **kwargs):
try:
if JWT_SECRET:
auth_type, auth_token = request.headers.get(
"Authorization", ""
).split()
authorization = request.headers.get("Authorization", "").split()

if len(authorization) != 2:
raise InvalidTokenError()

auth_token = authorization[1]

jwt.decode(
jwt=auth_token,
key=JWT_SECRET,
Expand All @@ -33,6 +37,12 @@ def decorator_f(*args, **kwargs):
"message": "Token expirado",
}, status.HTTP_401_UNAUTHORIZED

except InvalidTokenError:
return {
"status": "error",
"message": "Token inválido",
}, status.HTTP_401_UNAUTHORIZED

except PyJWTError as e:
logging.basicConfig()
logger = logging.getLogger("noharm.getname")
Expand Down

0 comments on commit a737258

Please sign in to comment.