1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-31 09:24:31 +03:00

graphql: Expose CAPTCHA config and whether password registration is enabled

This commit is contained in:
Olivier 'reivilibre
2024-07-26 12:14:21 +01:00
committed by reivilibre
parent 4a275fa4b9
commit 8737d6f89b
4 changed files with 161 additions and 1 deletions

View File

@ -14,15 +14,19 @@
#![allow(clippy::str_to_string)] // ComplexObject macro uses &str.to_string()
use async_graphql::{ComplexObject, SimpleObject, ID};
use async_graphql::{ComplexObject, Enum, SimpleObject, ID};
use url::Url;
pub const SITE_CONFIG_ID: &str = "site_config";
pub const CAPTCHA_CONFIG_ID: &str = "captcha_config";
#[derive(SimpleObject)]
#[graphql(complex)]
#[allow(clippy::struct_excessive_bools)]
pub struct SiteConfig {
/// The configuration of CAPTCHA provider.
captcha_config: Option<CaptchaConfig>,
/// The server name of the homeserver.
server_name: String,
@ -47,12 +51,33 @@ pub struct SiteConfig {
/// Whether passwords are enabled and users can change their own passwords.
password_change_allowed: bool,
/// Whether passwords are enabled and users can register using a password.
password_registration_enabled: bool,
/// Minimum password complexity, from 0 to 4, in terms of a zxcvbn score.
/// The exact scorer (including dictionaries and other data tables)
/// in use is <https://crates.io/crates/zxcvbn>.
minimum_password_complexity: u8,
}
#[derive(SimpleObject)]
#[graphql(complex)]
pub struct CaptchaConfig {
/// Which Captcha service is being used
pub service: CaptchaService,
/// The site key used by the instance
pub site_key: String,
}
/// Which Captcha service is being used
#[derive(Enum, Debug, Clone, Copy, PartialEq, Eq)]
pub enum CaptchaService {
RecaptchaV2,
CloudflareTurnstile,
HCaptcha,
}
#[ComplexObject]
impl SiteConfig {
/// The ID of the site configuration.
@ -66,6 +91,7 @@ impl SiteConfig {
/// [`mas_data_model:::SiteConfig`].
pub fn new(data_model: &mas_data_model::SiteConfig) -> Self {
Self {
captcha_config: data_model.captcha.as_ref().map(CaptchaConfig::new),
server_name: data_model.server_name.clone(),
policy_uri: data_model.policy_uri.clone(),
tos_uri: data_model.tos_uri.clone(),
@ -74,7 +100,32 @@ impl SiteConfig {
display_name_change_allowed: data_model.displayname_change_allowed,
password_login_enabled: data_model.password_login_enabled,
password_change_allowed: data_model.password_change_allowed,
password_registration_enabled: data_model.password_registration_enabled,
minimum_password_complexity: data_model.minimum_password_complexity,
}
}
}
#[ComplexObject]
impl CaptchaConfig {
pub async fn id(&self) -> ID {
CAPTCHA_CONFIG_ID.into()
}
}
impl CaptchaConfig {
/// Create a new [`CaptchaConfig`] from the data model
/// [`mas_data_model:::CaptchaConfig`].
pub fn new(data_model: &mas_data_model::CaptchaConfig) -> Self {
Self {
service: match data_model.service {
mas_data_model::CaptchaService::RecaptchaV2 => CaptchaService::RecaptchaV2,
mas_data_model::CaptchaService::CloudflareTurnstile => {
CaptchaService::CloudflareTurnstile
}
mas_data_model::CaptchaService::HCaptcha => CaptchaService::HCaptcha,
},
site_key: data_model.site_key.clone(),
}
}
}