verity_client/
client.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::str::FromStr;
use std::time::Duration;

use futures::stream::StreamExt;
use http::{HeaderValue, Method};
use reqwest::{IntoUrl, Response, Url};
use reqwest_eventsource::{Event, EventSource};
use serde::{Deserialize, Serialize};
use tokio::select;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::error;
use uuid::Uuid;

use crate::request::RequestBuilder;

/// Time to wait for a proof received over SSE connection since receiving HTTP response
const PROOF_TIMEOUT: Duration = Duration::from_millis(1000);

#[derive(Clone)]
pub struct VerityClientConfig {
    pub prover_url: String,
}

#[derive(Clone)]
pub struct VerityClient {
    pub(crate) inner: reqwest::Client,
    pub(crate) config: VerityClientConfig,
}

pub struct VerityResponse {
    pub subject: Response,
    pub proof: String,
    pub notary_pub_key: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NotaryInformation {
    pub version: String,
    pub public_key: String,
    pub git_commit_hash: String,
    pub git_commit_timestamp: String,
}

impl VerityClient {
    /// Creates a new `VerityClient` with the given configuration.
    pub fn new(config: VerityClientConfig) -> Self {
        return Self {
            inner: reqwest::Client::new(),
            config,
        };
    }

    /// Convenience method to make a `GET` request to a URL.
    ///
    /// # Errors
    ///
    /// This method fails whenever the supplied `Url` cannot be parsed.
    pub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder {
        self.request(Method::GET, url)
    }

    /// Convenience method to make a `POST` request to a URL.
    ///
    /// # Errors
    ///
    /// This method fails whenever the supplied `Url` cannot be parsed.
    pub fn post<U: IntoUrl>(&self, url: U) -> RequestBuilder {
        self.request(Method::POST, url)
    }

    /// Starts building a `Request` with the specified `Method` and `Url`.
    ///
    /// Returns a `RequestBuilder`, which allows setting headers and
    /// the request body before sending.
    ///
    /// # Errors
    ///
    /// This method fails whenever the supplied `Url` cannot be parsed.
    pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
        RequestBuilder {
            client: self.clone(),
            inner: self.inner.request(method, url),
        }
    }

    /// Executes a `Request` and returns a `VerityResponse`.
    ///
    /// A `Request` can be built manually with `Request::new()` or obtained
    /// from a RequestBuilder with `RequestBuilder::build()`.
    ///
    /// You should prefer to use the `RequestBuilder` and
    /// `RequestBuilder::send()`.
    ///
    /// # Errors
    ///
    /// This method fails if there was an error while sending the request,
    /// a redirect loop was detected, or the redirect limit was exhausted.
    pub async fn execute(&mut self, request: reqwest::Request) -> anyhow::Result<VerityResponse> {
        self.execute_request(request).await
    }

    /// Executes the given request and awaits proof of execution.
    ///
    /// # Errors
    ///
    /// This method fails if the request cannot be sent or if proof cannot be obtained.
    pub async fn execute_request(
        &mut self,
        mut req: reqwest::Request,
    ) -> anyhow::Result<VerityResponse> {
        let proxy_url = &String::from(req.url().as_str());
        let headers = req.headers_mut();

        let request_id = Uuid::new_v4();
        headers.append(
            "T-REQUEST-ID",
            HeaderValue::from_str(&format!("{}", request_id))?,
        );

        headers.append("T-PROXY-URL", HeaderValue::from_str(proxy_url)?);

        *req.url_mut() = Url::from_str(&format!("{}/proxy", self.config.prover_url))?;

        let req = reqwest::RequestBuilder::from_parts(self.inner.clone(), req);

        let request_cancellation_token = CancellationToken::new();
        let timeout_cancellation_token = CancellationToken::new();

        let proof_awaiter = self.await_proof(
            request_id.to_string(),
            request_cancellation_token.clone(),
            timeout_cancellation_token.clone(),
        )?;

        // prettier-ignore
        let (response, proof_msg) = tokio::try_join!(
            self.send_request(req, request_cancellation_token, timeout_cancellation_token),
            proof_awaiter
        )
        .map_err(|e| anyhow::anyhow!("Failed to prove the request: {}", e))?;

        let subject = response?;
        let (notary_pub_key, proof) = proof_msg?;

        Ok(VerityResponse {
            subject,
            proof,
            notary_pub_key,
        })
    }

    /// Sends the request and handles cancellation tokens.
    ///
    /// Returns a `JoinHandle` that resolves to the response or an error.
    fn send_request(
        &self,
        request: reqwest::RequestBuilder,
        request_cancellation_token: CancellationToken,
        timeout_cancellation_token: CancellationToken,
    ) -> JoinHandle<anyhow::Result<reqwest::Response>> {
        tokio::spawn(async move {
            let result = request.send().await;
            let response = result.map_err(|e| {
                error!("{}", e);
                e
            })?;

            if response.status().is_success() {
                tokio::spawn(async move {
                    tokio::time::sleep(PROOF_TIMEOUT).await;
                    timeout_cancellation_token.cancel();
                });
            } else {
                request_cancellation_token.cancel();
                error!(
                    "Request is not success: {} {}",
                    response.status().as_str(),
                    response.status().canonical_reason().unwrap_or_default()
                );
                return Ok(response);
            }

            Ok(response)
        })
    }

    /// Awaits proof of request execution.
    ///
    /// Returns a `JoinHandle` that resolves to the proof or an error.
    ///
    /// # Errors
    ///
    /// This method fails if the proof cannot be obtained.
    fn await_proof(
        &self,
        request_id: String,
        request_cancellation_token: CancellationToken,
        timeout_cancellation_token: CancellationToken,
    ) -> anyhow::Result<JoinHandle<anyhow::Result<(String, String)>>> {
        let url = Url::from_str(&format!("{}/proof/{}", self.config.prover_url, request_id))?;
        let mut event_source = EventSource::get(url);

        let awaiter = tokio::task::spawn(async move {
            while let Some(event) = event_source.next().await {
                match event {
                    Ok(Event::Open) => {}
                    Ok(Event::Message(message)) => {
                        let parts: Vec<&str> = message.data.splitn(2, "|").collect();
                        if parts.len() != 2 {
                            anyhow::bail!("Invalid proof response");
                        }

                        return Ok((parts[0].to_string(), parts[1].to_string()));
                    }
                    Err(err) => {
                        error!("{}", err);
                        Err(err)?;
                    }
                }
            }

            Ok((String::from(""), String::from("")))
        });

        let join_handle = tokio::spawn(async move {
            // Wait for either SSE message, timeout or cancellation
            select! {
                proof = awaiter => {
                    proof.unwrap()
                }
                () = timeout_cancellation_token.cancelled() => {
                    anyhow::bail!("Timeout reached while waiting for a proof")
                }
                () = request_cancellation_token.cancelled() => {
                    Ok((String::new(), String::new()))
                }
            }
        });

        Ok(join_handle)
    }

    /// Get the information of the connected notary
    pub async fn get_notary_info(&self) -> anyhow::Result<NotaryInformation> {
        let notary_info_url = format!("{}/notaryinfo", self.config.prover_url);
        let notary_information = reqwest::get(notary_info_url)
            .await?
            .json::<NotaryInformation>()
            .await?;

        Ok(notary_information)
    }
}