From 71ae2df2d71f84c4dbe0d3edcef076d6357d9887 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 25 Aug 2023 15:01:45 +0200 Subject: [PATCH] Output email in error in case of validation failure --- geneit_backend/Cargo.lock | 1 + geneit_backend/Cargo.toml | 3 ++- geneit_backend/src/controllers/members_controller.rs | 7 ++++--- geneit_backend/src/utils/encode_utils.rs | 6 ++++++ geneit_backend/src/utils/mod.rs | 1 + 5 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 geneit_backend/src/utils/encode_utils.rs diff --git a/geneit_backend/Cargo.lock b/geneit_backend/Cargo.lock index a9a9f15..4898bd2 100644 --- a/geneit_backend/Cargo.lock +++ b/geneit_backend/Cargo.lock @@ -1295,6 +1295,7 @@ dependencies = [ "actix-remote-ip", "actix-web", "anyhow", + "base64 0.21.2", "bcrypt", "clap", "diesel", diff --git a/geneit_backend/Cargo.toml b/geneit_backend/Cargo.toml index c6750f2..0d9800c 100644 --- a/geneit_backend/Cargo.toml +++ b/geneit_backend/Cargo.toml @@ -35,4 +35,5 @@ uuid = { version = "1.4.1", features = ["v4"] } httpdate = "1.0.2" zip = "0.6.6" mime_guess = "2.0.4" -tempfile = "3.7.1" \ No newline at end of file +tempfile = "3.7.1" +base64 = "0.21.2" \ No newline at end of file diff --git a/geneit_backend/src/controllers/members_controller.rs b/geneit_backend/src/controllers/members_controller.rs index 3749706..f6edff2 100644 --- a/geneit_backend/src/controllers/members_controller.rs +++ b/geneit_backend/src/controllers/members_controller.rs @@ -5,6 +5,7 @@ use crate::extractors::member_extractor::FamilyAndMemberInPath; use crate::models::{Member, MemberID, PhotoID, Sex}; use crate::services::{members_service, photos_service}; use crate::utils::countries_utils; +use crate::utils::encode_utils::base64_enc; use actix_multipart::form::tempfile::TempFile; use actix_multipart::form::MultipartForm; use actix_web::{web, HttpResponse}; @@ -62,8 +63,8 @@ enum MemberControllerErr { MalformedBirthLastname, #[error("Malformed email address!")] MalformedEmailAddress, - #[error("Invalid email address!")] - InvalidEmailAddress, + #[error("Invalid email address (base64={0}) !")] + InvalidEmailAddress(String), #[error("Malformed phone number!")] MalformedPhoneNumber, #[error("Malformed address!")] @@ -132,7 +133,7 @@ impl MemberRequest { if let Some(mail) = &self.email { if !mailchecker::is_valid(mail) { - return Err(MemberControllerErr::InvalidEmailAddress.into()); + return Err(MemberControllerErr::InvalidEmailAddress(base64_enc(mail)).into()); } } diff --git a/geneit_backend/src/utils/encode_utils.rs b/geneit_backend/src/utils/encode_utils.rs new file mode 100644 index 0000000..7c2152e --- /dev/null +++ b/geneit_backend/src/utils/encode_utils.rs @@ -0,0 +1,6 @@ +use base64::{engine::general_purpose, Engine as _}; + +/// Encode a base64 string +pub fn base64_enc>(b: T) -> String { + general_purpose::STANDARD_NO_PAD.encode(b) +} diff --git a/geneit_backend/src/utils/mod.rs b/geneit_backend/src/utils/mod.rs index b6479b6..f5e433e 100644 --- a/geneit_backend/src/utils/mod.rs +++ b/geneit_backend/src/utils/mod.rs @@ -2,5 +2,6 @@ pub mod countries_utils; pub mod crypt_utils; +pub mod encode_utils; pub mod string_utils; pub mod time_utils;