mas_config/sections/
matrix.rs

1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2022-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 rand::{
8    Rng,
9    distributions::{Alphanumeric, DistString},
10};
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use serde_with::serde_as;
14use url::Url;
15
16use super::ConfigurationSection;
17
18fn default_homeserver() -> String {
19    "localhost:8008".to_owned()
20}
21
22fn default_endpoint() -> Url {
23    Url::parse("http://localhost:8008/").unwrap()
24}
25
26/// The kind of homeserver it is.
27#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
28#[serde(rename_all = "snake_case")]
29pub enum HomeserverKind {
30    /// Homeserver is Synapse, using the legacy API
31    ///
32    /// This will switch to using the modern API in a few releases.
33    #[default]
34    Synapse,
35
36    /// Homeserver is Synapse, using the legacy API, in read-only mode
37    ///
38    /// This is meant for testing rolling out Matrix Authentication Service with
39    /// no risk of writing data to the homeserver.
40    SynapseReadOnly,
41
42    /// Homeserver is Synapse, using the legacy API,
43    SynapseLegacy,
44
45    /// Homeserver is Synapse, with the modern API available
46    SynapseModern,
47}
48
49/// Configuration related to the Matrix homeserver
50#[serde_as]
51#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
52pub struct MatrixConfig {
53    /// The kind of homeserver it is.
54    #[serde(default)]
55    pub kind: HomeserverKind,
56
57    /// The server name of the homeserver.
58    #[serde(default = "default_homeserver")]
59    pub homeserver: String,
60
61    /// Shared secret to use for calls to the admin API
62    pub secret: String,
63
64    /// The base URL of the homeserver's client API
65    #[serde(default = "default_endpoint")]
66    pub endpoint: Url,
67}
68
69impl ConfigurationSection for MatrixConfig {
70    const PATH: Option<&'static str> = Some("matrix");
71}
72
73impl MatrixConfig {
74    pub(crate) fn generate<R>(mut rng: R) -> Self
75    where
76        R: Rng + Send,
77    {
78        Self {
79            kind: HomeserverKind::default(),
80            homeserver: default_homeserver(),
81            secret: Alphanumeric.sample_string(&mut rng, 32),
82            endpoint: default_endpoint(),
83        }
84    }
85
86    pub(crate) fn test() -> Self {
87        Self {
88            kind: HomeserverKind::default(),
89            homeserver: default_homeserver(),
90            secret: "test".to_owned(),
91            endpoint: default_endpoint(),
92        }
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use figment::{
99        Figment, Jail,
100        providers::{Format, Yaml},
101    };
102
103    use super::*;
104
105    #[test]
106    fn load_config() {
107        Jail::expect_with(|jail| {
108            jail.create_file(
109                "config.yaml",
110                r"
111                    matrix:
112                      homeserver: matrix.org
113                      secret: test
114                ",
115            )?;
116
117            let config = Figment::new()
118                .merge(Yaml::file("config.yaml"))
119                .extract_inner::<MatrixConfig>("matrix")?;
120
121            assert_eq!(&config.homeserver, "matrix.org");
122            assert_eq!(&config.secret, "test");
123
124            Ok(())
125        });
126    }
127}