Skip to main content

core_api/host/
access.rs

1//! Cloud-issued, short-lived User authorization for Host management.
2//! Core may transport an access token but is never its management principal.
3use super::auth::PublicKey;
4use crate::{runtime_space::SpaceIdentity, RuntimeEnv};
5use serde::{Deserialize, Serialize};
6use std::collections::BTreeSet;
7
8pub const ACCESS_TTL_SECONDS: u64 = 300;
9pub const AUTHORIZATION_TTL_SECONDS: u64 = 600;
10pub const CLOUD_ISSUER: &str = "meow-host-authority";
11pub const ACCESS_AUDIENCE: &str = "meow-host-management";
12pub const ACCESS_TYPE: &str = "meow-host-access+jwt";
13pub const CLAIM_TYPE: &str = "meow-host-claim+jwt";
14pub const CHALLENGE_TYPE: &str = "meow-host-challenge+jwt";
15pub const RECEIPT_TYPE: &str = "meow-host-claim-receipt+jwt";
16pub const ADMIN_APPLICANT_TYPE: &str = "meow-host-administrator-applicant+jwt";
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub enum HostMode {
21    Admin,
22    SpaceSelf,
23    SpaceDep,
24}
25
26impl HostMode {
27    pub fn is_space(self) -> bool {
28        matches!(self, Self::SpaceSelf | Self::SpaceDep)
29    }
30    pub fn as_str(self) -> &'static str {
31        match self {
32            Self::Admin => "admin",
33            Self::SpaceSelf => "spaceSelf",
34            Self::SpaceDep => "spaceDep",
35        }
36    }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40#[serde(tag = "kind", rename_all = "camelCase", deny_unknown_fields)]
41pub enum Claim {
42    Admin {
43        #[serde(rename = "userIds")]
44        user_ids: BTreeSet<String>,
45    },
46    Space {
47        space: SpaceIdentity,
48    },
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
52#[serde(rename_all = "camelCase")]
53pub enum Permission {
54    Read,
55    ManageRuntime,
56    ManageAdministrators,
57    Reset,
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61#[serde(rename_all = "camelCase", deny_unknown_fields)]
62pub struct HostStatus {
63    pub host_id: String,
64    pub host_key_id: String,
65    pub env: RuntimeEnv,
66    pub mode: HostMode,
67    pub generation: String,
68    pub claim: Option<Claim>,
69}
70
71/// A Space here is the managed resource, not a Space Client principal. The
72/// issuer must authenticate a User and verify current ownership before signing.
73#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
74#[serde(rename_all = "camelCase", deny_unknown_fields)]
75pub struct AccessBinding {
76    pub host_id: String,
77    pub host_key_id: String,
78    pub env: RuntimeEnv,
79    pub generation: String,
80    pub user_id: String,
81    pub space: Option<SpaceIdentity>,
82}
83
84#[derive(Clone, Serialize, Deserialize)]
85#[serde(rename_all = "camelCase", deny_unknown_fields)]
86pub struct AccessClaims {
87    pub iss: String,
88    pub aud: String,
89    pub sub: String,
90    pub jti: String,
91    pub iat: u64,
92    pub exp: u64,
93    pub binding: AccessBinding,
94    pub permissions: BTreeSet<Permission>,
95}
96
97/// Claim proofs cannot be used as management tokens. The redemption secret is
98/// generated by the direct Client; only its digest is sent to the cloud.
99#[derive(Clone, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase", deny_unknown_fields)]
101pub struct ClaimClaims {
102    pub iss: String,
103    pub aud: String,
104    pub sub: String,
105    pub jti: String,
106    pub iat: u64,
107    pub exp: u64,
108    pub binding: AccessBinding,
109    pub challenge: String,
110    pub exchange_key_hash: String,
111    pub display_name: String,
112    pub email: String,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
116#[serde(rename_all = "camelCase", deny_unknown_fields)]
117pub struct ChallengeRequest {
118    pub probe_id: String,
119    pub exchange_key_hash: Option<String>,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize)]
123#[serde(rename_all = "camelCase", deny_unknown_fields)]
124pub struct HostChallenge {
125    /// Present only for a Host dependent on its local fixed-Space Client.
126    #[serde(default, skip_serializing_if = "Option::is_none")]
127    pub device_binding: Option<crate::device_binding::DeviceBinding>,
128    pub status: HostStatus,
129    pub host_name: String,
130    pub host_public_key: PublicKey,
131    pub probe_id: String,
132    pub exchange_key_hash: Option<String>,
133    pub iat: u64,
134    pub exp: u64,
135}
136
137#[derive(Clone, Serialize, Deserialize)]
138#[serde(rename_all = "camelCase", deny_unknown_fields)]
139pub struct ClaimRequest {
140    pub env: RuntimeEnv,
141    pub proof: String,
142    pub exchange_key: String,
143    pub claim_token: Option<String>,
144}
145
146/// Cloud attests only the applicant identity. This credential cannot manage a
147/// Host or grant membership; a current administrator must approve on the Host.
148#[derive(Clone, Serialize, Deserialize)]
149#[serde(rename_all = "camelCase", deny_unknown_fields)]
150pub struct AdministratorApplicantClaims {
151    pub iss: String,
152    pub aud: String,
153    pub sub: String,
154    pub jti: String,
155    pub iat: u64,
156    pub exp: u64,
157    pub binding: AccessBinding,
158    pub display_name: String,
159    pub email: String,
160}
161
162/// Emitted only after the local claim/ACL write is durable. The cloud must
163/// verify this receipt before committing its Host identity directory.
164#[derive(Clone, Serialize, Deserialize)]
165#[serde(rename_all = "camelCase", deny_unknown_fields)]
166pub struct AuthorizationReceipt {
167    pub proof: String,
168    pub status: HostStatus,
169    pub user_id: String,
170}