Skip to content

Commit 8ffe9cf

Browse files
committed
More documentation
1 parent 2462a4b commit 8ffe9cf

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

doc/jws.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,52 @@ or Message Authentication Codes (MACs) using JSON-based data structures.
99
It's assumed that you know all you need to know about key handling if not
1010
please spend some time reading keyhandling_ .
1111

12+
When it comes to JWS there are basically 2 things you want to be able to do: sign some data and verify that a
13+
signature over some data is correct. I'll deal with them in that order.
14+
15+
Signing a document
16+
------------------
17+
18+
There are few steps you have to go through. Let us start with an example and then break it into its parts::
19+
20+
>>> from cryptojwt.jwk.hmac import SYMKey
21+
>>> from cryptojwt.jws.jws import JWS
22+
23+
>>> key = SYMKey(key=b'My hollow echo chamber', alg="HS512")
24+
>>> payload = "Please take a moment to register today"
25+
>>> _signer = JWS(payload, alg="HS512")
26+
>>> _jws = _signer.sign_compact([key])
27+
28+
The steps:
29+
30+
1. You need keys, one of more. If you provide more then one the software will pick one that has all the necessary
31+
qualifications. The keys *MUST* be instances of :py:class:`cryptojwt.jwk.JWK` or of sub classes of that class.
32+
2. You need the information that are to be signed. It must be in the form of a string.
33+
3. You initiate the signer, providing it with the message and other needed information.
34+
4. You sign using the compact or the JSON method as described in section 7 of RFC7515_ .
35+
36+
37+
Verifying a signature
38+
---------------------
39+
40+
Verifying a signature works like this::
41+
42+
>>> from cryptojwt.jwk.hmac import SYMKey
43+
>>> from cryptojwt.jws.jws import JWS
44+
45+
>>> key = SYMKey(key=b'My hollow echo chamber', alg="HS512")
46+
>>> _verifier = JWS(alg="HS512")
47+
>>> _msg = _verifier.verify_compact([key])
48+
>>> print(_msg)
49+
"Please take a moment to register today"
50+
51+
The steps:
52+
53+
1. As with signing, you need a set of keys that can be used to verify the signature. If you provider more then
54+
one possible, then the default is to use then one by one until one works or the list is empty.
55+
2. Initiate the verifier. If you have a reason to expect that a particular signing algorithm is to be used you
56+
should give that information to the verifier as shown here. If you don't know you can leave it out.
57+
3. Verify, using the compact or JSON method.
58+
59+
60+
.. _RFC7515: https://tools.ietf.org/html/rfc7515

tests/test_06_jws.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from cryptojwt.jws.exception import FormatError
1212
from cryptojwt.jws.exception import NoSuitableSigningKeys
13+
from cryptojwt.jws.exception import SignerAlgError
1314
from cryptojwt.jws.rsa import RSASigner
1415
from cryptojwt.jws.utils import left_hash
1516
from cryptojwt.jws.utils import parse_rsa_algorithm
@@ -344,6 +345,20 @@ def test_jws_2():
344345
assert _jws2.key == key
345346

346347

348+
def test_jws_mm():
349+
msg = {"iss": "joe", "exp": 1300819380, "http://example.com/is_root": True}
350+
key = SYMKey(key=intarr2bin(HMAC_KEY))
351+
_jws = JWS(msg, cty="JWT", alg="HS256", jwk=key.serialize())
352+
res = _jws.sign_compact()
353+
354+
_jws2 = JWS(alg="HS512")
355+
356+
with pytest.raises(SignerAlgError):
357+
_jws2.verify_compact(res, keys=[key])
358+
359+
360+
361+
347362
@pytest.mark.parametrize("ec_func,alg", [
348363
(ec.SECP256R1, "ES256"),
349364
(ec.SECP384R1, "ES384"),
@@ -783,3 +798,12 @@ def test_extra_headers_2():
783798
sjwt = _jws.sign_compact(keys)
784799
_jwt = factory(sjwt)
785800
assert set(_jwt.jwt.headers.keys()) == {'alg', 'foo'}
801+
802+
803+
def test_mismatch_alg_and_key():
804+
pkey = import_private_rsa_key_from_file(full_path("./size2048.key"))
805+
payload = "Please take a moment to register today"
806+
keys = [RSAKey(priv_key=pkey)]
807+
_jws = JWS(payload, alg='ES256')
808+
with pytest.raises(NoSuitableSigningKeys):
809+
_jws.sign_compact(keys)

0 commit comments

Comments
 (0)