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/lib.rs | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 5fe3b18..e6bc24c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,8 @@ use std::sync::Arc; -use axum::extract::{FromRef, FromRequestParts}; -use axum_extra::extract::{cookie::Key, SignedCookieJar}; +use axum::extract::{FromRef, FromRequestParts, OptionalFromRequestParts}; +use axum_extra::extract::{cookie::{Cookie, Key}, SignedCookieJar}; use database::{FileStorage, PostgresStorage, Storage}; use indieauth::backend::{AuthBackend, FileBackend as FileAuthBackend}; use kittybox_util::queue::JobQueue; @@ -64,36 +64,23 @@ impl axum::response::IntoResponse for NoSessionError { } } -#[async_trait::async_trait] -impl FromRequestParts for Session +impl OptionalFromRequestParts for Session where SessionStore: FromRef, Key: FromRef, S: Send + Sync, { - type Rejection = NoSessionError; + type Rejection = std::convert::Infallible; - async fn from_request_parts(parts: &mut axum::http::request::Parts, state: &S) -> Result { + async fn from_request_parts(parts: &mut axum::http::request::Parts, state: &S) -> Result, Self::Rejection> { let jar = SignedCookieJar::::from_request_parts(parts, state).await.unwrap(); let session_store = SessionStore::from_ref(state).read_owned().await; - tracing::debug!("Cookie jar: {:#?}", jar); - let cookie = match jar.get("session_id") { - Some(cookie) => { - tracing::debug!("Session ID cookie: {}", cookie); - cookie - }, - None => { return Err(NoSessionError) } - }; - - session_store.get( - &dbg!(cookie.value_trimmed()) - .parse() - .map_err(|err| { - tracing::error!("Error parsing cookie: {}", err); - NoSessionError - })? - ).cloned().ok_or(NoSessionError) + Ok(jar.get("session_id") + .as_ref() + .map(Cookie::value_trimmed) + .and_then(|id| uuid::Uuid::parse_str(id).ok()) + .and_then(|id| session_store.get(&id).cloned())) } } @@ -264,7 +251,7 @@ pub mod companion { axum::Router::new() .route( - "/:filename", + "/{filename}", axum::routing::get(map_to_static) .layer(Extension(resources)) ) @@ -309,7 +296,7 @@ St: Clone + Send + Sync + 'static .route("/.kittybox/health", get(health_check::)) .nest("/.kittybox/login", crate::login::router::()) .route( - "/.kittybox/static/:path", + "/.kittybox/static/{path}", axum::routing::get(crate::frontend::statics) ) .route("/.kittybox/coffee", get(teapot_route)) -- cgit 1.4.1