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<'a>(
28		origin: OwnedServerName,
29		dest: OwnedServerName,
30		keypair: &'a Ed25519KeyPair,
31	) -> <Self as AuthScheme>::Input<'a>;
32}
33
34impl FedAuth for NoAuthentication {
35	fn input(_: OwnedServerName, _: OwnedServerName, _: &Ed25519KeyPair) {}
36}
37
38impl FedAuth for NoAccessToken {
39	fn input<'a>(
40		_: OwnedServerName,
41		_: OwnedServerName,
42		_: &'a Ed25519KeyPair,
43	) -> SendAccessToken<'a> {
44		SendAccessToken::None
45	}
46}
47
48impl FedAuth for ServerSignatures {
49	fn input<'a>(
50		origin: OwnedServerName,
51		dest: OwnedServerName,
52		keypair: &'a Ed25519KeyPair,
53	) -> ServerSignaturesInput<'a> {
54		ServerSignaturesInput::new(origin, dest, keypair)
55	}
56}
57
58pub trait FedPath: PathBuilder {
59	fn input<'a>(supported: &'a SupportedVersions) -> <Self as PathBuilder>::Input<'a>;
60}
61
62impl FedPath for SinglePath {
63	fn input(_: &SupportedVersions) {}
64}
65
66impl FedPath for VersionHistory {
67	fn input<'a>(supported: &'a SupportedVersions) -> Cow<'a, SupportedVersions> {
68		Cow::Borrowed(supported)
69	}
70}