restructure auth mode check

This commit is contained in:
glyph 2022-01-18 17:12:32 +02:00
parent 43344566de
commit 2bfba66dab
1 changed files with 15 additions and 19 deletions

View File

@ -45,28 +45,24 @@ impl<'r> FromRequest<'r> for Authenticated {
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> { async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
// retrieve auth state from managed state (returns `Option<bool>`). // retrieve auth state from managed state (returns `Option<bool>`).
// this value is read from the Rocket.toml config file on start-up // 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() .rocket()
.state::<RocketConfig>() .state::<RocketConfig>()
.map(|config| (&config.disable_auth)); .map(|config| (&config.disable_auth))
.unwrap_or(&false);
match authentication_is_disabled { if authentication_is_disabled {
Some(true) => { let auth = Authenticated {};
let auth = Authenticated {}; request::Outcome::Success(auth)
request::Outcome::Success(auth) } else {
} let authenticated = req
_ => { .cookies()
let authenticated = req .get_private(AUTH_COOKIE_KEY)
.cookies() .and_then(|cookie| cookie.value().parse().ok())
.get_private(AUTH_COOKIE_KEY) .map(|_value: String| Authenticated {});
.and_then(|cookie| cookie.value().parse().ok()) match authenticated {
.map(|_value: String| Authenticated {}); Some(auth) => request::Outcome::Success(auth),
match authenticated { None => request::Outcome::Failure((Status::Forbidden, LoginError::UserNotLoggedIn)),
Some(auth) => request::Outcome::Success(auth),
None => {
request::Outcome::Failure((Status::Forbidden, LoginError::UserNotLoggedIn))
}
}
} }
} }
} }