sdk: Replace hyper with axum for SSO login

hyper::Server was dropped in hyper 1, without a replacement.
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2024-04-27 17:12:10 +02:00
parent dddc607e07
commit d2bb17acd5
3 changed files with 207 additions and 66 deletions

232
Cargo.lock generated
View File

@@ -426,13 +426,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf"
dependencies = [
"async-trait",
"axum-core",
"axum-core 0.3.4",
"bitflags 1.3.2",
"bytes",
"futures-util",
"http",
"http-body",
"hyper",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"itoa",
"matchit",
"memchr",
@@ -441,12 +441,46 @@ dependencies = [
"pin-project-lite",
"rustversion",
"serde",
"sync_wrapper",
"sync_wrapper 0.1.2",
"tower",
"tower-layer",
"tower-service",
]
[[package]]
name = "axum"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf"
dependencies = [
"async-trait",
"axum-core 0.4.3",
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.0",
"http-body-util",
"hyper 1.3.1",
"hyper-util",
"itoa",
"matchit",
"memchr",
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper 1.0.1",
"tokio",
"tower",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "axum-core"
version = "0.3.4"
@@ -456,14 +490,35 @@ dependencies = [
"async-trait",
"bytes",
"futures-util",
"http",
"http-body",
"http 0.2.11",
"http-body 0.4.6",
"mime",
"rustversion",
"tower-layer",
"tower-service",
]
[[package]]
name = "axum-core"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3"
dependencies = [
"async-trait",
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.0",
"http-body-util",
"mime",
"pin-project-lite",
"rustversion",
"sync_wrapper 0.1.2",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "backoff"
version = "0.4.0"
@@ -1669,8 +1724,8 @@ dependencies = [
"anyhow",
"dirs",
"futures-util",
"http",
"hyper",
"http 0.2.11",
"hyper 0.14.28",
"matrix-sdk",
"matrix-sdk-ui",
"rand 0.8.5",
@@ -2187,7 +2242,7 @@ dependencies = [
"futures-core",
"futures-sink",
"futures-util",
"http",
"http 0.2.11",
"indexmap 2.2.2",
"slab",
"tokio",
@@ -2239,7 +2294,7 @@ dependencies = [
"base64 0.21.7",
"bytes",
"headers-core",
"http",
"http 0.2.11",
"httpdate",
"mime",
"sha1",
@@ -2251,7 +2306,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429"
dependencies = [
"http",
"http 0.2.11",
]
[[package]]
@@ -2321,6 +2376,17 @@ dependencies = [
"itoa",
]
[[package]]
name = "http"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258"
dependencies = [
"bytes",
"fnv",
"itoa",
]
[[package]]
name = "http-body"
version = "0.4.6"
@@ -2328,7 +2394,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
dependencies = [
"bytes",
"http",
"http 0.2.11",
"pin-project-lite",
]
[[package]]
name = "http-body"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643"
dependencies = [
"bytes",
"http 1.1.0",
]
[[package]]
name = "http-body-util"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d"
dependencies = [
"bytes",
"futures-core",
"http 1.1.0",
"http-body 1.0.0",
"pin-project-lite",
]
@@ -2348,7 +2437,7 @@ dependencies = [
"async-channel 1.9.0",
"base64 0.13.1",
"futures-lite",
"http",
"http 0.2.11",
"infer",
"pin-project-lite",
"rand 0.7.3",
@@ -2382,8 +2471,8 @@ dependencies = [
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"http 0.2.11",
"http-body 0.4.6",
"httparse",
"httpdate",
"itoa",
@@ -2395,6 +2484,25 @@ dependencies = [
"want",
]
[[package]]
name = "hyper"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d"
dependencies = [
"bytes",
"futures-channel",
"futures-util",
"http 1.1.0",
"http-body 1.0.0",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"smallvec",
"tokio",
]
[[package]]
name = "hyper-rustls"
version = "0.24.2"
@@ -2402,8 +2510,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590"
dependencies = [
"futures-util",
"http",
"hyper",
"http 0.2.11",
"hyper 0.14.28",
"rustls",
"tokio",
"tokio-rustls",
@@ -2415,7 +2523,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1"
dependencies = [
"hyper",
"hyper 0.14.28",
"pin-project-lite",
"tokio",
"tokio-io-timeout",
@@ -2428,12 +2536,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"hyper 0.14.28",
"native-tls",
"tokio",
"tokio-native-tls",
]
[[package]]
name = "hyper-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa"
dependencies = [
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.0",
"hyper 1.3.1",
"pin-project-lite",
"socket2",
"tokio",
]
[[package]]
name = "iana-time-zone"
version = "0.1.60"
@@ -2996,9 +3120,9 @@ dependencies = [
"bytes",
"futures-util",
"headers",
"http",
"http-body",
"hyper",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"opentelemetry",
"serde",
"serde_json",
@@ -3060,7 +3184,7 @@ dependencies = [
"form_urlencoded",
"futures-util",
"headers",
"http",
"http 0.2.11",
"language-tags",
"mas-http",
"mas-iana",
@@ -3130,6 +3254,7 @@ dependencies = [
"async-channel 2.1.1",
"async-stream",
"async-trait",
"axum 0.7.5",
"backoff",
"bytes",
"bytesize",
@@ -3145,8 +3270,7 @@ dependencies = [
"futures-executor",
"futures-util",
"gloo-timers",
"http",
"hyper",
"http 0.2.11",
"image",
"imbl",
"indexmap 2.2.2",
@@ -3201,7 +3325,7 @@ dependencies = [
"eyeball-im",
"futures-executor",
"futures-util",
"http",
"http 0.2.11",
"matrix-sdk-common",
"matrix-sdk-crypto",
"matrix-sdk-store-encryption",
@@ -3266,7 +3390,7 @@ dependencies = [
"futures-util",
"hkdf",
"hmac",
"http",
"http 0.2.11",
"indoc",
"itertools 0.12.1",
"js_option",
@@ -3303,7 +3427,7 @@ dependencies = [
"assert_matches2",
"futures-util",
"hmac",
"http",
"http 0.2.11",
"js_int",
"matrix-sdk-common",
"matrix-sdk-crypto",
@@ -3410,7 +3534,7 @@ dependencies = [
"futures",
"futures-core",
"futures-util",
"http",
"http 0.2.11",
"json-structural-diff",
"matrix-sdk",
"matrix-sdk-test",
@@ -3491,7 +3615,7 @@ version = "0.7.0"
dependencies = [
"ctor",
"getrandom 0.2.12",
"http",
"http 0.2.11",
"matrix-sdk-test-macros",
"once_cell",
"ruma",
@@ -3844,7 +3968,7 @@ source = "git+https://github.com/matrix-org/matrix-authentication-service?rev=09
dependencies = [
"chrono",
"data-encoding",
"http",
"http 0.2.11",
"language-tags",
"mas-iana",
"mas-jose",
@@ -3980,7 +4104,7 @@ checksum = "7cbfa5308166ca861434f0b0913569579b8e587430a3d6bcd7fd671921ec145a"
dependencies = [
"async-trait",
"bytes",
"http",
"http 0.2.11",
"opentelemetry",
"reqwest",
]
@@ -3993,7 +4117,7 @@ checksum = "1a016b8d9495c639af2145ac22387dcb88e44118e45320d9238fbf4e7889abcb"
dependencies = [
"async-trait",
"futures-core",
"http",
"http 0.2.11",
"opentelemetry",
"opentelemetry-http",
"opentelemetry-proto",
@@ -4876,9 +5000,9 @@ dependencies = [
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"hyper",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"hyper-rustls",
"hyper-tls",
"ipnet",
@@ -5037,7 +5161,7 @@ dependencies = [
"assign",
"bytes",
"date_header",
"http",
"http 0.2.11",
"js_int",
"js_option",
"maplit",
@@ -5060,7 +5184,7 @@ dependencies = [
"bytes",
"form_urlencoded",
"getrandom 0.2.12",
"http",
"http 0.2.11",
"indexmap 2.2.2",
"js-sys",
"js_int",
@@ -5469,6 +5593,16 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_path_to_error"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6"
dependencies = [
"itoa",
"serde",
]
[[package]]
name = "serde_qs"
version = "0.8.5"
@@ -5850,6 +5984,12 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]]
name = "sync_wrapper"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
[[package]]
name = "system-deps"
version = "6.2.2"
@@ -6160,13 +6300,13 @@ checksum = "76c4eb7a4e9ef9d4763600161f12f5070b92a578e1b634db88a6887844c91a13"
dependencies = [
"async-stream",
"async-trait",
"axum",
"axum 0.6.20",
"base64 0.21.7",
"bytes",
"h2",
"http",
"http-body",
"hyper",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"hyper-timeout",
"percent-encoding",
"pin-project",
@@ -6209,8 +6349,8 @@ dependencies = [
"bytes",
"futures-core",
"futures-util",
"http",
"http-body",
"http 0.2.11",
"http-body 0.4.6",
"http-range-header",
"pin-project-lite",
"tower-layer",
@@ -7084,7 +7224,7 @@ dependencies = [
"futures",
"futures-timer",
"http-types",
"hyper",
"hyper 0.14.28",
"log",
"once_cell",
"regex",

View File

@@ -37,7 +37,7 @@ markdown = ["ruma/markdown"]
native-tls = ["reqwest/native-tls"]
rustls-tls = ["reqwest/rustls-tls"]
socks = ["reqwest/socks"]
sso-login = ["dep:hyper", "dep:rand", "dep:tower"]
sso-login = ["dep:axum", "dep:rand", "dep:tower"]
image-proc = ["dep:image"]
image-rayon = ["image-proc", "image?/rayon"]
@@ -70,6 +70,7 @@ as_variant = { workspace = true }
async-channel = "2.1.0"
async-stream = { workspace = true }
async-trait = { workspace = true }
axum = { version = "0.7.5", optional = true }
bytes = "1.1.0"
bytesize = "1.1"
cfg-vis = "0.3.0"
@@ -82,7 +83,6 @@ eyre = { version = "0.6.8", optional = true }
futures-core = { workspace = true }
futures-util = { workspace = true }
http = { workspace = true }
hyper = { version = "0.14.20", features = ["http1", "http2", "server"], optional = true }
imbl = { workspace = true, features = ["serde"] }
indexmap = "2.0.2"
js_int = "0.2.2"

View File

@@ -324,11 +324,15 @@ where
sync::{Arc, Mutex},
};
use http::{Method, StatusCode};
use hyper::{server::conn::AddrIncoming, service::service_fn};
use axum::{
http::{self, Method, StatusCode},
response::IntoResponse,
routing::any_service,
};
use rand::{thread_rng, Rng};
use serde::Deserialize;
use tokio::{net::TcpListener, sync::oneshot};
use tower::service_fn;
use tracing::debug;
use url::Url;
@@ -381,7 +385,7 @@ where
data_tx.send(query.login_token).unwrap();
}
Ok(http::Response::new(response.clone()))
Ok(response.clone())
};
let listener = {
@@ -411,24 +415,21 @@ where
}
};
let incoming = AddrIncoming::from_listener(listener).unwrap();
let server = hyper::Server::builder(incoming)
.serve(tower::make::Shared::new(service_fn(move |request| {
let handle_request = handle_request.clone();
async move {
match handle_request(request) {
Ok(res) => Ok::<_, Infallible>(res.map(hyper::Body::from)),
Err(status_code) => {
let mut res = http::Response::new(hyper::Body::default());
*res.status_mut() = status_code;
Ok(res)
}
}
let router = any_service(service_fn(move |request| {
let handle_request = handle_request.clone();
async move {
match handle_request(request) {
Ok(res) => Ok::<_, Infallible>(res.into_response()),
Err(status_code) => Ok(status_code.into_response()),
}
})))
}
}));
let server = axum::serve(listener, router)
.with_graceful_shutdown(async {
signal_rx.await.ok();
});
})
.into_future();
tokio::spawn(server);