def test_sample_readme_signed_cwt_ed25519_old(self): with open(key_path("private_key_ed25519.pem")) as key_file: private_key = COSEKey.from_pem(key_file.read(), kid="01") encoded = cwt.encode_and_sign( Claims.from_json({"iss": "coaps://as.example", "sub": "dajiaji", "cti": "123"}), private_key, ) with open(key_path("public_key_ed25519.pem")) as key_file: public_key = COSEKey.from_pem(key_file.read(), kid="01") decoded = cwt.decode(encoded, public_key) assert 1 in decoded and decoded[1] == "coaps://as.example"
def test_sample_rfc8392_a3_with_encoding_old(self): key = COSEKey.from_bytes(bytes.fromhex(SAMPLE_COSE_KEY_RFC8392_A2_3)) encoded = cwt.encode_and_sign( { 1: "coap://as.example.com", 2: "erikw", 3: "coap://light.example.com", 4: 1444064944, 5: 1443944944, 6: 1443944944, 7: bytes.fromhex("0b71"), }, key=key, ) decoded = cwt.decode(encoded, keys=key, no_verify=True) assert 1 in decoded and decoded[1] == "coap://as.example.com"
def test_sample_readme_nested_cwt_old(self): with open(key_path("private_key_es256.pem")) as key_file: private_key = COSEKey.from_pem(key_file.read(), kid="01") encoded = cwt.encode_and_sign( Claims.from_json({"iss": "coaps://as.example", "sub": "dajiaji", "cti": "123"}), private_key, ) nonce = token_bytes(13) mysecret = token_bytes(32) enc_key = COSEKey.from_symmetric_key(mysecret, alg="AES-CCM-16-64-256", kid="02") nested = cwt.encode_and_encrypt(encoded, enc_key, nonce=nonce) with open(key_path("public_key_es256.pem")) as key_file: public_key = COSEKey.from_pem(key_file.read(), kid="01") decoded = cwt.decode(nested, [enc_key, public_key]) assert 1 in decoded and decoded[1] == "coaps://as.example"
def test_sample_rfc8392_a6_with_encoding_old(self): sig_key = COSEKey.from_bytes(bytes.fromhex(SAMPLE_COSE_KEY_RFC8392_A2_3)) signed = cwt.encode_and_sign( { 1: "coap://as.example.com", 2: "erikw", 3: "coap://light.example.com", 4: 1444064944, 5: 1443944944, 6: 1443944944, 7: bytes.fromhex("0b71"), }, key=sig_key, ) enc_key = COSEKey.from_bytes(bytes.fromhex(SAMPLE_COSE_KEY_RFC8392_A2_1)) nonce = bytes.fromhex("4a0694c0e69ee6b5956655c7b2") encrypted = cwt.encode_and_encrypt(signed, key=enc_key, nonce=nonce) decoded = cwt.decode(encrypted, keys=[enc_key, sig_key], no_verify=True) assert 1 in decoded and decoded[1] == "coap://as.example.com"
def test_key_builder_from_jwk_with_encode_and_sign(self, private_key_path, public_key_path): with open(key_path(private_key_path)) as key_file: private_key = COSEKey.from_jwk(key_file.read()) with open(key_path(public_key_path)) as key_file: public_key = COSEKey.from_jwk(key_file.read()) token = cwt.encode_and_sign( Claims.from_json({ "iss": "coaps://as.example", "sub": "dajiaji", "cti": "123" }), private_key, ) # token = cwt.encode( # {"iss": "coaps://as.example", "sub": "dajiaji", "cti": "123"}, # private_key, # ) decoded = cwt.decode(token, public_key) assert 1 in decoded and decoded[1] == "coaps://as.example"
def test_sample_readme_signed_cwt_es384_old(self): with open(key_path("private_key_es384.pem")) as key_file: private_key = COSEKey.from_pem(key_file.read(), kid="01") with open(key_path("public_key_es384.pem")) as key_file: public_key = COSEKey.from_pem(key_file.read(), kid="01") encoded = cwt.encode_and_sign( Claims.from_json( { "iss": "coaps://as.example", "sub": "dajiaji", "aud": ["coaps://rs1.example", "coaps://rs2.example"], "cti": "123", } ), private_key, ) decoded = cwt.decode(encoded, public_key) assert 1 in decoded and decoded[1] == "coaps://as.example" assert 3 in decoded and isinstance(decoded[3], list) assert 3 in decoded and decoded[3][0] == "coaps://rs1.example" assert 3 in decoded and decoded[3][1] == "coaps://rs2.example"