Skip to content

Commit ce2f53c

Browse files
authored
fix: treat invalid jwt as 401 (#444)
1 parent 86e03fb commit ce2f53c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

components/renku_data_services/base_api/error_handler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sqlite3 import Error as SqliteError
88
from typing import Any, Optional, Protocol, TypeVar, Union
99

10+
import jwt
1011
from asyncpg import exceptions as postgres_exceptions
1112
from pydantic import ValidationError as PydanticValidationError
1213
from sanic import HTTPResponse, Request, SanicException, json
@@ -129,6 +130,8 @@ def default(self, request: Request, exception: Exception) -> HTTPResponse:
129130
formatted_exception = errors.ValidationError(
130131
message="The provided input is too large to be stored in the database"
131132
)
133+
case jwt.exceptions.InvalidTokenError():
134+
formatted_exception = errors.InvalidTokenError()
132135
self.log(request, formatted_exception)
133136
if formatted_exception.status_code == 500 and "PYTEST_CURRENT_TEST" in os.environ:
134137
# TODO: Figure out how to do logging properly in here, I could not get the sanic logs to show up from here

components/renku_data_services/errors/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class UnauthorizedError(BaseError):
4545
quiet: bool = True
4646

4747

48+
@dataclass
49+
class InvalidTokenError(UnauthorizedError):
50+
"""The supplied jwt is invalid."""
51+
52+
message: str = "The supplied credentials (jwt) are not valid."
53+
54+
4855
@dataclass
4956
class ForbiddenError(BaseError):
5057
"""Raised when the provided credentials do not grant permission for the current operation."""

0 commit comments

Comments
 (0)