Skip to content

Commit b1e3c5c

Browse files
committed
fix: allow jwt.encode to set default "typ" value
1 parent 49520d5 commit b1e3c5c

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/joserfc/jwt.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def encode(
6161
algorithms: list[str] | None = None,
6262
registry: JWSRegistry | JWERegistry | None = None,
6363
encoder_cls: Type[JSONEncoder] | None = None,
64+
default_type: str | None = "JWT",
6465
) -> str:
6566
"""Encode a JSON Web Token with the given header, and claims.
6667
@@ -70,9 +71,12 @@ def encode(
7071
:param algorithms: a list of allowed algorithms
7172
:param registry: a ``JWSRegistry`` or ``JWERegistry`` to use
7273
:param encoder_cls: A JSONEncoder subclass to use
74+
:param default_type: default value of the ``typ`` header parameter
7375
"""
74-
# add ``typ`` in header
75-
_header = {"typ": "JWT", **header}
76+
if default_type is not None:
77+
_header = {"typ": default_type, **header}
78+
else:
79+
_header = {**header}
7680
payload = convert_claims(claims, encoder_cls)
7781
if isinstance(registry, JWERegistry):
7882
return encrypt_compact(_header, payload, key, algorithms, registry)

tests/jwt/test_jwt.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,34 @@ def use_embedded_jwk(obj: GuestProtocol) -> Key:
1515

1616

1717
class TestJWT(TestCase):
18+
oct_key = OctKey.generate_key()
19+
20+
def test_default_type(self):
21+
data = jwt.encode({"alg": "HS256"}, {"sub": "a"}, self.oct_key)
22+
token = jwt.decode(data, self.oct_key)
23+
self.assertEqual(token.header["typ"], "JWT")
24+
25+
data = jwt.encode({"alg": "HS256"}, {"sub": "a"}, self.oct_key, default_type=None)
26+
token = jwt.decode(data, self.oct_key)
27+
self.assertNotIn("typ", token.header)
28+
29+
data = jwt.encode({"alg": "HS256"}, {"sub": "a"}, self.oct_key, default_type="jwt+at")
30+
token = jwt.decode(data, self.oct_key)
31+
self.assertEqual(token.header["typ"], "jwt+at")
32+
1833
def test_invalid_payload(self):
19-
key = OctKey.import_key("secret")
20-
data = jws.serialize_compact({"alg": "HS256"}, b"hello", key)
21-
self.assertRaises(InvalidPayloadError, jwt.decode, data, key)
34+
data = jws.serialize_compact({"alg": "HS256"}, b"hello", self.oct_key)
35+
self.assertRaises(InvalidPayloadError, jwt.decode, data, self.oct_key)
2236

2337
def test_claims_registry(self):
24-
key = OctKey.import_key("secret")
25-
data = jwt.encode({"alg": "HS256"}, {"sub": "a"}, key)
26-
token = jwt.decode(data, key)
38+
data = jwt.encode({"alg": "HS256"}, {"sub": "a"}, self.oct_key)
39+
token = jwt.decode(data, self.oct_key)
2740

2841
claims_registry = jwt.JWTClaimsRegistry(iss={"essential": True})
2942
self.assertRaises(MissingClaimError, claims_registry.validate, token.claims)
3043

31-
data = jwt.encode({"alg": "HS256"}, {"iss": "a"}, key)
32-
obj = jwt.decode(data, key)
44+
data = jwt.encode({"alg": "HS256"}, {"iss": "a"}, self.oct_key)
45+
obj = jwt.decode(data, self.oct_key)
3346
self.assertEqual(obj.claims["iss"], "a")
3447

3548
def test_jwe_format(self):

0 commit comments

Comments
 (0)