Skip to content

Commit

Permalink
[#138] pass app json_encoder to jwt.encode
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Story <[email protected]>
  • Loading branch information
matthewstory committed Apr 11, 2018
1 parent e1ab258 commit a860550
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions flask_jwt_extended/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,8 @@ def user_claims_key(self):
def exempt_methods(self):
return {"OPTIONS"}

@property
def json_encoder(self):
return current_app.json_encoder

config = _Config()
4 changes: 3 additions & 1 deletion flask_jwt_extended/jwt_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ def _create_refresh_token(self, identity, expires_delta=None):
expires_delta=expires_delta,
csrf=config.csrf_protect,
identity_claim_key=config.identity_claim_key,
json_encoder=config.json_encoder
)
return refresh_token

Expand All @@ -395,7 +396,8 @@ def _create_access_token(self, identity, fresh=False, expires_delta=None):
user_claims=self._user_claims_callback(identity),
csrf=config.csrf_protect,
identity_claim_key=config.identity_claim_key,
user_claims_key=config.user_claims_key
user_claims_key=config.user_claims_key,
json_encoder=config.json_encoder
)
return access_token

14 changes: 9 additions & 5 deletions flask_jwt_extended/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def _create_csrf_token():
return str(uuid.uuid4())


def _encode_jwt(additional_token_data, expires_delta, secret, algorithm):
def _encode_jwt(additional_token_data, expires_delta, secret, algorithm,
json_encoder=None):
uid = str(uuid.uuid4())
now = datetime.datetime.utcnow()
token_data = {
Expand All @@ -31,7 +32,8 @@ def _encode_jwt(additional_token_data, expires_delta, secret, algorithm):


def encode_access_token(identity, secret, algorithm, expires_delta, fresh,
user_claims, csrf, identity_claim_key, user_claims_key):
user_claims, csrf, identity_claim_key, user_claims_key,
json_encoder=None):
"""
Creates a new encoded (utf-8) access token.
Expand Down Expand Up @@ -70,11 +72,12 @@ def encode_access_token(identity, secret, algorithm, expires_delta, fresh,

if csrf:
token_data['csrf'] = _create_csrf_token()
return _encode_jwt(token_data, expires_delta, secret, algorithm)
return _encode_jwt(token_data, expires_delta, secret, algorithm,
json_encoder=json_encoder)


def encode_refresh_token(identity, secret, algorithm, expires_delta, csrf,
identity_claim_key):
identity_claim_key, json_encoder=None):
"""
Creates a new encoded (utf-8) refresh token.
Expand All @@ -95,7 +98,8 @@ def encode_refresh_token(identity, secret, algorithm, expires_delta, csrf,
}
if csrf:
token_data['csrf'] = _create_csrf_token()
return _encode_jwt(token_data, expires_delta, secret, algorithm)
return _encode_jwt(token_data, expires_delta, secret, algorithm,
json_encoder=json_encoder)


def decode_jwt(encoded_token, secret, algorithm, identity_claim_key,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from datetime import timedelta
from flask import Flask
from flask.json import JSONEncoder

from flask_jwt_extended import JWTManager
from flask_jwt_extended.config import config
Expand Down Expand Up @@ -56,6 +57,8 @@ def test_default_configs(app):
assert config.identity_claim_key == 'identity'
assert config.user_claims_key == 'user_claims'

assert config.json_encoder is app.json_encoder


def test_override_configs(app):
app.config['JWT_TOKEN_LOCATION'] = ['cookies']
Expand Down Expand Up @@ -91,6 +94,11 @@ def test_override_configs(app):
app.config['JWT_IDENTITY_CLAIM'] = 'foo'
app.config['JWT_USER_CLAIMS'] = 'bar'

class CustomJSONEncoder(JSONEncoder):
pass

app.json_encoder = CustomJSONEncoder

with app.test_request_context():
assert config.token_location == ['cookies']
assert config.jwt_in_cookies is True
Expand Down Expand Up @@ -131,6 +139,8 @@ def test_override_configs(app):
assert config.identity_claim_key == 'foo'
assert config.user_claims_key == 'bar'

assert config.json_encoder is CustomJSONEncoder


def test_tokens_never_expire(app):
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = False
Expand Down
3 changes: 2 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def encode_token(app, token_data):
token = jwt.encode(
token_data,
config.decode_key,
algorithm=config.algorithm
algorithm=config.algorithm,
json_encoder=config.json_encoder
)
return token.decode('utf-8')

Expand Down

0 comments on commit a860550

Please sign in to comment.