From 99ff9cdc6890959cd4d2112c2e37b97ae83cb43c Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 9 Jul 2023 22:22:34 +0300 Subject: cargo update, part 2: Axum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Axum got some breaking changes and gained some nice features — however, features come later, breaking changes come first. Perhaps it would be nice to actually construct a State with all of my stuff, and then make functions generic over that. Could reduce the amount of generic stuff I am producing... maybe. --- kittybox-rs/src/frontend/onboarding.rs | 4 ++-- kittybox-rs/src/indieauth/mod.rs | 37 +++++++++++++++++++--------------- kittybox-rs/src/media/mod.rs | 4 ++-- kittybox-rs/src/micropub/mod.rs | 10 ++++----- kittybox-rs/src/webmentions/mod.rs | 2 +- 5 files changed, 31 insertions(+), 26 deletions(-) (limited to 'kittybox-rs/src') diff --git a/kittybox-rs/src/frontend/onboarding.rs b/kittybox-rs/src/frontend/onboarding.rs index 4b62d87..d5cde02 100644 --- a/kittybox-rs/src/frontend/onboarding.rs +++ b/kittybox-rs/src/frontend/onboarding.rs @@ -129,8 +129,8 @@ async fn onboard( pub async fn post( Extension(db): Extension, Host(host): Host, - Json(data): Json, Extension(http): Extension, + Json(data): Json, ) -> axum::response::Response { let user_uid = format!("https://{}/", host.as_str()); @@ -166,6 +166,6 @@ pub async fn post( pub fn router(database: S, http: reqwest::Client) -> axum::routing::MethodRouter { axum::routing::get(get) .post(post::) - .layer(axum::Extension(database)) + .layer::<_, _, std::convert::Infallible>(axum::Extension(database)) .layer(axum::Extension(http)) } diff --git a/kittybox-rs/src/indieauth/mod.rs b/kittybox-rs/src/indieauth/mod.rs index a86100d..0ad2702 100644 --- a/kittybox-rs/src/indieauth/mod.rs +++ b/kittybox-rs/src/indieauth/mod.rs @@ -73,20 +73,20 @@ impl axum::response::IntoResponse for IndieAuthResourceError { } #[async_trait::async_trait] -impl axum::extract::FromRequest for User { +impl axum::extract::FromRequestParts for User { type Rejection = IndieAuthResourceError; - async fn from_request(req: &mut axum::extract::RequestParts) -> Result { + async fn from_request_parts(req: &mut axum::http::request::Parts, state: &S) -> Result { let TypedHeader(Authorization(token)) = - TypedHeader::>::from_request(req) + TypedHeader::>::from_request_parts(req, state) .await .map_err(|_| IndieAuthResourceError::Unauthorized)?; - let axum::Extension(auth) = axum::Extension::::from_request(req) + let axum::Extension(auth) = axum::Extension::::from_request_parts(req, state) .await .unwrap(); - let Host(host) = Host::from_request(req) + let Host(host) = Host::from_request_parts(req, state) .await .map_err(|_| IndieAuthResourceError::InvalidRequest)?; @@ -253,9 +253,9 @@ async fn verify_credential( #[tracing::instrument(skip(backend, confirmation))] async fn authorization_endpoint_confirm( Host(host): Host, - Json(confirmation): Json, Extension(backend): Extension, cookies: CookieJar, + Json(confirmation): Json, ) -> Response { tracing::debug!("Received authorization confirmation from user"); #[cfg(feature = "webauthn")] @@ -318,11 +318,12 @@ async fn authorization_endpoint_confirm( .into_response() } +#[tracing::instrument(skip(backend, db))] async fn authorization_endpoint_post( Host(host): Host, - Form(grant): Form, Extension(backend): Extension, - Extension(db): Extension + Extension(db): Extension, + Form(grant): Form, ) -> Response { match grant { GrantRequest::AuthorizationCode { @@ -373,9 +374,9 @@ async fn authorization_endpoint_post( error_uri: None }.into_response() } - let profile = if dbg!(request.scope.as_ref() + let profile = if request.scope.as_ref() .map(|s| s.has(&Scope::Profile)) - .unwrap_or_default()) + .unwrap_or_default() { match get_profile( db, @@ -384,7 +385,10 @@ async fn authorization_endpoint_post( .map(|s| s.has(&Scope::Email)) .unwrap_or_default() ).await { - Ok(profile) => dbg!(profile), + Ok(profile) => { + tracing::debug!("Retrieved profile: {:?}", profile); + profile + }, Err(err) => { tracing::error!("Error retrieving profile from database: {}", err); @@ -408,9 +412,9 @@ async fn authorization_endpoint_post( #[tracing::instrument(skip(backend, db))] async fn token_endpoint_post( Host(host): Host, - Form(grant): Form, Extension(backend): Extension, - Extension(db): Extension + Extension(db): Extension, + Form(grant): Form, ) -> Response { #[inline] fn prepare_access_token(me: url::Url, client_id: url::Url, scope: Scopes) -> TokenData { @@ -655,11 +659,12 @@ async fn token_endpoint_post( } } +#[tracing::instrument(skip(backend, token_request))] async fn introspection_endpoint_post( Host(host): Host, - Form(token_request): Form, TypedHeader(Authorization(auth_token)): TypedHeader>, - Extension(backend): Extension + Extension(backend): Extension, + Form(token_request): Form, ) -> Response { use serde_json::json; @@ -693,8 +698,8 @@ async fn introspection_endpoint_post( async fn revocation_endpoint_post( Host(host): Host, + Extension(backend): Extension, Form(revocation): Form, - Extension(backend): Extension ) -> impl IntoResponse { let me: url::Url = format!("https://{}/", host).parse().unwrap(); diff --git a/kittybox-rs/src/media/mod.rs b/kittybox-rs/src/media/mod.rs index 297184a..71f875e 100644 --- a/kittybox-rs/src/media/mod.rs +++ b/kittybox-rs/src/media/mod.rs @@ -25,9 +25,9 @@ impl From for MicropubError { #[tracing::instrument(skip(blobstore))] pub(crate) async fn upload( - mut upload: Multipart, Extension(blobstore): Extension, - user: User + user: User, + mut upload: Multipart ) -> Response { if !user.check_scope(&Scope::Media) { return MicropubError { diff --git a/kittybox-rs/src/micropub/mod.rs b/kittybox-rs/src/micropub/mod.rs index 9351603..04bf0a5 100644 --- a/kittybox-rs/src/micropub/mod.rs +++ b/kittybox-rs/src/micropub/mod.rs @@ -497,9 +497,9 @@ async fn dispatch_body( pub(crate) async fn post( Extension(db): Extension, Extension(http): Extension, + TypedHeader(content_type): TypedHeader, user: User, body: BodyStream, - TypedHeader(content_type): TypedHeader, ) -> axum::response::Response { match dispatch_body(body, content_type).await { Ok(PostBody::Action(action)) => match post_action(action, db, user).await { @@ -639,15 +639,15 @@ where { axum::routing::get(query::) .post(post::) - .layer(tower_http::cors::CorsLayer::new() + .layer::<_, _, std::convert::Infallible>(tower_http::cors::CorsLayer::new() .allow_methods([ axum::http::Method::GET, axum::http::Method::POST, ]) .allow_origin(tower_http::cors::Any)) - .layer(axum::Extension(storage)) - .layer(axum::Extension(http)) - .layer(axum::Extension(auth)) + .layer::<_, _, std::convert::Infallible>(axum::Extension(storage)) + .layer::<_, _, std::convert::Infallible>(axum::Extension(http)) + .layer::<_, _, std::convert::Infallible>(axum::Extension(auth)) } #[cfg(test)] diff --git a/kittybox-rs/src/webmentions/mod.rs b/kittybox-rs/src/webmentions/mod.rs index 630a1a6..becddbc 100644 --- a/kittybox-rs/src/webmentions/mod.rs +++ b/kittybox-rs/src/webmentions/mod.rs @@ -18,8 +18,8 @@ impl queue::PostgresJobItem for Webmention { } async fn accept_webmention>( + Extension(queue): Extension, Form(webmention): Form, - Extension(queue): Extension ) -> Response { if let Err(err) = webmention.source.parse::() { return (StatusCode::BAD_REQUEST, err.to_string()).into_response() -- cgit 1.4.1