refactor: candidate details enc, dec methods

This commit is contained in:
Sebastian Pravda 2022-11-11 19:09:00 +01:00
parent 5b70d8df4f
commit 22ea58d521
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
2 changed files with 28 additions and 54 deletions

View file

@ -56,60 +56,36 @@ pub(crate) struct EncryptedCandidateDetails {
} }
impl EncryptedCandidateDetails { impl EncryptedCandidateDetails {
pub async fn new(form: CandidateDetails, recipients: Vec<&str>) -> EncryptedCandidateDetails { pub async fn new(form: CandidateDetails, recipients: Vec<&str>) -> Result<EncryptedCandidateDetails, ServiceError> {
let ( let d = tokio::try_join!(
Ok(name),
Ok(surname),
Ok(birthplace),
// Ok(enc_birthdate),
Ok(address),
Ok(telephone),
Ok(citizenship),
Ok(email),
Ok(sex),
Ok(study),
) = tokio::join!(
EncryptedString::new(&form.name, &recipients), EncryptedString::new(&form.name, &recipients),
EncryptedString::new(&form.surname, &recipients), EncryptedString::new(&form.surname, &recipients),
EncryptedString::new(&form.birthplace, &recipients), EncryptedString::new(&form.birthplace, &recipients),
// EncryptedString::new((&self.birthdate, &recipients), // TODO EncryptedString::new("&form.birthdate", &recipients), // TODO
EncryptedString::new(&form.address, &recipients), EncryptedString::new(&form.address, &recipients),
EncryptedString::new(&form.telephone, &recipients), EncryptedString::new(&form.telephone, &recipients),
EncryptedString::new(&form.citizenship, &recipients), EncryptedString::new(&form.citizenship, &recipients),
EncryptedString::new(&form.email, &recipients), EncryptedString::new(&form.email, &recipients),
EncryptedString::new(&form.sex, &recipients), EncryptedString::new(&form.sex, &recipients),
EncryptedString::new(&form.study, &recipients), EncryptedString::new(&form.study, &recipients),
) else { )?;
panic!("Failed to encrypt user details"); // TODO
};
EncryptedCandidateDetails { Ok(EncryptedCandidateDetails {
name, name: d.0,
surname, surname: d.1,
birthplace, birthplace: d.2,
// birthdate: NaiveDate::from_ymd(2000, 1, 1), // birthdate: NaiveDate::from_ymd(2000, 1, 1),
address, address: d.4,
telephone, telephone: d.5,
citizenship, citizenship: d.6,
email, email: d.7,
sex, sex: d.8,
study, study: d.9,
} })
} }
pub async fn decrypt(self, priv_key: String) -> Result<CandidateDetails, ServiceError> { pub async fn decrypt(self, priv_key: String) -> Result<CandidateDetails, ServiceError> {
let ( let d = tokio::try_join!(
Ok(name),
Ok(surname),
Ok(birthplace),
// Ok(enc_birthdate),
Ok(address),
Ok(telephone),
Ok(citizenship),
Ok(email),
Ok(sex),
Ok(study),
) = tokio::join!(
self.name.decrypt(&priv_key), self.name.decrypt(&priv_key),
self.surname.decrypt(&priv_key), self.surname.decrypt(&priv_key),
self.birthplace.decrypt(&priv_key), self.birthplace.decrypt(&priv_key),
@ -120,21 +96,19 @@ impl EncryptedCandidateDetails {
self.email.decrypt(&priv_key), self.email.decrypt(&priv_key),
self.sex.decrypt(&priv_key), self.sex.decrypt(&priv_key),
self.study.decrypt(&priv_key), self.study.decrypt(&priv_key),
) else { )?;
panic!("Failed to encrypt user details"); // TODO
};
Ok(CandidateDetails { Ok(CandidateDetails {
name, name: d.0,
surname, surname: d.1,
birthplace, birthplace: d.2,
// birthdate: NaiveDate::from_ymd(2000, 1, 1), // birthdate: NaiveDate::from_ymd(2000, 1, 1), // TODO
address, address: d.3,
telephone, telephone: d.4,
citizenship, citizenship: d.5,
email, email: d.6,
sex, sex: d.7,
study, study: d.8,
}) })
} }
} }

View file

@ -84,7 +84,7 @@ impl CandidateService {
recipients.append(&mut admin_public_keys_refrence); recipients.append(&mut admin_public_keys_refrence);
let enc_details = EncryptedCandidateDetails::new(form, recipients).await; let enc_details = EncryptedCandidateDetails::new(form, recipients).await?;
Mutation::add_candidate_details(db, candidate, enc_details) Mutation::add_candidate_details(db, candidate, enc_details)
.await .await