| 21 | #[async_trait::async_trait] |
| 22 | impl<State: Clone + Send + Sync + 'static> tide::Middleware<State> for AuthMiddleware { |
| 23 | async fn handle( |
| 24 | &self, |
| 25 | mut request: tide::Request<State>, |
| 26 | next: tide::Next<'_, State>, |
| 27 | ) -> tide::Result { |
| 28 | let maybe_exp: Option<chrono::DateTime<chrono::offset::Utc>> = request.session().get("exp"); |
| 29 | if let Some(exp) = maybe_exp { |
| 30 | let now = chrono::offset::Utc::now(); |
| 31 | if exp > now { |
| 32 | info!("authenticated session found"); |
| 33 | let response = next.run(request).await; |
| 34 | Ok(response) |
| 35 | } else { |
| 36 | info!("expired session, redirecting"); |
| 37 | request.session_mut().remove("exp"); |
| 38 | |
| 39 | Ok(tide::Redirect::new("/oauth/login").into()) |
| 40 | } |
| 41 | } else { |
| 42 | info!("authenticated NOT found, redirecting"); |
| 43 | Ok(tide::Redirect::new("/oauth/login").into()) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub fn configure_oauth( |