examples: Update to oauth2 5 (#3462)

This commit is contained in:
tottoto 2025-09-11 22:34:41 +09:00 committed by GitHub
parent 5d5ba01be9
commit e4550d23b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 90 additions and 233 deletions

285
Cargo.lock generated
View File

@ -314,10 +314,10 @@ dependencies = [
"futures-core",
"futures-sink",
"futures-util",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"http-body-util",
"hyper 1.5.2",
"hyper",
"hyper-util",
"itoa",
"matchit",
@ -328,13 +328,13 @@ dependencies = [
"pin-project-lite",
"quickcheck",
"quickcheck_macros",
"reqwest 0.12.12",
"reqwest",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sha1",
"sync_wrapper 1.0.2",
"sync_wrapper",
"time",
"tokio",
"tokio-stream",
@ -357,14 +357,14 @@ dependencies = [
"axum-macros",
"bytes",
"futures-core",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"http-body-util",
"hyper 1.5.2",
"hyper",
"mime",
"pin-project-lite",
"rustversion",
"sync_wrapper 1.0.2",
"sync_wrapper",
"tokio",
"tower-http 0.6.2",
"tower-layer",
@ -386,16 +386,16 @@ dependencies = [
"futures-core",
"futures-util",
"headers",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"http-body-util",
"hyper 1.5.2",
"hyper",
"mime",
"multer",
"percent-encoding",
"pin-project-lite",
"prost",
"reqwest 0.12.12",
"reqwest",
"rustversion",
"serde",
"serde_html_form",
@ -438,10 +438,10 @@ dependencies = [
"arc-swap",
"bytes",
"futures-util",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"http-body-util",
"hyper 1.5.2",
"hyper",
"hyper-util",
"pin-project-lite",
"rustls 0.21.12",
@ -461,10 +461,10 @@ dependencies = [
"arc-swap",
"bytes",
"futures-util",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"http-body-util",
"hyper 1.5.2",
"hyper",
"hyper-util",
"pin-project-lite",
"rustls 0.23.20",
@ -1357,7 +1357,7 @@ dependencies = [
"axum",
"brotli 6.0.0",
"flate2",
"http 1.2.0",
"http",
"serde_json",
"tokio",
"tower 0.5.2",
@ -1504,7 +1504,7 @@ version = "0.1.0"
dependencies = [
"axum",
"http-body-util",
"hyper 1.5.2",
"hyper",
"tokio",
"tower 0.5.2",
]
@ -1522,7 +1522,7 @@ name = "example-http-proxy"
version = "0.1.0"
dependencies = [
"axum",
"hyper 1.5.2",
"hyper",
"hyper-util",
"tokio",
"tower 0.5.2",
@ -1561,7 +1561,7 @@ name = "example-low-level-native-tls"
version = "0.1.0"
dependencies = [
"axum",
"hyper 1.5.2",
"hyper",
"hyper-util",
"tokio",
"tokio-native-tls",
@ -1575,7 +1575,7 @@ name = "example-low-level-openssl"
version = "0.1.0"
dependencies = [
"axum",
"hyper 1.5.2",
"hyper",
"hyper-util",
"openssl",
"tokio",
@ -1590,7 +1590,7 @@ name = "example-low-level-rustls"
version = "0.1.0"
dependencies = [
"axum",
"hyper 1.5.2",
"hyper",
"hyper-util",
"tokio",
"tokio-rustls 0.26.1",
@ -1631,9 +1631,9 @@ dependencies = [
"async-session",
"axum",
"axum-extra",
"http 1.2.0",
"http",
"oauth2",
"reqwest 0.12.12",
"reqwest",
"serde",
"tokio",
"tracing",
@ -1713,7 +1713,7 @@ name = "example-reqwest-response"
version = "0.1.0"
dependencies = [
"axum",
"reqwest 0.12.12",
"reqwest",
"tokio",
"tokio-stream",
"tower-http 0.6.2",
@ -1726,7 +1726,7 @@ name = "example-reverse-proxy"
version = "0.1.0"
dependencies = [
"axum",
"hyper 1.5.2",
"hyper",
"hyper-util",
"tokio",
]
@ -1744,7 +1744,7 @@ name = "example-serve-with-hyper"
version = "0.1.0"
dependencies = [
"axum",
"hyper 1.5.2",
"hyper",
"hyper-util",
"tokio",
"tower 0.5.2",
@ -1757,7 +1757,7 @@ dependencies = [
"axum",
"axum-extra",
"futures-executor",
"http 1.2.0",
"http",
"tower-service",
]
@ -1781,7 +1781,7 @@ dependencies = [
"eventsource-stream",
"futures-util",
"headers",
"reqwest 0.12.12",
"reqwest",
"reqwest-eventsource",
"tokio",
"tokio-stream",
@ -1944,7 +1944,7 @@ version = "0.1.0"
dependencies = [
"axum",
"http-body-util",
"hyper 1.5.2",
"hyper",
"hyper-util",
"tokio",
"tracing-subscriber",
@ -2226,25 +2226,6 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "h2"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8"
dependencies = [
"bytes",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http 0.2.12",
"indexmap 2.7.0",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "h2"
version = "0.4.7"
@ -2256,7 +2237,7 @@ dependencies = [
"fnv",
"futures-core",
"futures-sink",
"http 1.2.0",
"http",
"indexmap 2.7.0",
"slab",
"tokio",
@ -2314,7 +2295,7 @@ dependencies = [
"base64 0.21.7",
"bytes",
"headers-core",
"http 1.2.0",
"http",
"httpdate",
"mime",
"sha1",
@ -2326,7 +2307,7 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54b4a22553d4242c49fddb9ba998a99962b5cc6f22cb5a3482bec22522403ce4"
dependencies = [
"http 1.2.0",
"http",
]
[[package]]
@ -2440,17 +2421,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "http"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1"
dependencies = [
"bytes",
"fnv",
"itoa",
]
[[package]]
name = "http"
version = "1.2.0"
@ -2462,17 +2432,6 @@ dependencies = [
"itoa",
]
[[package]]
name = "http-body"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
dependencies = [
"bytes",
"http 0.2.12",
"pin-project-lite",
]
[[package]]
name = "http-body"
version = "1.0.1"
@ -2480,7 +2439,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184"
dependencies = [
"bytes",
"http 1.2.0",
"http",
]
[[package]]
@ -2491,8 +2450,8 @@ checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a"
dependencies = [
"bytes",
"futures-core",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"pin-project-lite",
]
@ -2523,30 +2482,6 @@ dependencies = [
"libm",
]
[[package]]
name = "hyper"
version = "0.14.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"h2 0.3.26",
"http 0.2.12",
"http-body 0.4.6",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"socket2",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]]
name = "hyper"
version = "1.5.2"
@ -2556,9 +2491,9 @@ dependencies = [
"bytes",
"futures-channel",
"futures-util",
"h2 0.4.7",
"http 1.2.0",
"http-body 1.0.1",
"h2",
"http",
"http-body",
"httparse",
"httpdate",
"itoa",
@ -2568,20 +2503,6 @@ dependencies = [
"want",
]
[[package]]
name = "hyper-rustls"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590"
dependencies = [
"futures-util",
"http 0.2.12",
"hyper 0.14.32",
"rustls 0.21.12",
"tokio",
"tokio-rustls 0.24.1",
]
[[package]]
name = "hyper-rustls"
version = "0.27.5"
@ -2589,8 +2510,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d191583f3da1305256f22463b9bb0471acad48a4e534a5218b9963e9c1f59b2"
dependencies = [
"futures-util",
"http 1.2.0",
"hyper 1.5.2",
"http",
"hyper",
"hyper-util",
"rustls 0.23.20",
"rustls-pki-types",
@ -2608,7 +2529,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [
"bytes",
"http-body-util",
"hyper 1.5.2",
"hyper",
"hyper-util",
"native-tls",
"tokio",
@ -2625,9 +2546,9 @@ dependencies = [
"bytes",
"futures-channel",
"futures-util",
"http 1.2.0",
"http-body 1.0.1",
"hyper 1.5.2",
"http",
"http-body",
"hyper",
"pin-project-lite",
"socket2",
"tokio",
@ -3238,7 +3159,7 @@ dependencies = [
"bytes",
"encoding_rs",
"futures-util",
"http 1.2.0",
"http",
"httparse",
"memchr",
"mime",
@ -3357,16 +3278,16 @@ dependencies = [
[[package]]
name = "oauth2"
version = "4.4.2"
version = "5.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c38841cdd844847e3e7c8d29cef9dcfed8877f8f56f9071f77843ecf3baf937f"
checksum = "51e219e79014df21a225b1860a479e2dcd7cbd9130f4defd4bd0e191ea31d67d"
dependencies = [
"base64 0.13.1",
"base64 0.22.1",
"chrono",
"getrandom 0.2.15",
"http 0.2.12",
"http",
"rand 0.8.5",
"reqwest 0.11.27",
"reqwest",
"serde",
"serde_json",
"serde_path_to_error",
@ -3967,47 +3888,6 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "reqwest"
version = "0.11.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62"
dependencies = [
"base64 0.21.7",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2 0.3.26",
"http 0.2.12",
"http-body 0.4.6",
"hyper 0.14.32",
"hyper-rustls 0.24.2",
"ipnet",
"js-sys",
"log",
"mime",
"once_cell",
"percent-encoding",
"pin-project-lite",
"rustls 0.21.12",
"rustls-pemfile 1.0.4",
"serde",
"serde_json",
"serde_urlencoded",
"sync_wrapper 0.1.2",
"system-configuration 0.5.1",
"tokio",
"tokio-rustls 0.24.1",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"webpki-roots 0.25.4",
"winreg",
]
[[package]]
name = "reqwest"
version = "0.12.12"
@ -4019,12 +3899,12 @@ dependencies = [
"encoding_rs",
"futures-core",
"futures-util",
"h2 0.4.7",
"http 1.2.0",
"http-body 1.0.1",
"h2",
"http",
"http-body",
"http-body-util",
"hyper 1.5.2",
"hyper-rustls 0.27.5",
"hyper",
"hyper-rustls",
"hyper-tls",
"hyper-util",
"ipnet",
@ -4043,8 +3923,8 @@ dependencies = [
"serde",
"serde_json",
"serde_urlencoded",
"sync_wrapper 1.0.2",
"system-configuration 0.6.1",
"sync_wrapper",
"system-configuration",
"tokio",
"tokio-native-tls",
"tokio-rustls 0.26.1",
@ -4072,7 +3952,7 @@ dependencies = [
"mime",
"nom",
"pin-project-lite",
"reqwest 0.12.12",
"reqwest",
"thiserror 1.0.69",
]
@ -4853,12 +4733,6 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sync_wrapper"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]]
name = "sync_wrapper"
version = "1.0.2"
@ -4879,17 +4753,6 @@ dependencies = [
"syn 2.0.93",
]
[[package]]
name = "system-configuration"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"system-configuration-sys 0.5.0",
]
[[package]]
name = "system-configuration"
version = "0.6.1"
@ -4898,17 +4761,7 @@ checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b"
dependencies = [
"bitflags 2.6.0",
"core-foundation",
"system-configuration-sys 0.6.0",
]
[[package]]
name = "system-configuration-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9"
dependencies = [
"core-foundation-sys",
"libc",
"system-configuration-sys",
]
[[package]]
@ -5261,7 +5114,7 @@ dependencies = [
"indexmap 2.7.0",
"pin-project-lite",
"slab",
"sync_wrapper 1.0.2",
"sync_wrapper",
"tokio",
"tokio-util",
"tower-layer",
@ -5278,8 +5131,8 @@ dependencies = [
"bitflags 2.6.0",
"bytes",
"futures-util",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"http-body-util",
"http-range-header",
"httpdate",
@ -5307,8 +5160,8 @@ dependencies = [
"bytes",
"futures-core",
"futures-util",
"http 1.2.0",
"http-body 1.0.1",
"http",
"http-body",
"http-body-util",
"http-range-header",
"httpdate",
@ -5442,7 +5295,7 @@ checksum = "eadc29d668c91fcc564941132e17b28a7ceb2f3ebf0b9dae3e03fd7a6748eb0d"
dependencies = [
"bytes",
"data-encoding",
"http 1.2.0",
"http",
"httparse",
"log",
"rand 0.9.1",

View File

@ -25,8 +25,6 @@ skip-tree = [
{ name = "spin" },
# lots still pulls in syn 1.x
{ name = "syn" },
# until 1.0 is out we're pulling in both 0.14 and 1.0-rc.x
{ name = "hyper" },
# pulled in by tracing-subscriber
{ name = "regex-syntax" },
# pulled in by tracing-subscriber
@ -36,8 +34,6 @@ skip-tree = [
# hyper-util hasn't upgraded to 0.5 yet, but it's the same service / layer
# crates beneath
{ name = "tower" },
# tower hasn't upgraded to 1.0 yet
{ name = "sync_wrapper" },
# pulled in by quickcheck and cookie
{ name = "rand" },
]

View File

@ -10,7 +10,7 @@ async-session = "3.0.0"
axum = { path = "../../axum" }
axum-extra = { path = "../../axum-extra", features = ["typed-header"] }
http = "1.0.0"
oauth2 = "4.1"
oauth2 = "5"
# Use Rustls because it makes it easier to cross-compile on CI
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "json"] }
serde = { version = "1.0", features = ["derive"] }

View File

@ -20,8 +20,8 @@ use axum::{
use axum_extra::{headers, typed_header::TypedHeaderRejectionReason, TypedHeader};
use http::{header, request::Parts, StatusCode};
use oauth2::{
basic::BasicClient, reqwest::async_http_client, AuthUrl, AuthorizationCode, ClientId,
ClientSecret, CsrfToken, RedirectUrl, Scope, TokenResponse, TokenUrl,
AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EndpointNotSet, EndpointSet,
RedirectUrl, Scope, TokenResponse, TokenUrl,
};
use serde::{Deserialize, Serialize};
use std::{convert::Infallible, env};
@ -30,6 +30,14 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
static COOKIE_NAME: &str = "SESSION";
static CSRF_TOKEN: &str = "csrf_token";
type BasicClient = oauth2::basic::BasicClient<
EndpointSet,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
EndpointSet,
>;
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
@ -110,15 +118,15 @@ fn oauth_client() -> Result<BasicClient, AppError> {
let token_url = env::var("TOKEN_URL")
.unwrap_or_else(|_| "https://discord.com/api/oauth2/token".to_string());
Ok(BasicClient::new(
ClientId::new(client_id),
Some(ClientSecret::new(client_secret)),
AuthUrl::new(auth_url).context("failed to create new authorization server URL")?,
Some(TokenUrl::new(token_url).context("failed to create new token endpoint URL")?),
)
.set_redirect_uri(
RedirectUrl::new(redirect_url).context("failed to create new redirection URL")?,
))
Ok(oauth2::basic::BasicClient::new(ClientId::new(client_id))
.set_client_secret(ClientSecret::new(client_secret))
.set_auth_uri(
AuthUrl::new(auth_url).context("failed to create new authorization server URL")?,
)
.set_token_uri(TokenUrl::new(token_url).context("failed to create new token endpoint URL")?)
.set_redirect_uri(
RedirectUrl::new(redirect_url).context("failed to create new redirection URL")?,
))
}
// The user data we'll get back from Discord.
@ -265,7 +273,7 @@ async fn login_authorized(
// Get an auth token
let token = oauth_client
.exchange_code(AuthorizationCode::new(query.code.clone()))
.request_async(async_http_client)
.request_async(&reqwest::Client::new())
.await
.context("failed in sending request request to authorization server")?;