Skip to content

Commit 23711e9

Browse files
author
Kamil Dębowski
committed
Add possibility to append custom headers in jwe.encrypt
1 parent 8572088 commit 23711e9

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

jose/jwe.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
def encrypt(plaintext, key, encryption=ALGORITHMS.A256GCM,
21-
algorithm=ALGORITHMS.DIR, zip=None, cty=None, kid=None):
21+
algorithm=ALGORITHMS.DIR, zip=None, cty=None, kid=None, headers=None):
2222
"""Encrypts plaintext and returns a JWE cmpact serialization string.
2323
2424
Args:
@@ -55,7 +55,7 @@ def encrypt(plaintext, key, encryption=ALGORITHMS.A256GCM,
5555
if encryption not in ALGORITHMS.SUPPORTED:
5656
raise JWEError('Algorithm %s not supported.' % encryption)
5757
key = jwk.construct(key, algorithm)
58-
encoded_header = _encoded_header(algorithm, encryption, zip, cty, kid)
58+
encoded_header = _encoded_header(algorithm, encryption, zip, cty, kid, headers)
5959

6060
plaintext = _compress(zip, plaintext)
6161
enc_cek, iv, cipher_text, auth_tag = _encrypt_and_auth(
@@ -335,7 +335,7 @@ def _jwe_compact_deserialize(jwe_bytes):
335335
return header, header_segment, encrypted_key, iv, ciphertext, auth_tag
336336

337337

338-
def _encoded_header(alg, enc, zip, cty, kid):
338+
def _encoded_header(alg, enc, zip, cty, kid, headers):
339339
"""
340340
Generate an appropriate JOSE header based on the values provided
341341
Args:
@@ -355,6 +355,8 @@ def _encoded_header(alg, enc, zip, cty, kid):
355355
header["cty"] = cty
356356
if kid:
357357
header["kid"] = kid
358+
if headers:
359+
header.update(headers)
358360
json_header = json.dumps(
359361
header,
360362
separators=(',', ':'),

0 commit comments

Comments
 (0)