Skip to main content

tuwunel_service/federation/
scheme.rs

1//! Adapters that build the `Input` for ruma's [`AuthScheme`] and
2//! [`PathBuilder`] traits from tuwunel's federation context.
3//!
4//! Federation endpoints span several auth/path-builder combinations
5//! (`ServerSignatures` + `VersionHistory`, `ServerSignatures` + `SinglePath`,
6//! and a handful of `NoAuthentication`/`NoAccessToken` variants). The
7//! generic-associated-type `Input<'a>` of each ruma trait varies per impl, so
8//! a single bound on `OutgoingRequest` cannot supply the right value uniformly.
9//! [`FedAuth`] and [`FedPath`] each accept tuwunel's federation context and
10//! return the appropriate `Input` for the concrete auth scheme or path builder
11//! at the call site.
12
13use std::borrow::Cow;
14
15use ruma::{
16	OwnedServerName,
17	api::{
18		SupportedVersions,
19		auth_scheme::{AuthScheme, NoAccessToken, NoAuthentication, SendAccessToken},
20		federation::authentication::{ServerSignatures, ServerSignaturesInput},
21		path_builder::{PathBuilder, SinglePath, VersionHistory},
22	},
23	signatures::Ed25519KeyPair,
24};
25
26pub trait FedAuth: AuthScheme {
27	fn input(
28		origin: OwnedServerName,
29		dest: OwnedServerName,
30		keypair: &Ed25519KeyPair,
31	) -> <Self as AuthScheme>::Input<'_>;
32}
33
34impl FedAuth for NoAuthentication {
35	fn input(_: OwnedServerName, _: OwnedServerName, _: &Ed25519KeyPair) {}
36}
37
38impl FedAuth for NoAccessToken {
39	fn input(_: OwnedServerName, _: OwnedServerName, _: &Ed25519KeyPair) -> SendAccessToken<'_> {
40		SendAccessToken::None
41	}
42}
43
44impl FedAuth for ServerSignatures {
45	fn input(
46		origin: OwnedServerName,
47		dest: OwnedServerName,
48		keypair: &Ed25519KeyPair,
49	) -> ServerSignaturesInput<'_> {
50		ServerSignaturesInput::new(origin, dest, keypair)
51	}
52}
53
54pub trait FedPath: PathBuilder {
55	fn input(supported: &SupportedVersions) -> <Self as PathBuilder>::Input<'_>;
56}
57
58impl FedPath for SinglePath {
59	fn input(_: &SupportedVersions) {}
60}
61
62impl FedPath for VersionHistory {
63	fn input(supported: &SupportedVersions) -> Cow<'_, SupportedVersions> {
64		Cow::Borrowed(supported)
65	}
66}