Skip to content

Commit

Permalink
fix http response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sxyntheon committed Nov 20, 2024
1 parent 66c39ba commit 598335b
Show file tree
Hide file tree
Showing 56 changed files with 525 additions and 622 deletions.
25 changes: 13 additions & 12 deletions backend/src/api/change_email.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use actix_identity::Identity;
use actix_web::{web, Responder, Result};
use actix_web::{error, web, Responder, Result};
use chrono::{DateTime, Days, Utc};
use lettre::message::header::ContentType;
use log::error;
use surrealdb::sql::Thing;

use super::response::Response;

use crate::{
mail::{
api_wrapper::utils::TextResponse, mail::{
mailing::{build_mail, send_mail}, utils::{load_template, Mailer}
}, models::{
links_model::{Link, LinkType}, model::{ConnectionData, CRUD}, user_model::User
Expand All @@ -18,15 +18,15 @@ pub async fn change_email_get(
id: Option<Identity>, db: ConnectionData, mailer: web::Data<Mailer>,
) -> Result<impl Responder> {
if id.is_none() {
return Ok(web::Json(Response::new_error(403, "Not logged in".into())));
return Err(error::ErrorForbidden( "Not logged in"));
}

let id = id.unwrap();
let id = match id.id() {
Ok(a) => a,
Err(e) => {
error!("Error trying to get id.id()\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
};

Expand All @@ -35,12 +35,12 @@ pub async fn change_email_get(
Some(a) => a,
None => {
error!("User not found?");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
},
Err(e) => {
error!("Error getting user from id\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
};

Expand All @@ -52,30 +52,31 @@ pub async fn change_email_get(
Ok(a) => a.construct_link(),
Err(e) => {
error!("Error creating link\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
};

let template = match load_template("email_change.html").await {
Ok(a) => a.replace("${{CHANGE_URL}}", &link),
Err(e) => {
error!("Error loading template\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
};

let message = match build_mail(&mail, "E-Mail Änderung", ContentType::TEXT_HTML, template) {
Ok(a) => a,
Err(e) => {
error!("Error building message\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
};

if let Err(e) = send_mail(mailer, message).await {
error!("Error sending mail\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
};

Ok(web::Json(Response::new_success("Sent E-Mail, check your inbox".to_string())))
Ok(web::Json(TextResponse { message: "Sent E-Mail, check your inbox".to_string()}))
}

35 changes: 17 additions & 18 deletions backend/src/api/change_password.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix_identity::Identity;
use actix_web::{
web::{self, Json}, Responder, Result
error, web::{self, Json}, Responder, Result
};
use argon2::{password_hash::SaltString, Argon2, PasswordHasher};
use chrono::{Days, Utc};
Expand All @@ -10,13 +10,12 @@ use rand_core::OsRng;
use serde::Deserialize;
use surrealdb::sql::Thing;

use super::response::Response;
use crate::{
database::sessions::delete_user_sessions, mail::{
api_wrapper::utils::TextResponse, database::sessions::delete_user_sessions, error::Error, mail::{
mailing::{build_mail, send_mail}, utils::{load_template, Mailer}
}, models::{
links_model::{Link, LinkType}, model::{ConnectionData, CRUD}, user_model::User
}, prelude::Error, utils::password::valid_password
}, utils::password::valid_password
};

#[derive(Debug, Deserialize)]
Expand All @@ -30,15 +29,15 @@ pub async fn change_password_post(
body: Json<PasswordChange>, id: Option<Identity>, db: ConnectionData, mailer: web::Data<Mailer>,
) -> Result<impl Responder> {
if id.is_none() {
return Ok(web::Json(Response::new_error(403, "Not logged in".into())));
return Err(error::ErrorForbidden( "Not logged in"));
}

let id = id.unwrap();
let id = match id.id() {
Ok(a) => a,
Err(e) => {
error!("Error trying to get id\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
};

Expand All @@ -47,12 +46,12 @@ pub async fn change_password_post(
Some(a) => a,
None => {
error!("User not found?");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
},
Err(e) => {
error!("Error trying to get user\n{e}");
return Ok(Response::new_error(500, "Interal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Interal Server Error"));
}
};

Expand All @@ -61,12 +60,12 @@ pub async fn change_password_post(
};

if body.old_password == body.new_password {
return Ok(Response::new_error(400, "New Password can't be Old Password".into()).into());
return Err(error::ErrorUnprocessableEntity( "New Password can't be Old Password"));
}

if user.verify_password(body.old_password.clone()).is_err() {
debug!("Wrong password");
return Ok(Response::new_error(403, "Wrong password".into()).into());
return Err(error::ErrorForbidden( "Wrong password"));
}

let argon2 = Argon2::default();
Expand All @@ -76,7 +75,7 @@ pub async fn change_password_post(
Ok(a) => a,
Err(e) => {
error!("Error hashing password\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}
};

Expand All @@ -93,12 +92,12 @@ pub async fn change_password_post(

if let Err(e) = User::update_replace(db.clone(), old_user.id, new_user.clone()).await {
error!("Error updating user\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}

if let Err(e) = delete_user_sessions(db.clone(), new_user.clone().id.to_string()).await {
error!("Error logging user out\n{e}");
return Ok(Response::new_error(500, "Internal Server Error".into()).into());
return Err(error::ErrorInternalServerError( "Internal Server Error"));
}

let expiry_time = Utc::now().checked_add_days(Days::new(2)).unwrap();
Expand All @@ -107,15 +106,15 @@ pub async fn change_password_post(
Ok(a) => a.construct_link(),
Err(e) => {
error!("Error creating link\n{e}");
return Ok(Response::new_error(500, "Error sending mail".into()).into());
return Err(error::ErrorInternalServerError( "Error sending mail"));
}
};

let template = match load_template("password_changed.html").await {
Ok(a) => a.replace("${{RESET_URL}}", &reset_link),
Err(e) => {
error!("Error loading template\n{e}");
return Ok(Response::new_error(500, "Error sending mail".into()).into());
return Err(error::ErrorInternalServerError( "Error sending mail"));
}
};

Expand All @@ -124,14 +123,14 @@ pub async fn change_password_post(
Ok(a) => a,
Err(e) => {
error!("Error building mail\n{e}");
return Ok(Response::new_error(500, "Error sending mail".into()).into());
return Err(error::ErrorInternalServerError( "Error sending mail"));
}
};

if let Err(e) = send_mail(mailer, message).await {
error!("Error sending mail\n{e}");
return Ok(Response::new_error(500, "Error sending mail".into()).into());
return Err(error::ErrorInternalServerError( "Error sending mail"));
}

Ok(web::Json(Response::new_success("Successfully changed Password".to_string())))
Ok(web::Json(TextResponse { message: "Successfully changed Password".to_string()}))
}
19 changes: 9 additions & 10 deletions backend/src/api/change_untis_data.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use actix_identity::Identity;
use actix_web::{web, Responder, Result};
use actix_web::{error, web, Responder, Result};
use log::{error, warn};
use serde::Deserialize;
use surrealdb::sql::Thing;

use super::response::Response;
use crate::{
database::sessions::delete_user_sessions, internalError, models::{
api_wrapper::utils::TextResponse, database::sessions::delete_user_sessions, models::{
model::{ConnectionData, CRUD}, user_model::User
}
};
Expand All @@ -22,15 +21,15 @@ pub async fn change_untis_data_post(
body: web::Json<UntisData>, db: ConnectionData, id: Option<Identity>,
) -> Result<impl Responder> {
if id.is_none() {
return Ok(web::Json(Response::new_error(403, "Not logged in".into())));
return Err(error::ErrorForbidden( "Not logged in"));
}

let id = id.unwrap();
let id = match id.id() {
Ok(a) => Thing::from(a.split_once(':').unwrap()),
Err(e) => {
error!("Error trying to get id\n{e}");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
}
};

Expand All @@ -39,17 +38,17 @@ pub async fn change_untis_data_post(
Some(a) => a,
None => {
error!("User not found?");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
}
},
Err(e) => {
error!("Error trying to get user\n{e}");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
}
};

if user.verify_password(body.password.clone()).is_err() {
return Ok(web::Json(Response::new_error(403, "Incorrect Password".to_string())));
return Err(error::ErrorForbidden( "Incorrect Password".to_string()));
}

let new_user = User {
Expand All @@ -63,12 +62,12 @@ pub async fn change_untis_data_post(

if let Err(e) = User::update_replace(db.clone(), id.clone(), new_user).await {
error!("Error updating user\n{e}");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
}

if let Err(e) = delete_user_sessions(db, id.to_string()).await {
warn!("Error deleting user sessions, ignoring\n{e}");
}

Ok(web::Json(Response::new_success("Successfully changed Untis Data".to_string())))
Ok(web::Json(TextResponse { message: "Successfully changed Untis Data".to_string()}))
}
12 changes: 7 additions & 5 deletions backend/src/api/check_session.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use actix_identity::Identity;
use actix_web::{web, Responder, Result};
use actix_web::{error, web, Responder, Result};
use log::error;

use crate::api::response::Response;
use crate::api_wrapper::utils::TextResponse;



pub async fn check_session_get(id: Option<Identity>) -> Result<impl Responder> {
if let Some(id) = id {
match id.id() {
Ok(_) => Ok(web::Json(Response::new_success("Authenticated".to_string()))),
Ok(_) => Ok(web::Json(TextResponse { message: "Authenticated".to_string()})),
Err(e) => {
error!("Error trying to get id.id()\n{}", e);
Ok(Response::new_error(500, "NOPE Server Error".to_string()).into())
Err(error::ErrorInternalServerError( "NOPE Server Error".to_string()).into())
}
}
} else {
Ok(Response::new_error(403, "Not Authenticated".to_string()).into())
Err(error::ErrorForbidden( "Not Authenticated".to_string()).into())
}
}
18 changes: 9 additions & 9 deletions backend/src/api/delete.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use actix_identity::Identity;
use actix_web::{web, Responder, Result};
use actix_web::{error, web, Responder, Result};
use log::{error, warn};
use serde::Deserialize;
use surrealdb::sql::Thing;

use crate::{
api::response::Response, database::sessions::delete_user_sessions, internalError, models::{
api_wrapper::utils::TextResponse, database::sessions::delete_user_sessions, models::{
model::{ConnectionData, CRUD}, user_model::User
}
};
Expand All @@ -19,15 +19,15 @@ pub async fn delete_post(
body: web::Json<DeleteBody>, id: Option<Identity>, db: ConnectionData,
) -> Result<impl Responder> {
if id.is_none() {
return Ok(web::Json(Response::new_error(403, "Not logged in".into())));
return Err(error::ErrorForbidden( "Not logged in"));
}

let id = id.unwrap();
let id = match id.id() {
Ok(a) => Thing::from(a.split_once(':').unwrap()),
Err(e) => {
error!("Error trying to get id\n{e}");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
}
};

Expand All @@ -36,18 +36,18 @@ pub async fn delete_post(
Some(a) => a,
None => {
error!("User not found?");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
}
},
Err(e) => {
error!("Error trying to get user\n{e}");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
}
};

if user.verify_password(body.password.clone()).is_err() {
warn!("Incorrect password");
return Ok(web::Json(Response::new_error(403, "Incorrect Password".into())));
return Err(error::ErrorForbidden( "Incorrect Password"));
}

if let Err(e) = delete_user_sessions(db.clone(), id.to_string()).await {
Expand All @@ -56,8 +56,8 @@ pub async fn delete_post(

if let Err(e) = User::delete(db, id).await {
error!("Failed to delete account\n{e}");
internalError!()
return Err(error::ErrorInternalServerError("Internal Server Error"));
};

Ok(web::Json(Response::new_success("Deleted your Account, bye-bye!".to_string())))
Ok(web::Json(TextResponse { message: "Deleted your Account, bye-bye!".to_string()}))
}
Loading

0 comments on commit 598335b

Please sign in to comment.