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
use actix_http::{error::BlockingError, http::header::InvalidHeaderValue};
use awc::ClientRequest;
use std::{fmt::Display, future::Future, pin::Pin};

use crate::{create::Signed, Config, PrepareSignError, Sign};

impl Sign for ClientRequest {
    fn authorization_signature<F, E, K>(
        mut self,
        config: Config,
        key_id: K,
        f: F,
    ) -> Pin<Box<dyn Future<Output = Result<Self, E>>>>
    where
        F: FnOnce(&str) -> Result<String, E> + Send + 'static,
        E: From<BlockingError>
            + From<PrepareSignError>
            + From<InvalidHeaderValue>
            + std::fmt::Debug
            + Send
            + 'static,
        K: Display + 'static,
        Self: Sized,
    {
        Box::pin(async move {
            let signed = prepare(&self, &config, key_id, f).await?;
            signed.authorization_header(self.headers_mut())?;
            Ok(self)
        })
    }

    fn signature<F, E, K>(
        mut self,
        config: Config,
        key_id: K,
        f: F,
    ) -> Pin<Box<dyn Future<Output = Result<Self, E>>>>
    where
        F: FnOnce(&str) -> Result<String, E> + Send + 'static,
        E: From<BlockingError>
            + From<PrepareSignError>
            + From<InvalidHeaderValue>
            + std::fmt::Debug
            + Send
            + 'static,
        K: Display + 'static,
        Self: Sized,
    {
        Box::pin(async move {
            let signed = prepare(&self, &config, key_id, f).await?;
            signed.signature_header(self.headers_mut())?;
            Ok(self)
        })
    }
}

async fn prepare<F, E, K>(
    request: &ClientRequest,
    config: &Config,
    key_id: K,
    f: F,
) -> Result<Signed, E>
where
    F: FnOnce(&str) -> Result<String, E> + Send + 'static,
    E: From<BlockingError> + From<PrepareSignError> + std::fmt::Debug + Send + 'static,
    K: Display,
{
    let mut headers = request.headers().clone();
    if config.set_host {
        let header_string = request
            .get_uri()
            .host()
            .ok_or_else(|| PrepareSignError::Host(request.get_uri().to_string()))?
            .to_string();

        let header_string = match request.get_uri().port().map(|p| p.as_u16()) {
            None | Some(443) | Some(80) => header_string,
            Some(port) => format!("{}:{}", header_string, port),
        };
        headers.insert(
            "Host".parse().unwrap(),
            header_string
                .parse()
                .map_err(|_| PrepareSignError::Host(request.get_uri().to_string()))?,
        );
    }
    let unsigned = config.begin_sign(
        request.get_method(),
        request.get_uri().path_and_query(),
        headers,
    )?;

    let key_id = key_id.to_string();

    let signed = actix_rt::task::spawn_blocking(move || unsigned.sign(key_id, f)).await.map_err(|_| BlockingError)??;

    Ok(signed)
}