Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Apr 6, 2024
1 parent b0b4e6f commit 2b24e78
Show file tree
Hide file tree
Showing 24 changed files with 72 additions and 113 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/kitsune-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ license.workspace = true
[dependencies]
axum-core = "0.4.3"
eyre = "0.6.12"
garde = { version = "0.18.0", default-features = false }
http = "1.1.0"
simd-json = "0.13.9"
tracing = "0.1.40"

[lints]
Expand Down
9 changes: 9 additions & 0 deletions crates/kitsune-error/src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ impl IntoResponse for Error {
fn into_response(self) -> Response {
debug!(error = ?self.inner);

if let Some(garde_report) = self.inner.downcast_ref::<garde::Report>() {
let body = match simd_json::to_string(&garde_report) {
Ok(body) => body,
Err(error) => return Error::from(error).into_response(),
};

return to_response(StatusCode::BAD_REQUEST, Some(body));
}

match self.ty {
ErrorType::BadRequest(maybe_body) => to_response(StatusCode::BAD_REQUEST, maybe_body),
ErrorType::Forbidden(maybe_body) => to_response(StatusCode::FORBIDDEN, maybe_body),
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
#[macro_export]
macro_rules! bail {
($(type = $type:expr,)? $msg:expr) => {
return Err($crate::kitsune_error!($(type = $type,)? $msg));
return Err($crate::kitsune_error!($(type = $type,)? $msg).into());
};
}

Expand Down
1 change: 1 addition & 0 deletions crates/kitsune-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ redis = { version = "0.25.3", default-features = false, features = [
rsa = "0.9.6"
rusty-s3 = { version = "0.5.0", default-features = false }
serde = "1.0.197"
simd-json = "0.13.9"
smol_str = "0.2.1"
speedy-uuid = { path = "../../lib/speedy-uuid" }
tokio = { version = "1.37.0", features = ["macros", "sync"] }
Expand Down
20 changes: 5 additions & 15 deletions kitsune/src/http/extractor/signed_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use http::StatusCode;
use http_body_util::BodyExt;
use kitsune_core::{error::HttpError, traits::fetcher::AccountFetchOptions};
use kitsune_db::{model::account::Account, schema::accounts, with_connection, PgPool};
use kitsune_error::{Error, Result};
use kitsune_error::{bail, Error, ErrorType, Result};
use kitsune_type::ap::Activity;
use kitsune_wasm_mrf::Outcome;
use scoped_futures::ScopedFutureExt;
Expand Down Expand Up @@ -77,13 +77,8 @@ impl FromRequest<Zustand, Body> for SignedActivity {
};

let ap_id = activity.actor.as_str();
let Some(remote_user) = state
.fetcher
.fetch_account(ap_id.into())
.await
.map_err(Error::Fetcher)?
else {
return Err(Error::CoreHttp(HttpError::BadRequest).into());
let Some(remote_user) = state.fetcher.fetch_account(ap_id.into()).await? else {
bail!(type = ErrorType::BadRequest(None), "failed to fetch remote account");
};

let request = http::Request::from_parts(parts, ());
Expand All @@ -94,13 +89,8 @@ impl FromRequest<Zustand, Body> for SignedActivity {
.url(ap_id)
.build();

let Some(remote_user) = state
.fetcher
.fetch_account(opts)
.await
.map_err(Error::Fetcher)?
else {
return Err(Error::CoreHttp(HttpError::BadRequest).into());
let Some(remote_user) = state.fetcher.fetch_account(opts).await? else {
bail!(type = ErrorType::BadRequest(None), "failed to fetch remote account");
};

if !verify_signature(&request, &state.db_pool, Some(&remote_user)).await? {
Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/confirm_account.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{error::Result, state::Zustand};
use crate::state::Zustand;
use axum::{
debug_handler,
extract::{Path, State},
routing, Router,
};
use kitsune_email::MailingService;
use kitsune_error::Result;
use serde::Deserialize;

#[derive(Deserialize)]
Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/custom_emojis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{error::Result, http::responder::ActivityPubJson, state::Zustand};
use crate::{http::responder::ActivityPubJson, state::Zustand};
use axum::{debug_handler, extract::Path, extract::State, routing, Router};
use kitsune_activitypub::mapping::IntoObject;
use kitsune_error::Result;
use kitsune_service::custom_emoji::CustomEmojiService;
use kitsune_type::ap::emoji::Emoji;
use speedy_uuid::Uuid;
Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/media.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{error::Result, state::Zustand};
use crate::state::Zustand;
use axum::{
body::Body,
extract::{Path, State},
response::{IntoResponse, Response},
routing, Router,
};
use http::header::CONTENT_TYPE;
use kitsune_error::Result;
use kitsune_service::attachment::AttachmentService;
use speedy_uuid::Uuid;

Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/nodeinfo/two_one.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{error::Result, state::Zustand};
use crate::state::Zustand;
use axum::{debug_handler, extract::State, routing, Json, Router};
use diesel::{ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
Expand All @@ -7,6 +7,7 @@ use kitsune_db::{
schema::{posts, users},
with_connection, PgPool,
};
use kitsune_error::Result;
use kitsune_service::user::UserService;
use kitsune_type::nodeinfo::two_one::{
Protocol, Services, Software, TwoOne, Usage, UsageUsers, Version,
Expand Down
2 changes: 1 addition & 1 deletion kitsune/src/http/handler/oauth/authorize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::{Error, Result};
use crate::oauth2::{OAuthEndpoint, OAuthOwnerSolicitor};
use argon2::{Argon2, PasswordHash, PasswordVerifier};
use askama::Template;
Expand All @@ -21,6 +20,7 @@ use diesel::{ExpressionMethods, OptionalExtension, QueryDsl};
use diesel_async::RunQueryDsl;
use kitsune_db::with_connection;
use kitsune_db::{model::user::User, schema::users, PgPool};
use kitsune_error::{Error, Result};
use oxide_auth_async::endpoint::authorization::AuthorizationFlow;
use oxide_auth_axum::{OAuthRequest, OAuthResponse};
use serde::Deserialize;
Expand Down
8 changes: 3 additions & 5 deletions kitsune/src/http/handler/oauth/token.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
error::{Error, OAuth2Error, Result},
oauth2::OAuthEndpoint,
};
use crate::oauth2::OAuthEndpoint;
use axum::{debug_handler, extract::State};
use kitsune_error::{kitsune_error, Error, ErrorType, Result};
use oxide_auth::endpoint::QueryParameter;
use oxide_auth_async::endpoint::{
access_token::AccessTokenFlow, client_credentials::ClientCredentialsFlow, refresh::RefreshFlow,
Expand All @@ -18,7 +16,7 @@ pub async fn post(
let grant_type = oauth_req
.body()
.and_then(|body| body.unique_value("grant_type"))
.ok_or(OAuth2Error::MissingGrantType)?;
.ok_or_else(|| kitsune_error!(type = ErrorType::BadRequest(None), "missing grant type"))?;

match grant_type.as_ref() {
"authorization_code" => {
Expand Down
8 changes: 3 additions & 5 deletions kitsune/src/http/handler/oidc/callback.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
error::{OAuth2Error, Result},
oauth2::{AuthorisationCode, OAuth2Service},
};
use crate::oauth2::{AuthorisationCode, OAuth2Service};
use axum::{
extract::{Query, State},
response::Response,
Expand All @@ -13,6 +10,7 @@ use kitsune_db::{
schema::{oauth2_applications, users},
with_connection, PgPool,
};
use kitsune_error::Result;
use kitsune_oidc::OidcService;
use kitsune_service::user::{Register, UserService};
use serde::Deserialize;
Expand Down Expand Up @@ -67,7 +65,7 @@ pub async fn get(
.application(application)
.state(user_info.oauth2.state)
.user_id(user.id)
.scopes(user_info.oauth2.scope.parse().map_err(OAuth2Error::from)?)
.scopes(user_info.oauth2.scope.parse()?)
.build();

oauth_service
Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/posts/activity.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{error::Result, http::responder::ActivityPubJson, state::Zustand};
use crate::{http::responder::ActivityPubJson, state::Zustand};
use axum::{
debug_handler,
extract::{Path, State},
};
use kitsune_activitypub::mapping::IntoActivity;
use kitsune_error::Result;
use kitsune_type::ap::Activity;
use speedy_uuid::Uuid;

Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/posts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{error::Result, http::responder::ActivityPubJson, state::Zustand};
use crate::{http::responder::ActivityPubJson, state::Zustand};
use axum::{debug_handler, extract::Path, extract::State, routing, Router};
use kitsune_activitypub::mapping::IntoObject;
use kitsune_error::Result;
use kitsune_service::post::PostService;
use kitsune_type::ap::Object;
use speedy_uuid::Uuid;
Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/users/followers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{error::Result, http::responder::ActivityPubJson, state::Zustand};
use crate::{http::responder::ActivityPubJson, state::Zustand};
use axum::extract::{OriginalUri, Path, State};
use diesel::{BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;
use kitsune_db::{
schema::{accounts, accounts_follows},
with_connection,
};
use kitsune_error::Result;
use kitsune_type::ap::{
ap_context,
collection::{Collection, CollectionType},
Expand Down
3 changes: 2 additions & 1 deletion kitsune/src/http/handler/users/following.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{error::Result, http::responder::ActivityPubJson, state::Zustand};
use crate::{http::responder::ActivityPubJson, state::Zustand};
use axum::extract::{OriginalUri, Path, State};
use diesel::{BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;
use kitsune_db::{
schema::{accounts, accounts_follows},
with_connection,
};
use kitsune_error::Result;
use kitsune_type::ap::{
ap_context,
collection::{Collection, CollectionType},
Expand Down
Loading

0 comments on commit 2b24e78

Please sign in to comment.