mhome-core-api 1.60.0

Shared wire types and service contracts for the mHome core
Documentation
//! Cloud-issued, short-lived User authorization for Host management.
//! Core may transport an access token but is never its management principal.
use super::auth::PublicKey;
use crate::{runtime_space::SpaceIdentity, RuntimeEnv};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

pub const ACCESS_TTL_SECONDS: u64 = 300;
pub const AUTHORIZATION_TTL_SECONDS: u64 = 600;
pub const CLOUD_ISSUER: &str = "meow-host-authority";
pub const ACCESS_AUDIENCE: &str = "meow-host-management";
pub const ACCESS_TYPE: &str = "meow-host-access+jwt";
pub const CLAIM_TYPE: &str = "meow-host-claim+jwt";
pub const CHALLENGE_TYPE: &str = "meow-host-challenge+jwt";
pub const RECEIPT_TYPE: &str = "meow-host-claim-receipt+jwt";
pub const ADMIN_APPLICANT_TYPE: &str = "meow-host-administrator-applicant+jwt";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum HostMode {
    Admin,
    SpaceSelf,
    SpaceDep,
}

impl HostMode {
    pub fn is_space(self) -> bool {
        matches!(self, Self::SpaceSelf | Self::SpaceDep)
    }
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Admin => "admin",
            Self::SpaceSelf => "spaceSelf",
            Self::SpaceDep => "spaceDep",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", deny_unknown_fields)]
pub enum Claim {
    Admin {
        #[serde(rename = "userIds")]
        user_ids: BTreeSet<String>,
    },
    Space {
        space: SpaceIdentity,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Permission {
    Read,
    ManageRuntime,
    ManageAdministrators,
    Reset,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HostStatus {
    pub host_id: String,
    pub host_key_id: String,
    pub env: RuntimeEnv,
    pub mode: HostMode,
    pub generation: String,
    pub claim: Option<Claim>,
}

/// A Space here is the managed resource, not a Space Client principal. The
/// issuer must authenticate a User and verify current ownership before signing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AccessBinding {
    pub host_id: String,
    pub host_key_id: String,
    pub env: RuntimeEnv,
    pub generation: String,
    pub user_id: String,
    pub space: Option<SpaceIdentity>,
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AccessClaims {
    pub iss: String,
    pub aud: String,
    pub sub: String,
    pub jti: String,
    pub iat: u64,
    pub exp: u64,
    pub binding: AccessBinding,
    pub permissions: BTreeSet<Permission>,
}

/// Claim proofs cannot be used as management tokens. The redemption secret is
/// generated by the direct Client; only its digest is sent to the cloud.
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ClaimClaims {
    pub iss: String,
    pub aud: String,
    pub sub: String,
    pub jti: String,
    pub iat: u64,
    pub exp: u64,
    pub binding: AccessBinding,
    pub challenge: String,
    pub exchange_key_hash: String,
    pub display_name: String,
    pub email: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ChallengeRequest {
    pub probe_id: String,
    pub exchange_key_hash: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HostChallenge {
    /// Present only for a Host dependent on its local fixed-Space Client.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub device_binding: Option<crate::device_binding::DeviceBinding>,
    pub status: HostStatus,
    pub host_name: String,
    pub host_public_key: PublicKey,
    pub probe_id: String,
    pub exchange_key_hash: Option<String>,
    pub iat: u64,
    pub exp: u64,
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ClaimRequest {
    pub env: RuntimeEnv,
    pub proof: String,
    pub exchange_key: String,
    pub claim_token: Option<String>,
}

/// Cloud attests only the applicant identity. This credential cannot manage a
/// Host or grant membership; a current administrator must approve on the Host.
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AdministratorApplicantClaims {
    pub iss: String,
    pub aud: String,
    pub sub: String,
    pub jti: String,
    pub iat: u64,
    pub exp: u64,
    pub binding: AccessBinding,
    pub display_name: String,
    pub email: String,
}

/// Emitted only after the local claim/ACL write is durable. The cloud must
/// verify this receipt before committing its Host identity directory.
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AuthorizationReceipt {
    pub proof: String,
    pub status: HostStatus,
    pub user_id: String,
}