Skip to content

Commit

Permalink
auth_jwt: clarify exceptions
Browse files Browse the repository at this point in the history
Distinguish errors that lead to a 401
from internal configuration errors.
  • Loading branch information
sbidoul committed Jun 8, 2023
1 parent 8d2c85b commit 4b75df2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions auth_jwt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class UnauthorizedPartnerNotFound(Unauthorized):
pass


class CompositeJwtError(Unauthorized):
class UnauthorizedCompositeJwtError(Unauthorized):
"""Indicate that multiple errors occurred during JWT chain validation."""

def __init__(self, errors):
Expand All @@ -50,5 +50,5 @@ def __init__(self, errors):
)


class UnauthorizedConfigurationError(Unauthorized):
class ConfigurationError(InternalServerError):
pass
13 changes: 7 additions & 6 deletions auth_jwt/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from odoo.http import request

from ..exceptions import (
CompositeJwtError,
UnauthorizedConfigurationError,
ConfigurationError,
Unauthorized,
UnauthorizedCompositeJwtError,
UnauthorizedMalformedAuthorizationHeader,
UnauthorizedMissingAuthorizationHeader,
UnauthorizedMissingCookie,
Expand Down Expand Up @@ -61,7 +62,7 @@ def _get_jwt_cookie_secret(cls):
secret = request.env["ir.config_parameter"].sudo().get_param("database.secret")
if not secret:
_logger.error("database.secret system parameter is not set.")
raise UnauthorizedConfigurationError()
raise ConfigurationError()
return secret

@classmethod
Expand Down Expand Up @@ -94,19 +95,19 @@ def _auth_method_jwt(cls, validator_name=None):
try:
payload = cls._get_jwt_payload(validator)
break
except Exception as e:
except Unauthorized as e:
exceptions[validator.name] = e
validator = validator.next_validator_id

if not payload:
if len(exceptions) == 1:
raise list(exceptions.values())[0]
raise CompositeJwtError(exceptions)
raise UnauthorizedCompositeJwtError(exceptions)

if validator.cookie_enabled:
if not validator.cookie_name:
_logger.info("Cookie name not set for validator %s", validator.name)
raise UnauthorizedConfigurationError()
raise ConfigurationError()
request.future_response.set_cookie(
key=validator.cookie_name,
value=validator._encode(
Expand Down
4 changes: 2 additions & 2 deletions auth_jwt/tests/test_auth_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

from ..exceptions import (
AmbiguousJwtValidator,
CompositeJwtError,
JwtValidatorNotFound,
UnauthorizedCompositeJwtError,
UnauthorizedInvalidToken,
UnauthorizedMalformedAuthorizationHeader,
UnauthorizedMissingAuthorizationHeader,
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_auth_method_invalid_token_on_chain(self):

authorization = "Bearer " + self._create_token()
with self._mock_request(authorization=authorization):
with self.assertRaises(CompositeJwtError) as composite_error:
with self.assertRaises(UnauthorizedCompositeJwtError) as composite_error:
self.env["ir.http"]._auth_method_jwt_validator()
self.assertEqual(
str(composite_error.exception),
Expand Down

0 comments on commit 4b75df2

Please sign in to comment.