examples: Update to oauth2 5 (#3462)

This commit is contained in:
tottoto
2025-09-11 15:34:41 +02:00
committed by GitHub
parent 5d5ba01be9
commit e4550d23b1
4 changed files with 90 additions and 233 deletions
+1 -1
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"] }
+20 -12
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")?;