Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#138] pass app json_encoder to jwt.encode #139

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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