From 219f32faf03195f0e77f986ca37b329eefa51d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?McCoy=20Pati=C3=B1o?= Date: Thu, 4 Mar 2021 18:42:27 -0800 Subject: [PATCH] Add .from_jwk examples --- .../azure/keyvault/keys/crypto/_client.py | 7 +++++++ .../azure/keyvault/keys/crypto/aio/_client.py | 7 +++++++ .../tests/test_examples_crypto.py | 14 +++++++++++++- .../tests/test_examples_crypto_async.py | 13 +++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/_client.py b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/_client.py index 53f6431625381..fa8fafa7628c6 100644 --- a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/_client.py +++ b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/_client.py @@ -142,6 +142,13 @@ def from_jwk(cls, jwk): :param jwk: the key's cryptographic material, as a JsonWebKey or dictionary. :type jwk: JsonWebKey or dict :rtype: CryptographyClient + + .. literalinclude:: ../tests/test_examples_crypto.py + :start-after: [START from_jwk] + :end-before: [END from_jwk] + :caption: Create a CryptographyClient from a JsonWebKey + :language: python + :dedent: 8 """ if isinstance(jwk, JsonWebKey): key = vars(jwk) diff --git a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/aio/_client.py b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/aio/_client.py index 872060e67b9ce..7cb232064d29f 100644 --- a/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/aio/_client.py +++ b/sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/crypto/aio/_client.py @@ -95,6 +95,13 @@ def from_jwk(cls, jwk: "Union[JsonWebKey, dict]") -> "CryptographyClient": :param jwk: the key's cryptographic material, as a JsonWebKey or dictionary. :type jwk: JsonWebKey or dict :rtype: CryptographyClient + + .. literalinclude:: ../tests/test_examples_crypto_async.py + :start-after: [START from_jwk] + :end-before: [END from_jwk] + :caption: Create a CryptographyClient from a JsonWebKey + :language: python + :dedent: 8 """ if isinstance(jwk, JsonWebKey): key = vars(jwk) diff --git a/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto.py b/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto.py index c5f0da5b65174..eb4413e61b138 100644 --- a/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto.py +++ b/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto.py @@ -4,7 +4,7 @@ # ------------------------------------ import functools -from azure.keyvault.keys import KeyClient +from azure.keyvault.keys import JsonWebKey, KeyClient from azure.keyvault.keys.crypto import CryptographyClient from azure.keyvault.keys._shared import HttpChallengeCache from devtools_testutils import PowerShellPreparer @@ -18,6 +18,18 @@ ) +def test_create_client_from_jwk(): + # [START from_jwk] + # create a CryptographyClient using a JsonWebKey instance + key = JsonWebKey(kty="RSA") + crypto_client = CryptographyClient.from_jwk(jwk=key) + + # or a dictionary with JsonWebKey properties + key_dict = {"kty":"RSA"} + crypto_client = CryptographyClient.from_jwk(jwk=key_dict) + # [END from_jwk] + + class TestCryptoExamples(KeyVaultTestCase): def __init__(self, *args, **kwargs): kwargs["match_body"] = False diff --git a/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py b/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py index 926f5e554893a..19038c520be4e 100644 --- a/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py +++ b/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py @@ -4,6 +4,7 @@ # ------------------------------------ import functools +from azure.keyvault.keys import JsonWebKey from azure.keyvault.keys.aio import KeyClient from azure.keyvault.keys.crypto.aio import CryptographyClient from azure.keyvault.keys._shared import HttpChallengeCache @@ -17,6 +18,18 @@ ) +def test_create_client_from_jwk(): + # [START from_jwk] + # create a CryptographyClient using a JsonWebKey instance + key = JsonWebKey(kty="RSA") + crypto_client = CryptographyClient.from_jwk(jwk=key) + + # or a dictionary with JsonWebKey properties + key_dict = {"kty":"RSA"} + crypto_client = CryptographyClient.from_jwk(jwk=key_dict) + # [END from_jwk] + + class TestCryptoExamples(KeyVaultTestCase): def __init__(self, *args, **kwargs): kwargs["match_body"] = False