use anyhow::Result;
use ironoxide::prelude::*;
use std::convert::TryFrom;
use std::{fs::File, path::PathBuf};
#[tokio::main]
async fn main() -> Result<()> {
let (device_context, sdk) =
initialize_sdk_from_file(&"examples/example-ironoxide-device.json".into()).await?;
encrypt_to_group(&sdk).await?;
encrypt_to_user(&sdk, device_context.account_id()).await?;
encrypt_with_policy(&sdk).await?;
Ok(())
}
async fn encrypt_to_group(sdk: &IronOxide) -> Result<DocumentId> {
let group_id = create_group(sdk).await?;
let message = "This is my secret which a whole group should see.".to_string();
let encrypted_result = sdk
.document_encrypt(
message.into_bytes(),
&DocumentEncryptOpts::with_explicit_grants(None, None, true, vec![(&group_id).into()]),
)
.await?;
println!("Encrypted to group {}", group_id.id());
Ok(encrypted_result.id().clone())
}
async fn encrypt_to_user(sdk: &IronOxide, user_id: &UserId) -> Result<DocumentId> {
let message = "This is my secret for a single user.".to_string();
let encrypted_result = sdk
.document_encrypt(
message.into_bytes(),
&DocumentEncryptOpts::with_explicit_grants(None, None, true, vec![user_id.into()]),
)
.await?;
println!("Encrypted to user {}", user_id.id());
Ok(encrypted_result.id().clone())
}
async fn encrypt_with_policy(sdk: &IronOxide) -> Result<DocumentId> {
let message = "this is my secret which has some labels.".to_string();
let data_labels = PolicyGrant::new(
Some(Category::try_from("PII")?),
Some(Sensitivity::try_from("PRIVATE")?),
None,
None,
);
let encrypted_result = sdk
.document_encrypt(
message.into_bytes(),
&DocumentEncryptOpts::with_policy_grants(None, None, data_labels),
)
.await?;
println!("Encrypted with policy with labels PRIVATE/PII");
Ok(encrypted_result.id().clone())
}
async fn initialize_sdk_from_file(device_path: &PathBuf) -> Result<(DeviceContext, IronOxide)> {
if device_path.is_file() {
let device_context_file = File::open(device_path)?;
let device_context: DeviceContext = serde_json::from_reader(device_context_file)?;
let ironoxide = ironoxide::initialize(&device_context, &Default::default()).await?;
Ok((device_context, ironoxide))
} else {
panic!(
"Couldn't open file {} containing DeviceContext",
device_path.display()
)
}
}
async fn create_group(sdk: &IronOxide) -> Result<GroupId> {
let result = sdk.group_create(&Default::default()).await?;
Ok(result.id().clone())
}