|
4 | 4 | import time
|
5 | 5 | import uuid
|
6 | 6 | from json import JSONDecodeError
|
| 7 | +from typing import Dict |
| 8 | +from typing import Optional |
7 | 9 |
|
8 | 10 | from .exception import HeaderError
|
9 | 11 | from .exception import VerificationError
|
@@ -97,7 +99,7 @@ def __init__(
|
97 | 99 | ):
|
98 | 100 | self.key_jar = key_jar # KeyJar instance
|
99 | 101 | self.iss = iss # My identifier
|
100 |
| - self.lifetime = lifetime # default life time of the signature |
| 102 | + self.lifetime = lifetime # default lifetime of the signature |
101 | 103 | self.sign = sign # default signing or not
|
102 | 104 | self.alg = sign_alg # default signing algorithm
|
103 | 105 | self.encrypt = encrypt # default encrypting or not
|
@@ -206,16 +208,30 @@ def pack_key(self, issuer_id="", kid=""):
|
206 | 208 |
|
207 | 209 | return keys[0] # Might be more then one if kid == ''
|
208 | 210 |
|
209 |
| - def pack(self, payload=None, kid="", issuer_id="", recv="", aud=None, iat=None, **kwargs): |
| 211 | + def message(self, signing_key, **kwargs): |
| 212 | + return json.dumps(kwargs) |
| 213 | + |
| 214 | + def pack( |
| 215 | + self, |
| 216 | + payload: Optional[dict] = None, |
| 217 | + kid: Optional[str] = "", |
| 218 | + issuer_id: Optional[str] = "", |
| 219 | + recv: Optional[str] = "", |
| 220 | + aud: Optional[str] = None, |
| 221 | + iat: Optional[int] = None, |
| 222 | + jws_headers: Dict[str, str] = None, |
| 223 | + **kwargs |
| 224 | + ) -> str: |
210 | 225 | """
|
211 | 226 |
|
212 | 227 | :param payload: Information to be carried as payload in the JWT
|
213 | 228 | :param kid: Key ID
|
214 |
| - :param issuer_id: The owner of the the keys that are to be used for signing |
| 229 | + :param issuer_id: The owner of the keys that are to be used for signing |
215 | 230 | :param recv: The intended immediate receiver
|
216 | 231 | :param aud: Intended audience for this JWS/JWE, not expected to
|
217 | 232 | contain the recipient.
|
218 | 233 | :param iat: Override issued at (default current timestamp)
|
| 234 | + :param jws_headers: JWS headers |
219 | 235 | :param kwargs: Extra keyword arguments
|
220 | 236 | :return: A signed or signed and encrypted Json Web Token
|
221 | 237 | """
|
@@ -249,10 +265,10 @@ def pack(self, payload=None, kid="", issuer_id="", recv="", aud=None, iat=None,
|
249 | 265 | else:
|
250 | 266 | _key = None
|
251 | 267 |
|
252 |
| - _jws = JWS(json.dumps(_args), alg=self.alg) |
| 268 | + _jws = JWS(self.message(signing_key=_key, **_args), alg=self.alg, **jws_headers) |
253 | 269 | _sjwt = _jws.sign_compact([_key])
|
254 | 270 | else:
|
255 |
| - _sjwt = json.dumps(_args) |
| 271 | + _sjwt = self.message(signing_key=None, **_args) |
256 | 272 |
|
257 | 273 | if _encrypt:
|
258 | 274 | if not self.sign:
|
|
0 commit comments