jazz-rs 0.7.0

A framework for CRDT based, end-to-end enrypted distributed apps
Documentation
use credo::{ClaimBody, PermissionKind, WRITE_TO_SCOPE};
use ridl::signing::SignerSecret;
use caro::ObjectID;

use crate::doc::DocID;

pub(crate) fn log_key_secret_name(log_id: ObjectID) -> String {
    format!("logKey_{}", log_id)
}

pub const INCLUDE_LOG_STR: &str = "includeLog";

pub const READ_CONTENT: &str = "readContent";

pub(crate) fn claim_to_declare_log_part_of_doc(log_id: ObjectID) -> ClaimBody {
    ClaimBody::Statement {
        path: INCLUDE_LOG_STR.to_string(),
        // TODO-V1: #42 encrypt this
        value: litl::to_val(log_id).unwrap(),
    }
}

pub const NAMED_DOC_PREFIX: &str = "namedDoc_";

pub(crate) fn named_doc_path(name: &str) -> String {
    format!("{}{}", NAMED_DOC_PREFIX, name)
}

pub(crate) fn claim_to_name_doc(doc_id: DocID, name: &str) -> ClaimBody {
    ClaimBody::Statement {
        path: named_doc_path(name),
        value: litl::to_val(doc_id).unwrap(),
    }
}

pub const CREDENTIALS_DOC_NAME: &str = "credentials";
pub const PROFILE_DOC_NAME: &str = "profile";

pub const INTRODUCTION_PATH: &str = "introduction";

pub(crate) fn claim_for_introduction(
    profile: DocID,
    profile_signer_secret: &SignerSecret,
) -> ClaimBody {
    ClaimBody::Statement {
        path: INTRODUCTION_PATH.to_string(),
        value: litl::to_val(profile_signer_secret.sign(profile)).unwrap(),
    }
}

pub fn read_permissions() -> Vec<PermissionKind> {
    vec![]
}

pub fn write_permissions() -> Vec<PermissionKind> {
    let mut permissions = read_permissions();
    permissions.extend(vec![
        PermissionKind::MakeStatement {
            path_prefix: INCLUDE_LOG_STR.to_string(),
        },
        PermissionKind::MakeStatement {
            path_prefix: NAMED_DOC_PREFIX.to_string(),
        },
        PermissionKind::MakeStatement {
            path_prefix: INTRODUCTION_PATH.to_string(),
        },
        PermissionKind::InheritFrom,
        PermissionKind::EntrustToSharedSecret {
            secret_kind: READ_CONTENT.to_string(),
        },
        PermissionKind::TimeWitness,
    ]);
    permissions
}

pub fn admin_permissions() -> Vec<PermissionKind> {
    let mut permissions = write_permissions();
    // TODO: is this necessary if we have the delegate infinitely permission?
    permissions.extend(Some(PermissionKind::RevealSharedSecret {
        secret_kind: READ_CONTENT.to_string(),
    }));
    permissions.extend(Some(PermissionKind::DelegateInfinitely {
        delegated: Box::new(PermissionKind::RevealSharedSecret {
            secret_kind: READ_CONTENT.to_string(),
        }),
    }));
    // TODO: is this necessary if we have the delegate infinitely permission?
    permissions.extend(Some(PermissionKind::AddSharedSecretRecipient {
        secret_kind: READ_CONTENT.to_string(),
    }));
    permissions.extend(Some(PermissionKind::DelegateInfinitely {
        delegated: Box::new(PermissionKind::AddSharedSecretRecipient {
            secret_kind: READ_CONTENT.to_string(),
        }),
    }));
    // TODO: is this necessary if we have the delegate infinitely permission?
    permissions.extend(Some(PermissionKind::RevealSharedSecret {
        secret_kind: WRITE_TO_SCOPE.to_string(),
    }));
    permissions.extend(Some(PermissionKind::DelegateInfinitely {
        delegated: Box::new(PermissionKind::RevealSharedSecret {
            secret_kind: WRITE_TO_SCOPE.to_string(),
        }),
    }));
    // TODO: is this necessary if we have the delegate infinitely permission?
    permissions.extend(Some(PermissionKind::AddSharedSecretRecipient {
        secret_kind: WRITE_TO_SCOPE.to_string(),
    }));
    permissions.extend(Some(PermissionKind::DelegateInfinitely {
        delegated: Box::new(PermissionKind::AddSharedSecretRecipient {
            secret_kind: WRITE_TO_SCOPE.to_string(),
        }),
    }));
    permissions.extend(write_permissions().into_iter().map(|permission| {
        PermissionKind::DelegateInfinitely {
            delegated: Box::new(permission),
        }
    }));
    permissions
}