|
| 1 | +use crate::prelude::BlobServiceClient; |
| 2 | +use azure_core::{ |
| 3 | + date::iso8601, |
| 4 | + headers::Headers, |
| 5 | + xml::{read_xml_str, to_xml}, |
| 6 | + Method, |
| 7 | +}; |
| 8 | +use azure_storage::headers::CommonStorageResponseHeaders; |
| 9 | +use time::OffsetDateTime; |
| 10 | +use uuid::Uuid; |
| 11 | + |
| 12 | +operation! { |
| 13 | + GetUserDelegationKey, |
| 14 | + client: BlobServiceClient, |
| 15 | + start_time: OffsetDateTime, |
| 16 | + expiry_time: OffsetDateTime, |
| 17 | +} |
| 18 | + |
| 19 | +impl GetUserDelegationKeyBuilder { |
| 20 | + pub fn into_future(mut self) -> GetUserDelegationKey { |
| 21 | + Box::pin(async move { |
| 22 | + let mut url = self.client.url()?; |
| 23 | + |
| 24 | + url.query_pairs_mut() |
| 25 | + .extend_pairs([("restype", "service"), ("comp", "userdelegationkey")]); |
| 26 | + |
| 27 | + let body = GetUserDelegationKeyRequest { |
| 28 | + start: self.start_time, |
| 29 | + expiry: self.expiry_time, |
| 30 | + } |
| 31 | + .as_string()?; |
| 32 | + |
| 33 | + let mut request = BlobServiceClient::finalize_request( |
| 34 | + url, |
| 35 | + Method::Post, |
| 36 | + Headers::new(), |
| 37 | + Some(body.into()), |
| 38 | + )?; |
| 39 | + |
| 40 | + let response = self.client.send(&mut self.context, &mut request).await?; |
| 41 | + |
| 42 | + let (_, headers, body) = response.deconstruct(); |
| 43 | + let body = body.collect_string().await?; |
| 44 | + GetUserDelegationKeyResponse::try_from(&headers, &body) |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Serialize)] |
| 50 | +#[serde(rename = "KeyInfo")] |
| 51 | +struct GetUserDelegationKeyRequest { |
| 52 | + #[serde(rename = "Start", with = "iso8601")] |
| 53 | + start: OffsetDateTime, |
| 54 | + #[serde(rename = "Expiry", with = "iso8601")] |
| 55 | + expiry: OffsetDateTime, |
| 56 | +} |
| 57 | + |
| 58 | +impl GetUserDelegationKeyRequest { |
| 59 | + pub fn as_string(&self) -> azure_core::Result<String> { |
| 60 | + Ok(format!( |
| 61 | + "<?xml version=\"1.0\" encoding=\"utf-8\"?>{}", |
| 62 | + to_xml(self)? |
| 63 | + )) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] |
| 68 | +#[serde(rename_all = "PascalCase")] |
| 69 | +pub struct UserDeligationKey { |
| 70 | + pub signed_oid: Uuid, |
| 71 | + pub signed_tid: Uuid, |
| 72 | + #[serde(with = "iso8601")] |
| 73 | + pub signed_start: OffsetDateTime, |
| 74 | + #[serde(with = "iso8601")] |
| 75 | + pub signed_expiry: OffsetDateTime, |
| 76 | + pub signed_service: String, |
| 77 | + pub signed_version: String, |
| 78 | + pub value: String, |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Debug)] |
| 82 | +pub struct GetUserDelegationKeyResponse { |
| 83 | + pub common: CommonStorageResponseHeaders, |
| 84 | + pub user_deligation_key: UserDeligationKey, |
| 85 | +} |
| 86 | + |
| 87 | +impl GetUserDelegationKeyResponse { |
| 88 | + pub(crate) fn try_from(headers: &Headers, body: &str) -> azure_core::Result<Self> { |
| 89 | + let common = CommonStorageResponseHeaders::try_from(headers)?; |
| 90 | + let user_deligation_key: UserDeligationKey = read_xml_str(body)?; |
| 91 | + |
| 92 | + Ok(Self { |
| 93 | + common, |
| 94 | + user_deligation_key, |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod test { |
| 101 | + use super::*; |
| 102 | + |
| 103 | + const BASIC_REQUEST: &str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><KeyInfo><Start>1970-01-01T00:00:00Z</Start><Expiry>1970-01-01T00:00:01Z</Expiry></KeyInfo>"; |
| 104 | + const BASIC_RESPONSE: &str = " |
| 105 | + <UserDeligationKey> |
| 106 | + <SignedOid>00000000-0000-0000-0000-000000000000</SignedOid> |
| 107 | + <SignedTid>00000000-0000-0000-0000-000000000001</SignedTid> |
| 108 | + <SignedStart>1970-01-01T00:00:00Z</SignedStart> |
| 109 | + <SignedExpiry>1970-01-01T00:00:01Z</SignedExpiry> |
| 110 | + <SignedService>b</SignedService> |
| 111 | + <SignedVersion>c</SignedVersion> |
| 112 | + <Value>d</Value> |
| 113 | + </UserDeligationKey> |
| 114 | + "; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn request_xml() -> azure_core::Result<()> { |
| 118 | + let request = GetUserDelegationKeyRequest { |
| 119 | + start: OffsetDateTime::from_unix_timestamp(0).unwrap(), |
| 120 | + expiry: OffsetDateTime::from_unix_timestamp(1).unwrap(), |
| 121 | + } |
| 122 | + .as_string()?; |
| 123 | + assert_eq!(BASIC_REQUEST, request); |
| 124 | + Ok(()) |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn parse_response() -> azure_core::Result<()> { |
| 129 | + let expected = UserDeligationKey { |
| 130 | + signed_oid: Uuid::from_u128(0), |
| 131 | + signed_tid: Uuid::from_u128(1), |
| 132 | + signed_start: OffsetDateTime::from_unix_timestamp(0).unwrap(), |
| 133 | + signed_expiry: OffsetDateTime::from_unix_timestamp(1).unwrap(), |
| 134 | + signed_service: "b".to_owned(), |
| 135 | + signed_version: "c".to_owned(), |
| 136 | + value: "d".to_owned(), |
| 137 | + }; |
| 138 | + |
| 139 | + let deserialized: UserDeligationKey = read_xml_str(BASIC_RESPONSE)?; |
| 140 | + assert_eq!(deserialized, expected); |
| 141 | + |
| 142 | + Ok(()) |
| 143 | + } |
| 144 | +} |
0 commit comments