codlet_core/state/
session.rs1use crate::secret::SubjectId;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum SessionValidationOutcome {
11 Authenticated {
14 subject: SubjectId,
16 session_id: crate::secret::SessionId,
18 expires_at: u64,
21 },
22 Unauthenticated,
26}
27
28impl SessionValidationOutcome {
29 #[must_use]
31 pub fn is_authenticated(&self) -> bool {
32 matches!(self, Self::Authenticated { .. })
33 }
34
35 #[must_use]
37 pub fn subject(&self) -> Option<&SubjectId> {
38 match self {
39 Self::Authenticated { subject, .. } => Some(subject),
40 Self::Unauthenticated => None,
41 }
42 }
43}
44
45#[must_use]
51pub fn classify_session(
52 record: Option<(SubjectId, crate::secret::SessionId, u64)>,
53) -> SessionValidationOutcome {
54 match record {
55 Some((subject, session_id, expires_at)) => SessionValidationOutcome::Authenticated {
56 subject,
57 session_id,
58 expires_at,
59 },
60 None => SessionValidationOutcome::Unauthenticated,
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 fn subject() -> SubjectId {
69 SubjectId::new("user-42".to_string())
70 }
71
72 fn sid() -> crate::secret::SessionId {
73 crate::secret::SessionId::new("sess-abc".to_string())
74 }
75
76 #[test]
77 fn some_record_authenticates() {
78 let out = classify_session(Some((subject(), sid(), 9_999_999)));
79 assert!(out.is_authenticated());
80 assert_eq!(out.subject().unwrap().as_str(), "user-42");
81 }
82
83 #[test]
84 fn none_is_unauthenticated() {
85 assert_eq!(
86 classify_session(None),
87 SessionValidationOutcome::Unauthenticated
88 );
89 assert!(!classify_session(None).is_authenticated());
90 assert!(classify_session(None).subject().is_none());
91 }
92
93 #[test]
94 fn authenticated_carries_session_id_and_expiry() {
95 let out = classify_session(Some((subject(), sid(), 12_345)));
96 if let SessionValidationOutcome::Authenticated {
97 session_id,
98 expires_at,
99 ..
100 } = out
101 {
102 assert_eq!(session_id.as_str(), "sess-abc");
103 assert_eq!(expires_at, 12_345);
104 } else {
105 panic!("expected Authenticated");
106 }
107 }
108}