diff --git a/core/src/crypto.rs b/core/src/crypto.rs index cf95b2f..2b823db 100644 --- a/core/src/crypto.rs +++ b/core/src/crypto.rs @@ -197,7 +197,7 @@ pub fn create_identity() -> (String, String) { async fn age_encrypt_with_recipients( input_buffer: &[u8], output_buffer: &mut W, - recipients: Vec<&str>, + recipients: &Vec<&str>, ) -> Result<(), age::EncryptError> { let public_keys = recipients .into_iter() @@ -248,7 +248,7 @@ async fn age_decrypt_with_private_key( pub async fn encrypt_password_with_recipients( password_plain_text: &str, - recipients: Vec<&str>, + recipients: &Vec<&str>, ) -> Result { let mut encrypt_buffer = Vec::new(); @@ -287,7 +287,7 @@ pub async fn encrypt_file_with_recipients>( tokio::io::AsyncReadExt::read_to_end(&mut plain_file, &mut plain_file_contents).await?; - age_encrypt_with_recipients(plain_file_contents.as_slice(), &mut cipher_file, recipients).await + age_encrypt_with_recipients(plain_file_contents.as_slice(), &mut cipher_file, &recipients).await } pub async fn decrypt_file_with_private_key>( @@ -446,7 +446,7 @@ mod tests { const PASSWORD: &str = "test"; const PUBLIC_KEY: &str = "age1t220v5c8ye0pjx99kw8nr57y7a5qlw4ke0wchjuxnr2gcvfzt3hq7fufz0"; - let encrypted = super::encrypt_password_with_recipients(PASSWORD, vec![PUBLIC_KEY]) + let encrypted = super::encrypt_password_with_recipients(PASSWORD, &vec![PUBLIC_KEY]) .await .unwrap(); @@ -460,7 +460,7 @@ mod tests { const PUBLIC_KEY_2: &str = "age1ygswsk38cq9r64um5klqxyvzemfdvx6qe5zed99pdexakwwhpatsgatgpw"; let encrypted = - super::encrypt_password_with_recipients(PASSWORD, vec![PUBLIC_KEY_1, PUBLIC_KEY_2]) + super::encrypt_password_with_recipients(PASSWORD, &vec![PUBLIC_KEY_1, PUBLIC_KEY_2]) .await .unwrap(); diff --git a/core/src/database/mutation/candidate.rs b/core/src/database/mutation/candidate.rs index f6b80f0..14b94ce 100644 --- a/core/src/database/mutation/candidate.rs +++ b/core/src/database/mutation/candidate.rs @@ -1,6 +1,6 @@ -use crate::Mutation; +use crate::{Mutation, services::candidate_service::{AddUserDetailsForm, EncryptedAddUserData}}; -use ::entity::candidate; +use ::entity::candidate::{self, Model}; use sea_orm::{*}; impl Mutation { @@ -25,4 +25,26 @@ impl Mutation { .insert(db) .await } + + pub async fn add_user_details( + db: &DbConn, + user: Model, + details: EncryptedAddUserData, + ) -> Result { + let mut user: candidate::ActiveModel = user.into(); + user.name = Set(Some(details.name)); + user.surname = Set(Some(details.surname)); + user.birthplace = Set(Some(details.birthplace)); + user.birthdate = Set(Some(details.birthdate)); + user.address = Set(Some(details.address)); + user.telephone = Set(Some(details.telephone)); + user.citizenship = Set(Some(details.citizenship)); + user.email = Set(Some(details.email)); + user.sex = Set(Some(details.sex)); + user.study = Set(Some(details.study)); + + user.updated_at = Set(chrono::offset::Local::now().naive_local()); + + user.update(db).await + } } \ No newline at end of file diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index 145c953..b720426 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -1,3 +1,4 @@ +use chrono::NaiveDate; use entity::candidate; use sea_orm::{DbConn, prelude::Uuid}; @@ -7,6 +8,51 @@ use super::session_service::SessionService; const FIELD_OF_STUDY_PREFIXES: [&str; 3] = ["101", "102", "103"]; +pub struct EncryptedAddUserData { + pub name: String, + pub surname: String, + pub birthplace: String, + pub birthdate: NaiveDate, + pub address: String, + pub telephone: String, + pub citizenship: String, + pub email: String, + pub sex: String, + pub study: String, +} + +pub struct AddUserDetailsForm { + pub application_id: i32, + + pub name: String, + pub surname: String, + pub birthplace: String, + pub birthdate: NaiveDate, + pub address: String, + pub telephone: String, + pub citizenship: String, + pub email: String, + pub sex: String, + pub study: String, +} + +impl AddUserDetailsForm { + pub async fn to_encrypted(self, recipients: Vec<&str>) -> EncryptedAddUserData { + EncryptedAddUserData { + name: crypto::encrypt_password_with_recipients(&self.name, &recipients).await.unwrap(), + surname: crypto::encrypt_password_with_recipients(&self.surname, &recipients).await.unwrap(), + birthplace: crypto::encrypt_password_with_recipients(&self.birthplace, &recipients).await.unwrap(), + birthdate: self.birthdate, // TODO: encrypt + address: crypto::encrypt_password_with_recipients(&self.address, &recipients).await.unwrap(), + telephone: crypto::encrypt_password_with_recipients(&self.telephone, &recipients).await.unwrap(), + citizenship: crypto::encrypt_password_with_recipients(&self.citizenship, &recipients).await.unwrap(), + email: crypto::encrypt_password_with_recipients(&self.email, &recipients).await.unwrap(), + sex: crypto::encrypt_password_with_recipients(&self.sex, &recipients).await.unwrap(), + study: crypto::encrypt_password_with_recipients(&self.study, &recipients).await.unwrap(), + } + } +} + pub struct CandidateService; impl CandidateService { @@ -37,7 +83,7 @@ impl CandidateService { let encrypted_priv_key = crypto::encrypt_password(priv_key_plain_text, plain_text_password.to_string()).await.unwrap(); let encrypted_personal_id_number = crypto::encrypt_password_with_recipients( - &personal_id_number, vec![&pubkey] + &personal_id_number, &vec![&pubkey] ).await.unwrap(); Mutation::create_candidate( @@ -52,6 +98,16 @@ impl CandidateService { .map_err(|_| ServiceError::DbError) } + pub async fn add_user_details( + db: &DbConn, + details: AddUserDetailsForm, + ) -> Result { + let user = Query::find_candidate_by_id(db, details.application_id).await.unwrap().unwrap(); + let recipients = vec![&*user.public_key]; + let encrypted = details.to_encrypted(recipients).await; + Mutation::add_user_details(db, user, encrypted).await + } + pub async fn login( db: &DbConn, user_id: i32, @@ -123,7 +179,7 @@ mod tests { let candidate = CandidateService::create(&db, 103151, &plain_text_password, "".to_string()).await.ok().unwrap(); - let encrypted_message = crypto::encrypt_password_with_recipients(&secret_message, vec![&candidate.public_key]).await.unwrap(); + let encrypted_message = crypto::encrypt_password_with_recipients(&secret_message, &vec![&candidate.public_key]).await.unwrap(); let private_key_plain_text = crypto::decrypt_password(candidate.private_key, plain_text_password).await.unwrap();