diff options
author | Conrad Ludgate <conradludgate@gmail.com> | 2021-05-07 21:06:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-07 21:06:56 +0100 |
commit | 1c59f85ea8b6e64e5e43d9fe49c07a84806ef079 (patch) | |
tree | 80eda8cb2af860e4013049259e668ca2077aa067 /atuin-server/src/handlers/user.rs | |
parent | e2edcbf9941491b44996b0fcf6c6aecd7365a2c0 (diff) |
remove dyn Reply (#48)
* cleanup reply types
* cleanup error api
* small update
* improve api some more
* fmt
Diffstat (limited to 'atuin-server/src/handlers/user.rs')
-rw-r--r-- | atuin-server/src/handlers/user.rs | 77 |
1 files changed, 33 insertions, 44 deletions
diff --git a/atuin-server/src/handlers/user.rs b/atuin-server/src/handlers/user.rs index 6b142cdc4..ed7791688 100644 --- a/atuin-server/src/handlers/user.rs +++ b/atuin-server/src/handlers/user.rs @@ -1,14 +1,8 @@ -use std::convert::Infallible; - +use atuin_common::api::*; +use atuin_common::utils::hash_secret; use sodiumoxide::crypto::pwhash::argon2id13; use uuid::Uuid; use warp::http::StatusCode; -use warp::reply::json; - -use atuin_common::api::{ - ErrorResponse, LoginRequest, LoginResponse, RegisterRequest, RegisterResponse, UserResponse, -}; -use atuin_common::utils::hash_secret; use crate::database::Database; use crate::models::{NewSession, NewUser}; @@ -31,33 +25,32 @@ pub fn verify_str(secret: &str, verify: &str) -> bool { pub async fn get( username: String, db: impl Database + Clone + Send + Sync, -) -> Result<Box<dyn warp::Reply>, Infallible> { +) -> JSONResult<ErrorResponseStatus> { let user = match db.get_user(username).await { Ok(user) => user, Err(e) => { debug!("user not found: {}", e); - return Ok(Box::new(ErrorResponse::reply( - "user not found", - StatusCode::NOT_FOUND, - ))); + return reply_error( + ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), + ); } }; - Ok(Box::new(warp::reply::json(&UserResponse { + reply_json(UserResponse { username: user.username, - }))) + }) } pub async fn register( register: RegisterRequest, settings: Settings, db: impl Database + Clone + Send + Sync, -) -> Result<Box<dyn warp::Reply>, Infallible> { +) -> JSONResult<ErrorResponseStatus> { if !settings.open_registration { - return Ok(Box::new(ErrorResponse::reply( - "this server is not open for registrations", - StatusCode::BAD_REQUEST, - ))); + return reply_error( + ErrorResponse::reply("this server is not open for registrations") + .with_status(StatusCode::BAD_REQUEST), + ); } let hashed = hash_secret(register.password.as_str()); @@ -72,10 +65,9 @@ pub async fn register( Ok(id) => id, Err(e) => { error!("failed to add user: {}", e); - return Ok(Box::new(ErrorResponse::reply( - "failed to add user", - StatusCode::BAD_REQUEST, - ))); + return reply_error( + ErrorResponse::reply("failed to add user").with_status(StatusCode::BAD_REQUEST), + ); } }; @@ -87,13 +79,13 @@ pub async fn register( }; match db.add_session(&new_session).await { - Ok(_) => Ok(Box::new(json(&RegisterResponse { session: token }))), + Ok(_) => reply_json(RegisterResponse { session: token }), Err(e) => { error!("failed to add session: {}", e); - Ok(Box::new(ErrorResponse::reply( - "failed to register user", - StatusCode::BAD_REQUEST, - ))) + reply_error( + ErrorResponse::reply("failed to register user") + .with_status(StatusCode::BAD_REQUEST), + ) } } } @@ -101,16 +93,15 @@ pub async fn register( pub async fn login( login: LoginRequest, db: impl Database + Clone + Send + Sync, -) -> Result<Box<dyn warp::Reply>, Infallible> { +) -> JSONResult<ErrorResponseStatus> { let user = match db.get_user(login.username.clone()).await { Ok(u) => u, Err(e) => { error!("failed to get user {}: {}", login.username.clone(), e); - return Ok(Box::new(ErrorResponse::reply( - "user not found", - StatusCode::NOT_FOUND, - ))); + return reply_error( + ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), + ); } }; @@ -119,23 +110,21 @@ pub async fn login( Err(e) => { error!("failed to get session for {}: {}", login.username, e); - return Ok(Box::new(ErrorResponse::reply( - "user not found", - StatusCode::NOT_FOUND, - ))); + return reply_error( + ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), + ); } }; let verified = verify_str(user.password.as_str(), login.password.as_str()); if !verified { - return Ok(Box::new(ErrorResponse::reply( - "user not found", - StatusCode::NOT_FOUND, - ))); + return reply_error( + ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), + ); } - Ok(Box::new(warp::reply::json(&LoginResponse { + reply_json(LoginResponse { session: session.token, - }))) + }) } |