Skip to content

Commit

Permalink
Respect the json_provider_class
Browse files Browse the repository at this point in the history
Respect the json_provider_class when available and stay compatible
with flask < 2.2. Extended tox configuration to also test with flask < 2.2.
  • Loading branch information
jrast committed Aug 12, 2022
1 parent fcea059 commit 3f8c4b6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 40 deletions.
5 changes: 3 additions & 2 deletions flask_jwt_extended/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from json import JSONEncoder
from typing import Iterable
from typing import List
from typing import Optional
Expand All @@ -11,7 +12,7 @@
from flask import current_app
from jwt.algorithms import requires_cryptography

from flask_jwt_extended.json_encoder import JSONEncoder
from flask_jwt_extended.internal_utils import get_json_encoder
from flask_jwt_extended.typing import ExpiresDelta


Expand Down Expand Up @@ -284,7 +285,7 @@ def error_msg_key(self) -> str:

@property
def json_encoder(self) -> Type[JSONEncoder]:
return JSONEncoder
return get_json_encoder(current_app)

@property
def decode_audience(self) -> Union[str, Iterable[str]]:
Expand Down
41 changes: 41 additions & 0 deletions flask_jwt_extended/internal_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import json
from typing import Any
from typing import Type
from typing import TYPE_CHECKING

from flask import current_app
from flask import Flask

from flask_jwt_extended.exceptions import RevokedTokenError
from flask_jwt_extended.exceptions import UserClaimsVerificationError
from flask_jwt_extended.exceptions import WrongTokenError

try:
from flask.json.provider import DefaultJSONProvider

HAS_JSON_PROVIDER = True
except ModuleNotFoundError: # pragma: no cover
# The flask.json.provider module was added in Flask 2.2.
# Further details are handled in get_json_encoder.
HAS_JSON_PROVIDER = False


if TYPE_CHECKING: # pragma: no cover
from flask_jwt_extended import JWTManager

Expand Down Expand Up @@ -51,3 +64,31 @@ def custom_verification_for_token(jwt_header: dict, jwt_data: dict) -> None:
if not jwt_manager._token_verification_callback(jwt_header, jwt_data):
error_msg = "User claims verification failed"
raise UserClaimsVerificationError(error_msg, jwt_header, jwt_data)


def get_json_encoder(app: Flask) -> Type[json.JSONEncoder]:
"""Get the JSON Encoder for the provided flask app
Starting with flask version 2.2 the flask application provides a
interface to register a custom JSON Encoder/Decoder under the json_provider_class.
As this interface is not compatible with the standard JSONEncoder, the `default`
method of the class is wrapped.
Lookup Order:
- app.json_encoder - For Flask < 2.2
- app.json_provider_class.default
- flask.json.provider.DefaultJSONProvider.default
"""
if not HAS_JSON_PROVIDER: # pragma: no cover
return app.json_encoder

# If the registered JSON provider does not implement a default classmethod
# use the method defined by the DefaultJSONProvider
default = getattr(app.json_provider_class, "default", DefaultJSONProvider.default)

class JSONEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
return default(o) # pragma: no cover

return JSONEncoder
36 changes: 0 additions & 36 deletions flask_jwt_extended/json_encoder.py

This file was deleted.

2 changes: 1 addition & 1 deletion flask_jwt_extended/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import timedelta
from datetime import timezone
from hmac import compare_digest
from json import JSONEncoder
from typing import Any
from typing import Iterable
from typing import List
Expand All @@ -13,7 +14,6 @@

from flask_jwt_extended.exceptions import CSRFError
from flask_jwt_extended.exceptions import JWTDecodeError
from flask_jwt_extended.json_encoder import JSONEncoder
from flask_jwt_extended.typing import ExpiresDelta


Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = py37,py38,py39,py310,pypy3.9,mypy,coverage,style,docs
envlist = py{37,38,39,310}-{flask21,flask22},pypy3.9,mypy,coverage,style,docs

[testenv]
commands =
Expand Down

0 comments on commit 3f8c4b6

Please sign in to comment.