Skip to content

Commit

Permalink
Fixes #71: use StarletteJSONEncoder for jwt.encode
Browse files Browse the repository at this point in the history
  • Loading branch information
dolamroth committed Feb 7, 2024
1 parent 3c6fa18 commit 61e7415
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
19 changes: 11 additions & 8 deletions starlette_web/contrib/auth/jwt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import jwt

from starlette_web.common.utils.inspect import get_available_options


class JWTProcessor:
def __init__(self, **kwargs):
Expand Down Expand Up @@ -61,23 +63,24 @@ def _get_encode_options(self, **kwargs) -> dict:
res = {}
options = {**self.init_options, **kwargs}

if "algorithm" in options:
res["algorithm"] = options["algorithm"]
for _option in get_available_options(jwt.encode):
if _option in options:
res[_option] = options[_option]

return res

def _get_decode_options(self, **kwargs) -> dict:
res = {}
options = {**self.init_options, **kwargs}

if "algorithm" in options:
for _option in get_available_options(jwt.decode):
if _option in options:
res[_option] = options[_option]

if "algorithm" in options and not res.get("algorithms"):
res["algorithms"] = [options["algorithm"]]
elif "algorithms" in options:
res["algorithms"] = options["algorithms"]

if "options" in options:
res["options"] = options["options"]
else:
if "options" not in res:
res["options"] = options

return res
2 changes: 2 additions & 0 deletions starlette_web/contrib/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from functools import cached_property

from starlette_web.common.conf import settings
from starlette_web.common.utils.json import StarletteJSONEncoder
from starlette_web.contrib.auth.jwt_utils import JWTProcessor


Expand Down Expand Up @@ -47,6 +48,7 @@ def _enhance_payload_for_encode(self, payload: dict, **kwargs) -> None:
jwt_processor = AuthJWTProcessor(
algorithm=settings.AUTH_JWT_ALGORITHM,
verify_signature=True,
json_encoder=StarletteJSONEncoder,
)
encode_jwt = jwt_processor.encode_jwt
decode_jwt = jwt_processor.decode_jwt
25 changes: 25 additions & 0 deletions starlette_web/tests/contrib/test_jwt_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import datetime
import uuid

from starlette_web.contrib.auth.utils import encode_jwt, decode_jwt


def test_jwt_utils():
# Test, that StarletteJSONEncoder is used for jwt encoding
payload = {
"field": [1, 2.0, False, None, {"1": 2}],
"user_id": uuid.uuid4(),
"date": datetime.datetime.now(),
}

token, _ = encode_jwt(payload)
assert type(token) is str
assert token.count(".") == 2

decoded = decode_jwt(token)
assert "exp" in decoded
assert "exp_iso" in decoded
assert "token_type" in decoded
assert decoded["field"] == payload["field"]
assert decoded["user_id"] == str(payload["user_id"])
assert decoded["date"] == payload["date"].isoformat()[:23]

0 comments on commit 61e7415

Please sign in to comment.