feat: return ServiceError instead of using unwraps

This commit is contained in:
Sebastian Pravda 2022-11-08 14:57:25 +01:00
parent fc504efd58
commit a575556bdd

View file

@ -267,14 +267,18 @@ pub async fn encrypt_password_with_recipients(
pub async fn decrypt_password_with_private_key( pub async fn decrypt_password_with_private_key(
password_encrypted: &str, password_encrypted: &str,
key: &str, key: &str,
) -> Result<String, ServiceError> { ) -> Result<String, ServiceError> { // TODO More specific error handling
let encrypted = base64::decode(password_encrypted).unwrap(); let Ok(encrypted) = base64::decode(password_encrypted) else {
return Err(ServiceError::CryptoEncryptFailed);
};
let mut decrypt_buffer = Vec::new(); let mut decrypt_buffer = Vec::new();
age_decrypt_with_private_key(encrypted.as_slice(), &mut decrypt_buffer, key).await.unwrap(); if age_decrypt_with_private_key(encrypted.as_slice(), &mut decrypt_buffer, key).await.is_err() {
return Err(ServiceError::CryptoDecryptFailed);
};
Ok(String::from_utf8(decrypt_buffer).ok().unwrap()) String::from_utf8(decrypt_buffer).map_err(|_| ServiceError::CryptoDecryptFailed)
} }
pub async fn encrypt_file_with_recipients<P: AsRef<Path>>( pub async fn encrypt_file_with_recipients<P: AsRef<Path>>(