File size: 10,284 Bytes
c01fd1c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | // Copyright (C) 2026 ERRANT DIGITAL INSTITUTE OF TECHNOLOGY (SEIT NGO) % SNAPKITTY COLLECTIVE
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// See LICENSE-AGPL and LICENSE-COMMERCIAL in this repository for license terms.
//! Temporary, expiring sessions -- the second piece of Phase 13's IAM
//! surface (`cloud-identity`'s Phase 1 doc comment: "credentials,
//! sessions, federation, policy documents ... is Phase 13").
//!
//! A [`Session`] is produced one of two ways -- assuming a role, or
//! federating in an external identity -- and is valid only while it is
//! both un-revoked *and* before its expiry. This is deliberately the
//! same two-independent-invalidity-path shape
//! [`cloud_visibility::MessageLease`] (Phase 7) already established
//! for at-least-once redelivery: there, a message became undeliverable
//! either by being currently in flight or by exceeding its receive
//! count; here, a session becomes invalid either by explicit
//! [`SessionStore::revoke`] or by simply outliving [`Session::expires_at`].
//! Neither path implies the other, and both must be checked.
#![forbid(unsafe_code)]
use cloud_errors::CloudError;
use cloud_identity::Principal;
use cloud_types::{ResourceId, Timestamp};
use std::collections::BTreeMap;
/// How a session came to exist.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionSource {
/// The principal assumed a role, producing temporary credentials
/// scoped to that role.
AssumedRole { role: ResourceId },
/// The principal was federated in from an external identity
/// provider (SAML, OIDC, ...); `external_id` names that provider's
/// own identifier for it.
Federated { external_id: String },
}
/// A temporary session: a principal, how it got here, and the window
/// during which it's valid.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Session {
token: ResourceId,
principal: Principal,
source: SessionSource,
issued_at: Timestamp,
expires_at: Timestamp,
revoked: bool,
}
impl Session {
pub fn token(&self) -> &ResourceId {
&self.token
}
pub fn principal(&self) -> &Principal {
&self.principal
}
pub fn source(&self) -> &SessionSource {
&self.source
}
pub fn issued_at(&self) -> Timestamp {
self.issued_at
}
pub fn expires_at(&self) -> Timestamp {
self.expires_at
}
pub fn is_revoked(&self) -> bool {
self.revoked
}
/// Whether this session may still be used at `now`: not revoked,
/// and not yet expired.
pub fn is_valid(&self, now: Timestamp) -> bool {
!self.revoked && now < self.expires_at
}
}
/// A registry of live sessions, keyed by their token.
#[derive(Debug, Clone, Default)]
pub struct SessionStore {
sessions: BTreeMap<ResourceId, Session>,
}
impl SessionStore {
pub fn new() -> Self {
SessionStore {
sessions: BTreeMap::new(),
}
}
fn create(
&mut self,
token: ResourceId,
principal: Principal,
source: SessionSource,
issued_at: Timestamp,
duration_millis: u64,
) -> Result<&Session, CloudError> {
if self.sessions.contains_key(&token) {
return Err(CloudError::Conflict {
what: "session",
id: token.to_string(),
});
}
let session = Session {
token: token.clone(),
principal,
source,
issued_at,
expires_at: issued_at.saturating_add_millis(duration_millis),
revoked: false,
};
self.sessions.insert(token.clone(), session);
Ok(self
.sessions
.get(&token)
.expect("just inserted under this exact id"))
}
/// Creates a session from a role assumption.
pub fn assume_role(
&mut self,
token: ResourceId,
principal: Principal,
role: ResourceId,
issued_at: Timestamp,
duration_millis: u64,
) -> Result<&Session, CloudError> {
self.create(
token,
principal,
SessionSource::AssumedRole { role },
issued_at,
duration_millis,
)
}
/// Creates a session from federating in an external identity.
pub fn federate(
&mut self,
token: ResourceId,
principal: Principal,
external_id: impl Into<String>,
issued_at: Timestamp,
duration_millis: u64,
) -> Result<&Session, CloudError> {
self.create(
token,
principal,
SessionSource::Federated {
external_id: external_id.into(),
},
issued_at,
duration_millis,
)
}
pub fn get(&self, token: &ResourceId) -> Option<&Session> {
self.sessions.get(token)
}
pub fn is_valid(&self, token: &ResourceId, now: Timestamp) -> bool {
self.sessions.get(token).is_some_and(|s| s.is_valid(now))
}
/// Explicitly revokes a session ahead of its natural expiry.
pub fn revoke(&mut self, token: &ResourceId) -> Result<(), CloudError> {
let session = self
.sessions
.get_mut(token)
.ok_or_else(|| CloudError::NotFound {
what: "session",
id: token.to_string(),
})?;
session.revoked = true;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn principal(name: &str) -> Principal {
Principal::User(ResourceId::new(name).unwrap())
}
fn id(s: &str) -> ResourceId {
ResourceId::new(s).unwrap()
}
#[test]
fn a_freshly_assumed_role_session_is_valid_immediately() {
let mut store = SessionStore::new();
store
.assume_role(
id("tok-1"),
principal("alice"),
id("role-admin"),
Timestamp::from_millis(0),
3_600_000,
)
.unwrap();
assert!(store.is_valid(&id("tok-1"), Timestamp::from_millis(0)));
}
#[test]
fn a_session_becomes_invalid_once_its_expiry_passes() {
let mut store = SessionStore::new();
store
.assume_role(
id("tok-1"),
principal("alice"),
id("role-admin"),
Timestamp::from_millis(0),
1000,
)
.unwrap();
assert!(store.is_valid(&id("tok-1"), Timestamp::from_millis(999)));
assert!(!store.is_valid(&id("tok-1"), Timestamp::from_millis(1000)));
}
#[test]
fn revoking_invalidates_a_session_before_its_natural_expiry() {
let mut store = SessionStore::new();
store
.assume_role(
id("tok-1"),
principal("alice"),
id("role-admin"),
Timestamp::from_millis(0),
3_600_000,
)
.unwrap();
store.revoke(&id("tok-1")).unwrap();
assert!(!store.is_valid(&id("tok-1"), Timestamp::from_millis(0)));
}
#[test]
fn revoking_an_unknown_session_is_not_found() {
let mut store = SessionStore::new();
let err = store.revoke(&id("ghost")).unwrap_err();
assert!(matches!(err, CloudError::NotFound { .. }));
}
#[test]
fn a_duplicate_token_is_a_conflict() {
let mut store = SessionStore::new();
store
.assume_role(
id("tok-1"),
principal("alice"),
id("role-admin"),
Timestamp::from_millis(0),
1000,
)
.unwrap();
let err = store
.federate(
id("tok-1"),
principal("bob"),
"external-idp-1",
Timestamp::from_millis(0),
1000,
)
.unwrap_err();
assert!(matches!(err, CloudError::Conflict { .. }));
}
#[test]
fn federated_sessions_record_their_external_id() {
let mut store = SessionStore::new();
let session = store
.federate(
id("tok-1"),
principal("alice"),
"external-idp-1",
Timestamp::from_millis(0),
1000,
)
.unwrap();
assert_eq!(
session.source(),
&SessionSource::Federated {
external_id: "external-idp-1".to_string()
}
);
}
#[test]
fn assumed_role_sessions_record_the_role() {
let mut store = SessionStore::new();
let session = store
.assume_role(
id("tok-1"),
principal("alice"),
id("role-admin"),
Timestamp::from_millis(0),
1000,
)
.unwrap();
assert_eq!(
session.source(),
&SessionSource::AssumedRole {
role: id("role-admin")
}
);
}
#[test]
fn is_valid_on_an_unknown_token_is_false_not_an_error() {
let store = SessionStore::new();
assert!(!store.is_valid(&id("ghost"), Timestamp::from_millis(0)));
}
#[test]
fn expiry_and_revocation_are_independent_invalidity_paths() {
let mut store = SessionStore::new();
store
.assume_role(
id("tok-1"),
principal("alice"),
id("role-admin"),
Timestamp::from_millis(0),
1000,
)
.unwrap();
// Not revoked, but expired.
assert!(!store.get(&id("tok-1")).unwrap().is_revoked());
assert!(!store.is_valid(&id("tok-1"), Timestamp::from_millis(2000)));
store
.assume_role(
id("tok-2"),
principal("alice"),
id("role-admin"),
Timestamp::from_millis(0),
3_600_000,
)
.unwrap();
// Not expired, but revoked.
store.revoke(&id("tok-2")).unwrap();
assert!(!store.is_valid(&id("tok-2"), Timestamp::from_millis(0)));
}
}
|