From cb73ae3732d433cba44fd678eaf0b972ae44f9c0 Mon Sep 17 00:00:00 2001 From: strawberry Date: Wed, 22 May 2024 20:57:34 -0400 Subject: [PATCH] add registration token validity endpoint as per matrix 1.2 Signed-off-by: strawberry --- src/api/client_server/account.rs | 23 ++++++++++++++++++++++- src/api/router.rs | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 14c8fead..a786a836 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -4,7 +4,7 @@ use register::RegistrationKind; use ruma::{ api::client::{ account::{ - change_password, deactivate, get_3pids, get_username_availability, + change_password, check_registration_token_validity, deactivate, get_3pids, get_username_availability, register::{self, LoginType}, request_3pid_management_token_via_email, request_3pid_management_token_via_msisdn, whoami, ThirdPartyIdRemovalStatus, @@ -594,3 +594,24 @@ pub(crate) async fn request_3pid_management_token_via_msisdn_route( "Third party identifier is not allowed", )) } + +/// # `GET /_matrix/client/v1/register/m.login.registration_token/validity` +/// +/// Checks if the provided registration token is valid at the time of checking +/// +/// Currently does not have any ratelimiting, and this isn't very practical as +/// there is only one registration token allowed. +pub(crate) async fn check_registration_token_validity( + body: Ruma, +) -> Result { + let Some(reg_token) = services().globals.config.registration_token.clone() else { + return Err(Error::BadRequest( + ErrorKind::forbidden(), + "Server does not allow token registration.", + )); + }; + + Ok(check_registration_token_validity::v1::Response { + valid: reg_token == body.token, + }) +} diff --git a/src/api/router.rs b/src/api/router.rs index 068dc375..3ad88819 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -28,6 +28,7 @@ pub fn build(router: Router, server: &Server) -> Router { .ruma_route(client_server::third_party_route) .ruma_route(client_server::request_3pid_management_token_via_email_route) .ruma_route(client_server::request_3pid_management_token_via_msisdn_route) + .ruma_route(client_server::check_registration_token_validity) .ruma_route(client_server::get_capabilities_route) .ruma_route(client_server::get_pushrules_all_route) .ruma_route(client_server::set_pushrule_route)