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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! AWS API request signatures verification routines.
//!
//! This is essentially the server-side complement of [rusoto_signature](https://crates.io/crates/rusoto_signature)
//! but follows the implementation of [python-aws-sig](https://github.com/dacut/python-aws-sig).
//!
//! This implements the AWS [SigV4](http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)
//! and [SigV4S3](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html)
//! algorithms.
//!

use {
    crate::{crypto::hmac_sha256, GetSigningKeyRequest, GetSigningKeyResponse, SignatureError},
    chrono::{DateTime, Duration, Utc},
    derive_builder::Builder,
    log::{debug, trace},
    ring::digest::SHA256_OUTPUT_LEN,
    scratchstack_aws_principal::Principal,
    std::{
        fmt::{Debug, Formatter, Result as FmtResult},
        future::Future,
    },
    subtle::ConstantTimeEq,
    tower::{BoxError, Service, ServiceExt},
};

/// Algorithm for AWS SigV4
const AWS4_HMAC_SHA256: &str = "AWS4-HMAC-SHA256";

/// String included at the end of the AWS SigV4 credential scope
const AWS4_REQUEST: &str = "aws4_request";

/// Compact ISO8601 format used for the string to sign.
const ISO8601_COMPACT_FORMAT: &str = "%Y%m%dT%H%M%SZ";

/// Length of an ISO8601 date string in the UTC time zone.
const ISO8601_UTC_LENGTH: usize = 16;

/// Error message: `"Credential must have exactly 5 slash-delimited elements, e.g. keyid/date/region/service/term,"`
const MSG_CREDENTIAL_MUST_HAVE_FIVE_PARTS: &str =
    "Credential must have exactly 5 slash-delimited elements, e.g. keyid/date/region/service/term,";

/// Error message: `"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."`
const MSG_REQUEST_SIGNATURE_MISMATCH: &str = "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.";

/// SHA-256 of an empty string.
const SHA256_EMPTY: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

/// Length of a SHA-256 hex string.
const SHA256_HEX_LENGTH: usize = SHA256_EMPTY.len();

/// Low-level structure for performing AWS SigV4 authentication after a canonical request has been generated.
#[derive(Builder, Clone, Default)]
#[builder(derive(Debug))]
pub struct SigV4Authenticator {
    /// The SHA-256 hash of the canonical request.
    canonical_request_sha256: [u8; SHA256_OUTPUT_LEN],

    /// The credential passed into the request, in the form of `keyid/date/region/service/aws4_request`.
    /// This is allowed to be invalid upon creation since the validation of the credential is performed _after_ the
    /// validation of the request timestamp.
    credential: String,

    /// The optional session token.
    #[builder(setter(into, strip_option), default)]
    session_token: Option<String>,

    /// The signature passed into the request.
    signature: String,

    /// The timestamp of the request, from either `X-Amz-Date` query string/header or the `Date` header.
    request_timestamp: DateTime<Utc>,
}

impl SigV4Authenticator {
    /// Create a builder for `SigV4Authenticator`.
    pub fn builder() -> SigV4AuthenticatorBuilder {
        SigV4AuthenticatorBuilder::default()
    }

    /// Retrieve the SHA-256 hash of the canonical request.
    #[inline]
    pub fn canonical_request_sha256(&self) -> [u8; SHA256_OUTPUT_LEN] {
        self.canonical_request_sha256
    }

    /// Retrieve the credential passed into the request, in the form of `keyid/date/region/service/aws4_request`.
    #[inline]
    pub fn credential(&self) -> &str {
        &self.credential
    }

    /// Retrieve the optional session token.
    #[inline]
    pub fn session_token(&self) -> Option<&str> {
        self.session_token.as_deref()
    }

    /// Retrieve the signature passed into the request.
    #[inline]
    pub fn signature(&self) -> &str {
        &self.signature
    }

    /// Retrieve the timestamp of the request.
    #[inline]
    pub fn request_timestamp(&self) -> DateTime<Utc> {
        self.request_timestamp
    }

    /// Verify the request parameters make sense for the region, service, and specified timestamp.
    /// This must be called successfully before calling [validate_signature].
    fn prevalidate(
        &self,
        region: &str,
        service: &str,
        server_timestamp: DateTime<Utc>,
        allowed_mismatch: Duration,
    ) -> Result<(), SignatureError> {
        let req_ts = self.request_timestamp;
        let min_ts = server_timestamp.checked_sub_signed(allowed_mismatch).unwrap_or(server_timestamp);
        let max_ts = server_timestamp.checked_add_signed(allowed_mismatch).unwrap_or(server_timestamp);

        // Rule 10: Make sure date isn't expired...
        if req_ts < min_ts {
            trace!("prevalidate: request timestamp {} is before minimum timestamp {}", req_ts, min_ts);
            return Err(SignatureError::SignatureDoesNotMatch(Some(format!(
                "Signature expired: {} is now earlier than {} ({} - {}.)",
                req_ts.format(ISO8601_COMPACT_FORMAT),
                min_ts.format(ISO8601_COMPACT_FORMAT),
                server_timestamp.format(ISO8601_COMPACT_FORMAT),
                duration_to_string(allowed_mismatch)
            ))));
        }

        // Rule 11: ... or too far into the future.
        if req_ts > max_ts {
            trace!("prevalidate: request timestamp {} is after maximum timestamp {}", req_ts, max_ts);
            return Err(SignatureError::SignatureDoesNotMatch(Some(format!(
                "Signature not yet current: {} is still later than {} ({} + {}.)",
                req_ts.format(ISO8601_COMPACT_FORMAT),
                max_ts.format(ISO8601_COMPACT_FORMAT),
                server_timestamp.format(ISO8601_COMPACT_FORMAT),
                duration_to_string(allowed_mismatch)
            ))));
        }

        // Rule 12: Credential scope must have exactly five elements.
        let credential_parts = self.credential.split('/').collect::<Vec<&str>>();
        if credential_parts.len() != 5 {
            trace!("prevalidate: credential has {} parts, expected 5", credential_parts.len());
            return Err(SignatureError::IncompleteSignature(format!(
                "{} got '{}'",
                MSG_CREDENTIAL_MUST_HAVE_FIVE_PARTS, self.credential
            )));
        }

        let cscope_date = credential_parts[1];
        let cscope_region = credential_parts[2];
        let cscope_service = credential_parts[3];
        let cscope_term = credential_parts[4];

        // Rule 13: Credential scope must be correct for the region/service/date.
        let mut cscope_errors = Vec::new();
        if cscope_region != region {
            trace!("prevalidate: credential region '{}' does not match expected region '{}'", cscope_region, region);
            cscope_errors.push(format!("Credential should be scoped to a valid region, not '{}'.", cscope_region));
        }

        if cscope_service != service {
            trace!(
                "prevalidate: credential service '{}' does not match expected service '{}'",
                cscope_service,
                service
            );
            cscope_errors.push(format!("Credential should be scoped to correct service: '{}'.", service));
        }

        if cscope_term != AWS4_REQUEST {
            trace!(
                "prevalidate: credential terminator '{}' does not match expected terminator '{}'",
                cscope_term,
                AWS4_REQUEST
            );
            cscope_errors.push(format!(
                "Credential should be scoped with a valid terminator: 'aws4_request', not '{}'.",
                cscope_term
            ));
        }

        let expected_cscope_date = req_ts.format("%Y%m%d").to_string();
        if cscope_date != expected_cscope_date {
            trace!(
                "prevalidate: credential date '{}' does not match expected date '{}'",
                cscope_date,
                expected_cscope_date
            );
            cscope_errors.push(format!("Date in Credential scope does not match YYYYMMDD from ISO-8601 version of date from HTTP: '{}' != '{}', from '{}'.", cscope_date, expected_cscope_date, req_ts.format(ISO8601_COMPACT_FORMAT)));
        }

        if !cscope_errors.is_empty() {
            return Err(SignatureError::SignatureDoesNotMatch(Some(cscope_errors.join(" "))));
        }

        Ok(())
    }

    /// Return the signing key (`kSigning` from the [AWS documentation](https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html))
    /// for the re
    async fn get_signing_key<S, F>(
        &self,
        region: &str,
        service: &str,
        get_signing_key: &mut S,
    ) -> Result<GetSigningKeyResponse, SignatureError>
    where
        S: Service<GetSigningKeyRequest, Response = GetSigningKeyResponse, Error = BoxError, Future = F> + Send,
        F: Future<Output = Result<GetSigningKeyResponse, BoxError>> + Send,
    {
        let access_key = self.credential.split('/').next().expect("prevalidate must been called first").to_string();

        let req = GetSigningKeyRequest {
            access_key,
            session_token: self.session_token.clone(),
            request_date: self.request_timestamp.date(),
            region: region.to_string(),
            service: service.to_string(),
        };

        match get_signing_key.oneshot(req).await {
            Ok(key) => {
                trace!("get_signing_key: got signing key");
                Ok(key)
            }
            Err(e) => {
                debug!("get_signing_key: error getting signing key: {}", e);
                match e.downcast::<SignatureError>() {
                    Ok(sig_err) => Err(*sig_err),
                    Err(e) => Err(SignatureError::InternalServiceError(e)),
                }
            }
        }
    }

    pub(crate) fn get_string_to_sign(&self) -> Vec<u8> {
        let mut result = Vec::with_capacity(
            AWS4_HMAC_SHA256.len() + 1 + ISO8601_UTC_LENGTH + 1 + self.credential.len() + 1 + SHA256_HEX_LENGTH,
        );
        let hashed_canonical_request = hex::encode(self.canonical_request_sha256);

        // Remove the access key from the credential to get the credential scope. This requires that prevalidate() has
        // been called.
        let cscope = self.credential.splitn(2, '/').nth(1).expect("prevalidate should have been called first");

        result.extend(AWS4_HMAC_SHA256.as_bytes());
        result.push(b'\n');
        result.extend(self.request_timestamp.format(ISO8601_COMPACT_FORMAT).to_string().as_bytes());
        result.push(b'\n');
        result.extend(cscope.as_bytes());
        result.push(b'\n');
        result.extend(hashed_canonical_request.as_bytes());
        result
    }

    /// Validate the request signature.
    pub async fn validate_signature<S, F>(
        &self,
        region: &str,
        service: &str,
        server_timestamp: DateTime<Utc>,
        allowed_mismatch: Duration,
        get_signing_key: &mut S,
    ) -> Result<Principal, SignatureError>
    where
        S: Service<GetSigningKeyRequest, Response = GetSigningKeyResponse, Error = BoxError, Future = F> + Send,
        F: Future<Output = Result<GetSigningKeyResponse, BoxError>> + Send,
    {
        self.prevalidate(region, service, server_timestamp, allowed_mismatch)?;
        let string_to_sign = self.get_string_to_sign();
        trace!("String to sign: {:?}", String::from_utf8_lossy(string_to_sign.as_ref()));
        let response = self.get_signing_key(region, service, get_signing_key).await?;
        let expected_signature = hex::encode(hmac_sha256(response.signing_key.as_ref(), string_to_sign.as_ref()));
        let expected_signature_bytes = expected_signature.as_bytes();
        let signature_bytes = self.signature.as_bytes();
        let is_equal: bool = signature_bytes.ct_eq(expected_signature_bytes).into();
        if !is_equal {
            trace!("Signature mismatch: expected '{}', got '{}'", expected_signature, self.signature);
            Err(SignatureError::SignatureDoesNotMatch(Some(MSG_REQUEST_SIGNATURE_MISMATCH.to_string())))
        } else {
            Ok(response.principal)
        }
    }
}

impl Debug for SigV4Authenticator {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.debug_struct("SigV4Authenticator")
            .field("canonical_request_sha256", &hex::encode(&self.canonical_request_sha256))
            .field("session_token", &self.session_token)
            .field("signature", &self.signature)
            .field("request_timestamp", &self.request_timestamp)
            .finish()
    }
}

#[allow(dead_code)] // used in tests
impl SigV4AuthenticatorBuilder {
    pub(crate) fn get_credential(&self) -> &Option<String> {
        &self.credential
    }

    pub(crate) fn get_signature(&self) -> &Option<String> {
        &self.signature
    }

    pub(crate) fn get_session_token(&self) -> &Option<Option<String>> {
        &self.session_token
    }
}

fn duration_to_string(duration: Duration) -> String {
    let secs = duration.num_seconds();
    if secs % 60 == 0 {
        format!("{} min", duration.num_minutes())
    } else {
        format!("{} sec", secs)
    }
}

#[cfg(test)]
mod tests {
    use {
        super::duration_to_string,
        crate::{
            service_for_signing_key_fn, GetSigningKeyRequest, GetSigningKeyResponse, KSecretKey, SigV4Authenticator,
            SigV4AuthenticatorBuilder, SignatureError,
        },
        chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, Utc},
        log::LevelFilter,
        ring::digest::SHA256_OUTPUT_LEN,
        scratchstack_aws_principal::{Principal, SessionData, User},
        scratchstack_errors::ServiceError,
        std::{error::Error, fs::File},
        tower::BoxError,
    };

    fn init() {
        let _ = env_logger::builder().is_test(true).filter_level(LevelFilter::Trace).try_init();
    }

    #[test]
    fn test_derived() {
        init();
        let epoch = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc);
        let test_time = DateTime::<Utc>::from_utc(
            NaiveDateTime::new(NaiveDate::from_ymd(2015, 8, 30), NaiveTime::from_hms(12, 36, 0)),
            Utc,
        );
        let auth1: SigV4Authenticator = Default::default();
        assert_eq!(
            auth1.canonical_request_sha256().as_slice(),
            b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
        );
        assert!(auth1.credential().is_empty());
        assert!(auth1.session_token().is_none());
        assert!(auth1.signature().is_empty());
        assert_eq!(auth1.request_timestamp(), epoch);

        let sha256: [u8; 32] = [
            0, 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,
        ];
        let auth2 = SigV4AuthenticatorBuilder::default()
            .canonical_request_sha256(sha256)
            .credential("AKIA1/20151231/us-east-1/example/aws4_request".to_string())
            .session_token("token".to_string())
            .signature("1234".to_string())
            .request_timestamp(test_time)
            .build()
            .unwrap();

        assert_eq!(
            auth2.canonical_request_sha256.as_slice(),
            &[
                0u8, 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,
            ]
        );
        assert_eq!(auth2.credential(), "AKIA1/20151231/us-east-1/example/aws4_request");
        assert_eq!(auth2.session_token(), Some("token"));
        assert_eq!(auth2.signature(), "1234");
        assert_eq!(auth2.request_timestamp(), test_time);

        assert_eq!(auth2.credential(), auth2.clone().credential());
        let _ = format!("{:?}", auth2);
    }

    async fn get_signing_key(request: GetSigningKeyRequest) -> Result<GetSigningKeyResponse, BoxError> {
        if let Some(ref token) = request.session_token {
            match token.as_str() {
                "internal-service-error" => {
                    return Err("internal service error".into());
                }
                "invalid" => {
                    return Err(Box::new(SignatureError::InvalidClientTokenId(
                        "The security token included in the request is invalid".to_string(),
                    )))
                }
                "io-error" => {
                    let e = File::open("/00Hi1i6V4qad5nF/6KPlcyW4H9miTOD02meLgTaV09O2UToMPTE9j6sNmHZ/08EzM4qOs8bYOINWJ9RheQVadpgixRTh0VjcwpVPoo1Rh4gNAJhS4cj/this-path/does//not/exist").unwrap_err();
                    return Err(Box::new(SignatureError::from(e)));
                }
                "expired" => {
                    return Err(Box::new(SignatureError::ExpiredToken(
                        "The security token included in the request is expired".to_string(),
                    )))
                }
                _ => (),
            }
        }

        match request.access_key.as_str() {
            "AKIDEXAMPLE" => {
                let principal = Principal::from(vec![User::new("aws", "123456789012", "/", "test").unwrap().into()]);
                let k_secret = KSecretKey::from_str("wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY");
                let k_signing =
                    k_secret.to_ksigning(request.request_date, request.region.as_str(), request.service.as_str());

                let response = GetSigningKeyResponse {
                    principal,
                    session_data: SessionData::default(),
                    signing_key: k_signing,
                };
                Ok(response)
            }
            _ => Err(Box::new(SignatureError::InvalidClientTokenId(
                "The AWS access key provided does not exist in our records".to_string(),
            ))),
        }
    }

    #[tokio::test]
    async fn test_error_ordering() {
        init();

        // Test that the error ordering is correct.
        let creq_sha256: [u8; SHA256_OUTPUT_LEN] = [0; SHA256_OUTPUT_LEN];
        let test_timestamp = DateTime::<Utc>::from_utc(
            NaiveDateTime::new(NaiveDate::from_ymd(2015, 8, 30), NaiveTime::from_hms(12, 36, 0)),
            Utc,
        );
        let outdated_timestamp = DateTime::<Utc>::from_utc(
            NaiveDateTime::new(NaiveDate::from_ymd(2015, 8, 30), NaiveTime::from_hms(12, 20, 59)),
            Utc,
        );
        let future_timestamp = DateTime::<Utc>::from_utc(
            NaiveDateTime::new(NaiveDate::from_ymd(2015, 8, 30), NaiveTime::from_hms(12, 51, 1)),
            Utc,
        );
        let get_signing_key_svc = service_for_signing_key_fn(get_signing_key);
        let mismatch = Duration::minutes(15);

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20130101/wrong-region/wrong-service".to_string())
            .session_token("expired")
            .signature("invalid".to_string())
            .request_timestamp(outdated_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::SignatureDoesNotMatch(ref msg) = e {
            assert_eq!(
                msg.as_ref().unwrap(),
                "Signature expired: 20150830T122059Z is now earlier than 20150830T122100Z (20150830T123600Z - 15 min.)"
            );
            assert_eq!(e.error_code(), "SignatureDoesNotMatch");
            assert_eq!(e.http_status(), 403);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20130101/wrong-region/wrong-service".to_string())
            .session_token("expired")
            .signature("invalid".to_string())
            .request_timestamp(future_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::SignatureDoesNotMatch(ref msg) = e {
            assert_eq!(
                msg.as_ref().unwrap(),
                "Signature not yet current: 20150830T125101Z is still later than 20150830T125100Z (20150830T123600Z + 15 min.)"
            );
            assert_eq!(e.error_code(), "SignatureDoesNotMatch");
            assert_eq!(e.http_status(), 403);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20130101/wrong-region/wrong-service".to_string())
            .session_token("expired")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::IncompleteSignature(_) = e {
            assert_eq!(
                e.to_string(),
                "Credential must have exactly 5 slash-delimited elements, e.g. keyid/date/region/service/term, got 'AKIDFOO/20130101/wrong-region/wrong-service'"
            );
            assert_eq!(e.error_code(), "IncompleteSignature");
            assert_eq!(e.http_status(), 400);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20130101/wrong-region/wrong-service/aws5_request".to_string())
            .session_token("expired")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::SignatureDoesNotMatch(_) = e {
            assert_eq!(
                e.to_string(),
                "Credential should be scoped to a valid region, not 'wrong-region'. Credential should be scoped to correct service: 'example'. Credential should be scoped with a valid terminator: 'aws4_request', not 'aws5_request'. Date in Credential scope does not match YYYYMMDD from ISO-8601 version of date from HTTP: '20130101' != '20150830', from '20150830T123600Z'."
            );
            assert_eq!(e.error_code(), "SignatureDoesNotMatch");
            assert_eq!(e.http_status(), 403);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20150830/us-east-1/example/aws4_request".to_string())
            .session_token("invalid")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::InvalidClientTokenId(_) = e {
            assert_eq!(e.to_string(), "The security token included in the request is invalid");
            assert_eq!(e.error_code(), "InvalidClientTokenId");
            assert_eq!(e.http_status(), 403);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20150830/us-east-1/example/aws4_request".to_string())
            .session_token("expired")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::ExpiredToken(_) = e {
            assert_eq!(e.to_string(), "The security token included in the request is expired");
            assert_eq!(e.error_code(), "ExpiredToken");
            assert_eq!(e.http_status(), 403);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20150830/us-east-1/example/aws4_request".to_string())
            .session_token("internal-service-error")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::InternalServiceError(ref err) = e {
            assert_eq!(format!("{:?}", err), r#""internal service error""#);
            assert_eq!(e.to_string(), "internal service error");
            assert_eq!(e.error_code(), "InternalFailure");
            assert_eq!(e.http_status(), 500);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20150830/us-east-1/example/aws4_request".to_string())
            .session_token("io-error")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::IO(_) = e {
            let e_string = e.to_string();
            assert!(
                e_string.contains("No such file or directory")
                    || e_string.contains("The system cannot find the file specified"),
                "Error message: {:#?}",
                e_string
            );
            assert_eq!(e.error_code(), "InternalFailure");
            assert_eq!(e.http_status(), 500);
            assert!(e.source().is_some());
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDFOO/20150830/us-east-1/example/aws4_request".to_string())
            .session_token("ok")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::InvalidClientTokenId(_) = e {
            assert_eq!(e.to_string(), "The AWS access key provided does not exist in our records");
            assert_eq!(e.error_code(), "InvalidClientTokenId");
            assert_eq!(e.http_status(), 403);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDEXAMPLE/20150830/us-east-1/example/aws4_request".to_string())
            .session_token("ok")
            .signature("invalid".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let e = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap_err();

        if let SignatureError::SignatureDoesNotMatch(_) = e {
            assert_eq!(e.to_string(), "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.");
            assert_eq!(e.error_code(), "SignatureDoesNotMatch");
            assert_eq!(e.http_status(), 403);
        } else {
            panic!("Unexpected error: {:?}", e);
        }

        let auth = SigV4Authenticator::builder()
            .canonical_request_sha256(creq_sha256)
            .credential("AKIDEXAMPLE/20150830/us-east-1/example/aws4_request".to_string())
            .session_token("ok")
            .signature("88bf1ccb1e3e4df7bb2ed6d89bcd8558d6770845007e1a5c392ac9edce0d5deb".to_string())
            .request_timestamp(test_timestamp)
            .build()
            .unwrap();

        let _ = auth
            .validate_signature("us-east-1", "example", test_timestamp, mismatch, &mut get_signing_key_svc.clone())
            .await
            .unwrap();
    }

    #[test]
    fn test_duration_formatting() {
        init();
        assert_eq!(duration_to_string(Duration::seconds(32)).as_str(), "32 sec");
        assert_eq!(duration_to_string(Duration::seconds(60)).as_str(), "1 min");
        assert_eq!(duration_to_string(Duration::seconds(61)).as_str(), "61 sec");
        assert_eq!(duration_to_string(Duration::seconds(600)).as_str(), "10 min");
    }
}