Skip to main content

mas_config/sections/
oauth.rs

1// Copyright 2025 New Vector Ltd.
2//
3// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
4// Please see LICENSE files in the repository root for full details.
5
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9use crate::ConfigurationSection;
10
11const fn default_true() -> bool {
12    true
13}
14
15#[allow(clippy::trivially_copy_pass_by_ref)]
16const fn is_default_true(value: &bool) -> bool {
17    *value == default_true()
18}
19
20/// Configuration section for OAuth 2.0 protocol options
21#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
22pub struct OAuthConfig {
23    /// Whether the Device Authorization Grant (RFC 8628) is enabled. Defaults
24    /// to `true`.
25    ///
26    /// When disabled, the device authorization endpoint will reject requests,
27    /// the discovery metadata will not advertise the device authorization
28    /// endpoint, and dynamic client registrations requesting the
29    /// `urn:ietf:params:oauth:grant-type:device_code` grant type will be
30    /// rejected.
31    #[serde(default = "default_true", skip_serializing_if = "is_default_true")]
32    pub device_code_grant_enabled: bool,
33}
34
35impl Default for OAuthConfig {
36    fn default() -> Self {
37        Self {
38            device_code_grant_enabled: default_true(),
39        }
40    }
41}
42
43impl OAuthConfig {
44    /// Returns true if the configuration is the default one
45    pub(crate) fn is_default(&self) -> bool {
46        is_default_true(&self.device_code_grant_enabled)
47    }
48}
49
50impl ConfigurationSection for OAuthConfig {
51    const PATH: Option<&'static str> = Some("oauth");
52}