From 1e2bf39a7c0f34884705e25ea8fdec95bbb52816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= <76261501+zecakeh@users.noreply.github.com> Date: Tue, 14 Oct 2025 15:32:32 +0200 Subject: [PATCH] Update Ruma Brings changes to the requests metadata. It was changed from a struct to a trait, and the authentication scheme is now an associated type. This allows to forbid at compile time requests that use an unsupported authentication scheme. --- Cargo.lock | 18 ++++++------- Cargo.toml | 2 +- crates/matrix-sdk/CHANGELOG.md | 4 +++ crates/matrix-sdk/src/client/futures.rs | 2 ++ crates/matrix-sdk/src/client/mod.rs | 4 ++- crates/matrix-sdk/src/error.rs | 4 --- crates/matrix-sdk/src/http_client/mod.rs | 32 ++++++++++++++---------- 7 files changed, 38 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca900f45f..e45bd4b95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4821,7 +4821,7 @@ dependencies = [ [[package]] name = "ruma" version = "0.13.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "assign", "js_int", @@ -4838,7 +4838,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.21.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "as_variant", "assign", @@ -4861,7 +4861,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.16.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "as_variant", "base64", @@ -4894,7 +4894,7 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.31.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "as_variant", "indexmap", @@ -4920,7 +4920,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.12.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "headers", "http", @@ -4940,7 +4940,7 @@ dependencies = [ [[package]] name = "ruma-html" version = "0.5.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "as_variant", "html5ever", @@ -4951,7 +4951,7 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" version = "0.11.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "js_int", "thiserror 2.0.16", @@ -4960,7 +4960,7 @@ dependencies = [ [[package]] name = "ruma-macros" version = "0.16.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "cfg-if", "proc-macro-crate", @@ -4975,7 +4975,7 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.18.0" -source = "git+https://github.com/ruma/ruma?rev=c441eccb92a36467217ff929cd71462fbeeeaf1a#c441eccb92a36467217ff929cd71462fbeeeaf1a" +source = "git+https://github.com/ruma/ruma?rev=670349f66f7e3f403c6730967768472dcf1bcf60#670349f66f7e3f403c6730967768472dcf1bcf60" dependencies = [ "base64", "ed25519-dalek", diff --git a/Cargo.toml b/Cargo.toml index 9967cef71..4b8ca1a52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,7 +66,7 @@ rand = "0.8.5" regex = "1.11.2" reqwest = { version = "0.12.23", default-features = false } rmp-serde = "1.3.0" -ruma = { git = "https://github.com/ruma/ruma", rev = "c441eccb92a36467217ff929cd71462fbeeeaf1a", features = [ +ruma = { git = "https://github.com/ruma/ruma", rev = "670349f66f7e3f403c6730967768472dcf1bcf60", features = [ "client-api-c", "compat-upload-signatures", "compat-arbitrary-length-ids", diff --git a/crates/matrix-sdk/CHANGELOG.md b/crates/matrix-sdk/CHANGELOG.md index 4d933acfa..f54f6ca37 100644 --- a/crates/matrix-sdk/CHANGELOG.md +++ b/crates/matrix-sdk/CHANGELOG.md @@ -17,6 +17,10 @@ All notable changes to this project will be documented in this file. ### Refactor +- [**breaking**]: `Client::send()` has an extra bound where + `Request::Authentication: SupportedAuthScheme`. This allows to drop the + `HttpError::NotClientRequest` error in favor of a compile-time error. + ([#5781](https://github.com/matrix-org/matrix-rust-sdk/pull/5781)) - [**breaking**]: The `waveform` field was moved from `AttachmentInfo::Voice` to `BaseAudioInfo`, allowing to set it for any audio message. Its format also changed, and it is now a list of `f32` between 0 and 1. diff --git a/crates/matrix-sdk/src/client/futures.rs b/crates/matrix-sdk/src/client/futures.rs index 61e93db19..fd4107fd1 100644 --- a/crates/matrix-sdk/src/client/futures.rs +++ b/crates/matrix-sdk/src/client/futures.rs @@ -33,6 +33,7 @@ use crate::{ authentication::oauth::OAuthError, config::RequestConfig, error::{HttpError, HttpResult}, + http_client::SupportedAuthScheme, media::MediaError, }; @@ -77,6 +78,7 @@ impl SendRequest { impl IntoFuture for SendRequest where R: OutgoingRequest + Clone + Debug + SendOutsideWasm + SyncOutsideWasm + 'static, + R::Authentication: SupportedAuthScheme, R::IncomingResponse: SendOutsideWasm + SyncOutsideWasm, HttpError: From>, { diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index f853bd570..e42684dde 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -98,7 +98,7 @@ use crate::{ EventHandler, EventHandlerContext, EventHandlerDropGuard, EventHandlerHandle, EventHandlerStore, ObservableEventHandler, SyncEvent, }, - http_client::HttpClient, + http_client::{HttpClient, SupportedAuthScheme}, latest_events::LatestEvents, media::MediaError, notification_settings::NotificationSettings, @@ -1863,6 +1863,7 @@ impl Client { pub fn send(&self, request: Request) -> SendRequest where Request: OutgoingRequest + Clone + Debug, + Request::Authentication: SupportedAuthScheme, HttpError: From>, { SendRequest { @@ -1881,6 +1882,7 @@ impl Client { ) -> HttpResult where Request: OutgoingRequest + Debug, + Request::Authentication: SupportedAuthScheme, HttpError: From>, { let homeserver = self.homeserver().to_string(); diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index 4f8ce2f85..4522b5b81 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -96,10 +96,6 @@ pub enum HttpError { #[error(transparent)] Reqwest(#[from] ReqwestError), - /// Queried endpoint is not meant for clients. - #[error("the queried endpoint is not meant for clients")] - NotClientRequest, - /// API response error (deserialization, or a Matrix-specific error). // `Box` its inner value to reduce the enum size. #[error(transparent)] diff --git a/crates/matrix-sdk/src/http_client/mod.rs b/crates/matrix-sdk/src/http_client/mod.rs index 64a41d447..557574f8e 100644 --- a/crates/matrix-sdk/src/http_client/mod.rs +++ b/crates/matrix-sdk/src/http_client/mod.rs @@ -28,7 +28,7 @@ use bytesize::ByteSize; use eyeball::SharedObservable; use http::Method; use ruma::api::{ - AuthScheme, OutgoingRequest, SendAccessToken, SupportedVersions, + OutgoingRequest, SendAccessToken, SupportedVersions, auth_scheme, error::{FromHttpResponseError, IntoHttpError}, }; use tokio::sync::{Semaphore, SemaphorePermit}; @@ -151,6 +151,7 @@ impl HttpClient { ) -> Result where R: OutgoingRequest + Debug, + R::Authentication: SupportedAuthScheme, HttpError: From>, { let config = match config { @@ -168,18 +169,6 @@ impl HttpClient { // why we record it here, instead of in the #[instrument] macro. span.record("config", debug(config)).record("request_id", request_id); - let auth_scheme = R::METADATA.authentication; - match auth_scheme { - AuthScheme::AccessToken - | AuthScheme::AccessTokenOptional - | AuthScheme::AppserviceToken - | AuthScheme::AppserviceTokenOptional - | AuthScheme::None => {} - AuthScheme::ServerSignatures => { - return Err(HttpError::NotClientRequest); - } - } - let request = self .serialize_request(request, config, homeserver, access_token, supported_versions) .map_err(HttpError::IntoHttp)?; @@ -254,6 +243,23 @@ async fn response_to_http_response( Ok(http_builder.body(body).expect("Can't construct a response using the given body")) } +/// Marker trait to identify the authentication schemes that the [`HttpClient`] +/// supports. +/// +/// This trait can also be implemented for custom +/// [`AuthScheme`](auth_scheme::AuthScheme)s if necessary. +pub trait SupportedAuthScheme: auth_scheme::AuthScheme {} + +impl SupportedAuthScheme for auth_scheme::NoAuthentication {} + +impl SupportedAuthScheme for auth_scheme::AccessToken {} + +impl SupportedAuthScheme for auth_scheme::AccessTokenOptional {} + +impl SupportedAuthScheme for auth_scheme::AppserviceToken {} + +impl SupportedAuthScheme for auth_scheme::AppserviceTokenOptional {} + #[cfg(all(test, not(target_family = "wasm")))] mod tests { use std::{