mas_config/sections/
captcha.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 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 schemars::JsonSchema;
8use serde::{Deserialize, Serialize, de::Error};
9
10use crate::ConfigurationSection;
11
12/// Which service should be used for CAPTCHA protection
13#[derive(Clone, Copy, Debug, Deserialize, JsonSchema, Serialize)]
14pub enum CaptchaServiceKind {
15    /// Use Google's reCAPTCHA v2 API
16    #[serde(rename = "recaptcha_v2")]
17    RecaptchaV2,
18
19    /// Use Cloudflare Turnstile
20    #[serde(rename = "cloudflare_turnstile")]
21    CloudflareTurnstile,
22
23    /// Use ``HCaptcha``
24    #[serde(rename = "hcaptcha")]
25    HCaptcha,
26}
27
28/// Configuration section to setup CAPTCHA protection on a few operations
29#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize, Default)]
30pub struct CaptchaConfig {
31    /// Which service should be used for CAPTCHA protection
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub service: Option<CaptchaServiceKind>,
34
35    /// The site key to use
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub site_key: Option<String>,
38
39    /// The secret key to use
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub secret_key: Option<String>,
42}
43
44impl CaptchaConfig {
45    /// Returns true if the configuration is the default one
46    pub(crate) fn is_default(&self) -> bool {
47        self.service.is_none() && self.site_key.is_none() && self.secret_key.is_none()
48    }
49}
50
51impl ConfigurationSection for CaptchaConfig {
52    const PATH: Option<&'static str> = Some("captcha");
53
54    fn validate(&self, figment: &figment::Figment) -> Result<(), figment::Error> {
55        let metadata = figment.find_metadata(Self::PATH.unwrap());
56
57        let error_on_field = |mut error: figment::error::Error, field: &'static str| {
58            error.metadata = metadata.cloned();
59            error.profile = Some(figment::Profile::Default);
60            error.path = vec![Self::PATH.unwrap().to_owned(), field.to_owned()];
61            error
62        };
63
64        let missing_field = |field: &'static str| {
65            error_on_field(figment::error::Error::missing_field(field), field)
66        };
67
68        if let Some(CaptchaServiceKind::RecaptchaV2) = self.service {
69            if self.site_key.is_none() {
70                return Err(missing_field("site_key"));
71            }
72
73            if self.secret_key.is_none() {
74                return Err(missing_field("secret_key"));
75            }
76        }
77
78        Ok(())
79    }
80}