Skip to main content

qualia_peer/
ops.rs

1//! E20.4–E20.6 organisational trust, redacted ops, honest device-study flag.
2//!
3//! No universal organisational superuser. Dashboards return counts, never
4//! patient identifiers. Failures are [`QdnfError`] codes, not payload echoes.
5
6use qualia_core_db::net::qdnf::errors::QdnfError;
7use qualia_core_db::net::qdnf::types::{Generation, StrongDigest};
8
9/// There is no organisation-wide superuser in this slice.
10pub fn universal_org_superuser() -> bool {
11    false
12}
13
14/// Realistic-user/device study (E20.6) has not been executed.
15pub fn realistic_device_study_executed() -> bool {
16    false
17}
18
19/// Clinician / mission credential bound to one org, purpose and generation.
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct CredentialScope {
22    pub principal: StrongDigest,
23    pub organisation: StrongDigest,
24    pub purpose: StrongDigest,
25    pub compartment: StrongDigest,
26    pub issued_generation: Generation,
27}
28
29/// Admit a scoped credential against the live revocation generation.
30///
31/// A revocation generation greater than the issued generation is Revoked.
32/// Zero identity fields are Unauthorized. This is not a superuser grant.
33pub fn admit_scoped_credential(
34    scope: CredentialScope,
35    revocation_generation: Generation,
36) -> Result<(), QdnfError> {
37    if universal_org_superuser() {
38        return Err(QdnfError::Denied);
39    }
40    if scope.principal.is_zero()
41        || scope.organisation.is_zero()
42        || scope.purpose.is_zero()
43        || scope.compartment.is_zero()
44    {
45        return Err(QdnfError::Unauthorized);
46    }
47    if scope.issued_generation == Generation::ZERO {
48        return Err(QdnfError::Unauthorized);
49    }
50    if revocation_generation.0 > scope.issued_generation.0 {
51        return Err(QdnfError::Revoked);
52    }
53    Ok(())
54}
55
56/// Redacted operational counts. No patient / clinician identifier fields.
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub struct RedactedDashboard {
59    pub active_sessions: u32,
60    pub denied_ops: u32,
61    pub revoked_creds: u32,
62}
63
64/// Build a dashboard from counts only. Callers never pass patient ids.
65pub fn redacted_dashboard(
66    active_sessions: u32,
67    denied_ops: u32,
68    revoked_creds: u32,
69) -> RedactedDashboard {
70    RedactedDashboard {
71        active_sessions,
72        denied_ops,
73        revoked_creds,
74    }
75}
76
77/// `active-session` count.
78pub fn active_session_count(dashboard: RedactedDashboard) -> u32 {
79    dashboard.active_sessions
80}
81
82/// `denied-op` count.
83pub fn denied_op_count(dashboard: RedactedDashboard) -> u32 {
84    dashboard.denied_ops
85}
86
87/// `revoked-credential` count.
88pub fn revoked_credential_count(dashboard: RedactedDashboard) -> u32 {
89    dashboard.revoked_creds
90}
91
92/// Dashboard APIs do not surface patient identifiers.
93pub fn patient_identifiers_on_dashboard() -> bool {
94    false
95}
96
97/// Fail closed with a typed code. `untrusted` is not copied into the error.
98pub fn safe_failure(untrusted: &[u8]) -> QdnfError {
99    let _ = untrusted;
100    QdnfError::Denied
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    fn digest(tag: u8) -> StrongDigest {
108        let mut d = StrongDigest::ZERO;
109        d.0[0] = tag;
110        d.0[47] = 0xA5;
111        d
112    }
113
114    fn scope() -> CredentialScope {
115        CredentialScope {
116            principal: digest(1),
117            organisation: digest(2),
118            purpose: digest(3),
119            compartment: digest(4),
120            issued_generation: Generation(3),
121        }
122    }
123
124    #[test]
125    fn no_universal_superuser_and_revocation_generation() {
126        assert!(!universal_org_superuser());
127        assert!(admit_scoped_credential(scope(), Generation(3)).is_ok());
128        assert!(admit_scoped_credential(scope(), Generation(1)).is_ok());
129        assert_eq!(
130            admit_scoped_credential(scope(), Generation(4)),
131            Err(QdnfError::Revoked)
132        );
133        let mut zero = scope();
134        zero.principal = StrongDigest::ZERO;
135        assert_eq!(
136            admit_scoped_credential(zero, Generation(1)),
137            Err(QdnfError::Unauthorized)
138        );
139        let mut issued0 = scope();
140        issued0.issued_generation = Generation::ZERO;
141        assert_eq!(
142            admit_scoped_credential(issued0, Generation::ZERO),
143            Err(QdnfError::Unauthorized)
144        );
145    }
146
147    #[test]
148    fn dashboard_returns_counts_not_patient_ids() {
149        let dash = redacted_dashboard(4, 2, 1);
150        assert_eq!(active_session_count(dash), 4);
151        assert_eq!(denied_op_count(dash), 2);
152        assert_eq!(revoked_credential_count(dash), 1);
153        assert!(!patient_identifiers_on_dashboard());
154        let text = format!("{dash:?}");
155        assert!(text.contains("active_sessions"));
156        assert!(!text.contains("patient"));
157        assert!(!text.contains("MRN"));
158        assert!(!text.contains("did:"));
159    }
160
161    #[test]
162    fn safe_failure_is_code_not_payload_echo() {
163        let payload = b"patient-MRN-999-secret";
164        let err = safe_failure(payload);
165        assert_eq!(err, QdnfError::Denied);
166        let text = err.to_string();
167        assert_eq!(text, "denied");
168        assert!(!text.contains("patient"));
169        assert!(!text.contains("MRN"));
170        assert!(!text.contains("999"));
171        assert!(!text.contains("secret"));
172    }
173
174    #[test]
175    fn realistic_device_study_is_not_claimed() {
176        assert!(!realistic_device_study_executed());
177    }
178}