From 0b34b776acd09fb7a4ad529d05dcfdab08eaaea3 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 3 May 2023 15:59:28 +0200 Subject: [PATCH 1/2] remove unwrap :-) --- src/model/user.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/model/user.rs b/src/model/user.rs index c9d6a63..4918030 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -29,6 +29,7 @@ pub enum LoginError { NotAnAdmin, NotACox, NoPasswordSet(User), + DeserializationError, } impl User { @@ -162,10 +163,12 @@ impl<'r> FromRequest<'r> for User { async fn from_request(req: &'r Request<'_>) -> request::Outcome { match req.cookies().get_private("loggedin_user") { - Some(user) => { - let user: User = serde_json::from_str(user.value()).unwrap(); //TODO: fixme - Outcome::Success(user) - } + Some(user) => match serde_json::from_str(user.value()) { + Ok(user) => Outcome::Success(user), + Err(_) => { + Outcome::Failure((Status::Unauthorized, LoginError::DeserializationError)) + } + }, None => Outcome::Failure((Status::Unauthorized, LoginError::NotLoggedIn)), } } From 3f01bf39b37a2cfd624744494874df14c19b9eb0 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 3 May 2023 16:06:27 +0200 Subject: [PATCH 2/2] deduplicate code --- src/model/user.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/model/user.rs b/src/model/user.rs index 4918030..f46ba6e 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -203,15 +203,13 @@ impl<'r> FromRequest<'r> for CoxUser { type Error = LoginError; async fn from_request(req: &'r Request<'_>) -> request::Outcome { - match req.cookies().get_private("loggedin_user") { - Some(user) => { - let user: User = serde_json::from_str(user.value()).unwrap(); //TODO: fixme - match user.try_into() { - Ok(user) => Outcome::Success(user), - Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotAnAdmin)), - } - } - None => Outcome::Failure((Status::Unauthorized, LoginError::NotLoggedIn)), + match User::from_request(req).await { + Outcome::Success(user) => match user.try_into() { + Ok(user) => Outcome::Success(user), + Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotACox)), + }, + Outcome::Failure(f) => Outcome::Failure(f), + Outcome::Forward(f) => Outcome::Forward(f), } } } @@ -238,15 +236,13 @@ impl<'r> FromRequest<'r> for AdminUser { type Error = LoginError; async fn from_request(req: &'r Request<'_>) -> request::Outcome { - match req.cookies().get_private("loggedin_user") { - Some(user) => { - let user: User = serde_json::from_str(user.value()).unwrap(); //TODO: fixme - match user.try_into() { - Ok(user) => Outcome::Success(user), - Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotAnAdmin)), - } - } - None => Outcome::Failure((Status::Unauthorized, LoginError::NotLoggedIn)), + match User::from_request(req).await { + Outcome::Success(user) => match user.try_into() { + Ok(user) => Outcome::Success(user), + Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotAnAdmin)), + }, + Outcome::Failure(f) => Outcome::Failure(f), + Outcome::Forward(f) => Outcome::Forward(f), } } }