Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Apr 1, 2024
1 parent bb58d52 commit 9b189fc
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 182 deletions.
140 changes: 0 additions & 140 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/kitsune-activitypub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ kitsune-wasm-mrf = { path = "../kitsune-wasm-mrf" }
mime = "0.3.17"
mime_guess = { version = "2.0.4", default-features = false }
rsa = "0.9.6"
scoped-futures = "0.1.3"
serde = "1.0.197"
sha2 = "0.10.8"
simd-json = "0.13.9"
Expand Down
7 changes: 6 additions & 1 deletion crates/kitsune-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ kitsune-language = { path = "../kitsune-language" }
kitsune-type = { path = "../kitsune-type" }
num-derive = "0.4.2"
num-traits = "0.2.18"
rustls = "0.23.4"
rustls = { version = "0.23.4", default-features = false, features = [
"logging",
"ring",
"std",
"tls12",
] }
rustls-native-certs = "0.7.0"
serde = { version = "1.0.197", features = ["derive"] }
simd-json = "0.13.9"
Expand Down
15 changes: 11 additions & 4 deletions crates/kitsune-db/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ macro_rules! with_connection {
}};
}

#[macro_export]
macro_rules! catch_error {
($($tt:tt)*) => {{
let result: ::std::result::Result<_, ::diesel_async::pooled_connection::bb8::RunError> = async {
Ok({ $($tt)* })
}.await;
result
}};
}

#[macro_export]
macro_rules! with_connection_panicky {
($pool:expr, $($other:tt)*) => {{
let result: ::std::result::Result<_, Box<dyn ::std::error::Error>> = async {
Ok($crate::with_connection!($pool, $($other)*))
}.await;
result.unwrap()
$crate::catch_error!($crate::with_connection!($pool, $($other)*)).unwrap()
}};
}

Expand Down
6 changes: 4 additions & 2 deletions kitsune/src/http/extractor/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use diesel_async::RunQueryDsl;
use headers::{authorization::Bearer, Authorization};
use http::request::Parts;
use kitsune_db::{
catch_error,
model::{account::Account, user::User},
schema::{accounts, oauth2_access_tokens, users},
with_connection,
Expand Down Expand Up @@ -62,13 +63,14 @@ impl<const ENFORCE_EXPIRATION: bool> FromRequestParts<Zustand>
.filter(oauth2_access_tokens::expires_at.gt(OffsetDateTime::now_utc()));
}

let (user, account) = with_connection!(state.db_pool, |db_conn| {
let (user, account) = catch_error!(with_connection!(state.db_pool, |db_conn| {
user_account_query
.select(<(User, Account)>::as_select())
.get_result(db_conn)
.await
.map_err(Error::from)
})?;
}))
.map_err(Error::from)??;

Ok(Self(UserData { account, user }))
}
Expand Down
8 changes: 4 additions & 4 deletions kitsune/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::Parser;
use color_eyre::eyre::{self, Context};
use kitsune::consts::STARTUP_FIGLET;
use kitsune_config::Configuration;
use kitsune_core::consts::VERSION;
use kitsune_job_runner::JobDispatcherState;
use miette::{Context, IntoDiagnostic};
use std::{env, path::PathBuf};

#[global_allocator]
Expand All @@ -18,7 +18,7 @@ struct Args {
config: PathBuf,
}

async fn boot() -> miette::Result<()> {
async fn boot() -> eyre::Result<()> {
println!("{STARTUP_FIGLET}");

let args = Args::parse();
Expand Down Expand Up @@ -64,8 +64,8 @@ async fn boot() -> miette::Result<()> {
Ok(())
}

fn main() -> miette::Result<()> {
miette::set_panic_hook();
fn main() -> eyre::Result<()> {
color_eyre::install()?;

let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
Expand Down
11 changes: 7 additions & 4 deletions kitsune/src/oauth2/authorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use async_trait::async_trait;
use diesel::{OptionalExtension, QueryDsl};
use diesel_async::RunQueryDsl;
use kitsune_db::{
catch_error,
model::oauth2,
schema::{oauth2_applications, oauth2_authorization_codes},
with_connection, PgPool,
Expand All @@ -25,7 +26,7 @@ impl Authorizer for OAuthAuthorizer {
let secret = generate_secret();
let expires_at = chrono_to_timestamp(grant.until);

with_connection!(self.db_pool, |db_conn| {
catch_error!(with_connection!(self.db_pool, |db_conn| {
diesel::insert_into(oauth2_authorization_codes::table)
.values(oauth2::NewAuthorizationCode {
code: secret.as_str(),
Expand All @@ -37,19 +38,21 @@ impl Authorizer for OAuthAuthorizer {
.returning(oauth2_authorization_codes::code)
.get_result(db_conn)
.await
})
}))
.map_err(|_| ())?
.map_err(|_| ())
}

async fn extract(&mut self, authorization_code: &str) -> Result<Option<Grant>, ()> {
let oauth_data = with_connection!(self.db_pool, |db_conn| {
let oauth_data = catch_error!(with_connection!(self.db_pool, |db_conn| {
oauth2_authorization_codes::table
.find(authorization_code)
.inner_join(oauth2_applications::table)
.first::<(oauth2::AuthorizationCode, oauth2::Application)>(db_conn)
.await
.optional()
})
}))
.map_err(|_| ())?
.map_err(|_| ())?;

let oauth_data = oauth_data.map(|(code, app)| {
Expand Down
Loading

0 comments on commit 9b189fc

Please sign in to comment.