feat: improve error handling, add crypto errors

This commit is contained in:
EETagent 2022-11-05 17:25:29 +01:00
parent 0a33695210
commit 5ef966341f
2 changed files with 33 additions and 18 deletions

View file

@ -9,6 +9,8 @@ pub enum ServiceError {
DbError, DbError,
UserNotFoundByJwtId, UserNotFoundByJwtId,
UserNotFoundBySessionId, UserNotFoundBySessionId,
CryptoHashFailed,
CryptoEncryptFailed,
} }
impl ServiceError { impl ServiceError {
@ -24,6 +26,8 @@ impl ServiceError {
ServiceError::DbError => (500, "Database error".to_string()), ServiceError::DbError => (500, "Database error".to_string()),
ServiceError::UserNotFoundByJwtId => (500, "User not found, please contact technical support".to_string()), ServiceError::UserNotFoundByJwtId => (500, "User not found, please contact technical support".to_string()),
ServiceError::UserNotFoundBySessionId => (500, "User not found, please contact technical support".to_string()), ServiceError::UserNotFoundBySessionId => (500, "User not found, please contact technical support".to_string()),
ServiceError::CryptoHashFailed => (500, "Crypto hash failed, please contact technical support".to_string()),
ServiceError::CryptoEncryptFailed => (500, "Crypto encryption failed, please contact technical support".to_string()),
} }
} }

View file

@ -40,17 +40,19 @@ impl CandidateService {
return Err(ServiceError::UserAlreadyExists); return Err(ServiceError::UserAlreadyExists);
} }
// TODO: unwrap pro testing.. let Ok(hashed_password) = hash_password(plain_text_password.to_string()).await else {
let hashed_password = hash_password(plain_text_password.to_string()) return Err(ServiceError::CryptoHashFailed);
.await };
.unwrap();
let (pubkey, priv_key_plain_text) = crypto::create_identity();
let encrypted_priv_key =
crypto::encrypt_password(priv_key_plain_text, plain_text_password.to_string())
.await
.unwrap();
let hashed_personal_id_number = hash_password(personal_id_number).await.unwrap(); let (pubkey, priv_key_plain_text) = crypto::create_identity();
let Ok(encrypted_priv_key) = crypto::encrypt_password(priv_key_plain_text, plain_text_password.to_string()).await else {
return Err(ServiceError::CryptoEncryptFailed);
};
let Ok(hashed_personal_id_number) = hash_password(personal_id_number).await else {
return Err(ServiceError::CryptoHashFailed);
};
/* let encrypted_personal_id_number = crypto::encrypt_password_with_recipients( /* let encrypted_personal_id_number = crypto::encrypt_password_with_recipients(
&personal_id_number, &vec![&pubkey] &personal_id_number, &vec![&pubkey]
).await.unwrap(); */ ).await.unwrap(); */
@ -80,15 +82,23 @@ impl CandidateService {
email: String, email: String,
sex: String, sex: String,
study: String, study: String,
) -> Result<entity::candidate::Model, sea_orm::DbErr> { ) -> Result<entity::candidate::Model, ServiceError> {
let user = Query::find_candidate_by_id(db, application_id) let Ok(user) = Query::find_candidate_by_id(db, application_id).await else {
.await? return Err(ServiceError::DbError);
.unwrap(); };
let admin_public_keys = Query::get_all_admin_public_keys(db).await?; let Some(user_unwrapped) = user else {
let mut admin_public_keys_refrence: Vec<&str> = admin_public_keys.iter().map(|s| &**s).collect(); return Err(ServiceError::UserNotFound);
};
let mut recipients = vec![&*user.public_key]; let Ok(admin_public_keys) = Query::get_all_admin_public_keys(db).await else {
return Err(ServiceError::DbError);
};
let mut admin_public_keys_refrence: Vec<&str> =
admin_public_keys.iter().map(|s| &**s).collect();
let mut recipients = vec![&*user_unwrapped.public_key];
recipients.append(&mut admin_public_keys_refrence); recipients.append(&mut admin_public_keys_refrence);
@ -118,7 +128,7 @@ impl CandidateService {
Mutation::add_candidate_details( Mutation::add_candidate_details(
db, db,
user, user_unwrapped,
enc_name.unwrap(), enc_name.unwrap(),
enc_surname.unwrap(), enc_surname.unwrap(),
enc_birthplace.unwrap(), enc_birthplace.unwrap(),
@ -131,6 +141,7 @@ impl CandidateService {
enc_study.unwrap(), enc_study.unwrap(),
) )
.await .await
.map_err(|_| ServiceError::DbError)
} }
pub async fn login( pub async fn login(