azure_storage_blobs/service/operations/
get_user_delegation_key.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::prelude::BlobServiceClient;
use azure_core::{
    date::iso8601,
    headers::Headers,
    xml::{read_xml_str, to_xml},
    Method,
};
use azure_storage::{
    headers::CommonStorageResponseHeaders, shared_access_signature::service_sas::UserDeligationKey,
};
use bytes::{Bytes, BytesMut};
use time::OffsetDateTime;

operation! {
    GetUserDelegationKey,
    client: BlobServiceClient,
    start_time: OffsetDateTime,
    expiry_time: OffsetDateTime,
}

impl GetUserDelegationKeyBuilder {
    pub fn into_future(mut self) -> GetUserDelegationKey {
        Box::pin(async move {
            let mut url = self.client.url()?;

            url.query_pairs_mut()
                .extend_pairs([("restype", "service"), ("comp", "userdelegationkey")]);

            let body = GetUserDelegationKeyRequest {
                start: self.start_time,
                expiry: self.expiry_time,
            }
            .encode()?;

            let mut request = BlobServiceClient::finalize_request(
                url,
                Method::Post,
                Headers::new(),
                Some(body.into()),
            )?;

            let response = self.client.send(&mut self.context, &mut request).await?;

            let (_, headers, body) = response.deconstruct();
            let body = body.collect_string().await?;
            GetUserDelegationKeyResponse::try_from(&headers, &body)
        })
    }
}

#[derive(Serialize)]
#[serde(rename = "KeyInfo")]
struct GetUserDelegationKeyRequest {
    #[serde(rename = "Start", with = "iso8601")]
    start: OffsetDateTime,
    #[serde(rename = "Expiry", with = "iso8601")]
    expiry: OffsetDateTime,
}

impl GetUserDelegationKeyRequest {
    pub fn encode(&self) -> azure_core::Result<Bytes> {
        let mut body = BytesMut::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        body.extend(to_xml(self)?);
        Ok(body.freeze())
    }
}

#[derive(Debug)]
pub struct GetUserDelegationKeyResponse {
    pub common: CommonStorageResponseHeaders,
    pub user_deligation_key: UserDeligationKey,
}

impl GetUserDelegationKeyResponse {
    pub(crate) fn try_from(headers: &Headers, body: &str) -> azure_core::Result<Self> {
        let common = CommonStorageResponseHeaders::try_from(headers)?;
        let user_deligation_key: UserDeligationKey = read_xml_str(body)?;

        Ok(Self {
            common,
            user_deligation_key,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use azure_core::auth::Secret;
    use uuid::Uuid;

    const BASIC_REQUEST: &[u8] = b"<?xml version=\"1.0\" encoding=\"utf-8\"?><KeyInfo><Start>1970-01-01T00:00:00Z</Start><Expiry>1970-01-01T00:00:01Z</Expiry></KeyInfo>";
    const BASIC_RESPONSE: &str = "
        <UserDeligationKey>
            <SignedOid>00000000-0000-0000-0000-000000000000</SignedOid>
            <SignedTid>00000000-0000-0000-0000-000000000001</SignedTid>
            <SignedStart>1970-01-01T00:00:00Z</SignedStart>
            <SignedExpiry>1970-01-01T00:00:01Z</SignedExpiry>
            <SignedService>b</SignedService>
            <SignedVersion>c</SignedVersion>
            <Value>d</Value>
        </UserDeligationKey>
    ";

    #[test]
    fn request_xml() -> azure_core::Result<()> {
        let request = GetUserDelegationKeyRequest {
            start: OffsetDateTime::from_unix_timestamp(0).unwrap(),
            expiry: OffsetDateTime::from_unix_timestamp(1).unwrap(),
        }
        .encode()?;
        assert_eq!(BASIC_REQUEST, request);
        Ok(())
    }

    #[test]
    fn parse_response() -> azure_core::Result<()> {
        let expected = UserDeligationKey {
            signed_oid: Uuid::from_u128(0),
            signed_tid: Uuid::from_u128(1),
            signed_start: OffsetDateTime::from_unix_timestamp(0).unwrap(),
            signed_expiry: OffsetDateTime::from_unix_timestamp(1).unwrap(),
            signed_service: "b".to_owned(),
            signed_version: "c".to_owned(),
            value: Secret::new("d"),
        };

        let deserialized: UserDeligationKey = read_xml_str(BASIC_RESPONSE)?;
        assert_eq!(deserialized, expected);

        Ok(())
    }
}