Skip to content

Commit 48338b4

Browse files
committed
Make it possible to wrap the payload in a message class dependent on the header parameter "typ".
New method: add_keys()
1 parent 322b5bc commit 48338b4

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

src/cryptojwt/jwk/ec.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def ec_construct_public(num):
5555

5656
def ec_construct_private(num):
5757
"""
58-
Given a set of values on public and private attributes build a elliptic
58+
Given a set of values on public and private attributes build an elliptic
5959
curve private key instance.
6060
6161
:param num: A dictionary with public and private attributes and their values

src/cryptojwt/jwt.py

+36-35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Basic JSON Web Token implementation."""
22
import json
3+
from json import JSONDecodeError
34
import logging
45
import time
5-
import uuid
6-
from json import JSONDecodeError
76
from typing import Dict
7+
from typing import List
8+
from typing import MutableMapping
89
from typing import Optional
10+
import uuid
911

1012
from .exception import HeaderError
1113
from .exception import VerificationError
@@ -79,23 +81,24 @@ class JWT:
7981
"""The basic JSON Web Token class."""
8082

8183
def __init__(
82-
self,
83-
key_jar=None,
84-
iss="",
85-
lifetime=0,
86-
sign=True,
87-
sign_alg="RS256",
88-
encrypt=False,
89-
enc_enc="A128GCM",
90-
enc_alg="RSA-OAEP-256",
91-
msg_cls=None,
92-
iss2msg_cls=None,
93-
skew=15,
94-
allowed_sign_algs=None,
95-
allowed_enc_algs=None,
96-
allowed_enc_encs=None,
97-
allowed_max_lifetime=None,
98-
zip="",
84+
self,
85+
key_jar=None,
86+
iss: str="",
87+
lifetime: int = 0,
88+
sign: bool = True,
89+
sign_alg: str = "RS256",
90+
encrypt: bool = False,
91+
enc_enc: str = "A128GCM",
92+
enc_alg: str = "RSA-OAEP-256",
93+
msg_cls: MutableMapping = None,
94+
iss2msg_cls: Dict[str, str] = None,
95+
skew: int = 15,
96+
allowed_sign_algs: List[str] = None,
97+
allowed_enc_algs: List[str] = None,
98+
allowed_enc_encs: List[str] = None,
99+
allowed_max_lifetime: int = None,
100+
zip: str = "",
101+
typ2msg_cls: Dict[str, str] = None
99102
):
100103
self.key_jar = key_jar # KeyJar instance
101104
self.iss = iss # My identifier
@@ -212,15 +215,15 @@ def message(self, signing_key, **kwargs):
212215
return json.dumps(kwargs)
213216

214217
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
218+
self,
219+
payload: Optional[dict] = None,
220+
kid: Optional[str] = "",
221+
issuer_id: Optional[str] = "",
222+
recv: Optional[str] = "",
223+
aud: Optional[str] = None,
224+
iat: Optional[int] = None,
225+
jws_headers: Dict[str, str] = None,
226+
**kwargs
224227
) -> str:
225228
"""
226229
@@ -319,8 +322,7 @@ def verify_profile(msg_cls, info, **kwargs):
319322
:return: The verified message as a msg_cls instance.
320323
"""
321324
_msg = msg_cls(**info)
322-
if not _msg.verify(**kwargs):
323-
raise VerificationError()
325+
_msg.verify(**kwargs)
324326
return _msg
325327

326328
def unpack(self, token, timestamp=None):
@@ -392,11 +394,10 @@ def unpack(self, token, timestamp=None):
392394
if self.msg_cls:
393395
_msg_cls = self.msg_cls
394396
else:
395-
try:
396-
# try to find a issuer specific message class
397-
_msg_cls = self.iss2msg_cls[_info["iss"]]
398-
except KeyError:
399-
_msg_cls = None
397+
# try to find an issuer specific message class
398+
_msg_cls = self.iss2msg_cls.get(_info["iss"])
399+
if not _msg_cls:
400+
_msg_cls = self.typ2msg_cls.get(_jws_header['typ'])
400401

401402
timestamp = timestamp or utc_time_sans_frac()
402403

src/cryptojwt/key_bundle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ def append(self, key):
681681

682682
@keys_writer
683683
def extend(self, keys):
684-
"""Add a key to the list of keys."""
684+
"""Add a list of keys to the list of keys."""
685685
self._keys.extend(keys)
686686

687687
@keys_writer

src/cryptojwt/key_jar.py

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import List
44
from typing import Optional
55

6+
from cryptojwt.jwk import JWK
67
from requests import request
78

89
from .exception import IssuerNotFound
@@ -161,6 +162,11 @@ def add_kb(self, issuer_id, kb):
161162
issuer.add_kb(kb)
162163
self._issuers[issuer_id] = issuer
163164

165+
def add_keys(self, issuer_id: str, keys: List[JWK], **kwargs):
166+
_kb = KeyBundle(**kwargs)
167+
_kb.extend(keys)
168+
self.add_kb(issuer_id, _kb)
169+
164170
@deprecated_alias(issuer="issuer_id", owner="issuer_id")
165171
def get(self, key_use, key_type="", issuer_id="", kid=None, **kwargs):
166172
"""

0 commit comments

Comments
 (0)