Skip to content

Commit 53c54db

Browse files
committed
Verify alg
1 parent 8776f24 commit 53c54db

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/jws.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ The steps:
3737
Verifying a signature
3838
---------------------
3939

40-
Verifying a signature works like this::
40+
Verifying a signature works like this (_jws comes from the first signing example)::
4141

4242
>>> from cryptojwt.jwk.hmac import SYMKey
4343
>>> from cryptojwt.jws.jws import JWS
4444

4545
>>> key = SYMKey(key=b'My hollow echo chamber', alg="HS512")
4646
>>> _verifier = JWS(alg="HS512")
47-
>>> _msg = _verifier.verify_compact([key])
47+
>>> _msg = _verifier.verify_compact(_jws, [key])
4848
>>> print(_msg)
4949
"Please take a moment to register today"
5050

@@ -56,5 +56,16 @@ The steps:
5656
should give that information to the verifier as shown here. If you don't know you can leave it out.
5757
3. Verify, using the compact or JSON method.
5858

59+
Or slightly different::
60+
61+
>>> from cryptojwt.jws.jws import factory
62+
>>> from cryptojwt.jwk.hmac import SYMKey
63+
64+
>>> key = SYMKey(key=b'My hollow echo chamber', alg="HS512")
65+
>>> _verifier = factory(_jwt)
66+
>>> print(_verifier.verify_compact(_jwt, [key]))
67+
68+
69+
5970

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

src/cryptojwt/jws/jws.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,12 @@ def alg2keytype(self, alg):
392392
def set_header_claim(self, key, value):
393393
self._header[key] = value
394394

395+
def verify_alg(self, alg):
396+
if alg == self.jwt.headers['alg']:
397+
return True
398+
else:
399+
return False
400+
395401

396402
def factory(token):
397403
_jw = JWS()

tests/test_06_jws.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ def test_signer_ps512():
451451
_rj = factory(_jwt)
452452
info = _rj.verify_compact(_jwt, vkeys)
453453
assert info == payload
454+
assert _rj.verify_alg('PS512')
454455

455456

456457
def test_no_alg_and_alg_none_same():

0 commit comments

Comments
 (0)