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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! This module contains lower-level primitives for working with the [`KeyserverClient`].

use std::{fmt, pin::Pin};

use futures_core::{
    task::{Context, Poll},
    Future,
};
use futures_util::future::{join, join_all};
use hyper::{
    body::{aggregate, to_bytes},
    http::header::AUTHORIZATION,
    http::Method,
    Body, Error as HyperError, Request, Response, StatusCode,
};
pub use hyper::{
    client::{connect::Connect, HttpConnector},
    Uri,
};
use prost::{DecodeError, Message as _};
use thiserror::Error;
use tower_service::Service;

use super::{KeyserverClient, MetadataPackage, RawAuthWrapperPackage};
use crate::models::*;

type FutResponse<Response, Error> =
    Pin<Box<dyn Future<Output = Result<Response, Error>> + 'static + Send>>;

/// Represents a request for the [`Peers`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetPeers;

/// Error associated with getting [`Peers`] from a keyserver.
#[derive(Debug, Error)]
pub enum GetPeersError<E: fmt::Debug + fmt::Display> {
    /// Error while processing the body.
    #[error("processing body failed: {0}")]
    Body(HyperError),
    /// A connection error occured.
    #[error("connection failure: {0}")]
    Service(E),
    /// Error while decoding the body.
    #[error("body decoding failure: {0}")]
    Decode(DecodeError),
    /// Unexpected status code.
    #[error("unexpected status code: {0}")]
    UnexpectedStatusCode(u16),
    /// Peering is disabled on the keyserver.
    #[error("peering disabled")]
    PeeringDisabled,
}

impl<S> Service<(Uri, GetPeers)> for KeyserverClient<S>
where
    S: Service<Request<Body>, Response = Response<Body>>,
    S: Send + Clone + 'static,
    S::Error: fmt::Debug,
    <S as Service<Request<Body>>>::Error: fmt::Display,
    <S as Service<Request<Body>>>::Future: Send,
{
    type Response = Peers;
    type Error = GetPeersError<S::Error>;
    type Future = FutResponse<Self::Response, Self::Error>;

    fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner_client
            .poll_ready(context)
            .map_err(GetPeersError::Service)
    }

    fn call(&mut self, (uri, _): (Uri, GetPeers)) -> Self::Future {
        let mut client = self.inner_client.clone();
        let http_request = Request::builder()
            .method(Method::GET)
            .uri(uri)
            .body(Body::empty())
            .unwrap(); // This is safe

        let fut = async move {
            let response = client
                .call(http_request)
                .await
                .map_err(Self::Error::Service)?;
            match response.status() {
                StatusCode::OK => (),
                StatusCode::NOT_IMPLEMENTED => return Err(Self::Error::PeeringDisabled),
                code => return Err(Self::Error::UnexpectedStatusCode(code.as_u16())),
            }
            let body = response.into_body();
            let buf = aggregate(body).await.map_err(Self::Error::Body)?;
            let peers = Peers::decode(buf).map_err(Self::Error::Decode)?;
            Ok(peers)
        };
        Box::pin(fut)
    }
}

/// Represents a request for the raw [`AuthWrapper`].
///
/// This will not error on invalid bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GetRawAuthWrapper;

/// Error associated with getting raw [`AuthWrapper`] from a keyserver.
#[derive(Debug, Error)]
pub enum GetRawAuthWrapperError<E: fmt::Debug + fmt::Display> {
    /// Error while processing the body.
    #[error("processing body failed: {0}")]
    Body(HyperError),
    /// A connection error occured.
    #[error("connection failure: {0}")]
    Service(E),
    /// Unexpected status code.
    #[error("unexpected status code: {0}")]
    UnexpectedStatusCode(u16),
    /// POP token missing from headers.
    #[error("missing token")]
    MissingToken,
}

impl<S> Service<(Uri, GetRawAuthWrapper)> for KeyserverClient<S>
where
    S: Service<Request<Body>, Response = Response<Body>>,
    S: Send + Clone + 'static,
    S::Future: Send,
    S::Error: fmt::Debug + fmt::Display,
{
    type Response = RawAuthWrapperPackage;
    type Error = GetRawAuthWrapperError<S::Error>;
    type Future = FutResponse<Self::Response, Self::Error>;

    fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner_client
            .poll_ready(context)
            .map_err(GetRawAuthWrapperError::Service)
    }

    fn call(&mut self, (uri, _): (Uri, GetRawAuthWrapper)) -> Self::Future {
        let mut client = self.inner_client.clone();
        let http_request = Request::builder()
            .method(Method::GET)
            .uri(uri)
            .body(Body::empty())
            .unwrap(); // This is safe
        let fut = async move {
            // Get response
            let response = client
                .call(http_request)
                .await
                .map_err(Self::Error::Service)?;

            // Check status code
            // TODO: Fix this
            match response.status() {
                StatusCode::OK => (),
                code => return Err(Self::Error::UnexpectedStatusCode(code.as_u16())),
            }

            #[allow(clippy::borrow_interior_mutable_const)]
            let token = response
                .headers()
                .into_iter()
                .find(|(name, value)| {
                    *name == AUTHORIZATION && value.as_bytes()[..4] == b"POP "[..]
                })
                .ok_or(Self::Error::MissingToken)?
                .0
                .to_string();

            // Aggregate body
            let body = response.into_body();
            let raw_auth_wrapper = to_bytes(body).await.map_err(Self::Error::Body)?;

            Ok(RawAuthWrapperPackage {
                token,
                raw_auth_wrapper,
            })
        };
        Box::pin(fut)
    }
}

/// Represents a request for the [`AddressMetadata`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetMetadata;

/// Error associated with getting [`AddressMetadata`] from a keyserver.
#[derive(Debug, Error)]
pub enum GetMetadataError<E: fmt::Debug + fmt::Display> {
    /// Error while decoding the [`AddressMetadata`]
    #[error("metadata decoding failure: {0}")]
    MetadataDecode(DecodeError),
    /// Error while decoding the [`AuthWrapper`].
    #[error("authwrapper decoding failure: {0}")]
    AuthWrapperDecode(DecodeError),
    /// Error while parsing the [`AuthWrapper`].
    #[error("authwrapper parsing failure: {0}")]
    AuthWrapperParse(ParseError),
    /// Error while parsing the [`AuthWrapper`].
    #[error("authwrapper verification failure: {0}")]
    AuthWrapperVerify(VerifyError),
    /// Error while processing the body.
    #[error("processing body failed: {0}")]
    Body(HyperError),
    /// A connection error occured.
    #[error("connection failure: {0}")]
    Service(E),
    /// Unexpected status code.
    #[error("unexpected status code: {0}")]
    UnexpectedStatusCode(u16),
    /// POP token missing from headers.
    #[error("missing token")]
    MissingToken,
}

impl<S> Service<(Uri, GetMetadata)> for KeyserverClient<S>
where
    S: Service<Request<Body>, Response = Response<Body>>,
    S: Send + Clone + 'static,
    S::Future: Send,
    S::Error: fmt::Debug + fmt::Display,
{
    type Response = MetadataPackage;
    type Error = GetMetadataError<S::Error>;
    type Future = FutResponse<Self::Response, Self::Error>;

    fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner_client
            .poll_ready(context)
            .map_err(GetMetadataError::Service)
    }

    fn call(&mut self, (uri, _): (Uri, GetMetadata)) -> Self::Future {
        let mut client = self.inner_client.clone();
        let http_request = Request::builder()
            .method(Method::GET)
            .uri(uri)
            .body(Body::empty())
            .unwrap(); // This is safe
        let fut = async move {
            // Get response
            let response = client
                .call(http_request)
                .await
                .map_err(Self::Error::Service)?;

            // Check status code
            // TODO: Fix this
            match response.status() {
                StatusCode::OK => (),
                code => return Err(Self::Error::UnexpectedStatusCode(code.as_u16())),
            }

            #[allow(clippy::borrow_interior_mutable_const)]
            let token = response
                .headers()
                .into_iter()
                .find(|(name, value)| {
                    *name == AUTHORIZATION && value.as_bytes()[..4] == b"POP "[..]
                })
                .ok_or(Self::Error::MissingToken)?
                .0
                .to_string();

            // Deserialize and decode body
            let body = response.into_body();
            let raw_auth_wrapper = to_bytes(body).await.map_err(Self::Error::Body)?;
            let auth_wrapper = AuthWrapper::decode(raw_auth_wrapper.clone())
                .map_err(Self::Error::AuthWrapperDecode)?;

            // Parse auth wrapper
            let parsed_auth_wrapper = auth_wrapper
                .parse()
                .map_err(Self::Error::AuthWrapperParse)?;

            // Verify signature
            parsed_auth_wrapper
                .verify()
                .map_err(Self::Error::AuthWrapperVerify)?;

            // Decode metadata
            let metadata = AddressMetadata::decode(&mut parsed_auth_wrapper.payload.as_slice())
                .map_err(Self::Error::MetadataDecode)?;

            Ok(MetadataPackage {
                token,
                public_key: parsed_auth_wrapper.public_key,
                metadata,
                raw_auth_wrapper,
            })
        };
        Box::pin(fut)
    }
}

/// Request for putting [`AuthWrapper`] to the keyserver.
#[derive(Debug, Clone, PartialEq)]
pub struct PutMetadata {
    /// POP authorization token.
    pub token: String,
    /// The [`AuthWrapper`] to be put to the keyserver.
    pub auth_wrapper: AuthWrapper,
}

/// Error associated with putting [`AddressMetadata`] to the keyserver.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum PutMetadataError<E: fmt::Debug + fmt::Display> {
    /// A connection error occured.
    #[error("connection failure: {0}")]
    Service(E),
    /// Unexpected status code.
    #[error("unexpected status code: {0}")]
    UnexpectedStatusCode(u16),
}

impl<S> Service<(Uri, PutMetadata)> for KeyserverClient<S>
where
    S: Service<Request<Body>, Response = Response<Body>>,
    S: Send + Clone + 'static,
    S::Error: fmt::Debug + fmt::Display,
    S::Future: Send,
{
    type Response = ();
    type Error = PutMetadataError<S::Error>;
    type Future = FutResponse<Self::Response, Self::Error>;

    fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner_client
            .poll_ready(context)
            .map_err(PutMetadataError::Service)
    }

    fn call(&mut self, (uri, request): (Uri, PutMetadata)) -> Self::Future {
        let mut client = self.inner_client.clone();

        // Construct body
        let mut body = Vec::with_capacity(request.auth_wrapper.encoded_len());
        request.auth_wrapper.encode(&mut body).unwrap();

        let http_request = Request::builder()
            .method(Method::PUT)
            .uri(uri)
            .header(AUTHORIZATION, request.token)
            .body(Body::from(body))
            .unwrap(); // This is safe

        let fut = async move {
            // Get response
            let response = client
                .call(http_request)
                .await
                .map_err(Self::Error::Service)?;

            // Check status code
            // TODO: Fix this
            match response.status() {
                StatusCode::OK => (),
                code => return Err(Self::Error::UnexpectedStatusCode(code.as_u16())),
            }

            Ok(())
        };
        Box::pin(fut)
    }
}

/// Request for putting a raw [`AuthWrapper`] to the keyserver.
#[derive(Debug, Clone, PartialEq)]
pub struct PutRawAuthWrapper {
    /// POP authorization token.
    pub token: String,
    /// The raw [`AuthWrapper`] to be put to the keyserver.
    pub raw_auth_wrapper: Vec<u8>,
}

impl<S> Service<(Uri, PutRawAuthWrapper)> for KeyserverClient<S>
where
    S: Service<Request<Body>, Response = Response<Body>>,
    S: Send + Clone + 'static,
    S::Error: fmt::Debug + fmt::Display,
    S::Future: Send,
{
    type Response = ();
    type Error = PutMetadataError<S::Error>;
    type Future = FutResponse<Self::Response, Self::Error>;

    fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner_client
            .poll_ready(context)
            .map_err(PutMetadataError::Service)
    }

    fn call(&mut self, (uri, request): (Uri, PutRawAuthWrapper)) -> Self::Future {
        let mut client = self.inner_client.clone();

        // Construct body
        let body = request.raw_auth_wrapper;

        let http_request = Request::builder()
            .method(Method::PUT)
            .uri(uri)
            .header(AUTHORIZATION, request.token)
            .body(Body::from(body))
            .unwrap(); // This is safe

        let fut = async move {
            // Get response
            let response = client
                .call(http_request)
                .await
                .map_err(Self::Error::Service)?;

            // Check status code
            // TODO: Fix this
            match response.status() {
                StatusCode::OK => (),
                code => return Err(Self::Error::UnexpectedStatusCode(code.as_u16())),
            }

            Ok(())
        };
        Box::pin(fut)
    }
}

/// Request for performing multiple requests to a range of keyservers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SampleRequest<T> {
    /// The [`Uri`]s of the targetted keyservers.
    pub uris: Vec<Uri>,
    /// The request to be broadcast.
    pub request: T,
}

/// Error associated with sending sample requests.
#[derive(Debug, Error)]
pub enum SampleError<E: fmt::Debug + fmt::Display> {
    /// Error while polling service.
    #[error("polling failure: {0}")]
    Poll(E),
    /// Sample totally failed. Contains errors paired with the [`Uri`] of the keyserver they originated at.
    #[error("sampling failure: {0:?}")] // TODO: Make this prettier
    Sample(Vec<(Uri, E)>),
}

impl<S, T> Service<SampleRequest<T>> for KeyserverClient<S>
where
    T: Send + 'static + Clone + Sized,
    S: Send + Clone + 'static,
    Self: Service<(Uri, T)>,
    <Self as Service<(Uri, T)>>::Response: Send + fmt::Debug,
    <Self as Service<(Uri, T)>>::Error: fmt::Debug + fmt::Display + Send,
    <Self as Service<(Uri, T)>>::Future: Send,
{
    #[allow(clippy::type_complexity)]
    type Response = Vec<(
        Uri,
        Result<<Self as Service<(Uri, T)>>::Response, <Self as Service<(Uri, T)>>::Error>,
    )>;
    type Error = SampleError<<Self as Service<(Uri, T)>>::Error>;
    type Future = FutResponse<Self::Response, Self::Error>;

    fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.poll_ready(context).map_err(SampleError::Poll)
    }

    fn call(&mut self, SampleRequest { uris, request }: SampleRequest<T>) -> Self::Future {
        let mut inner_client = self.clone();

        let fut = async move {
            // Collect futures
            let response_futs = uris.into_iter().map(move |uri| {
                let response_fut = inner_client.call((uri.clone(), request.clone()));
                let uri_fut = async move { uri };
                join(uri_fut, response_fut)
            });
            let responses: Vec<(Uri, Result<_, _>)> = join_all(response_futs).await;

            // If no successes then return all errors
            if responses.iter().all(|(_, res)| res.is_err()) {
                let errors = responses
                    .into_iter()
                    .map(|(uri, result)| (uri, result.unwrap_err()))
                    .collect();
                return Err(SampleError::Sample(errors));
            }

            Ok(responses)
        };
        Box::pin(fut)
    }
}