mas_config/sections/
experimental.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use chrono::Duration;
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use serde_with::serde_as;
11
12use crate::ConfigurationSection;
13
14fn default_true() -> bool {
15    true
16}
17
18fn default_token_ttl() -> Duration {
19    Duration::microseconds(5 * 60 * 1000 * 1000)
20}
21
22fn is_default_token_ttl(value: &Duration) -> bool {
23    *value == default_token_ttl()
24}
25
26/// Configuration options for the inactive session expiration feature
27#[serde_as]
28#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
29pub struct InactiveSessionExpirationConfig {
30    /// Time after which an inactive session is automatically finished
31    #[schemars(with = "u64", range(min = 600, max = 7_776_000))]
32    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
33    pub ttl: Duration,
34
35    /// Should compatibility sessions expire after inactivity
36    #[serde(default = "default_true")]
37    pub expire_compat_sessions: bool,
38
39    /// Should OAuth 2.0 sessions expire after inactivity
40    #[serde(default = "default_true")]
41    pub expire_oauth_sessions: bool,
42
43    /// Should user sessions expire after inactivity
44    #[serde(default = "default_true")]
45    pub expire_user_sessions: bool,
46}
47
48/// Configuration sections for experimental options
49///
50/// Do not change these options unless you know what you are doing.
51#[serde_as]
52#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
53pub struct ExperimentalConfig {
54    /// Time-to-live of access tokens in seconds. Defaults to 5 minutes.
55    #[schemars(with = "u64", range(min = 60, max = 86400))]
56    #[serde(
57        default = "default_token_ttl",
58        skip_serializing_if = "is_default_token_ttl"
59    )]
60    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
61    pub access_token_ttl: Duration,
62
63    /// Time-to-live of compatibility access tokens in seconds. Defaults to 5
64    /// minutes.
65    #[schemars(with = "u64", range(min = 60, max = 86400))]
66    #[serde(
67        default = "default_token_ttl",
68        skip_serializing_if = "is_default_token_ttl"
69    )]
70    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
71    pub compat_token_ttl: Duration,
72
73    /// Experimetal feature to automatically expire inactive sessions
74    ///
75    /// Disabled by default
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub inactive_session_expiration: Option<InactiveSessionExpirationConfig>,
78}
79
80impl Default for ExperimentalConfig {
81    fn default() -> Self {
82        Self {
83            access_token_ttl: default_token_ttl(),
84            compat_token_ttl: default_token_ttl(),
85            inactive_session_expiration: None,
86        }
87    }
88}
89
90impl ExperimentalConfig {
91    pub(crate) fn is_default(&self) -> bool {
92        is_default_token_ttl(&self.access_token_ttl)
93            && is_default_token_ttl(&self.compat_token_ttl)
94            && self.inactive_session_expiration.is_none()
95    }
96}
97
98impl ConfigurationSection for ExperimentalConfig {
99    const PATH: Option<&'static str> = Some("experimental");
100}