about summary refs log tree commit diff
path: root/kittybox-rs/src
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2023-07-09 22:22:34 +0300
committerVika <vika@fireburn.ru>2023-07-17 01:53:42 +0300
commit99ff9cdc6890959cd4d2112c2e37b97ae83cb43c (patch)
tree31cdaf82de9d95ad4ee857f9148ad442f67db23d /kittybox-rs/src
parentdc7dfc61ec839175ebb51fcbaef1156fea5fdcf4 (diff)
downloadkittybox-99ff9cdc6890959cd4d2112c2e37b97ae83cb43c.tar.zst
cargo update, part 2: Axum
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.
Diffstat (limited to 'kittybox-rs/src')
-rw-r--r--kittybox-rs/src/frontend/onboarding.rs4
-rw-r--r--kittybox-rs/src/indieauth/mod.rs37
-rw-r--r--kittybox-rs/src/media/mod.rs4
-rw-r--r--kittybox-rs/src/micropub/mod.rs10
-rw-r--r--kittybox-rs/src/webmentions/mod.rs2
5 files changed, 31 insertions, 26 deletions
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<D: Storage + 'static>(
 pub async fn post<D: Storage + 'static>(
     Extension(db): Extension<D>,
     Host(host): Host,
-    Json(data): Json<OnboardingData>,
     Extension(http): Extension<reqwest::Client>,
+    Json(data): Json<OnboardingData>,
 ) -> axum::response::Response {
     let user_uid = format!("https://{}/", host.as_str());
 
@@ -166,6 +166,6 @@ pub async fn post<D: Storage + 'static>(
 pub fn router<S: Storage + 'static>(database: S, http: reqwest::Client) -> axum::routing::MethodRouter {
     axum::routing::get(get)
         .post(post::<S>)
-        .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 <B: Send, A: AuthBackend> axum::extract::FromRequest<B> for User<A> {
+impl <S: Send + Sync, A: AuthBackend> axum::extract::FromRequestParts<S> for User<A> {
     type Rejection = IndieAuthResourceError;
 
-    async fn from_request(req: &mut axum::extract::RequestParts<B>) -> Result<Self, Self::Rejection> {
+    async fn from_request_parts(req: &mut axum::http::request::Parts, state: &S) -> Result<Self, Self::Rejection> {
         let TypedHeader(Authorization(token)) =
-            TypedHeader::<Authorization<Bearer>>::from_request(req)
+            TypedHeader::<Authorization<Bearer>>::from_request_parts(req, state)
             .await
             .map_err(|_| IndieAuthResourceError::Unauthorized)?;
 
-        let axum::Extension(auth) = axum::Extension::<A>::from_request(req)
+        let axum::Extension(auth) = axum::Extension::<A>::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<A: AuthBackend>(
 #[tracing::instrument(skip(backend, confirmation))]
 async fn authorization_endpoint_confirm<A: AuthBackend>(
     Host(host): Host,
-    Json(confirmation): Json<AuthorizationConfirmation>,
     Extension(backend): Extension<A>,
     cookies: CookieJar,
+    Json(confirmation): Json<AuthorizationConfirmation>,
 ) -> Response {
     tracing::debug!("Received authorization confirmation from user");
     #[cfg(feature = "webauthn")]
@@ -318,11 +318,12 @@ async fn authorization_endpoint_confirm<A: AuthBackend>(
         .into_response()
 }
 
+#[tracing::instrument(skip(backend, db))]
 async fn authorization_endpoint_post<A: AuthBackend, D: Storage + 'static>(
     Host(host): Host,
-    Form(grant): Form<GrantRequest>,
     Extension(backend): Extension<A>,
-    Extension(db): Extension<D>
+    Extension(db): Extension<D>,
+    Form(grant): Form<GrantRequest>,
 ) -> Response {
     match grant {
         GrantRequest::AuthorizationCode {
@@ -373,9 +374,9 @@ async fn authorization_endpoint_post<A: AuthBackend, D: Storage + 'static>(
                     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<A: AuthBackend, D: Storage + 'static>(
                         .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<A: AuthBackend, D: Storage + 'static>(
 #[tracing::instrument(skip(backend, db))]
 async fn token_endpoint_post<A: AuthBackend, D: Storage + 'static>(
     Host(host): Host,
-    Form(grant): Form<GrantRequest>,
     Extension(backend): Extension<A>,
-    Extension(db): Extension<D>
+    Extension(db): Extension<D>,
+    Form(grant): Form<GrantRequest>,
 ) -> 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<A: AuthBackend, D: Storage + 'static>(
     }
 }
 
+#[tracing::instrument(skip(backend, token_request))]
 async fn introspection_endpoint_post<A: AuthBackend>(
     Host(host): Host,
-    Form(token_request): Form<TokenIntrospectionRequest>,
     TypedHeader(Authorization(auth_token)): TypedHeader<Authorization<Bearer>>,
-    Extension(backend): Extension<A>
+    Extension(backend): Extension<A>,
+    Form(token_request): Form<TokenIntrospectionRequest>,
 ) -> Response {
     use serde_json::json;
 
@@ -693,8 +698,8 @@ async fn introspection_endpoint_post<A: AuthBackend>(
 
 async fn revocation_endpoint_post<A: AuthBackend>(
     Host(host): Host,
+    Extension(backend): Extension<A>,
     Form(revocation): Form<TokenRevocationRequest>,
-    Extension(backend): Extension<A>
 ) -> 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<MediaStoreError> for MicropubError {
 
 #[tracing::instrument(skip(blobstore))]
 pub(crate) async fn upload<S: MediaStore, A: AuthBackend>(
-    mut upload: Multipart,
     Extension(blobstore): Extension<S>,
-    user: User<A>
+    user: User<A>,
+    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<D: Storage + 'static, A: AuthBackend>(
     Extension(db): Extension<D>,
     Extension(http): Extension<reqwest::Client>,
+    TypedHeader(content_type): TypedHeader<ContentType>,
     user: User<A>,
     body: BodyStream,
-    TypedHeader(content_type): TypedHeader<ContentType>,
 ) -> 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::<S, A>)
         .post(post::<S, A>)
-        .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<Q: JobQueue<Webmention>>(
+    Extension(queue): Extension<Q>,
     Form(webmention): Form<Webmention>,
-    Extension(queue): Extension<Q>
 ) -> Response {
     if let Err(err) = webmention.source.parse::<url::Url>() {
         return (StatusCode::BAD_REQUEST, err.to_string()).into_response()