pub use crate::internal::document_api::file_ops::{
DocumentFileDecryptResult, DocumentFileDecryptUnmanagedResult, DocumentFileEncryptResult,
DocumentFileEncryptUnmanagedResult,
};
use crate::{
Result, SdkOperation,
document::{DocumentEncryptOpts, partition_user_or_group},
internal::{add_optional_timeout, document_api::file_ops},
};
use futures::Future;
use itertools::EitherOrBoth;
pub trait DocumentFileOps {
fn document_file_encrypt(
&self,
source_path: &str,
destination_path: &str,
opts: &DocumentEncryptOpts,
) -> impl Future<Output = Result<DocumentFileEncryptResult>> + Send;
fn document_file_decrypt(
&self,
source_path: &str,
destination_path: &str,
) -> impl Future<Output = Result<DocumentFileDecryptResult>> + Send;
}
pub trait DocumentFileAdvancedOps {
fn document_file_encrypt_unmanaged(
&self,
source_path: &str,
destination_path: &str,
opts: &DocumentEncryptOpts,
) -> impl Future<Output = Result<DocumentFileEncryptUnmanagedResult>> + Send;
fn document_file_decrypt_unmanaged(
&self,
source_path: &str,
destination_path: &str,
encrypted_deks: &[u8],
) -> impl Future<Output = Result<DocumentFileDecryptUnmanagedResult>> + Send;
}
impl DocumentFileOps for crate::IronOxide {
async fn document_file_encrypt(
&self,
source_path: &str,
destination_path: &str,
opts: &DocumentEncryptOpts,
) -> Result<DocumentFileEncryptResult> {
let encrypt_opts = opts.clone();
let (explicit_users, explicit_groups, grant_to_author, policy_grants) =
match encrypt_opts.grants {
EitherOrBoth::Left(explicit_grants) => {
let (users, groups) = partition_user_or_group(&explicit_grants.grants);
(users, groups, explicit_grants.grant_to_author, None)
}
EitherOrBoth::Right(policy_grant) => (vec![], vec![], false, Some(policy_grant)),
EitherOrBoth::Both(explicit_grants, policy_grant) => {
let (users, groups) = partition_user_or_group(&explicit_grants.grants);
(
users,
groups,
explicit_grants.grant_to_author,
Some(policy_grant),
)
}
};
add_optional_timeout(
file_ops::encrypt_file_to_path(
self.device.auth(),
&self.config,
&self.recrypt,
&self.user_master_pub_key,
&self.rng,
source_path,
destination_path,
encrypt_opts.id,
encrypt_opts.name,
grant_to_author,
&explicit_users,
&explicit_groups,
policy_grants.as_ref(),
&self.policy_eval_cache,
&self.public_key_cache,
),
self.config.sdk_operation_timeout,
SdkOperation::DocumentEncrypt,
)
.await?
}
async fn document_file_decrypt(
&self,
source_path: &str,
destination_path: &str,
) -> Result<DocumentFileDecryptResult> {
add_optional_timeout(
file_ops::decrypt_file_to_path(
self.device.auth(),
self.recrypt.clone(),
self.device.device_private_key(),
source_path,
destination_path,
),
self.config.sdk_operation_timeout,
SdkOperation::DocumentDecrypt,
)
.await?
}
}
impl DocumentFileAdvancedOps for crate::IronOxide {
async fn document_file_encrypt_unmanaged(
&self,
source_path: &str,
destination_path: &str,
opts: &DocumentEncryptOpts,
) -> Result<DocumentFileEncryptUnmanagedResult> {
let encrypt_opts = opts.clone();
let (explicit_users, explicit_groups, grant_to_author, policy_grants) =
match encrypt_opts.grants {
EitherOrBoth::Left(explicit_grants) => {
let (users, groups) = partition_user_or_group(&explicit_grants.grants);
(users, groups, explicit_grants.grant_to_author, None)
}
EitherOrBoth::Right(policy_grant) => (vec![], vec![], false, Some(policy_grant)),
EitherOrBoth::Both(explicit_grants, policy_grant) => {
let (users, groups) = partition_user_or_group(&explicit_grants.grants);
(
users,
groups,
explicit_grants.grant_to_author,
Some(policy_grant),
)
}
};
add_optional_timeout(
file_ops::encrypt_file_unmanaged(
self.device.auth(),
&self.config,
&self.recrypt,
&self.user_master_pub_key,
&self.rng,
source_path,
destination_path,
encrypt_opts.id,
grant_to_author,
&explicit_users,
&explicit_groups,
policy_grants.as_ref(),
&self.policy_eval_cache,
&self.public_key_cache,
),
self.config.sdk_operation_timeout,
SdkOperation::DocumentEncryptUnmanaged,
)
.await?
}
async fn document_file_decrypt_unmanaged(
&self,
source_path: &str,
destination_path: &str,
encrypted_deks: &[u8],
) -> Result<DocumentFileDecryptUnmanagedResult> {
add_optional_timeout(
file_ops::decrypt_file_unmanaged(
self.device.auth(),
&self.recrypt,
self.device.device_private_key(),
source_path,
destination_path,
encrypted_deks,
),
self.config.sdk_operation_timeout,
SdkOperation::DocumentDecryptUnmanaged,
)
.await?
}
}