Skip to main content

acton_service/middleware/
revocation.rs

1//! Persistent token-ID and subject revocation.
2//!
3//! Initialize database schemas with each backend's `initialize()` before serving.
4//! Authentication fails closed while a configured pool or schema is unavailable.
5
6use super::token::TokenRevocation;
7use crate::error::Error;
8
9#[cfg(feature = "cache")]
10mod redis;
11#[cfg(feature = "cache")]
12pub use redis::RedisTokenRevocation;
13#[cfg(feature = "database")]
14mod postgres;
15#[cfg(feature = "database")]
16pub use postgres::PgTokenRevocation;
17#[cfg(feature = "mssql")]
18mod mssql;
19#[cfg(feature = "mssql")]
20pub use mssql::MssqlTokenRevocation;
21#[cfg(feature = "turso")]
22mod turso;
23#[cfg(feature = "turso")]
24pub use turso::TursoTokenRevocation;
25#[cfg(feature = "surrealdb")]
26mod surreal;
27#[cfg(feature = "surrealdb")]
28pub use surreal::SurrealTokenRevocation;
29mod configured;
30pub(crate) use configured::ConfiguredRevocation;
31
32/// Namespace isolating services that share the same revocation database.
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct RevocationNamespace(String);
35
36impl RevocationNamespace {
37    /// Validate a nonempty namespace of at most 128 bytes.
38    pub fn new(namespace: impl Into<String>) -> Result<Self, Error> {
39        let namespace = namespace.into();
40        if namespace.trim().is_empty() || namespace.len() > 128 {
41            return Err(Error::Internal(
42                "Revocation namespace must contain 1 to 128 bytes".into(),
43            ));
44        }
45        Ok(Self(namespace))
46    }
47    /// Get the exact namespace used by storage.
48    pub fn as_str(&self) -> &str {
49        &self.0
50    }
51}
52
53impl std::fmt::Display for RevocationNamespace {
54    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        formatter.write_str(self.as_str())
56    }
57}
58impl std::str::FromStr for RevocationNamespace {
59    type Err = Error;
60    fn from_str(namespace: &str) -> Result<Self, Self::Err> {
61        Self::new(namespace)
62    }
63}
64impl AsRef<str> for RevocationNamespace {
65    fn as_ref(&self) -> &str {
66        self.as_str()
67    }
68}
69
70#[cfg(any(
71    feature = "database",
72    feature = "mssql",
73    feature = "turso",
74    feature = "surrealdb"
75))]
76fn expires_at(ttl_secs: u64) -> Result<i64, Error> {
77    let ttl = i64::try_from(ttl_secs).map_err(|_| {
78        Error::Internal("Revocation lifetime exceeds supported timestamp range".into())
79    })?;
80    chrono::Utc::now()
81        .timestamp()
82        .checked_add(ttl)
83        .ok_or_else(|| {
84            Error::Internal("Revocation expiration exceeds supported timestamp range".into())
85        })
86}