diff options
| author | Conrad Ludgate <conrad.ludgate@truelayer.com> | 2022-04-12 23:06:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-12 23:06:19 +0100 |
| commit | a95018cc9039851e707973bc19faf907132ae4f3 (patch) | |
| tree | e135f1da64c5d020f336d437f83a333298861ca0 /atuin-server/src/handlers/user.rs | |
| parent | fix env config parsing (#295) (diff) | |
| download | atuin-a95018cc9039851e707973bc19faf907132ae4f3.zip | |
goodbye warp, hello axum (#296)
Diffstat (limited to 'atuin-server/src/handlers/user.rs')
| -rw-r--r-- | atuin-server/src/handlers/user.rs | 72 |
1 files changed, 31 insertions, 41 deletions
diff --git a/atuin-server/src/handlers/user.rs b/atuin-server/src/handlers/user.rs index 8144adab..1bcfce2f 100644 --- a/atuin-server/src/handlers/user.rs +++ b/atuin-server/src/handlers/user.rs @@ -2,11 +2,13 @@ use std::borrow::Borrow; use atuin_common::api::*; use atuin_common::utils::hash_secret; +use axum::extract::Path; +use axum::{Extension, Json}; +use http::StatusCode; use sodiumoxide::crypto::pwhash::argon2id13; use uuid::Uuid; -use warp::http::StatusCode; -use crate::database::Database; +use crate::database::{Database, Postgres}; use crate::models::{NewSession, NewUser}; use crate::settings::Settings; @@ -25,31 +27,29 @@ pub fn verify_str(secret: &str, verify: &str) -> bool { } pub async fn get( - username: impl AsRef<str>, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'static>> { + Path(username): Path<String>, + db: Extension<Postgres>, +) -> Result<Json<UserResponse>, ErrorResponseStatus<'static>> { let user = match db.get_user(username.as_ref()).await { Ok(user) => user, Err(e) => { debug!("user not found: {}", e); - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } }; - reply_json(UserResponse { - username: user.username.into(), - }) + Ok(Json(UserResponse { + username: user.username, + })) } pub async fn register( - register: RegisterRequest<'_>, - settings: Settings, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'static>> { + Json(register): Json<RegisterRequest>, + settings: Extension<Settings>, + db: Extension<Postgres>, +) -> Result<Json<RegisterResponse>, ErrorResponseStatus<'static>> { if !settings.open_registration { - return reply_error( + return Err( ErrorResponse::reply("this server is not open for registrations") .with_status(StatusCode::BAD_REQUEST), ); @@ -60,15 +60,15 @@ pub async fn register( let new_user = NewUser { email: register.email, username: register.username, - password: hashed.into(), + password: hashed, }; let user_id = match db.add_user(&new_user).await { Ok(id) => id, Err(e) => { error!("failed to add user: {}", e); - return reply_error( - ErrorResponse::reply("failed to add user").with_status(StatusCode::BAD_REQUEST), + return Err( + ErrorResponse::reply("failed to add user").with_status(StatusCode::BAD_REQUEST) ); } }; @@ -81,31 +81,25 @@ pub async fn register( }; match db.add_session(&new_session).await { - Ok(_) => reply_json(RegisterResponse { - session: token.into(), - }), + Ok(_) => Ok(Json(RegisterResponse { session: token })), Err(e) => { error!("failed to add session: {}", e); - reply_error( - ErrorResponse::reply("failed to register user") - .with_status(StatusCode::BAD_REQUEST), - ) + Err(ErrorResponse::reply("failed to register user") + .with_status(StatusCode::BAD_REQUEST)) } } } pub async fn login( - login: LoginRequest<'_>, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'_>> { + login: Json<LoginRequest>, + db: Extension<Postgres>, +) -> Result<Json<LoginResponse>, ErrorResponseStatus<'static>> { let user = match db.get_user(login.username.borrow()).await { Ok(u) => u, Err(e) => { error!("failed to get user {}: {}", login.username.clone(), e); - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } }; @@ -114,21 +108,17 @@ pub async fn login( Err(e) => { error!("failed to get session for {}: {}", login.username, e); - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } }; let verified = verify_str(user.password.as_str(), login.password.borrow()); if !verified { - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } - reply_json(LoginResponse { - session: session.token.into(), - }) + Ok(Json(LoginResponse { + session: session.token, + })) } |
