ruma_federation_api/discovery/
get_remote_server_keys_batch.rs

1//! `POST /_matrix/key/*/query`
2//!
3//! Query for keys from multiple servers in a batch format. The receiving (notary) server must sign
4//! the keys returned by the queried servers.
5
6pub mod v2 {
7    //! `/v2/` ([spec])
8    //!
9    //! [spec]: https://spec.matrix.org/latest/server-server-api/#post_matrixkeyv2query
10
11    use std::collections::BTreeMap;
12
13    use ruma_common::{
14        api::{auth_scheme::NoAuthentication, request, response},
15        metadata,
16        serde::Raw,
17        MilliSecondsSinceUnixEpoch, OwnedServerName, OwnedServerSigningKeyId,
18    };
19    use serde::{Deserialize, Serialize};
20
21    use crate::discovery::ServerSigningKeys;
22
23    metadata! {
24        method: POST,
25        rate_limited: false,
26        authentication: NoAuthentication,
27        path: "/_matrix/key/v2/query",
28    }
29
30    /// Request type for the `get_remote_server_keys_batch` endpoint.
31    #[request]
32    pub struct Request {
33        /// The query criteria.
34        ///
35        /// The outer string key on the object is the server name (eg: matrix.org). The inner
36        /// string key is the Key ID to query for the particular server. If no key IDs are given to
37        /// be queried, the notary server should query for all keys. If no servers are given, the
38        /// notary server must return an empty server_keys array in the response.
39        ///
40        /// The notary server may return multiple keys regardless of the Key IDs given.
41        pub server_keys:
42            BTreeMap<OwnedServerName, BTreeMap<OwnedServerSigningKeyId, QueryCriteria>>,
43    }
44
45    /// Response type for the `get_remote_server_keys_batch` endpoint.
46    #[response]
47    pub struct Response {
48        /// The queried server's keys, signed by the notary server.
49        pub server_keys: Vec<Raw<ServerSigningKeys>>,
50    }
51
52    impl Request {
53        /// Creates a new `Request` with the given query criteria.
54        pub fn new(
55            server_keys: BTreeMap<
56                OwnedServerName,
57                BTreeMap<OwnedServerSigningKeyId, QueryCriteria>,
58            >,
59        ) -> Self {
60            Self { server_keys }
61        }
62    }
63
64    impl Response {
65        /// Creates a new `Response` with the given keys.
66        pub fn new(server_keys: Vec<Raw<ServerSigningKeys>>) -> Self {
67            Self { server_keys }
68        }
69    }
70
71    /// The query criteria.
72    #[derive(Clone, Debug, Default, Deserialize, Serialize)]
73    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
74    pub struct QueryCriteria {
75        /// A millisecond POSIX timestamp in milliseconds indicating when the
76        /// returned certificates will need to be valid until to be useful to the
77        /// requesting server.
78        ///
79        /// If not supplied, the current time as determined by the notary server is
80        /// used.
81        // This doesn't use `serde(default)` because the default would then be
82        // determined by the client rather than the server (and it would take more
83        // bandwidth because `skip_serializing_if` couldn't be used).
84        #[serde(skip_serializing_if = "Option::is_none")]
85        pub minimum_valid_until_ts: Option<MilliSecondsSinceUnixEpoch>,
86    }
87
88    impl QueryCriteria {
89        /// Creates empty `QueryCriteria`.
90        pub fn new() -> Self {
91            Default::default()
92        }
93    }
94}