From 05c9e26c0ffd885c9c2519abbeef3d70a5d18f19 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 3 Oct 2023 17:57:11 -0400 Subject: [PATCH] Remove the Iron middleware files --- file-service/src/middleware/authentication.rs | 51 ------------------- file-service/src/middleware/logging.rs | 16 ------ file-service/src/middleware/mod.rs | 6 --- file-service/src/middleware/restform.rs | 34 ------------- 4 files changed, 107 deletions(-) delete mode 100644 file-service/src/middleware/authentication.rs delete mode 100644 file-service/src/middleware/logging.rs delete mode 100644 file-service/src/middleware/mod.rs delete mode 100644 file-service/src/middleware/restform.rs diff --git a/file-service/src/middleware/authentication.rs b/file-service/src/middleware/authentication.rs deleted file mode 100644 index 658bee8..0000000 --- a/file-service/src/middleware/authentication.rs +++ /dev/null @@ -1,51 +0,0 @@ -use iron::headers; -use iron::middleware::BeforeMiddleware; -use iron::prelude::*; -use iron::typemap::Key; -use orizentic::{filedb, OrizenticCtx, Secret}; -use params::{FromValue, Params}; - -use crate::cookies::{Cookie, CookieJar}; - -pub struct Authentication { - pub auth: OrizenticCtx, -} - -impl Key for Authentication { - type Value = orizentic::VerifiedToken; -} - -impl Authentication { - pub fn new(secret: Secret, auth_db_path: String) -> Authentication { - let claims = filedb::load_claims_from_file(&auth_db_path).expect("claims did not load"); - let orizentic = OrizenticCtx::new(secret, claims); - Authentication { auth: orizentic } - } - - fn authenticate_user( - &self, - token_str: String, - ) -> Result { - self.auth.decode_and_validate_text(token_str) - } -} - -impl BeforeMiddleware for Authentication { - fn before(&self, req: &mut Request) -> IronResult<()> { - let params = req.get_ref::().unwrap(); - let token = match params.get("auth").and_then(|v| String::from_value(v)) { - Some(token_str) => self.authenticate_user(token_str).ok(), - None => { - let m_jar = req - .headers - .get::() - .map(|cookies| CookieJar::from(cookies)); - m_jar - .and_then(|jar| jar.lookup("auth").cloned()) - .and_then(|Cookie { value, .. }| self.authenticate_user(value.clone()).ok()) - } - }; - token.map(|t| req.extensions.insert::(t)); - Ok(()) - } -} diff --git a/file-service/src/middleware/logging.rs b/file-service/src/middleware/logging.rs deleted file mode 100644 index 2aceb1d..0000000 --- a/file-service/src/middleware/logging.rs +++ /dev/null @@ -1,16 +0,0 @@ -use iron::middleware::{AfterMiddleware, BeforeMiddleware}; -use iron::prelude::*; - -pub struct Logging {} - -impl BeforeMiddleware for Logging { - fn before(&self, _: &mut Request) -> IronResult<()> { - Ok(()) - } -} - -impl AfterMiddleware for Logging { - fn after(&self, _: &mut Request, res: Response) -> IronResult { - Ok(res) - } -} diff --git a/file-service/src/middleware/mod.rs b/file-service/src/middleware/mod.rs deleted file mode 100644 index 1bab46b..0000000 --- a/file-service/src/middleware/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod authentication; -mod logging; -mod restform; - -pub use authentication::Authentication; -pub use restform::RestForm; diff --git a/file-service/src/middleware/restform.rs b/file-service/src/middleware/restform.rs deleted file mode 100644 index 5cfadd9..0000000 --- a/file-service/src/middleware/restform.rs +++ /dev/null @@ -1,34 +0,0 @@ -use iron::method::Method; -use iron::middleware::BeforeMiddleware; -use iron::prelude::*; -use params::{Params, Value}; - -pub struct RestForm {} - -impl RestForm { - fn method(&self, v: &Value) -> Option { - match v { - Value::String(method_str) => match method_str.as_str() { - "delete" => Some(Method::Delete), - _ => None, - }, - _ => None, - } - } -} - -impl BeforeMiddleware for RestForm { - fn before(&self, req: &mut Request) -> IronResult<()> { - if req.method == Method::Post { - let method = { - let params = req.get_ref::().unwrap(); - params - .get("_method") - .and_then(|m| self.method(m)) - .unwrap_or(Method::Post) - }; - req.method = method; - } - Ok(()) - } -}