Skip to content

Commit

Permalink
Merge pull request #124 from dajiaji/rename-from-json-to-from-jwk
Browse files Browse the repository at this point in the history
Rename Recipient.from_json to from_jwk.
  • Loading branch information
dajiaji authored Jun 13, 2021
2 parents c9dfb2d + 61110ef commit d27edc5
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes
Unreleased
----------

- Rename from_json to from_jwk. `#124 <https://github.com/dajiaji/python-cwt/pull/124>`__
- Add support for X25519/X448. `#123 <https://github.com/dajiaji/python-cwt/pull/123>`__
- Add derive_key to EC2Key. `#122 <https://github.com/dajiaji/python-cwt/pull/122>`__
- Add key to OKPKey. `#122 <https://github.com/dajiaji/python-cwt/pull/122>`__
- Add support for key derivation without kid. `#120 <https://github.com/dajiaji/python-cwt/pull/120>`__
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ Create a COSE MAC message, verify and decode it as follows:
```py
from cwt import COSE, COSEKey

recipient = Recipient.from_json({"alg": "direct", "kid": "01"})
recipient = Recipient.from_jwk({"alg": "direct", "kid": "01"})
mac_key = COSEKey.from_symmetric_key(alg="HS512", kid="01")
ctx = COSE.new()
encoded = ctx.encode_and_mac(b"Hello world!", mac_key, recipients=[recipient])
Expand All @@ -449,7 +449,7 @@ Create a COSE Encrypt message, verify and decode it as follows:
```py
from cwt import COSE, COSEKey

recipient = Recipient.from_json({"alg": "direct", "kid": "01"})
recipient = Recipient.from_jwk({"alg": "direct", "kid": "01"})
enc_key = COSEKey.from_symmetric_key(alg="ChaCha20/Poly1305", kid="01")
ctx = COSE.new()
encoded = ctx.encode_and_encrypt(
Expand Down
4 changes: 2 additions & 2 deletions cwt/recipient.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def new(
raise ValueError(f"Unsupported or unknown alg(1): {alg}.")

@classmethod
def from_json(cls, data: Union[str, bytes, Dict[str, Any]]) -> RecipientInterface:
def from_jwk(cls, data: Union[str, bytes, Dict[str, Any]]) -> RecipientInterface:
"""
Create a recipient from JSON-formatted recipient data.
Create a recipient from JWK-like data.
Args:
data (Union[str, bytes, Dict[str, Any]]): JSON-formatted recipient data.
Expand Down
8 changes: 4 additions & 4 deletions docs/cose_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ key distribution method.
from cwt import COSE, COSEKey, Recipient
# The sender makes a COSE MAC message as follows:
recipient = Recipient.from_json({"alg": "direct", "kid": "01"})
recipient = Recipient.from_jwk({"alg": "direct", "kid": "01"})
mac_key = COSEKey.from_symmetric_key(alg="HS512", kid="01")
ctx = COSE.new()
encoded = ctx.encode_and_mac(b"Hello world!", mac_key, recipients=[recipient])
Expand All @@ -96,7 +96,7 @@ Following samples are other ways of writing the above sample:
from cwt import COSE, COSEKey, Recipient
# The sender side:
# In contrast to from_json(), new() is low-level constructor.
# In contrast to from_jwk(), new() is low-level constructor.
recipient = Recipient.new(unprotected={"alg": "direct", "kid": "01"})
mac_key = COSEKey.from_symmetric_key(alg="HS512", kid="01")
ctx = COSE.new()
Expand Down Expand Up @@ -130,7 +130,7 @@ The AES key wrap algorithm can be used to wrap a MAC key as follows:
# The sender side:
mac_key = COSEKey.from_symmetric_key(alg="HS512")
recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"alg": "A128KW",
"kid": "our-secret",
Expand Down Expand Up @@ -216,7 +216,7 @@ key distribution method.
from cwt import COSE, COSEKey, Recipient
# The sender side:
recipient = Recipient.from_json({"alg": "direct", "kid": "01"})
recipient = Recipient.from_jwk({"alg": "direct", "kid": "01"})
enc_key = COSEKey.from_symmetric_key(alg="ChaCha20/Poly1305", kid="01")
ctx = COSE.new()
encoded = ctx.encode_and_encrypt(
Expand Down
14 changes: 7 additions & 7 deletions tests/test_cose.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_cose_encode_and_decode_with_options(self):
assert b"Hello world!" == ctx.decode(encoded, enc_key)

# Encrypt
rec = Recipient.from_json({"alg": "direct", "kid": "02"})
rec = Recipient.from_jwk({"alg": "direct", "kid": "02"})
encoded = ctx.encode_and_encrypt(
b"Hello world!",
enc_key,
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_cose_encode_and_decode_with_recipient_builder(self):
ctx = COSE.new()

mac_key = COSEKey.from_symmetric_key(alg="HS256", kid="01")
recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"alg": "direct",
"kid": "01",
Expand Down Expand Up @@ -538,7 +538,7 @@ def test_cose_sample_cose_wg_examples_chacha_poly_enc_01(self, ctx):

def test_cose_sample_cose_wg_rfc8152_c_3_2(self):
cwt_str = "D8608443A1010AA1054D89F52F65A1C580933B5261A76C581C753548A19B1307084CA7B2056924ED95F2E3B17006DFE931B687B847818343A10129A2335061616262636364646565666667676868044A6F75722D73656372657440"
recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"alg": "direct+HKDF-SHA-256",
"kid": "our-secret",
Expand Down Expand Up @@ -585,7 +585,7 @@ def test_cose_sample_cose_wg_rfc8152_c_3_2(self):

def test_cose_sample_cose_wg_rfc8152_c_3_2_with_json(self):
cwt_str = "D8608443A1010AA1054D89F52F65A1C580933B5261A76C581C753548A19B1307084CA7B2056924ED95F2E3B17006DFE931B687B847818343A10129A2335061616262636364646565666667676868044A6F75722D73656372657440"
recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"alg": "direct+HKDF-SHA-256",
"kid": "our-secret",
Expand Down Expand Up @@ -644,7 +644,7 @@ def test_cose_sample_cose_wg_aes_wrap_128_03(self):
),
alg="HS512",
)
recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"alg": "A128KW",
"kid": "our-secret",
Expand All @@ -664,7 +664,7 @@ def test_cose_sample_cose_wg_aes_wrap_128_03(self):
assert res == b"This is the content."

def test_cose_sample_cose_wg_ecdh_direct_p256_hkdf_256_01(self):
rec = Recipient.from_json(
rec = Recipient.from_jwk(
{
"kty": "EC",
"alg": "ECDH-ES+HKDF-256",
Expand Down Expand Up @@ -977,7 +977,7 @@ def test_cose_decode_with_key_not_found(self, ctx):
def test_cose_decode_ecdh_es_hkdf_256_without_context(self):
with open(key_path("public_key_es256.pem")) as key_file:
public_key = COSEKey.from_pem(key_file.read(), kid="01")
recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{"kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256"}
)
enc_key = recipient.derive_key(
Expand Down
12 changes: 6 additions & 6 deletions tests/test_cose_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_cose_usage_examples_cose_mac0(self):

def test_cose_usage_examples_cose_mac_direct(self):
mac_key = COSEKey.from_symmetric_key(alg="HS512", kid="01")
recipient = Recipient.from_json({"alg": "direct", "kid": "01"})
recipient = Recipient.from_jwk({"alg": "direct", "kid": "01"})

ctx = COSE.new()
encoded = ctx.encode_and_mac(b"Hello world!", mac_key, recipients=[recipient])
Expand All @@ -52,7 +52,7 @@ def test_cose_usage_examples_cose_mac_direct(self):

def test_cose_usage_examples_cose_mac_aes_key_wrap(self):
mac_key = COSEKey.from_symmetric_key(alg="HS512")
recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"alg": "A128KW",
"kid": "our-secret",
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_cose_usage_examples_cose_encrypt0(self):
def test_cose_usage_examples_cose_encrypt(self):
enc_key = COSEKey.from_symmetric_key(alg="ChaCha20/Poly1305", kid="01")
nonce = enc_key.generate_nonce()
recipient = Recipient.from_json({"alg": "direct", "kid": "01"})
recipient = Recipient.from_jwk({"alg": "direct", "kid": "01"})

ctx = COSE.new()
encoded = ctx.encode_and_encrypt(
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_cose_usage_examples_cose_encrypt(self):

def test_cose_usage_examples_cose_encrypt_ecdh_direct_hkdf_p256(self):

recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"kty": "EC",
"alg": "ECDH-ES+HKDF-256",
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_cose_usage_examples_cose_encrypt_ecdh_direct_hkdf_p256(self):

def test_cose_usage_examples_cose_encrypt_ecdh_direct_hkdf_x25519(self):

recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"kty": "OKP",
"alg": "ECDH-ES+HKDF-256",
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_cose_usage_examples_cose_encrypt_ecdh_direct_hkdf_x25519(self):

def test_cose_usage_examples_cose_encrypt_ecdh_direct_hkdf_x448(self):

recipient = Recipient.from_json(
recipient = Recipient.from_jwk(
{
"kty": "OKP",
"alg": "ECDH-ES+HKDF-256",
Expand Down
12 changes: 6 additions & 6 deletions tests/test_ecdh_direct_hkdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_ecdh_direct_hkdf_derive_key_with_ecdh_es(
)

def test_ecdh_direct_hkdf_derive_key_with_ecdh_es_p256(self):
rec = Recipient.from_json(
rec = Recipient.from_jwk(
{
"kty": "EC",
"alg": "ECDH-SS+HKDF-256",
Expand All @@ -96,7 +96,7 @@ def test_ecdh_direct_hkdf_derive_key_with_ecdh_es_p256(self):
)

def test_ecdh_direct_hkdf_derive_key_with_ecdh_es_p521(self):
rec = Recipient.from_json(
rec = Recipient.from_jwk(
{
"kty": "EC",
"alg": "ECDH-SS+HKDF-512",
Expand All @@ -122,7 +122,7 @@ def test_ecdh_direct_hkdf_derive_key_with_ecdh_es_p521(self):
)

def test_ecdh_direct_hkdf_derive_key_with_raw_context(self):
rec = Recipient.from_json(
rec = Recipient.from_jwk(
{"kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256"}
)
with open(key_path("public_key_es256.pem")) as key_file:
Expand All @@ -143,7 +143,7 @@ def test_ecdh_direct_hkdf_derive_key_with_raw_context(self):
)

def test_ecdh_direct_hkdf_derive_key_without_kid(self):
rec = Recipient.from_json(
rec = Recipient.from_jwk(
{"kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256"}
)
with open(key_path("public_key_es256.pem")) as key_file:
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_ecdh_direct_hkdf_derive_key_without_cose_key(self):
)

def test_ecdh_direct_hkdf_derive_key_without_public_key(self):
rec = Recipient.from_json(
rec = Recipient.from_jwk(
{"kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256"}
)
with pytest.raises(ValueError) as err:
Expand All @@ -180,7 +180,7 @@ def test_ecdh_direct_hkdf_derive_key_without_public_key(self):
assert "public_key should be set." in str(err.value)

def test_ecdh_direct_hkdf_derive_key_with_invalid_private_key(self):
rec = Recipient.from_json(
rec = Recipient.from_jwk(
{"kty": "EC", "crv": "P-256", "alg": "ECDH-ES+HKDF-256"}
)
with open(key_path("public_key_es256.pem")) as key_file:
Expand Down
22 changes: 11 additions & 11 deletions tests/test_recipient.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ def test_recipient_new_with_invalid_arg(self, protected, unprotected, msg):
pytest.fail("Recipient() should fail.")
assert msg in str(err.value)

def test_recipient_from_json_with_str(self):
recipient = Recipient.from_json('{"alg": "direct"}')
def test_recipient_from_jwk_with_str(self):
recipient = Recipient.from_jwk('{"alg": "direct"}')
assert isinstance(recipient, RecipientInterface)
assert recipient.alg == -6

def test_recipient_from_json_with_dict(self):
recipient = Recipient.from_json({"alg": "A128KW", "key_ops": ["wrapKey"]})
def test_recipient_from_jwk_with_dict(self):
recipient = Recipient.from_jwk({"alg": "A128KW", "key_ops": ["wrapKey"]})
assert isinstance(recipient, RecipientInterface)
assert recipient.alg == -3
assert len(recipient.key_ops) == 1
Expand Down Expand Up @@ -288,9 +288,9 @@ def test_recipient_from_json_with_dict(self):
),
],
)
def test_recipient_from_json_with_invalid_arg(self, data, msg):
def test_recipient_from_jwk_with_invalid_arg(self, data, msg):
with pytest.raises(ValueError) as err:
Recipient.from_json(data)
Recipient.from_jwk(data)
pytest.fail("Recipient() should fail.")
assert msg in str(err.value)

Expand Down Expand Up @@ -359,13 +359,13 @@ def test_recipients_extract_key_with_empty_recipients(self, material, context):
assert "Failed to derive a key." in str(err.value)

def test_recipients_extract_key_with_multiple_materials(self, material, context):
r1 = Recipient.from_json(
r1 = Recipient.from_jwk(
{
"alg": "direct",
"kid": "01",
}
)
r2 = Recipient.from_json(
r2 = Recipient.from_jwk(
{
"alg": "direct+HKDF-SHA-256",
"kid": "02",
Expand All @@ -384,20 +384,20 @@ def test_recipients_extract_key_with_multiple_keys(self, material):
),
alg="HS512",
)
r1 = Recipient.from_json(
r1 = Recipient.from_jwk(
{
"alg": "A128KW",
"kid": "01",
}
)
r2 = Recipient.from_json(
r2 = Recipient.from_jwk(
{
"alg": "direct+HKDF-SHA-256",
"kid": "02",
"salt": "aabbccddeeffgghh",
},
)
r3 = Recipient.from_json(
r3 = Recipient.from_jwk(
{
"alg": "A128KW",
"kid": "02",
Expand Down

0 comments on commit d27edc5

Please sign in to comment.