Skip to content

Commit 9db7e44

Browse files
committed
fix(jwe): auto add kid to recipient when kid exists
1 parent 1330b49 commit 9db7e44

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/joserfc/_rfc7516/json.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def represent_general_json(obj: GeneralJSONEncryption) -> GeneralJSONSerializati
3636
recipients = []
3737
for recipient in obj.recipients:
3838
item: JSONRecipientDict = {}
39-
if recipient.header:
40-
item["header"] = recipient.header
41-
if recipient.encrypted_key:
42-
item["encrypted_key"] = to_str(urlsafe_b64encode(recipient.encrypted_key))
39+
assert recipient.header is not None
40+
assert recipient.encrypted_key is not None
41+
item["header"] = recipient.header
42+
item["encrypted_key"] = to_str(urlsafe_b64encode(recipient.encrypted_key))
4343
recipients.append(item)
4444
data["recipients"] = recipients
4545
return data
@@ -49,10 +49,10 @@ def represent_flattened_json(obj: FlattenedJSONEncryption) -> FlattenedJSONSeria
4949
data: FlattenedJSONSerialization = __represent_json_serialization(obj)
5050
recipient = obj.recipients[0]
5151
assert recipient is not None
52-
if recipient.header:
53-
data["header"] = recipient.header
54-
if recipient.encrypted_key:
55-
data["encrypted_key"] = to_str(urlsafe_b64encode(recipient.encrypted_key))
52+
assert recipient.header is not None
53+
assert recipient.encrypted_key is not None
54+
data["header"] = recipient.header
55+
data["encrypted_key"] = to_str(urlsafe_b64encode(recipient.encrypted_key))
5656
return data
5757

5858

src/joserfc/_rfc7516/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ class GeneralJSONEncryption(BaseJSONEncryption):
156156

157157
def add_recipient(self, header: Header | None = None, key: Key | None = None) -> None:
158158
recipient = Recipient(self, header, key)
159-
if key:
160-
key.ensure_kid()
161-
recipient.set_kid(t.cast(str, key.kid))
159+
if key and key.kid:
160+
recipient.set_kid(key.kid)
162161
self.recipients.append(recipient)
163162

164163

@@ -180,7 +179,10 @@ class FlattenedJSONEncryption(BaseJSONEncryption):
180179
flattened = True
181180

182181
def add_recipient(self, header: Header | None = None, key: Key | None = None) -> None:
183-
self.recipients = [Recipient(self, header, key)]
182+
recipient = Recipient(self, header, key)
183+
if key and key.kid:
184+
recipient.set_kid(key.kid)
185+
self.recipients = [recipient]
184186

185187

186188
class JWEEncModel(metaclass=ABCMeta):

tests/jwe/test_json.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22
from joserfc import jwe
3-
from joserfc.jwe import GeneralJSONEncryption
3+
from joserfc.jwe import GeneralJSONEncryption, FlattenedJSONEncryption
44
from joserfc.jwk import KeySet, RSAKey, ECKey, OctKey
55
from joserfc.errors import (
66
DecodeError,
@@ -72,3 +72,19 @@ def test_decode_multiple_recipients(self):
7272
key3,
7373
registry=registry,
7474
)
75+
76+
def test_flattened_encryption(self):
77+
key = OctKey.generate_key(128)
78+
protected = {"enc": "A128CBC-HS256"}
79+
plaintext = b"hello world"
80+
obj0 = FlattenedJSONEncryption(protected, plaintext)
81+
obj0.add_recipient({"alg": "A128KW"})
82+
value = jwe.encrypt_json(obj0, key)
83+
obj1 = jwe.decrypt_json(value, key)
84+
self.assertEqual(obj1.plaintext, plaintext)
85+
86+
obj2 = FlattenedJSONEncryption(protected, plaintext)
87+
obj2.add_recipient({"alg": "A128KW"}, key)
88+
value = jwe.encrypt_json(obj0, None)
89+
obj3 = jwe.decrypt_json(value, key)
90+
self.assertEqual(obj3.plaintext, plaintext)

0 commit comments

Comments
 (0)