EinsatzOnline/src/helper/server_errors.rs

54 lines
2.0 KiB
Rust

use crate::helper::session_cookies::model::SessionCookieStorage;
use crate::helper::settings::Settings;
use chrono::{Duration, Utc};
use rocket::request::Outcome;
use rocket::response::{Redirect, Responder};
use rocket::{Request, State, Response};
use rocket::http::Status;
use rocket::response::status::Custom;
/// Catches all 401 errors and redirects to front page to show error / portal page if cookie valled
/// Will be called when session cookies are invalid / if user has no permission to access feature
#[catch(401)]
pub async fn unauthorized (req: & Request<'_>) -> Redirect {
let storage = match req.guard::<&State<SessionCookieStorage>>().await {
Outcome::Success(storage) => storage,
_ => return Redirect::to("/?error=unauthorized"),
};
let settings = req
.guard::<&State<Settings>>().await
.expect("Settings not available!");
match req.cookies().get_private("session") {
Some(cookie) => match storage.get(cookie.value().to_string()) {
Ok(cookie) => {
let mut updated_cookie = cookie.clone();
updated_cookie.expires = Utc::now()
.checked_add_signed(Duration::seconds(settings.application.session_timeout))
.expect("Session timeout specified in configuration is too great!");
storage.update(updated_cookie);
Redirect::to("/portal?error=unauthorized")
}
Err(_) => Redirect::to("/?error=unauthorized"),
},
None => Redirect::to("/?error=unauthorized"),
}
}
/*
/// Catches all 403 errors and redirects to main portal page to show error
/// Will be called when member tries to access module/action without required permissions
#[catch(403)]
pub fn forbidden() -> Redirect {
Ok(Redirect::to("/portal?error=forbidden")
}
#[catch(404)]
pub fn notfound() -> Redirect {
Ok(Redirect::to("/portal?error=notfound")
}
#[catch(501)]
pub fn notimplemented() -> Redirect {
Ok(Redirect::to("/portal?error=notimplemented")
}
*/