webgates-sessions 1.0.0

Framework-agnostic session lifecycle and renewal primitives for webgates.
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Renewal orchestration models for session auto-renewal.
//!
//! This module defines framework-agnostic renewal inputs, typed auth-token
//! state, deterministic decision logic, and high-level renewal outcomes.

use std::time::{Duration, SystemTime};

use crate::lease::LeaseAcquisition;
use crate::session::{Session, SessionId};
use crate::tokens::IssuedTokenPair;

/// Indicates whether a caller is attempting renewal proactively or because the
/// current auth token can no longer be used.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RenewalRequirement {
    /// Attempt renewal opportunistically while the current auth token remains
    /// valid.
    Proactive,
    /// Require a successful renewal before the request can proceed.
    Required,
}

/// Describes the observed state of the current auth token.
///
/// This type is intentionally framework agnostic. HTTP adapters and other
/// delivery layers can translate transport-specific validation results into
/// this domain-level model before invoking session renewal logic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AuthTokenState {
    /// The auth token is currently valid and does not need renewal.
    Valid {
        /// Remaining time until the token expires.
        expires_in: Duration,
    },
    /// The auth token is still valid but has entered the proactive renewal
    /// window.
    NearExpiry {
        /// Remaining time until the token expires.
        expires_in: Duration,
    },
    /// The auth token has expired.
    Expired {
        /// Amount of time elapsed since expiry.
        expired_for: Duration,
    },
    /// The auth token is malformed, revoked, or otherwise invalid.
    Invalid,
}

impl AuthTokenState {
    /// Returns `true` when the current token may still authorize the request.
    #[must_use]
    pub fn is_currently_usable(self) -> bool {
        matches!(self, Self::Valid { .. } | Self::NearExpiry { .. })
    }

    /// Returns `true` when the token has entered the proactive renewal window.
    #[must_use]
    pub fn should_attempt_proactive_renewal(self) -> bool {
        matches!(self, Self::NearExpiry { .. })
    }

    /// Returns `true` when successful renewal is required before a caller may
    /// continue.
    #[must_use]
    pub fn requires_renewal(self) -> bool {
        matches!(self, Self::Expired { .. })
    }

    /// Returns `true` when the token must be rejected without renewal.
    #[must_use]
    pub fn is_invalid(self) -> bool {
        matches!(self, Self::Invalid)
    }
}

/// Input passed into the renewal decision flow.
///
/// This request captures only the deterministic, framework-agnostic inputs
/// needed to decide whether renewal should be attempted.
///
/// # Examples
///
/// ```
/// use std::time::{Duration, SystemTime};
/// use webgates_sessions::renewal::{
///     AuthTokenState, RenewalDecision, RenewalOrchestrator, RenewalRequest,
///     RenewalRequirement,
/// };
/// use webgates_sessions::session::SessionId;
///
/// let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000);
/// let request = RenewalRequest::new(
///     SessionId::new(),
///     AuthTokenState::NearExpiry { expires_in: Duration::from_secs(60) },
///     RenewalRequirement::Proactive,
///     now,
/// );
///
/// let decision = RenewalOrchestrator::new().decide(&request);
/// assert_eq!(decision, RenewalDecision::AttemptProactiveRenewal);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenewalRequest {
    /// Session currently associated with the caller.
    pub session_id: SessionId,
    /// The current auth-token state observed by the caller.
    pub auth_token_state: AuthTokenState,
    /// Whether the renewal is opportunistic or mandatory.
    pub requirement: RenewalRequirement,
    /// Current wall-clock time used for deterministic decision making.
    pub now: SystemTime,
}

impl RenewalRequest {
    /// Creates a new renewal request.
    #[must_use]
    pub fn new(
        session_id: SessionId,
        auth_token_state: AuthTokenState,
        requirement: RenewalRequirement,
        now: SystemTime,
    ) -> Self {
        Self {
            session_id,
            auth_token_state,
            requirement,
            now,
        }
    }
}

/// High-level decision produced before repository-backed renewal work begins.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RenewalDecision {
    /// No renewal attempt should be made.
    NoRenewal,
    /// Renewal may be attempted, but failure does not necessarily block the
    /// caller.
    AttemptProactiveRenewal,
    /// Renewal must succeed before the caller can continue.
    RequireRenewal,
    /// The caller must be rejected immediately without attempting renewal.
    Reject,
}

/// Context captured while a renewal attempt is in progress.
///
/// # Examples
///
/// ```
/// use std::time::{Duration, SystemTime};
/// use webgates_sessions::renewal::{
///     AuthTokenState, RenewalAttempt, RenewalRequest, RenewalRequirement,
/// };
/// use webgates_sessions::session::{Session, SessionFamilyId, SessionId};
///
/// let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000);
/// let session = Session::new(
///     SessionFamilyId::new(),
///     "user-42",
///     now,
///     now + Duration::from_secs(3_600),
/// );
/// let request = RenewalRequest::new(
///     session.session_id,
///     AuthTokenState::Expired { expired_for: Duration::from_secs(5) },
///     RenewalRequirement::Required,
///     now,
/// );
/// let attempt = RenewalAttempt::new(session.clone(), request);
///
/// assert_eq!(attempt.session_id(), session.session_id);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenewalAttempt {
    /// Session selected for renewal.
    pub session: Session,
    /// Original request that triggered the attempt.
    pub request: RenewalRequest,
}

impl RenewalAttempt {
    /// Creates a new renewal attempt context.
    #[must_use]
    pub fn new(session: Session, request: RenewalRequest) -> Self {
        Self { session, request }
    }

    /// Returns the identifier of the session being renewed.
    #[must_use]
    pub fn session_id(&self) -> SessionId {
        self.request.session_id
    }
}

/// Outcome returned by the renewal orchestration layer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RenewalOutcome {
    /// No renewal was necessary.
    NotNeeded,
    /// Renewal was attempted but could not proceed because another actor
    /// currently owns the lease or renewal slot.
    LeaseUnavailable {
        /// Observed lease-acquisition result.
        acquisition: LeaseAcquisition,
    },
    /// Renewal completed successfully and produced replacement tokens.
    Renewed {
        /// Session that remains active after renewal.
        session: Session,
        /// Newly issued auth and refresh tokens.
        tokens: IssuedTokenPair,
    },
    /// Renewal detected refresh-token replay and requires broader revocation.
    ReplayDetected {
        /// Session associated with the detected replay.
        session_id: SessionId,
    },
    /// Renewal cannot proceed because the request is not authorized.
    Rejected,
}

impl RenewalOutcome {
    /// Returns the newly issued token pair when renewal succeeded.
    #[must_use]
    pub fn issued_tokens(&self) -> Option<&IssuedTokenPair> {
        match self {
            Self::Renewed { tokens, .. } => Some(tokens),
            Self::NotNeeded
            | Self::LeaseUnavailable { .. }
            | Self::ReplayDetected { .. }
            | Self::Rejected => None,
        }
    }

    /// Returns the renewed session when renewal succeeded.
    #[must_use]
    pub fn session(&self) -> Option<&Session> {
        match self {
            Self::Renewed { session, .. } => Some(session),
            Self::NotNeeded
            | Self::LeaseUnavailable { .. }
            | Self::ReplayDetected { .. }
            | Self::Rejected => None,
        }
    }
}

/// Stateless decision helper for renewal orchestration.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use webgates_sessions::renewal::{
///     AuthTokenState, RenewalDecision, RenewalOrchestrator, RenewalRequirement,
/// };
///
/// let orchestrator = RenewalOrchestrator::new();
///
/// assert_eq!(
///     orchestrator.decide_for_state(
///         AuthTokenState::Valid { expires_in: Duration::from_secs(300) },
///         RenewalRequirement::Proactive,
///     ),
///     RenewalDecision::NoRenewal,
/// );
///
/// assert_eq!(
///     orchestrator.decide_for_state(
///         AuthTokenState::Expired { expired_for: Duration::from_secs(10) },
///         RenewalRequirement::Required,
///     ),
///     RenewalDecision::RequireRenewal,
/// );
/// ```
#[derive(Debug, Default, Clone, Copy)]
pub struct RenewalOrchestrator;

impl RenewalOrchestrator {
    /// Creates a new renewal orchestrator.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Decides whether a renewal attempt should be made from token state alone.
    #[must_use]
    pub fn decide_for_state(
        &self,
        auth_token_state: AuthTokenState,
        requirement: RenewalRequirement,
    ) -> RenewalDecision {
        match (auth_token_state, requirement) {
            (AuthTokenState::Invalid, _) => RenewalDecision::Reject,
            (AuthTokenState::Expired { .. }, _) => RenewalDecision::RequireRenewal,
            (AuthTokenState::NearExpiry { .. }, RenewalRequirement::Proactive) => {
                RenewalDecision::AttemptProactiveRenewal
            }
            (AuthTokenState::NearExpiry { .. }, RenewalRequirement::Required) => {
                RenewalDecision::RequireRenewal
            }
            (AuthTokenState::Valid { .. }, _) => RenewalDecision::NoRenewal,
        }
    }

    /// Decides whether a renewal attempt should be made for the given request.
    #[must_use]
    pub fn decide(&self, request: &RenewalRequest) -> RenewalDecision {
        self.decide_for_state(request.auth_token_state, request.requirement)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use uuid::Uuid;

    fn session_id() -> SessionId {
        SessionId::from_uuid(Uuid::now_v7())
    }

    #[test]
    fn proactive_near_expiry_requests_attempt_renewal() {
        let orchestrator = RenewalOrchestrator::new();
        let request = RenewalRequest::new(
            session_id(),
            AuthTokenState::NearExpiry {
                expires_in: Duration::from_secs(45),
            },
            RenewalRequirement::Proactive,
            SystemTime::UNIX_EPOCH,
        );

        assert_eq!(
            orchestrator.decide(&request),
            RenewalDecision::AttemptProactiveRenewal
        );
    }

    #[test]
    fn required_near_expiry_requests_require_renewal() {
        let orchestrator = RenewalOrchestrator::new();
        let request = RenewalRequest::new(
            session_id(),
            AuthTokenState::NearExpiry {
                expires_in: Duration::from_secs(10),
            },
            RenewalRequirement::Required,
            SystemTime::UNIX_EPOCH,
        );

        assert_eq!(
            orchestrator.decide(&request),
            RenewalDecision::RequireRenewal
        );
    }

    #[test]
    fn expired_requests_require_renewal() {
        let orchestrator = RenewalOrchestrator::new();
        let request = RenewalRequest::new(
            session_id(),
            AuthTokenState::Expired {
                expired_for: Duration::from_secs(30),
            },
            RenewalRequirement::Required,
            SystemTime::UNIX_EPOCH,
        );

        assert_eq!(
            orchestrator.decide(&request),
            RenewalDecision::RequireRenewal
        );
    }

    #[test]
    fn valid_requests_do_not_trigger_renewal() {
        let orchestrator = RenewalOrchestrator::new();
        let request = RenewalRequest::new(
            session_id(),
            AuthTokenState::Valid {
                expires_in: Duration::from_secs(600),
            },
            RenewalRequirement::Proactive,
            SystemTime::UNIX_EPOCH,
        );

        assert_eq!(orchestrator.decide(&request), RenewalDecision::NoRenewal);
    }

    #[test]
    fn invalid_requests_are_rejected() {
        let orchestrator = RenewalOrchestrator::new();
        let request = RenewalRequest::new(
            session_id(),
            AuthTokenState::Invalid,
            RenewalRequirement::Proactive,
            SystemTime::UNIX_EPOCH,
        );

        assert_eq!(orchestrator.decide(&request), RenewalDecision::Reject);
    }

    #[test]
    fn auth_token_state_helpers_match_expected_behavior() {
        let valid = AuthTokenState::Valid {
            expires_in: Duration::from_secs(60),
        };
        let near_expiry = AuthTokenState::NearExpiry {
            expires_in: Duration::from_secs(15),
        };
        let expired = AuthTokenState::Expired {
            expired_for: Duration::from_secs(5),
        };
        let invalid = AuthTokenState::Invalid;

        assert!(valid.is_currently_usable());
        assert!(!valid.should_attempt_proactive_renewal());
        assert!(!valid.requires_renewal());
        assert!(!valid.is_invalid());

        assert!(near_expiry.is_currently_usable());
        assert!(near_expiry.should_attempt_proactive_renewal());
        assert!(!near_expiry.requires_renewal());
        assert!(!near_expiry.is_invalid());

        assert!(!expired.is_currently_usable());
        assert!(!expired.should_attempt_proactive_renewal());
        assert!(expired.requires_renewal());
        assert!(!expired.is_invalid());

        assert!(!invalid.is_currently_usable());
        assert!(!invalid.should_attempt_proactive_renewal());
        assert!(!invalid.requires_renewal());
        assert!(invalid.is_invalid());
    }
}