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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! # OAuth 1.0a client
//!
//! This module provide an oauth1a client implementation. It was firstly designed
//! to interact with the Clever-Cloud's api, but has been extended to be more
//! generic.

#[cfg(feature = "metrics")]
use std::time::Instant;
use std::{
    collections::BTreeMap,
    convert::TryFrom,
    error::Error,
    fmt::{self, Debug, Display, Formatter},
    time::{SystemTime, SystemTimeError},
};

use async_trait::async_trait;
use bytes::Buf;
use crypto_common::InvalidLength;
use hmac::{Hmac, Mac};
use hyper::{
    client::{
        connect::{dns::GaiResolver, Connect},
        HttpConnector,
    },
    header, Body, Method, StatusCode,
};
use hyper_tls::HttpsConnector;
#[cfg(feature = "metrics")]
use lazy_static::lazy_static;
#[cfg(feature = "logging")]
use log::{error, log_enabled, trace, Level};
#[cfg(feature = "metrics")]
use prometheus::{opts, register_counter_vec, CounterVec};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sha2::Sha512;
use uuid::Uuid;

pub mod connector;
#[cfg(feature = "proxy")]
pub mod proxy;

// -----------------------------------------------------------------------------
// Telemetry

#[cfg(feature = "metrics")]
lazy_static! {
    static ref CLIENT_REQUEST: CounterVec = register_counter_vec!(
        opts!("oauth10a_client_request", "number of request on api"),
        &["endpoint", "method", "status"]
    )
    .expect("metrics 'oauth10a_client_request' to not be initialized");
    static ref CLIENT_REQUEST_DURATION: CounterVec = register_counter_vec!(
        opts!(
            "oauth10a_client_request_duration",
            "duration of request on api"
        ),
        &["endpoint", "method", "status", "unit"]
    )
    .expect("metrics 'oauth10a_client_request_duration' to not be initialized");
}

// -----------------------------------------------------------------------------
// Types
type HmacSha512 = Hmac<Sha512>;

// -----------------------------------------------------------------------------
// Request trait

#[async_trait]
pub trait Request {
    type Error;

    async fn request<T, U>(
        &self,
        method: &Method,
        endpoint: &str,
        payload: &T,
    ) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync;
}

// -----------------------------------------------------------------------------
// RestClient trait

#[async_trait]
pub trait RestClient
where
    Self: Debug,
{
    type Error;

    async fn get<T>(&self, endpoint: &str) -> Result<T, Self::Error>
    where
        T: DeserializeOwned + Debug + Send + Sync;

    async fn post<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync;

    async fn put<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync;

    async fn patch<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync;

    async fn delete(&self, endpoint: &str) -> Result<(), Self::Error>;
}

// -----------------------------------------------------------------------------
// ClientCredentials structure

#[derive(PartialEq, Eq, Clone, Debug, Default)]
pub struct Credentials {
    pub token: String,
    pub secret: String,
    pub consumer_key: String,
    pub consumer_secret: String,
}

// -----------------------------------------------------------------------------
// OAuth1 trait

pub const OAUTH1_CONSUMER_KEY: &str = "oauth_consumer_key";
pub const OAUTH1_NONCE: &str = "oauth_nonce";
pub const OAUTH1_SIGNATURE: &str = "oauth_signature";
pub const OAUTH1_SIGNATURE_METHOD: &str = "oauth_signature_method";
pub const OAUTH1_SIGNATURE_HMAC_SHA512: &str = "HMAC-SHA512";
pub const OAUTH1_TIMESTAMP: &str = "oauth_timestamp";
pub const OAUTH1_VERSION: &str = "oauth_version";
pub const OAUTH1_VERSION_1: &str = "1.0";
pub const OAUTH1_TOKEN: &str = "oauth_token";

pub trait OAuth1
where
    Self: Debug,
{
    type Error;

    // `params` returns OAuth1 parameters without the signature one
    fn params(&self) -> BTreeMap<String, String>;

    // `signature` returns the computed signature from given parameters
    fn signature(&self, method: &str, endpoint: &str) -> Result<String, Self::Error>;

    // `signing_key` returns the key that is used to signed the signature
    fn signing_key(&self) -> String;

    #[cfg_attr(feature = "trace", tracing::instrument)]
    // `sign` returns OAuth1 formatted Authorization header value
    fn sign(&self, method: &str, endpoint: &str) -> Result<String, Self::Error> {
        let signature = self.signature(method, endpoint)?;
        let mut params = self.params();

        params.insert(OAUTH1_SIGNATURE.to_string(), signature);

        let mut base = params
            .iter()
            .map(|(k, v)| format!("{}=\"{}\"", k, urlencoding::encode(v)))
            .collect::<Vec<_>>();

        base.sort();

        Ok(format!("OAuth {}", base.join(", ")))
    }
}

// -----------------------------------------------------------------------------
// ResponseError structure

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ResponseError {
    #[serde(rename = "id")]
    pub id: u32,
    #[serde(rename = "message")]
    pub message: String,
    #[serde(rename = "type")]
    pub kind: String,
}

impl Display for ResponseError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "got response {} {}, {}",
            self.kind, self.id, self.message
        )
    }
}

impl Error for ResponseError {}

// -----------------------------------------------------------------------------
// SignerError enum

#[derive(thiserror::Error, Debug)]
pub enum SignerError {
    #[error("failed to compute invalid key length, {0}")]
    Digest(InvalidLength),
    #[error("failed to compute time since unix epoch, {0}")]
    UnixEpochTime(SystemTimeError),
    #[error("failed to parse signature paramater, {0}")]
    Parse(String),
}

// -----------------------------------------------------------------------------
// Signer structure

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Signer {
    pub nonce: String,
    pub timestamp: u64,
    pub credentials: Credentials,
}

impl OAuth1 for Signer {
    type Error = SignerError;

    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn params(&self) -> BTreeMap<String, String> {
        let mut params = BTreeMap::new();

        params.insert(
            OAUTH1_CONSUMER_KEY.to_string(),
            self.credentials.consumer_key.to_string(),
        );
        params.insert(OAUTH1_NONCE.to_string(), self.nonce.to_string());
        params.insert(
            OAUTH1_SIGNATURE_METHOD.to_string(),
            OAUTH1_SIGNATURE_HMAC_SHA512.to_string(),
        );
        params.insert(OAUTH1_TIMESTAMP.to_string(), self.timestamp.to_string());
        params.insert(OAUTH1_VERSION.to_string(), OAUTH1_VERSION_1.to_string());
        params.insert(OAUTH1_TOKEN.to_string(), self.credentials.token.to_string());
        params
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn signing_key(&self) -> String {
        format!(
            "{}&{}",
            urlencoding::encode(&self.credentials.consumer_secret.to_owned()),
            urlencoding::encode(&self.credentials.secret.to_owned())
        )
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn signature(&self, method: &str, endpoint: &str) -> Result<String, Self::Error> {
        let (host, query) = match endpoint.find(|c| '?' == c) {
            None => (endpoint, ""),
            // split one character further to not get the '?' character
            Some(position) => endpoint.split_at(position),
        };

        let query = query.strip_prefix('?').unwrap_or(query);
        let mut params = self.params();

        if !query.is_empty() {
            for qparam in query.split('&') {
                let (k, v) = qparam.split_at(qparam.find('=').ok_or_else(|| {
                    SignerError::Parse(format!("failed to parse query parameter, {}", qparam))
                })?);

                if !params.contains_key(k) {
                    params.insert(k.to_string(), v.strip_prefix('=').unwrap_or(v).to_owned());
                }
            }
        }

        let mut params = params
            .iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect::<Vec<_>>();

        params.sort();

        let base = format!(
            "{}&{}&{}",
            urlencoding::encode(method),
            urlencoding::encode(host),
            urlencoding::encode(&params.join("&"))
        );

        let mut hasher = HmacSha512::new_from_slice(self.signing_key().as_bytes())
            .map_err(SignerError::Digest)?;

        hasher.update(base.as_bytes());

        let digest = hasher.finalize().into_bytes();
        Ok(base64::encode(digest.as_slice()))
    }
}

impl TryFrom<Credentials> for Signer {
    type Error = SignerError;

    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn try_from(credentials: Credentials) -> Result<Self, Self::Error> {
        let nonce = Uuid::new_v4().to_string();
        let timestamp = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map_err(SignerError::UnixEpochTime)?
            .as_secs();

        Ok(Self {
            nonce,
            timestamp,
            credentials,
        })
    }
}

// -----------------------------------------------------------------------------
// ClientError enum

#[derive(thiserror::Error, Debug)]
pub enum ClientError {
    #[error("failed to build request, {0}")]
    RequestBuilder(hyper::http::Error),
    #[error("failed to execute request, {0}")]
    Request(hyper::Error),
    #[error("failed to execute request, got status code {0}, {1}")]
    StatusCode(StatusCode, ResponseError),
    #[error("failed to aggregate body, {0}")]
    BodyAggregation(hyper::Error),
    #[error("failed to serialize body, {0}")]
    Serialize(serde_json::Error),
    #[error("failed to deserialize body, {0}")]
    Deserialize(serde_json::Error),
    #[error("failed to create request signer, {0}")]
    Signer(SignerError),
    #[error("failed to compute request digest, {0}")]
    Digest(SignerError),
}

// -----------------------------------------------------------------------------
// Client structure

pub const APPLICATION_JSON: &str = "application/json";
pub const UTF8: &str = "utf-8";

#[derive(Clone, Debug)]
pub struct Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    inner: hyper::Client<C, Body>,
    credentials: Option<Credentials>,
}

#[async_trait]
impl<C> Request for Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    type Error = ClientError;

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn request<T, U>(
        &self,
        method: &Method,
        endpoint: &str,
        payload: &T,
    ) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        let buf = serde_json::to_vec(payload).map_err(ClientError::Serialize)?;
        let mut builder = hyper::Request::builder();
        if let Some(credentials) = &self.credentials {
            let signer = Signer::try_from(credentials.to_owned()).map_err(ClientError::Signer)?;

            builder = builder.header(
                header::AUTHORIZATION,
                signer
                    .sign(method.as_str(), endpoint)
                    .map_err(ClientError::Digest)?,
            );
        }

        let req = builder
            .method(method)
            .uri(endpoint)
            .header(
                header::CONTENT_TYPE,
                &format!("{}; charset={}", APPLICATION_JSON, UTF8),
            )
            .header(header::CONTENT_LENGTH, &format!("{}", buf.len()))
            .header(header::ACCEPT_CHARSET, UTF8)
            .header(header::ACCEPT, APPLICATION_JSON)
            .body(Body::from(buf.to_owned()))
            .map_err(ClientError::RequestBuilder)?;

        #[cfg(feature = "logging")]
        if log_enabled!(Level::Trace) {
            trace!(
                "execute request, endpoint: '{}', method: '{}', body: '{}'",
                endpoint,
                method.to_string(),
                String::from_utf8_lossy(&buf).to_string()
            );
        }

        #[cfg(feature = "metrics")]
        let instant = Instant::now();
        let res = self
            .inner
            .request(req)
            .await
            .map_err(ClientError::Request)?;

        let status = res.status();
        let buf = hyper::body::aggregate(res.into_body())
            .await
            .map_err(ClientError::BodyAggregation)?;

        #[cfg(feature = "logging")]
        if log_enabled!(Level::Trace) {
            trace!(
                "received response, endpoint: '{}', method: '{}', status: '{}'",
                endpoint,
                method.to_string(),
                status.as_u16()
            );
        }

        #[cfg(feature = "metrics")]
        CLIENT_REQUEST
            .with_label_values(&[endpoint, method.as_ref(), &status.as_u16().to_string()])
            .inc();

        #[cfg(feature = "metrics")]
        CLIENT_REQUEST_DURATION
            .with_label_values(&[
                endpoint,
                method.as_ref(),
                &status.as_u16().to_string(),
                "us",
            ])
            .inc_by(Instant::now().duration_since(instant).as_micros() as f64);

        if !status.is_success() {
            return Err(ClientError::StatusCode(
                status,
                serde_json::from_reader(buf.reader()).map_err(ClientError::Deserialize)?,
            ));
        }

        Ok(serde_json::from_reader(buf.reader()).map_err(ClientError::Deserialize)?)
    }
}

#[async_trait]
impl<C> RestClient for Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    type Error = ClientError;

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn get<T>(&self, endpoint: &str) -> Result<T, Self::Error>
    where
        T: DeserializeOwned + Debug + Send + Sync,
    {
        let method = &Method::GET;
        let mut builder = hyper::Request::builder();
        if let Some(credentials) = &self.credentials {
            let signer = Signer::try_from(credentials.to_owned()).map_err(ClientError::Signer)?;

            builder = builder.header(
                header::AUTHORIZATION,
                signer
                    .sign(method.as_str(), endpoint)
                    .map_err(ClientError::Digest)?,
            );
        }

        let req = builder
            .method(method)
            .header(header::ACCEPT_CHARSET, UTF8)
            .header(header::ACCEPT, APPLICATION_JSON)
            .uri(endpoint)
            .body(Body::empty())
            .map_err(ClientError::RequestBuilder)?;

        #[cfg(feature = "logging")]
        if log_enabled!(Level::Trace) {
            trace!(
                "execute request, endpoint: '{}', method: '{}', body: '<none>'",
                endpoint,
                method.to_string()
            );
        }

        #[cfg(feature = "metrics")]
        let instant = Instant::now();
        let res = self
            .inner
            .request(req)
            .await
            .map_err(ClientError::Request)?;

        let status = res.status();
        let buf = hyper::body::aggregate(res.into_body())
            .await
            .map_err(ClientError::BodyAggregation)?;

        #[cfg(feature = "logging")]
        if log_enabled!(Level::Trace) {
            trace!(
                "received response, endpoint: '{}', method: '{}', status: '{}'",
                endpoint,
                method.to_string(),
                status.as_u16()
            );
        }

        #[cfg(feature = "metrics")]
        CLIENT_REQUEST
            .with_label_values(&[endpoint, method.as_ref(), &status.as_u16().to_string()])
            .inc();

        #[cfg(feature = "metrics")]
        CLIENT_REQUEST_DURATION
            .with_label_values(&[
                endpoint,
                method.as_ref(),
                &status.as_u16().to_string(),
                "us",
            ])
            .inc_by(Instant::now().duration_since(instant).as_micros() as f64);

        if !status.is_success() {
            return Err(ClientError::StatusCode(
                status,
                serde_json::from_reader(buf.reader()).map_err(ClientError::Deserialize)?,
            ));
        }

        Ok(serde_json::from_reader(buf.reader()).map_err(ClientError::Deserialize)?)
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn post<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        self.request(&Method::POST, endpoint, payload).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn put<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        self.request(&Method::PUT, endpoint, payload).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn patch<T, U>(&self, endpoint: &str, payload: &T) -> Result<U, Self::Error>
    where
        T: Serialize + Debug + Send + Sync,
        U: DeserializeOwned + Debug + Send + Sync,
    {
        self.request(&Method::PATCH, endpoint, payload).await
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    async fn delete(&self, endpoint: &str) -> Result<(), Self::Error> {
        let method = &Method::DELETE;
        let mut builder = hyper::Request::builder();
        if let Some(credentials) = &self.credentials {
            let signer = Signer::try_from(credentials.to_owned()).map_err(ClientError::Signer)?;

            builder = builder.header(
                header::AUTHORIZATION,
                signer
                    .sign(method.as_str(), endpoint)
                    .map_err(ClientError::Digest)?,
            );
        }

        let req = builder
            .method(method)
            .uri(endpoint)
            .body(Body::empty())
            .map_err(ClientError::RequestBuilder)?;

        #[cfg(feature = "logging")]
        if log_enabled!(Level::Trace) {
            trace!(
                "execute request, endpoint: '{}', method: '{}', body: '<none>'",
                endpoint,
                method.to_string()
            );
        }

        #[cfg(feature = "metrics")]
        let instant = Instant::now();
        let res = self
            .inner
            .request(req)
            .await
            .map_err(ClientError::Request)?;

        let status = res.status();
        let buf = hyper::body::aggregate(res.into_body())
            .await
            .map_err(ClientError::BodyAggregation)?;

        #[cfg(feature = "logging")]
        if log_enabled!(Level::Trace) {
            trace!(
                "received response, endpoint: '{}', method: '{}', status: '{}'",
                endpoint,
                method.to_string(),
                status.as_u16()
            );
        }

        #[cfg(feature = "metrics")]
        CLIENT_REQUEST
            .with_label_values(&[endpoint, method.as_ref(), &status.as_u16().to_string()])
            .inc();

        #[cfg(feature = "metrics")]
        CLIENT_REQUEST_DURATION
            .with_label_values(&[
                endpoint,
                method.as_ref(),
                &status.as_u16().to_string(),
                "us",
            ])
            .inc_by(Instant::now().duration_since(instant).as_micros() as f64);

        if !status.is_success() {
            return Err(ClientError::StatusCode(
                status,
                serde_json::from_reader(buf.reader()).map_err(ClientError::Deserialize)?,
            ));
        }

        Ok(())
    }
}

impl Default for Client<HttpsConnector<HttpConnector<GaiResolver>>> {
    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn default() -> Self {
        Self::from(HttpsConnector::new())
    }
}

impl From<Credentials> for Client<HttpsConnector<HttpConnector<GaiResolver>>> {
    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn from(credentials: Credentials) -> Self {
        Self::new(HttpsConnector::new(), Some(credentials))
    }
}

impl<C> From<C> for Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    #[cfg_attr(feature = "trace", tracing::instrument)]
    fn from(connector: C) -> Self {
        Self::new(connector, None)
    }
}

impl<C> Client<C>
where
    C: Connect + Clone + Debug + Send + Sync + 'static,
{
    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn new(connector: C, credentials: Option<Credentials>) -> Self {
        let inner = hyper::Client::builder().build(connector);

        Self { inner, credentials }
    }

    #[cfg_attr(feature = "trace", tracing::instrument)]
    pub fn set_credentials(&mut self, credentials: Option<Credentials>) {
        self.credentials = credentials;
    }
}