From 2bfba66dab398a8b3a1cb6832bcf30a293234420 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 18 Jan 2022 17:12:32 +0200 Subject: [PATCH] restructure auth mode check --- peach-web/src/routes/authentication.rs | 34 ++++++++++++-------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/peach-web/src/routes/authentication.rs b/peach-web/src/routes/authentication.rs index b16469e..e3e51a3 100644 --- a/peach-web/src/routes/authentication.rs +++ b/peach-web/src/routes/authentication.rs @@ -45,28 +45,24 @@ impl<'r> FromRequest<'r> for Authenticated { async fn from_request(req: &'r Request<'_>) -> request::Outcome { // retrieve auth state from managed state (returns `Option`). // this value is read from the Rocket.toml config file on start-up - let authentication_is_disabled = req + let authentication_is_disabled: bool = *req .rocket() .state::() - .map(|config| (&config.disable_auth)); + .map(|config| (&config.disable_auth)) + .unwrap_or(&false); - match authentication_is_disabled { - Some(true) => { - let auth = Authenticated {}; - request::Outcome::Success(auth) - } - _ => { - let authenticated = req - .cookies() - .get_private(AUTH_COOKIE_KEY) - .and_then(|cookie| cookie.value().parse().ok()) - .map(|_value: String| Authenticated {}); - match authenticated { - Some(auth) => request::Outcome::Success(auth), - None => { - request::Outcome::Failure((Status::Forbidden, LoginError::UserNotLoggedIn)) - } - } + if authentication_is_disabled { + let auth = Authenticated {}; + request::Outcome::Success(auth) + } else { + let authenticated = req + .cookies() + .get_private(AUTH_COOKIE_KEY) + .and_then(|cookie| cookie.value().parse().ok()) + .map(|_value: String| Authenticated {}); + match authenticated { + Some(auth) => request::Outcome::Success(auth), + None => request::Outcome::Failure((Status::Forbidden, LoginError::UserNotLoggedIn)), } } }