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
pub mod prelude {
  pub use tide::http::{headers, headers::HeaderName, StatusCode};
}

use prelude::*;

use tide::{utils::async_trait, Middleware, Next, Request, Response, Result};

#[derive(Clone)]
pub struct RequiresAuthorization {
  pub authorization: HeaderName,
  pub authenticate: HeaderName,
  pub status: StatusCode,
  pub scheme: String,
}

impl RequiresAuthorization {
  pub fn www(scheme: String) -> Self {
    Self {
      authorization: headers::AUTHORIZATION,
      authenticate: headers::WWW_AUTHENTICATE,
      status: StatusCode::Unauthorized,
      scheme,
    }
  }

  pub fn proxy(scheme: String) -> Self {
    Self {
      authorization: headers::PROXY_AUTHORIZATION,
      authenticate: headers::PROXY_AUTHENTICATE,
      status: StatusCode::ProxyAuthenticationRequired,
      scheme,
    }
  }
}

#[async_trait]
impl<State: Clone + Send + Sync + 'static> Middleware<State> for RequiresAuthorization {
  async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> Result {
    match request.header(&self.authorization) {
      Some(_) => Ok(next.run(request).await),
      None => {
        let mut response = Response::new(self.status);
        response.insert_header(&self.authenticate, &self.scheme);
        Ok(response)
      }
    }
  }
}