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