|
| 1 | +use crate::prelude::*; |
| 2 | +use azure_core::{headers::Headers, CollectedResponse, Method}; |
| 3 | +use serde_json::{Map, Value}; |
| 4 | + |
| 5 | +operation! { |
| 6 | + UnwrapKey, |
| 7 | + client: KeyClient, |
| 8 | + name: String, |
| 9 | + unwrap_key_parameters: UnwrapKeyParameters, |
| 10 | + ?version: String |
| 11 | +} |
| 12 | + |
| 13 | +impl UnwrapKeyBuilder { |
| 14 | + pub fn into_future(mut self) -> UnwrapKey { |
| 15 | + Box::pin(async move { |
| 16 | + // POST {vaultBaseUrl}/keys/{key-name}/{key-version}/decrypt?api-version=7.2 |
| 17 | + let version = self.version.unwrap_or_default(); |
| 18 | + let mut uri = self.client.keyvault_client.vault_url.clone(); |
| 19 | + let path = format!("keys/{}/{}/unwrapkey", self.name, version); |
| 20 | + |
| 21 | + uri.set_path(&path); |
| 22 | + |
| 23 | + let mut request_body = Map::new(); |
| 24 | + request_body.insert( |
| 25 | + "value".to_owned(), |
| 26 | + Value::String(String::from_utf8(self.unwrap_key_parameters.ciphertext)?), |
| 27 | + ); |
| 28 | + |
| 29 | + let algorithm = match self.unwrap_key_parameters.decrypt_parameters_encryption { |
| 30 | + CryptographParamtersEncryption::Rsa(RsaEncryptionParameters { algorithm }) => { |
| 31 | + request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?); |
| 32 | + algorithm |
| 33 | + } |
| 34 | + CryptographParamtersEncryption::AesGcm(AesGcmEncryptionParameters { |
| 35 | + algorithm, |
| 36 | + iv, |
| 37 | + authentication_tag, |
| 38 | + additional_authenticated_data, |
| 39 | + }) => { |
| 40 | + request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?); |
| 41 | + request_body.insert("iv".to_owned(), serde_json::to_value(iv)?); |
| 42 | + request_body |
| 43 | + .insert("tag".to_owned(), serde_json::to_value(authentication_tag)?); |
| 44 | + if let Some(aad) = additional_authenticated_data { |
| 45 | + request_body.insert("aad".to_owned(), serde_json::to_value(aad)?); |
| 46 | + }; |
| 47 | + algorithm |
| 48 | + } |
| 49 | + CryptographParamtersEncryption::AesCbc(AesCbcEncryptionParameters { |
| 50 | + algorithm, |
| 51 | + iv, |
| 52 | + }) => { |
| 53 | + request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?); |
| 54 | + request_body.insert("iv".to_owned(), serde_json::to_value(iv)?); |
| 55 | + algorithm |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + let headers = Headers::new(); |
| 60 | + let mut request = self.client.keyvault_client.finalize_request( |
| 61 | + uri, |
| 62 | + Method::Post, |
| 63 | + headers, |
| 64 | + Some(Value::Object(request_body).to_string().into()), |
| 65 | + )?; |
| 66 | + |
| 67 | + let response = self |
| 68 | + .client |
| 69 | + .keyvault_client |
| 70 | + .send(&mut self.context, &mut request) |
| 71 | + .await?; |
| 72 | + |
| 73 | + let response = CollectedResponse::from_response(response).await?; |
| 74 | + let body = response.body(); |
| 75 | + |
| 76 | + let mut result = serde_json::from_slice::<UnwrapKeyResult>(body)?; |
| 77 | + result.algorithm = algorithm; |
| 78 | + Ok(result) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +type UnwrapKeyResponse = UnwrapKeyResult; |
0 commit comments