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 std::num::NonZeroU64;
8
9use chrono::Duration;
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12use serde_with::serde_as;
13
14use crate::ConfigurationSection;
15
16fn default_true() -> bool {
17    true
18}
19
20fn default_token_ttl() -> Duration {
21    Duration::microseconds(5 * 60 * 1000 * 1000)
22}
23
24fn is_default_token_ttl(value: &Duration) -> bool {
25    *value == default_token_ttl()
26}
27
28/// Configuration options for the inactive session expiration feature
29#[serde_as]
30#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
31pub struct InactiveSessionExpirationConfig {
32    /// Time after which an inactive session is automatically finished
33    #[schemars(with = "u64", range(min = 600, max = 7_776_000))]
34    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
35    pub ttl: Duration,
36
37    /// Should compatibility sessions expire after inactivity
38    #[serde(default = "default_true")]
39    pub expire_compat_sessions: bool,
40
41    /// Should OAuth 2.0 sessions expire after inactivity
42    #[serde(default = "default_true")]
43    pub expire_oauth_sessions: bool,
44
45    /// Should user sessions expire after inactivity
46    #[serde(default = "default_true")]
47    pub expire_user_sessions: bool,
48}
49
50/// Configuration sections for experimental options
51///
52/// Do not change these options unless you know what you are doing.
53#[serde_as]
54#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
55pub struct ExperimentalConfig {
56    /// Time-to-live of access tokens in seconds. Defaults to 5 minutes.
57    #[schemars(with = "u64", range(min = 60, max = 86400))]
58    #[serde(
59        default = "default_token_ttl",
60        skip_serializing_if = "is_default_token_ttl"
61    )]
62    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
63    pub access_token_ttl: Duration,
64
65    /// Time-to-live of compatibility access tokens in seconds. Defaults to 5
66    /// minutes.
67    #[schemars(with = "u64", range(min = 60, max = 86400))]
68    #[serde(
69        default = "default_token_ttl",
70        skip_serializing_if = "is_default_token_ttl"
71    )]
72    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
73    pub compat_token_ttl: Duration,
74
75    /// Experimetal feature to automatically expire inactive sessions
76    ///
77    /// Disabled by default
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub inactive_session_expiration: Option<InactiveSessionExpirationConfig>,
80
81    /// Experimental feature to show a plan management tab and iframe.
82    /// This value is passed through "as is" to the client without any
83    /// validation.
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub plan_management_iframe_uri: Option<String>,
86
87    /// Experimental feature to limit the number of application sessions per
88    /// user.
89    ///
90    /// Disabled by default.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub session_limit: Option<SessionLimitConfig>,
93}
94
95impl Default for ExperimentalConfig {
96    fn default() -> Self {
97        Self {
98            access_token_ttl: default_token_ttl(),
99            compat_token_ttl: default_token_ttl(),
100            inactive_session_expiration: None,
101            plan_management_iframe_uri: None,
102            session_limit: None,
103        }
104    }
105}
106
107impl ExperimentalConfig {
108    pub(crate) fn is_default(&self) -> bool {
109        is_default_token_ttl(&self.access_token_ttl)
110            && is_default_token_ttl(&self.compat_token_ttl)
111            && self.inactive_session_expiration.is_none()
112            && self.plan_management_iframe_uri.is_none()
113            && self.session_limit.is_none()
114    }
115}
116
117impl ConfigurationSection for ExperimentalConfig {
118    const PATH: Option<&'static str> = Some("experimental");
119}
120
121/// Configuration options for the session limit feature
122#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
123pub struct SessionLimitConfig {
124    pub soft_limit: NonZeroU64,
125    pub hard_limit: NonZeroU64,
126}