add federation endpoints for authenticated media

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-27 02:56:17 +00:00
parent 47f9c69eff
commit 84a8e36120
5 changed files with 101 additions and 14 deletions

26
Cargo.lock generated
View file

@ -2975,7 +2975,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma" name = "ruma"
version = "0.10.1" version = "0.10.1"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"assign", "assign",
"js_int", "js_int",
@ -2997,7 +2997,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-appservice-api" name = "ruma-appservice-api"
version = "0.10.0" version = "0.10.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -3009,7 +3009,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.18.0" version = "0.18.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"as_variant", "as_variant",
"assign", "assign",
@ -3032,7 +3032,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-common" name = "ruma-common"
version = "0.13.0" version = "0.13.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"as_variant", "as_variant",
"base64 0.22.1", "base64 0.22.1",
@ -3062,7 +3062,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events" name = "ruma-events"
version = "0.28.1" version = "0.28.1"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"as_variant", "as_variant",
"indexmap 2.4.0", "indexmap 2.4.0",
@ -3086,7 +3086,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-federation-api" name = "ruma-federation-api"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"bytes", "bytes",
"http", "http",
@ -3104,7 +3104,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-validation" name = "ruma-identifiers-validation"
version = "0.9.5" version = "0.9.5"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"js_int", "js_int",
"thiserror", "thiserror",
@ -3113,7 +3113,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identity-service-api" name = "ruma-identity-service-api"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -3123,7 +3123,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-macros" name = "ruma-macros"
version = "0.13.0" version = "0.13.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"proc-macro-crate", "proc-macro-crate",
@ -3138,7 +3138,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-push-gateway-api" name = "ruma-push-gateway-api"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-common", "ruma-common",
@ -3150,7 +3150,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-server-util" name = "ruma-server-util"
version = "0.3.0" version = "0.3.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"headers", "headers",
"http", "http",
@ -3163,7 +3163,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-signatures" name = "ruma-signatures"
version = "0.15.0" version = "0.15.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"ed25519-dalek", "ed25519-dalek",
@ -3179,7 +3179,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-state-res" name = "ruma-state-res"
version = "0.11.0" version = "0.11.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=a0cc9a80dd5da700fb9b992b6f92cb6be4c27487#a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" source = "git+https://github.com/girlbossceo/ruwuma?rev=89cee2d2d9f3776cb0a94fcea04fca456ed8a064#89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
dependencies = [ dependencies = [
"itertools 0.12.1", "itertools 0.12.1",
"js_int", "js_int",

View file

@ -314,7 +314,7 @@ version = "0.1.2"
[workspace.dependencies.ruma] [workspace.dependencies.ruma]
git = "https://github.com/girlbossceo/ruwuma" git = "https://github.com/girlbossceo/ruwuma"
#branch = "conduwuit-changes" #branch = "conduwuit-changes"
rev = "a0cc9a80dd5da700fb9b992b6f92cb6be4c27487" rev = "89cee2d2d9f3776cb0a94fcea04fca456ed8a064"
features = [ features = [
"compat", "compat",
"rand", "rand",

View file

@ -230,6 +230,8 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(server::get_openid_userinfo_route) .ruma_route(server::get_openid_userinfo_route)
.ruma_route(server::get_hierarchy_route) .ruma_route(server::get_hierarchy_route)
.ruma_route(server::well_known_server) .ruma_route(server::well_known_server)
.ruma_route(server::get_content_route)
.ruma_route(server::get_content_thumbnail_route)
.route("/_conduwuit/local_user_count", get(client::conduwuit_local_user_count)) .route("/_conduwuit/local_user_count", get(client::conduwuit_local_user_count))
} else { } else {
router router

83
src/api/server/media.rs Normal file
View file

@ -0,0 +1,83 @@
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduit::{utils::content_disposition::make_content_disposition, Err, Result};
use conduit_service::media::{Dim, FileMeta};
use ruma::{
api::federation::authenticated_media::{
get_content, get_content_thumbnail, Content, ContentMetadata, FileOrLocation,
},
Mxc,
};
use crate::Ruma;
/// # `GET /_matrix/federation/v1/media/download/{mediaId}`
///
/// Load media from our server.
#[tracing::instrument(skip_all, fields(%client), name = "media_get")]
pub(crate) async fn get_content_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_content::v1::Request>,
) -> Result<get_content::v1::Response> {
let mxc = Mxc {
server_name: services.globals.server_name(),
media_id: &body.media_id,
};
let Some(FileMeta {
content,
content_type,
content_disposition,
}) = services.media.get(&mxc).await?
else {
return Err!(Request(NotFound("Media not found.")));
};
let content_disposition = make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
let content = Content {
file: content.expect("entire file contents"),
content_type: content_type.map(Into::into),
content_disposition: Some(content_disposition),
};
Ok(get_content::v1::Response {
content: FileOrLocation::File(content),
metadata: ContentMetadata::new(),
})
}
/// # `GET /_matrix/federation/v1/media/thumbnail/{mediaId}`
///
/// Load media thumbnail from our server.
#[tracing::instrument(skip_all, fields(%client), name = "media_thumbnail_get")]
pub(crate) async fn get_content_thumbnail_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_content_thumbnail::v1::Request>,
) -> Result<get_content_thumbnail::v1::Response> {
let dim = Dim::from_ruma(body.width, body.height, body.method.clone())?;
let mxc = Mxc {
server_name: services.globals.server_name(),
media_id: &body.media_id,
};
let Some(FileMeta {
content,
content_type,
content_disposition,
}) = services.media.get_thumbnail(&mxc, &dim).await?
else {
return Err!(Request(NotFound("Media not found.")));
};
let content_disposition = make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
let content = Content {
file: content.expect("entire file contents"),
content_type: content_type.map(Into::into),
content_disposition: Some(content_disposition),
};
Ok(get_content_thumbnail::v1::Response {
content: FileOrLocation::File(content),
metadata: ContentMetadata::new(),
})
}

View file

@ -7,6 +7,7 @@ pub(super) mod invite;
pub(super) mod key; pub(super) mod key;
pub(super) mod make_join; pub(super) mod make_join;
pub(super) mod make_leave; pub(super) mod make_leave;
pub(super) mod media;
pub(super) mod openid; pub(super) mod openid;
pub(super) mod publicrooms; pub(super) mod publicrooms;
pub(super) mod query; pub(super) mod query;
@ -28,6 +29,7 @@ pub(super) use invite::*;
pub(super) use key::*; pub(super) use key::*;
pub(super) use make_join::*; pub(super) use make_join::*;
pub(super) use make_leave::*; pub(super) use make_leave::*;
pub(super) use media::*;
pub(super) use openid::*; pub(super) use openid::*;
pub(super) use publicrooms::*; pub(super) use publicrooms::*;
pub(super) use query::*; pub(super) use query::*;