Remove the Iron middleware files

This commit is contained in:
Savanni D'Gerinel 2023-10-03 17:57:11 -04:00
parent a187582f16
commit 6fb872569d
4 changed files with 0 additions and 107 deletions

View File

@ -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<orizentic::VerifiedToken, orizentic::Error> {
self.auth.decode_and_validate_text(token_str)
}
}
impl BeforeMiddleware for Authentication {
fn before(&self, req: &mut Request) -> IronResult<()> {
let params = req.get_ref::<Params>().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::<headers::Cookie>()
.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::<Authentication>(t));
Ok(())
}
}

View File

@ -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<Response> {
Ok(res)
}
}

View File

@ -1,6 +0,0 @@
mod authentication;
mod logging;
mod restform;
pub use authentication::Authentication;
pub use restform::RestForm;

View File

@ -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<Method> {
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::<Params>().unwrap();
params
.get("_method")
.and_then(|m| self.method(m))
.unwrap_or(Method::Post)
};
req.method = method;
}
Ok(())
}
}