Skip to content
Merged
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude_lines = [

[tool.poetry]
name = "cryptojwt"
version = "1.8.3"
version = "1.8.4"
description = "Python implementation of JWT, JWE, JWS and JWK"
authors = ["Roland Hedberg <[email protected]>"]
license = "Apache-2.0"
Expand Down
29 changes: 24 additions & 5 deletions src/cryptojwt/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import time
import uuid
from json import JSONDecodeError
from typing import Dict
from typing import Optional

from .exception import HeaderError
from .exception import VerificationError
Expand Down Expand Up @@ -97,7 +99,7 @@ def __init__(
):
self.key_jar = key_jar # KeyJar instance
self.iss = iss # My identifier
self.lifetime = lifetime # default life time of the signature
self.lifetime = lifetime # default lifetime of the signature
self.sign = sign # default signing or not
self.alg = sign_alg # default signing algorithm
self.encrypt = encrypt # default encrypting or not
Expand Down Expand Up @@ -206,16 +208,30 @@ def pack_key(self, issuer_id="", kid=""):

return keys[0] # Might be more then one if kid == ''

def pack(self, payload=None, kid="", issuer_id="", recv="", aud=None, iat=None, **kwargs):
def message(self, signing_key, **kwargs):
return json.dumps(kwargs)

def pack(
self,
payload: Optional[dict] = None,
kid: Optional[str] = "",
issuer_id: Optional[str] = "",
recv: Optional[str] = "",
aud: Optional[str] = None,
iat: Optional[int] = None,
jws_headers: Dict[str, str] = None,
**kwargs
) -> str:
"""

:param payload: Information to be carried as payload in the JWT
:param kid: Key ID
:param issuer_id: The owner of the the keys that are to be used for signing
:param issuer_id: The owner of the keys that are to be used for signing
:param recv: The intended immediate receiver
:param aud: Intended audience for this JWS/JWE, not expected to
contain the recipient.
:param iat: Override issued at (default current timestamp)
:param jws_headers: JWS headers
:param kwargs: Extra keyword arguments
:return: A signed or signed and encrypted Json Web Token
"""
Expand Down Expand Up @@ -249,10 +265,13 @@ def pack(self, payload=None, kid="", issuer_id="", recv="", aud=None, iat=None,
else:
_key = None

_jws = JWS(json.dumps(_args), alg=self.alg)
if jws_headers is None:
jws_headers = {}

_jws = JWS(self.message(signing_key=_key, **_args), alg=self.alg, **jws_headers)
_sjwt = _jws.sign_compact([_key])
else:
_sjwt = json.dumps(_args)
_sjwt = self.message(signing_key=None, **_args)

if _encrypt:
if not self.sign:
Expand Down