Skip to content

Commit 5d2efb2

Browse files
committed
Verify alg
1 parent cb375b6 commit 5d2efb2

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

doc/jws.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ Or slightly different::
6868
>>> print(_verifier.verify_compact(_jwt, [key]))
6969
"Please take a moment to register today"
7070

71+
Or
72+
73+
>>> from cryptojwt.jws.jws import factory
74+
>>> from cryptojwt.jwk.hmac import SYMKey
75+
76+
>>> key = SYMKey(key=b'My hollow echo chamber', alg="HS512")
77+
>>> _verifier = factory(_jwt, alg="HS512")
78+
>>> print(_verifier.verify_compact(_jwt, [key]))
79+
"Please take a moment to register today"
80+
81+
In which case the check of the signing algorithm is done by default.
7182

7283

7384
.. _RFC7515: https://tools.ietf.org/html/rfc7515

src/cryptojwt/jws/jws.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,17 +387,53 @@ def _is_compact_jws(self, jws):
387387
return True
388388

389389
def alg2keytype(self, alg):
390+
"""
391+
Translate a signing algorithm into a specific key type.
392+
393+
:param alg: The signing algorithm
394+
:return: A key type or None if there is no key type matching the
395+
algorithm
396+
"""
390397
return alg2keytype(alg)
391398

392399
def set_header_claim(self, key, value):
400+
"""
401+
Set a specific claim in the header to a specific value.
402+
403+
:param key: The name of the claim
404+
:param value: The value of the claim
405+
"""
393406
self._header[key] = value
394407

395408
def verify_alg(self, alg):
409+
"""
410+
Specifically check that the 'alg' claim has a specific value
411+
412+
:param alg: The expected alg value
413+
:raises: KeyError if the 'alg' is not present in the header
414+
:return: True if the alg value in the header is the same as the one
415+
given.
416+
"""
396417
if alg == self.jwt.headers['alg']:
397418
return True
398419
else:
399420
return False
400421

422+
def verify_header(self, key, val):
423+
"""
424+
Check that a particular header claim is present as a has specific value
425+
426+
:param key: The claim
427+
:param val: The value of the claim
428+
:raises: KeyError if the claim is not present in the header
429+
:return: True if the claim exists in the header and has the prescribed
430+
value
431+
"""
432+
if val == self.jwt.headers[key]:
433+
return True
434+
else:
435+
return False
436+
401437

402438
def factory(token, **kwargs):
403439
_jw = JWS(**kwargs)

0 commit comments

Comments
 (0)