rowdy 0.0.9

`rowdy` is a Rocket based JSON Web token based authentication server.
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! Authentication module, including traits for identity provider and `Responder`s for
//! authentication.
use std::error;
use std::fmt;
use std::ops::Deref;

use hyper;
use hyper::header;
use rocket;
use rocket::http::Status;
use rocket::request::{self, FromRequest, Request};
use rocket::response;
use rocket::Outcome;
use serde::de::DeserializeOwned;
use serde::Serialize;

pub mod util;

mod noop;
pub use self::noop::NoOp;
pub use self::noop::NoOpConfiguration;

#[cfg(feature = "simple_authenticator")]
pub mod simple;
#[cfg(feature = "simple_authenticator")]
pub use self::simple::SimpleAuthenticator;
#[cfg(feature = "simple_authenticator")]
pub use self::simple::SimpleAuthenticatorConfiguration;

#[cfg(feature = "ldap_authenticator")]
mod ldap;
#[cfg(feature = "ldap_authenticator")]
pub use self::ldap::LdapAuthenticator;

use crate::JsonValue;

/// Re-exported [`hyper::header::Scheme`]
pub type Scheme = dyn hyper::header::Scheme<Err = hyper::error::Error>;
/// Re-exported [`hyper::header::Basic`].
pub type Basic = hyper::header::Basic;
/// Re-exported [`hyper::header::Bearer`].
pub type Bearer = hyper::header::Bearer;

/// A typedef for an `Authenticator` trait object that requires HTTP Basic authentication
pub type BasicAuthenticator = dyn Authenticator<Basic>;
/// A typedef for an `Authenticator` trait object that requires Bearer authentication.
pub type BearerAuthenticator = dyn Authenticator<Bearer>;
/// A typedef for an `Authenticator` trait object that uses an arbitrary string
pub type StringAuthenticator = dyn Authenticator<String>;

/// Authentication errors
#[derive(Debug)]
pub enum Error {
    /// Authentication was attempted successfully, but failed because of bad user credentials,
    /// or other reasons.
    AuthenticationFailure,
    /// A generic error
    GenericError(String),
    /// An error due to `hyper`, such as header parsing failure
    HyperError(hyper::error::Error),
    /// The `Authorization` HTTP request header was required but was missing. This variant will
    /// `respond` with the
    /// appropriate `WWW-Authenticate` header.
    MissingAuthorization {
        /// The HTTP basic authentication realm
        realm: String,
    },
}

impl_from_error!(String, Error::GenericError);
impl_from_error!(hyper::error::Error, Error::HyperError);

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::AuthenticationFailure => "Authentication has failed",
            Error::MissingAuthorization { .. } => {
                "The request header `Authorization` is required but is missing"
            }
            Error::GenericError(ref e) => &**e,
            Error::HyperError(ref e) => e.description(),
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        match *self {
            Error::HyperError(ref e) => Some(e),
            _ => Some(self),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Error::HyperError(ref e) => fmt::Display::fmt(e, f),
            _ => write!(f, "{}", error::Error::description(self)),
        }
    }
}

impl<'r> response::Responder<'r> for Error {
    fn respond_to(self, _: &Request<'_>) -> Result<response::Response<'r>, Status> {
        error_!("Authentication Error: {:?}", self);
        match self {
            Error::MissingAuthorization { ref realm } => {
                // TODO: Support other schemes!
                let www_header =
                    rocket::http::Header::new("WWW-Authenticate", format!("Basic realm={}", realm));

                Ok(response::Response::build()
                    .status(Status::Unauthorized)
                    .header(www_header)
                    .finalize())
            }
            Error::AuthenticationFailure => Err(Status::Unauthorized),
            Error::HyperError(_) => Err(Status::BadRequest),
            _ => Err(Status::InternalServerError),
        }
    }
}

/// `Authorization` HTTP Request Header
#[derive(Debug)]
pub struct Authorization<S: header::Scheme + 'static>(pub header::Authorization<S>);

impl<'a, 'r, S: header::Scheme + 'static> FromRequest<'a, 'r> for Authorization<S> {
    type Error = Error;

    fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Error> {
        match request.headers().get_one("Authorization") {
            Some(authorization) => match Self::new(authorization) {
                Err(_) => Outcome::Forward(()),
                Ok(parsed) => Outcome::Success(parsed),
            },
            None => Outcome::Forward(()),
        }
    }
}

impl<S: header::Scheme + 'static> Authorization<S> {
    /// Create a new Authorization header
    pub fn new<'a>(header: &'a str) -> Result<Self, Error> {
        use hyper::header::Header;

        let bytes: Vec<u8> = header.as_bytes().to_vec();
        let parsed = header::Authorization::parse_header(&[bytes])?;
        Ok(Authorization(parsed))
    }

    /// Convenience function to check if the Authorization is `Basic`
    pub fn is_basic(&self) -> bool {
        if let Some("Basic") = S::scheme() {
            true
        } else {
            false
        }
    }

    /// Convenience function to check if the Authorization is `Bearer`
    pub fn is_bearer(&self) -> bool {
        if let Some("Bearer") = S::scheme() {
            true
        } else {
            false
        }
    }

    /// Convenience function to check if the Authorization is `None`
    pub fn is_string(&self) -> bool {
        S::scheme().is_none()
    }
}

impl Authorization<Basic> {
    /// Convenience method to retrieve the username from a HTTP Basic Authorization request header
    pub fn username(&self) -> String {
        let Authorization(header::Authorization(Basic { ref username, .. })) = *self;
        username.to_string()
    }

    /// Convenience method to retrieve the password from a HTTP Basic Authorization request header
    pub fn password(&self) -> Option<String> {
        let Authorization(header::Authorization(Basic { ref password, .. })) = *self;
        password.clone()
    }
}

impl Authorization<Bearer> {
    /// Convenience method to retrieve the token from a bearer Authorization request header.
    pub fn token(&self) -> String {
        let Authorization(header::Authorization(Bearer { ref token })) = *self;
        token.to_string()
    }
}

impl Authorization<String> {
    /// Convenience method to retrieve the token from an arbitrary Authorization request header.
    pub fn string(&self) -> String {
        let Authorization(header::Authorization(ref s)) = *self;
        s.to_string()
    }
}

impl<S: header::Scheme + 'static> Deref for Authorization<S> {
    type Target = header::Authorization<S>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Authenticator trait to be implemented by identity provider (idp) adapters to
/// provide authentication.
/// Each idp may support all the
/// [schemes](https://hyper.rs/hyper/v0.10.5/hyper/header/trait.Scheme.html)
/// supported, or just one.
///
/// Usually, you will want to include an `Authenticator` trait object as part of Rocket's
/// [managed state](https://rocket.rs/guide/state/). Before you can do that, however,
/// you will need to `Box` it up.
/// See example below.
///
/// # Examples
///
/// # No-op authenticator
/// You can refer to the [source code](../../src/rowdy/auth/noop.rs.html) for the `NoOp`
/// authenticator for a simple implementation.
///
/// ## Simple Authenticator
///
/// Refer to the `MockAuthenticator`[../../src/rowdy/auth/mod.rs.html] implemented
///  in the test code for this module.
pub trait Authenticator<S: header::Scheme + 'static>: Send + Sync {
    /// Verify the credentials provided in the headers with the authenticator for
    /// the initial issuing of Access Tokens.
    ///
    /// If the Authenticator supports re-issuing of access tokens subsequently using refresh tokens,
    /// and it is requested for, the function should return a `JsonValue`
    /// containing the payload to include with the refresh token.
    ///
    /// Users should not user`authenticate` directly and use `prepare_authentication_response`
    /// instead.
    fn authenticate(
        &self,
        authorization: &Authorization<S>,
        include_refresh_payload: bool,
    ) -> Result<AuthenticationResult, crate::Error>;

    /// Verify the credentials provided with the refresh token payload, if supported by the
    /// authenticator.
    ///
    /// A default implementation that returns an `Err(::Error::UnsupportedOperation)` is provided.
    ///
    /// Users should not user `authenticate` directly and use `prepare_refresh_response` instead.
    fn authenticate_refresh_token(
        &self,
        _payload: &JsonValue,
    ) -> Result<AuthenticationResult, crate::Error> {
        Err(crate::Error::UnsupportedOperation)
    }

    /// Prepare a response to an authentication request
    /// by first verifying credentials. If validation fails, will return an `Err` with the response
    /// to be sent. Otherwise, the unwrapped authentication result will be returned in an `Ok`.
    /// This function will also check that the authenticator behaves correctly by checking that
    /// it does not return a refresh token payload when it is not requested for
    fn prepare_authentication_response(
        &self,
        authorization: &Authorization<S>,
        request_refresh_token: bool,
    ) -> Result<AuthenticationResult, crate::Error> {
        let result = self.authenticate(authorization, request_refresh_token)?;
        if !request_refresh_token && result.refresh_payload.is_some() {
            Err(Error::GenericError(
                "Misbehaving authenticator: refresh token payload was \
                 returned when it was not requested for"
                    .to_string(),
            ))?;
        }
        Ok(result)
    }

    /// Prepare a response to a refresh request by first verifying the refresh payload.
    ///
    /// If validation fails, will return an `Err` with the response
    /// to be sent. Otherwise, the unwrapped authentication result will be returned in an `Ok`.
    /// This function will also check that the authenticator behaves correctly by checking that
    /// it does not return a refresh token payload
    fn prepare_refresh_response(
        &self,
        refresh_payload: &JsonValue,
    ) -> Result<AuthenticationResult, crate::Error> {
        let result = self.authenticate_refresh_token(refresh_payload)?;
        if result.refresh_payload.is_some() {
            Err(Error::GenericError(
                "Misbehaving authenticator: refresh token payload was \
                 returned for a refresh operation"
                    .to_string(),
            ))?;
        }
        Ok(result)
    }
}

/// Convenience function to respond with a missing authorization error
pub fn missing_authorization<T>(realm: &str) -> Result<T, crate::Error> {
    Err(Error::MissingAuthorization {
        realm: realm.to_string(),
    })?
}

/// Configuration for the associated type `Authenticator`. [`crate::Configuration`] expects its
/// `authenticator` field to implement this trait.
///
/// Before launching, `rowdy` will attempt to make an `Authenticator` based off the
/// configuration by calling the `make_authenticator` method.
pub trait AuthenticatorConfiguration<S: header::Scheme + 'static>:
    Send + Sync + Serialize + DeserializeOwned + 'static
{
    /// The `Authenticator` type this configuration is associated with
    type Authenticator: Authenticator<S>;

    /// Using the configuration struct, create a new `Authenticator`.
    fn make_authenticator(&self) -> Result<Self::Authenticator, crate::Error>;
}

/// Result from a successful authentication operation
#[derive(Clone, PartialEq, Debug)]
pub struct AuthenticationResult {
    /// The subject of the authentication
    pub subject: String,
    /// Additional private claims to be included in the authentication token, if any
    pub private_claims: JsonValue,
    /// The payload to be included in a Refresh token, if any
    pub refresh_payload: Option<JsonValue>,
}

#[cfg(test)]
pub mod tests {
    #[allow(deprecated)]
    use hyper::header::{self, Header, HeaderFormatter};
    use rocket::http;
    use rocket::local::Client;
    use rocket::{self, Rocket, State};

    use super::*;
    use crate::{Error, JsonMap};

    /// Mock authenticator that authenticates only the following:
    ///
    /// - Basic: user `mei` with password `冻住,不许走!`
    /// - Bearer: token `这样可以挡住他们。`
    /// - String: 哦,对不起啦。
    pub struct MockAuthenticator {}

    /// Payload for the `MockAuthenticator` Refresh Token
    #[derive(Serialize, Deserialize, Debug)]
    struct RefreshTokenPayload {
        header: String,
    }

    impl MockAuthenticator {
        /// Convert a header to string
        #[allow(deprecated)]
        fn format<S: header::Scheme + 'static>(authorization: &header::Authorization<S>) -> String {
            HeaderFormatter(authorization).to_string()
        }

        /// Generate a refresh token payload from the header
        fn serialize_refresh_token_payload<S: header::Scheme + 'static>(
            authorization: &header::Authorization<S>,
        ) -> JsonValue {
            let string = From::from(Self::format(authorization));
            let mut map = JsonMap::with_capacity(1);
            let _ = map.insert("header".to_string(), string);
            JsonValue::Object(map)
        }

        /// From a refresh token payload, get the header back
        ///
        /// # Panics
        /// Panics if the refresh token payload is not in the right shape,
        /// or if the content is invalid
        fn deserialize_refresh_token_payload<S: header::Scheme + 'static>(
            refresh_payload: &JsonValue,
        ) -> header::Authorization<S> {
            match *refresh_payload {
                JsonValue::Object(ref map) => {
                    // will panic if the shape is incorrect
                    let header = map["header"].as_str().unwrap();
                    let header = header.as_bytes().to_vec();
                    header::Authorization::parse_header(&[header]).unwrap()
                }
                _ => panic!("Refresh token payload was not a map"),
            }
        }
    }

    impl Authenticator<Basic> for MockAuthenticator {
        fn authenticate(
            &self,
            authorization: &Authorization<Basic>,
            include_refresh_payload: bool,
        ) -> Result<AuthenticationResult, Error> {
            let username = authorization.username();
            let password = authorization.password().unwrap_or_else(|| "".to_string());

            if username == "mei" && password == "冻住,不许走!" {
                let refresh_payload = if include_refresh_payload {
                    Some(Self::serialize_refresh_token_payload(authorization))
                } else {
                    None
                };
                Ok(AuthenticationResult {
                    subject: username,
                    private_claims: JsonValue::Object(JsonMap::new()),
                    refresh_payload,
                })
            } else {
                Err(super::Error::AuthenticationFailure)?
            }
        }

        fn authenticate_refresh_token(
            &self,
            refresh_payload: &JsonValue,
        ) -> Result<AuthenticationResult, Error> {
            let header: header::Authorization<Basic> =
                Self::deserialize_refresh_token_payload(refresh_payload);
            self.authenticate(&Authorization(header), false)
        }
    }

    impl Authenticator<Bearer> for MockAuthenticator {
        fn authenticate(
            &self,
            authorization: &Authorization<Bearer>,
            include_refresh_payload: bool,
        ) -> Result<AuthenticationResult, Error> {
            let token = authorization.token();

            if token == "这样可以挡住他们。" {
                let refresh_payload = if include_refresh_payload {
                    Some(Self::serialize_refresh_token_payload(authorization))
                } else {
                    None
                };
                Ok(AuthenticationResult {
                    subject: "这样可以挡住他们。".to_string(),
                    private_claims: JsonValue::Object(JsonMap::new()),
                    refresh_payload,
                })
            } else {
                Err(super::Error::AuthenticationFailure)?
            }
        }

        fn authenticate_refresh_token(
            &self,
            refresh_payload: &JsonValue,
        ) -> Result<AuthenticationResult, Error> {
            let header: header::Authorization<Bearer> =
                Self::deserialize_refresh_token_payload(refresh_payload);
            self.authenticate(&Authorization(header), false)
        }
    }

    impl Authenticator<String> for MockAuthenticator {
        fn authenticate(
            &self,
            authorization: &Authorization<String>,
            include_refresh_payload: bool,
        ) -> Result<AuthenticationResult, Error> {
            let string = authorization.string();

            if string == "哦,对不起啦。" {
                let refresh_payload = if include_refresh_payload {
                    Some(Self::serialize_refresh_token_payload(authorization))
                } else {
                    None
                };
                Ok(AuthenticationResult {
                    subject: "哦,对不起啦。".to_string(),
                    private_claims: JsonValue::Object(JsonMap::new()),
                    refresh_payload,
                })
            } else {
                Err(super::Error::AuthenticationFailure)?
            }
        }

        fn authenticate_refresh_token(
            &self,
            refresh_payload: &JsonValue,
        ) -> Result<AuthenticationResult, Error> {
            let header: header::Authorization<String> =
                Self::deserialize_refresh_token_payload(refresh_payload);
            self.authenticate(&Authorization(header), false)
        }
    }

    /// Configuration struct for `MockAuthenticator`.
    #[derive(Serialize, Deserialize, Debug)]
    pub struct MockAuthenticatorConfiguration {}

    impl<S> AuthenticatorConfiguration<S> for MockAuthenticatorConfiguration
    where
        S: header::Scheme + 'static,
        MockAuthenticator: Authenticator<S>,
    {
        type Authenticator = MockAuthenticator;

        fn make_authenticator(&self) -> Result<Self::Authenticator, Error> {
            Ok(Self::Authenticator {})
        }
    }

    /// Ignite a Rocket with a Basic authenticator
    pub fn ignite_basic(authenticator: Box<dyn Authenticator<Basic>>) -> Rocket {
        // Ignite rocket
        rocket::ignite()
            .mount("/", routes![auth_basic])
            .manage(authenticator)
    }

    #[get("/")]
    #[allow(unmounted_route)]
    #[allow(needless_pass_by_value)]
    fn auth_basic(
        authorization: Option<Authorization<Basic>>,
        authenticator: State<'_, Box<dyn Authenticator<Basic>>>,
    ) -> Result<(), Error> {
        let authorization = authorization
            .ok_or_else(|| missing_authorization::<()>("https://www.acme.com").unwrap_err())?;
        authenticator
            .prepare_authentication_response(&authorization, true)
            .and_then(|_| Ok(()))
    }

    /// Ignite a Rocket with a Bearer authenticator
    pub fn ignite_bearer(authenticator: Box<dyn Authenticator<Bearer>>) -> Rocket {
        // Ignite rocket
        rocket::ignite()
            .mount("/", routes![auth_bearer])
            .manage(authenticator)
    }

    #[get("/")]
    #[allow(unmounted_route)]
    #[allow(needless_pass_by_value)]
    fn auth_bearer(
        authorization: Option<Authorization<Bearer>>,
        authenticator: State<'_, Box<dyn Authenticator<Bearer>>>,
    ) -> Result<(), Error> {
        let authorization = authorization
            .ok_or_else(|| missing_authorization::<()>("https://www.acme.com").unwrap_err())?;
        authenticator
            .prepare_authentication_response(&authorization, true)
            .and_then(|_| Ok(()))
    }

    /// Ignite a Rocket with a String authenticator
    pub fn ignite_string(authenticator: Box<dyn Authenticator<String>>) -> Rocket {
        // Ignite rocket
        rocket::ignite()
            .mount("/", routes![auth_string])
            .manage(authenticator)
    }

    #[get("/")]
    #[allow(unmounted_route)]
    #[allow(needless_pass_by_value)]
    fn auth_string(
        authorization: Option<Authorization<String>>,
        authenticator: State<'_, Box<dyn Authenticator<String>>>,
    ) -> Result<(), Error> {
        let authorization = authorization
            .ok_or_else(|| missing_authorization::<()>("https://www.acme.com").unwrap_err())?;
        authenticator
            .prepare_authentication_response(&authorization, true)
            .and_then(|_| Ok(()))
    }

    #[test]
    #[allow(deprecated)]
    fn parses_basic_auth_correctly() {
        let auth = header::Authorization(Basic {
            username: "Aladdin".to_owned(),
            password: Some("open sesame".to_string()),
        });

        let header = HeaderFormatter(&auth).to_string();
        let parsed_header = not_err!(Authorization::new(&header));
        let Authorization(header::Authorization(Basic { username, password })) = parsed_header;
        assert_eq!(username, "Aladdin");
        assert_eq!(password, Some("open sesame".to_string()));
    }

    #[test]
    #[allow(deprecated)]
    fn parses_bearer_auth_correctly() {
        let auth = header::Authorization(Bearer {
            token: "token".to_string(),
        });
        let header = HeaderFormatter(&auth).to_string();
        let parsed_header = not_err!(Authorization::new(&header));
        let Authorization(header::Authorization(Bearer { token })) = parsed_header;
        assert_eq!(token, "token");
    }

    #[test]
    #[allow(deprecated)]
    fn parses_string_auth_correctly() {
        let auth = header::Authorization("hello".to_string());
        let header = HeaderFormatter(&auth).to_string();
        let parsed_header: Authorization<String> = not_err!(Authorization::new(&header));
        let Authorization(header::Authorization(ref s)) = parsed_header;
        assert_eq!(s, "hello");
    }

    #[test]
    #[allow(deprecated)]
    fn mock_basic_auth_get_test() {
        let rocket = ignite_basic(Box::new(MockAuthenticator {}));
        let client = not_err!(Client::new(rocket));

        // Make headers
        let auth_header = hyper::header::Authorization(Basic {
            username: "mei".to_owned(),
            password: Some("冻住,不许走!".to_string()),
        });
        let auth_header =
            http::Header::new("Authorization", HeaderFormatter(&auth_header).to_string());
        // Make and dispatch request
        let req = client.get("/").header(auth_header);
        let response = req.dispatch();

        // Assert
        assert_eq!(response.status(), Status::Ok);
    }

    #[test]
    #[allow(deprecated)]
    fn mock_bearer_auth_get_test() {
        let rocket = ignite_bearer(Box::new(MockAuthenticator {}));
        let client = not_err!(Client::new(rocket));

        // Make headers
        let auth_header = hyper::header::Authorization(Bearer {
            token: "这样可以挡住他们。".to_string(),
        });
        let auth_header =
            http::Header::new("Authorization", HeaderFormatter(&auth_header).to_string());
        // Make and dispatch request
        let req = client.get("/").header(auth_header);
        let response = req.dispatch();

        // Assert
        assert_eq!(response.status(), Status::Ok);
    }

    #[test]
    #[allow(deprecated)]
    fn mock_string_auth_get_test() {
        let rocket = ignite_string(Box::new(MockAuthenticator {}));
        let client = not_err!(Client::new(rocket));

        // Make headers
        let auth_header = hyper::header::Authorization("哦,对不起啦。".to_string());
        let auth_header =
            http::Header::new("Authorization", HeaderFormatter(&auth_header).to_string());
        // Make and dispatch request
        let req = client.get("/").header(auth_header);
        let response = req.dispatch();

        // Assert
        assert_eq!(response.status(), Status::Ok);
    }

    #[test]
    #[allow(deprecated)]
    fn mock_basic_auth_get_invalid_credentials() {
        // Ignite rocket
        let rocket = ignite_basic(Box::new(MockAuthenticator {}));
        let client = not_err!(Client::new(rocket));

        // Make headers
        let auth_header = hyper::header::Authorization(Basic {
            username: "Aladin".to_owned(),
            password: Some("let me in".to_string()),
        });
        let auth_header =
            http::Header::new("Authorization", HeaderFormatter(&auth_header).to_string());
        // Make and dispatch request
        let req = client.get("/").header(auth_header);
        let response = req.dispatch();

        // Assert
        assert_eq!(response.status(), Status::Unauthorized);
    }

    #[test]
    #[allow(deprecated)]
    fn mock_bearer_auth_get_invalid_credentials() {
        // Ignite rocket
        let rocket = ignite_bearer(Box::new(MockAuthenticator {}));
        let client = not_err!(Client::new(rocket));

        // Make headers
        let auth_header = hyper::header::Authorization(Bearer {
            token: "bad".to_string(),
        });
        let auth_header =
            http::Header::new("Authorization", HeaderFormatter(&auth_header).to_string());
        // Make and dispatch request
        let req = client.get("/").header(auth_header);
        let response = req.dispatch();

        // Assert
        assert_eq!(response.status(), Status::Unauthorized);
    }

    #[test]
    #[allow(deprecated)]
    fn mock_string_auth_get_invalid_credentials() {
        // Ignite rocket
        let rocket = ignite_string(Box::new(MockAuthenticator {}));
        let client = not_err!(Client::new(rocket));

        // Make headers
        let auth_header = hyper::header::Authorization("bad".to_string());
        let auth_header =
            http::Header::new("Authorization", HeaderFormatter(&auth_header).to_string());
        // Make and dispatch request
        let req = client.get("/").header(auth_header);
        let response = req.dispatch();

        // Assert
        assert_eq!(response.status(), Status::Unauthorized);
    }

    #[test]
    #[allow(deprecated)]
    fn mock_basic_auth_get_missing_credentials() {
        // Ignite rocket
        let rocket = ignite_basic(Box::new(MockAuthenticator {}));
        let client = not_err!(Client::new(rocket));

        // Make and dispatch request
        let req = client.get("/");
        let response = req.dispatch();

        // Assert
        assert_eq!(response.status(), Status::Unauthorized);

        let www_header: Vec<_> = response.headers().get("WWW-Authenticate").collect();
        assert_eq!(www_header, vec!["Basic realm=https://www.acme.com"]);
    }
}