From d10710326da703f69eaa06723dc66e330fd32745 Mon Sep 17 00:00:00 2001 From: Vika Date: Wed, 1 Jan 2025 23:17:56 +0300 Subject: axum: 0.7.9 → 0.8.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some breaking changes. For better or for worse. The optional extractor breaking change is a double-edged sword, since not all extractors can be used with `Option` now, and you have to use `Result` even when you want to ignore an error coming from an extractor, such as `Query`. However, this allows catching errors on authorization extractors even in places where authorization is optional. Change-Id: I35f809d3adf27dbef0e7ee93dc1a7af178b7d014 --- src/indieauth/mod.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/indieauth/mod.rs') diff --git a/src/indieauth/mod.rs b/src/indieauth/mod.rs index 0ac3dfd..ab38715 100644 --- a/src/indieauth/mod.rs +++ b/src/indieauth/mod.rs @@ -3,10 +3,10 @@ use microformats::types::Class; use tracing::error; use serde::Deserialize; use axum::{ - extract::{Form, FromRef, Host, Json, Query, State}, http::StatusCode, response::{Html, IntoResponse, Response} + extract::{Form, FromRef, Json, Query, State}, http::StatusCode, response::{Html, IntoResponse, Response} }; #[cfg_attr(not(feature = "webauthn"), allow(unused_imports))] -use axum_extra::extract::cookie::{CookieJar, Cookie}; +use axum_extra::extract::{Host, cookie::{CookieJar, Cookie}}; use axum_extra::{headers::{authorization::Bearer, Authorization, ContentType, HeaderMapExt}, TypedHeader}; use crate::database::Storage; use kittybox_indieauth::{ @@ -65,7 +65,20 @@ impl axum::response::IntoResponse for IndieAuthResourceError { } } -#[async_trait::async_trait] +impl , St: Clone + Send + Sync + 'static> axum::extract::OptionalFromRequestParts for User { + type Rejection = >::Rejection; + + async fn from_request_parts(req: &mut axum::http::request::Parts, state: &St) -> Result, Self::Rejection> { + let res = >::from_request_parts(req, state).await; + + match res { + Ok(user) => Ok(Some(user)), + Err(IndieAuthResourceError::Unauthorized) => Ok(None), + Err(err) => Err(err), + } + } +} + impl , St: Clone + Send + Sync + 'static> axum::extract::FromRequestParts for User { type Rejection = IndieAuthResourceError; -- cgit 1.4.1