mas_config/sections/
experimental.rs

1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5// Please see LICENSE files 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    /// Experimental feature to show a plan management tab and iframe.
80    /// This value is passed through "as is" to the client without any
81    /// validation.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub plan_management_iframe_uri: Option<String>,
84}
85
86impl Default for ExperimentalConfig {
87    fn default() -> Self {
88        Self {
89            access_token_ttl: default_token_ttl(),
90            compat_token_ttl: default_token_ttl(),
91            inactive_session_expiration: None,
92            plan_management_iframe_uri: None,
93        }
94    }
95}
96
97impl ExperimentalConfig {
98    pub(crate) fn is_default(&self) -> bool {
99        is_default_token_ttl(&self.access_token_ttl)
100            && is_default_token_ttl(&self.compat_token_ttl)
101            && self.inactive_session_expiration.is_none()
102            && self.plan_management_iframe_uri.is_none()
103    }
104}
105
106impl ConfigurationSection for ExperimentalConfig {
107    const PATH: Option<&'static str> = Some("experimental");
108}