Skip to main content

ruma_federation_api/rtc/livekit/
get_token.rs

1//! `POST /_matrix/federation/*/rtc/livekit/get_token`
2//!
3//! Get a token to authenticate with a LiveKit SFU of the receiving server for a MatrixRTC slot.
4
5pub mod msc4195 {
6    //! `MSC4195` ([MSC])
7    //!
8    //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4195
9
10    use ruma_common::{
11        OwnedRoomId, OwnedUserId,
12        api::{request, response},
13        metadata,
14    };
15
16    use crate::authentication::ServerSignatures;
17
18    metadata! {
19        method: POST,
20        rate_limited: false,
21        authentication: ServerSignatures,
22        path: "/_matrix/federation/unstable/io.element.msc4195/rtc/livekit/get_token",
23    }
24
25    /// Request type for the `get_token` endpoint.
26    #[request]
27    pub struct Request {
28        /// The Matrix ID of the user requesting the token.
29        ///
30        /// Must belong to the origin server.
31        pub user_id: OwnedUserId,
32
33        /// The WebSocket URL of the LiveKit SFU.
34        pub url: String,
35
36        /// The room where the `m.rtc.member` event is present.
37        pub room_id: OwnedRoomId,
38
39        /// The slot ID from the `m.rtc.member` event.
40        pub slot_id: String,
41
42        /// The `member.id` property of the requesting user's own `m.rtc.member` event.
43        pub member_id: String,
44    }
45
46    impl Request {
47        /// Creates a new `Request` with the given user ID, SFU URL, room ID, slot ID and member
48        /// ID.
49        pub fn new(
50            user_id: OwnedUserId,
51            url: String,
52            room_id: OwnedRoomId,
53            slot_id: String,
54            member_id: String,
55        ) -> Self {
56            Self { user_id, url, room_id, slot_id, member_id }
57        }
58    }
59
60    /// Response type for the `get_token` endpoint.
61    #[response]
62    pub struct Response {
63        /// The JWT token to use for authentication with the SFU.
64        pub jwt: String,
65    }
66
67    impl Response {
68        /// Creates a new `Response` with the given JWT token. The corresponding SFU url is already
69        /// known via the request params.
70        pub fn new(jwt: String) -> Self {
71            Self { jwt }
72        }
73    }
74
75    #[cfg(all(test, feature = "server"))]
76    mod server_tests {
77        use ruma_common::api::IncomingRequestExt as _;
78
79        use super::Request;
80
81        #[test]
82        fn deserialize_request() {
83            let request = http::Request::builder()
84                .method("POST")
85                .uri("/_matrix/federation/unstable/io.element.msc4195/rtc/livekit/get_token")
86                .body(
87                    br#"{
88                        "user_id": "@alice:example.org",
89                        "url": "wss://livekit.example.org",
90                        "room_id": "!tDLCaLXijNtYcJZEey:example.org",
91                        "slot_id": "the_id",
92                        "member_id": "xyzABCDEF10123"
93                    }"# as &[u8],
94                )
95                .unwrap();
96
97            let request = Request::try_from_http_request(request, &[] as &[&str]).unwrap();
98
99            assert_eq!(request.user_id, "@alice:example.org");
100            assert_eq!(request.url, "wss://livekit.example.org");
101            assert_eq!(request.room_id, "!tDLCaLXijNtYcJZEey:example.org");
102            assert_eq!(request.slot_id, "the_id");
103            assert_eq!(request.member_id, "xyzABCDEF10123");
104        }
105    }
106}