Skip to main content

qualia_core_db/net/qdnf/session/
bind.rs

1//! Complete QSession bind (NET-05.01 partial).
2//!
3//! Application admission requires a fully bound context: target, route, service,
4//! purpose, profile, policy, and a non-zero operation. Incomplete context cannot
5//! admit application. A connection or path id is not a grant. Payment is not
6//! evaluated here.
7
8use crate::net::qdnf::authority::PolicyOutcome;
9use crate::net::qdnf::errors::QdnfError;
10use crate::net::qdnf::types::{OperationId, StrongDigest};
11
12/// Sole session profile admitted by this bind: `qpr-pq-1`.
13pub const PROFILE_QPR_PQ_1: u16 = 1;
14
15/// Bound session application context. Policy is recorded; admission is separate.
16#[repr(C)]
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub struct SessionContext {
19    pub operation: OperationId,
20    pub target: StrongDigest,
21    pub route: StrongDigest,
22    pub service: StrongDigest,
23    pub purpose: StrongDigest,
24    pub profile: u16,
25    pub policy: PolicyOutcome,
26}
27
28/// Bind a complete application context. Zero identifiers fail closed as Incomplete.
29/// Unknown profile fails as Unauthorized. Non-Allow policy is stored, not rejected.
30pub fn bind_complete(
31    operation: OperationId,
32    target: StrongDigest,
33    route: StrongDigest,
34    service: StrongDigest,
35    purpose: StrongDigest,
36    profile: u16,
37    policy: PolicyOutcome,
38) -> Result<SessionContext, QdnfError> {
39    if operation == OperationId::ZERO {
40        return Err(QdnfError::Incomplete);
41    }
42    if target == StrongDigest::ZERO
43        || route == StrongDigest::ZERO
44        || service == StrongDigest::ZERO
45        || purpose == StrongDigest::ZERO
46    {
47        return Err(QdnfError::Incomplete);
48    }
49    if profile != PROFILE_QPR_PQ_1 {
50        return Err(QdnfError::Unauthorized);
51    }
52    Ok(SessionContext {
53        operation,
54        target,
55        route,
56        service,
57        purpose,
58        profile,
59        policy,
60    })
61}
62
63/// Admit application on a bound context. Only Allow with a current grant succeeds.
64pub fn admit_bound(ctx: &SessionContext, grant_current: bool) -> Result<(), QdnfError> {
65    match ctx.policy {
66        PolicyOutcome::Allow if grant_current => Ok(()),
67        PolicyOutcome::Allow => Err(QdnfError::Unauthorized),
68        PolicyOutcome::Deny => Err(QdnfError::Denied),
69        PolicyOutcome::Challenge => Err(QdnfError::Challenge),
70        PolicyOutcome::NeedsHuman => Err(QdnfError::NeedsHuman),
71        PolicyOutcome::Incomplete => Err(QdnfError::Incomplete),
72        PolicyOutcome::Error => Err(QdnfError::Unauthorized),
73    }
74}
75
76/// A connection or path identifier is not a grant handle.
77#[inline]
78pub fn connection_id_is_grant() -> bool {
79    false
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    fn digest(tag: u8) -> StrongDigest {
87        let mut d = StrongDigest::ZERO;
88        d.0[0] = tag;
89        d
90    }
91
92    fn operation(tag: u8) -> OperationId {
93        let mut id = OperationId::ZERO;
94        id.0[0] = tag;
95        id
96    }
97
98    fn bind(
99        policy: PolicyOutcome,
100        profile: u16,
101        target: StrongDigest,
102    ) -> Result<SessionContext, QdnfError> {
103        bind_complete(
104            operation(1),
105            target,
106            digest(2),
107            digest(3),
108            digest(4),
109            profile,
110            policy,
111        )
112    }
113
114    #[test]
115    fn zero_target_is_incomplete() {
116        assert_eq!(
117            bind(PolicyOutcome::Allow, PROFILE_QPR_PQ_1, StrongDigest::ZERO),
118            Err(QdnfError::Incomplete)
119        );
120    }
121
122    #[test]
123    fn profile_zero_is_unauthorized() {
124        assert_eq!(
125            bind(PolicyOutcome::Allow, 0, digest(1)),
126            Err(QdnfError::Unauthorized)
127        );
128    }
129
130    #[test]
131    fn allow_with_current_grant_admits() {
132        let ctx = bind(PolicyOutcome::Allow, PROFILE_QPR_PQ_1, digest(1)).unwrap();
133        assert!(admit_bound(&ctx, true).is_ok());
134    }
135
136    #[test]
137    fn allow_without_current_grant_is_unauthorized() {
138        let ctx = bind(PolicyOutcome::Allow, PROFILE_QPR_PQ_1, digest(1)).unwrap();
139        assert_eq!(admit_bound(&ctx, false), Err(QdnfError::Unauthorized));
140    }
141
142    #[test]
143    fn deny_with_current_grant_is_denied() {
144        let ctx = bind(PolicyOutcome::Deny, PROFILE_QPR_PQ_1, digest(1)).unwrap();
145        assert_eq!(ctx.policy, PolicyOutcome::Deny);
146        assert_eq!(admit_bound(&ctx, true), Err(QdnfError::Denied));
147    }
148
149    #[test]
150    fn connection_id_is_not_a_grant() {
151        assert!(!connection_id_is_grant());
152    }
153
154    #[test]
155    fn needs_human_policy_admits_as_needs_human() {
156        let ctx = bind(PolicyOutcome::NeedsHuman, PROFILE_QPR_PQ_1, digest(1)).unwrap();
157        assert_eq!(admit_bound(&ctx, true), Err(QdnfError::NeedsHuman));
158    }
159
160    #[test]
161    fn zero_operation_is_incomplete() {
162        assert_eq!(
163            bind_complete(
164                OperationId::ZERO,
165                digest(1),
166                digest(2),
167                digest(3),
168                digest(4),
169                PROFILE_QPR_PQ_1,
170                PolicyOutcome::Allow,
171            ),
172            Err(QdnfError::Incomplete)
173        );
174    }
175
176    #[test]
177    fn zero_route_service_or_purpose_is_incomplete() {
178        assert_eq!(
179            bind_complete(
180                operation(1),
181                digest(1),
182                StrongDigest::ZERO,
183                digest(3),
184                digest(4),
185                PROFILE_QPR_PQ_1,
186                PolicyOutcome::Allow,
187            ),
188            Err(QdnfError::Incomplete)
189        );
190        assert_eq!(
191            bind_complete(
192                operation(1),
193                digest(1),
194                digest(2),
195                StrongDigest::ZERO,
196                digest(4),
197                PROFILE_QPR_PQ_1,
198                PolicyOutcome::Allow,
199            ),
200            Err(QdnfError::Incomplete)
201        );
202        assert_eq!(
203            bind_complete(
204                operation(1),
205                digest(1),
206                digest(2),
207                digest(3),
208                StrongDigest::ZERO,
209                PROFILE_QPR_PQ_1,
210                PolicyOutcome::Allow,
211            ),
212            Err(QdnfError::Incomplete)
213        );
214    }
215
216    #[test]
217    fn challenge_and_error_map_without_grant_override() {
218        let challenge = bind(PolicyOutcome::Challenge, PROFILE_QPR_PQ_1, digest(1)).unwrap();
219        assert_eq!(admit_bound(&challenge, true), Err(QdnfError::Challenge));
220        let error = bind(PolicyOutcome::Error, PROFILE_QPR_PQ_1, digest(1)).unwrap();
221        assert_eq!(admit_bound(&error, true), Err(QdnfError::Unauthorized));
222        let incomplete = bind(PolicyOutcome::Incomplete, PROFILE_QPR_PQ_1, digest(1)).unwrap();
223        assert_eq!(admit_bound(&incomplete, true), Err(QdnfError::Incomplete));
224    }
225}