diff --git a/pubky-homeserver/src/config.rs b/pubky-homeserver/src/config.rs index 22686cf..8fea08b 100644 --- a/pubky-homeserver/src/config.rs +++ b/pubky-homeserver/src/config.rs @@ -1,7 +1,7 @@ //! Configuration for the server use anyhow::{anyhow, Result}; -use pkarr::{mainline::dht::DhtSettings, Keypair}; +use pkarr::Keypair; // use serde::{Deserialize, Serialize}; use std::{fmt::Debug, path::PathBuf, time::Duration}; diff --git a/pubky-homeserver/src/database.rs b/pubky-homeserver/src/database.rs index bbd21b8..f7174ee 100644 --- a/pubky-homeserver/src/database.rs +++ b/pubky-homeserver/src/database.rs @@ -2,18 +2,12 @@ use std::fs; use std::path::Path; -use bytes::Bytes; -use heed::{types::Str, Database, Env, EnvOpenOptions, RwTxn}; +use heed::{Env, EnvOpenOptions}; mod migrations; pub mod tables; -use pubky_common::crypto::Hasher; - -use tables::{entries::Entry, Tables, TABLES_COUNT}; - -use pkarr::PublicKey; -use tables::blobs::{BlobsTable, BLOBS_TABLE}; +use tables::{Tables, TABLES_COUNT}; #[derive(Debug, Clone)] pub struct DB { @@ -37,12 +31,11 @@ impl DB { #[cfg(test)] mod tests { + use bytes::Bytes; use pkarr::Keypair; use pubky_common::timestamp::Timestamp; - use crate::config::Config; - - use super::{Bytes, DB}; + use super::DB; #[tokio::test] async fn entries() { @@ -61,13 +54,15 @@ mod tests { let cloned_keypair = keypair.clone(); let done = tokio::task::spawn_blocking(move || { - cloned.put_entry(&cloned_keypair.public_key(), path, rx); + cloned + .put_entry(&cloned_keypair.public_key(), path, rx) + .unwrap(); }); - tx.send(vec![1, 2, 3, 4, 5].into()); + tx.send(vec![1, 2, 3, 4, 5].into()).unwrap(); drop(tx); - done.await; + done.await.unwrap(); let blob = db.get_blob(&keypair.public_key(), path).unwrap().unwrap(); diff --git a/pubky-homeserver/src/database/migrations.rs b/pubky-homeserver/src/database/migrations.rs index 32f2909..eb5a5f8 100644 --- a/pubky-homeserver/src/database/migrations.rs +++ b/pubky-homeserver/src/database/migrations.rs @@ -1,4 +1,4 @@ -use heed::{types::Str, Database, Env, RwTxn}; +use heed::Env; mod m0; @@ -7,7 +7,7 @@ use super::tables::Tables; pub fn run(env: &Env) -> anyhow::Result { let mut wtxn = env.write_txn()?; - m0::run(env, &mut wtxn); + m0::run(env, &mut wtxn)?; let tables = Tables::new(env, &mut wtxn)?; diff --git a/pubky-homeserver/src/database/migrations/m0.rs b/pubky-homeserver/src/database/migrations/m0.rs index d7dac79..a690049 100644 --- a/pubky-homeserver/src/database/migrations/m0.rs +++ b/pubky-homeserver/src/database/migrations/m0.rs @@ -1,4 +1,4 @@ -use heed::{types::Str, Database, Env, RwTxn}; +use heed::{Env, RwTxn}; use crate::database::tables::{blobs, entries, sessions, users}; diff --git a/pubky-homeserver/src/database/tables/blobs.rs b/pubky-homeserver/src/database/tables/blobs.rs index f65dbe9..1a02a09 100644 --- a/pubky-homeserver/src/database/tables/blobs.rs +++ b/pubky-homeserver/src/database/tables/blobs.rs @@ -1,9 +1,4 @@ -use std::{borrow::Cow, time::SystemTime}; - -use heed::{ - types::{Bytes, Str}, - BoxedError, BytesDecode, BytesEncode, Database, -}; +use heed::{types::Bytes, Database}; use pkarr::PublicKey; use crate::database::DB; @@ -36,7 +31,7 @@ impl DB { None }; - rtxn.commit(); + rtxn.commit()?; Ok(result) } diff --git a/pubky-homeserver/src/database/tables/entries.rs b/pubky-homeserver/src/database/tables/entries.rs index 6398dfa..22a3aa4 100644 --- a/pubky-homeserver/src/database/tables/entries.rs +++ b/pubky-homeserver/src/database/tables/entries.rs @@ -1,12 +1,11 @@ use pkarr::PublicKey; use postcard::{from_bytes, to_allocvec}; use serde::{Deserialize, Serialize}; -use std::{borrow::Cow, fmt::Result, time::SystemTime}; use tracing::{debug, instrument}; use heed::{ types::{Bytes, Str}, - BoxedError, BytesDecode, BytesEncode, Database, RoTxn, + Database, RoTxn, }; use pubky_common::{ @@ -53,7 +52,9 @@ impl DB { let key = format!("{public_key}/{path}"); - self.tables.entries.put(&mut wtxn, &key, &entry.serialize()); + self.tables + .entries + .put(&mut wtxn, &key, &entry.serialize())?; wtxn.commit()?; @@ -126,11 +127,11 @@ impl DB { .unwrap_or(next_threshold(path, "", false, reverse, shallow)); for _ in 0..limit { - if let Some((key, _)) = (if reverse { + if let Some((key, _)) = if reverse { self.tables.entries.get_lower_than(txn, &threshold)? } else { self.tables.entries.get_greater_than(txn, &threshold)? - }) { + } { if !key.starts_with(path) { break; } @@ -228,25 +229,12 @@ impl Entry { self } - pub fn set_content_type(&mut self, content_type: &str) -> &mut Self { - self.content_type = content_type.to_string(); - self - } - // === Getters === pub fn content_hash(&self) -> &[u8; 32] { &self.content_hash } - pub fn content_length(&self) -> usize { - self.content_length - } - - pub fn content_type(&self) -> &str { - &self.content_type - } - // === Public Method === pub fn serialize(&self) -> Vec { diff --git a/pubky-homeserver/src/database/tables/sessions.rs b/pubky-homeserver/src/database/tables/sessions.rs index db6c53d..4ecd228 100644 --- a/pubky-homeserver/src/database/tables/sessions.rs +++ b/pubky-homeserver/src/database/tables/sessions.rs @@ -1,12 +1,9 @@ -use std::{borrow::Cow, time::SystemTime}; - use heed::{ types::{Bytes, Str}, - BoxedError, BytesDecode, BytesEncode, Database, + Database, }; use pkarr::PublicKey; use pubky_common::session::Session; -use serde::Deserialize; use tower_cookies::Cookies; use crate::database::DB; @@ -21,9 +18,8 @@ impl DB { &mut self, cookies: Cookies, public_key: &PublicKey, - path: &str, ) -> anyhow::Result> { - if let Some(bytes) = self.get_session_bytes(cookies, public_key, path)? { + if let Some(bytes) = self.get_session_bytes(cookies, public_key)? { return Ok(Some(Session::deserialize(&bytes)?)); }; @@ -34,7 +30,6 @@ impl DB { &mut self, cookies: Cookies, public_key: &PublicKey, - path: &str, ) -> anyhow::Result>> { if let Some(cookie) = cookies.get(&public_key.to_string()) { let rtxn = self.env.read_txn()?; diff --git a/pubky-homeserver/src/database/tables/users.rs b/pubky-homeserver/src/database/tables/users.rs index 9666637..cf9b44e 100644 --- a/pubky-homeserver/src/database/tables/users.rs +++ b/pubky-homeserver/src/database/tables/users.rs @@ -1,14 +1,12 @@ -use std::{borrow::Cow, time::SystemTime}; +use std::borrow::Cow; use postcard::{from_bytes, to_allocvec}; -use pubky_common::timestamp::Timestamp; use serde::{Deserialize, Serialize}; -use heed::{types::Str, BoxedError, BytesDecode, BytesEncode, Database}; +use heed::{BoxedError, BytesDecode, BytesEncode, Database}; use pkarr::PublicKey; extern crate alloc; -use alloc::vec::Vec; /// PublicKey => User. pub type UsersTable = Database; diff --git a/pubky-homeserver/src/error.rs b/pubky-homeserver/src/error.rs index 57081ab..5fad2f0 100644 --- a/pubky-homeserver/src/error.rs +++ b/pubky-homeserver/src/error.rs @@ -6,7 +6,6 @@ use axum::{ response::IntoResponse, }; use pubky_common::auth::AuthnSignatureError; -use tracing::debug; pub type Result = core::result::Result; @@ -70,6 +69,22 @@ impl From for Error { } } +// === Pubky specific errors === + +impl From for Error { + fn from(error: AuthnSignatureError) -> Self { + Self::new(StatusCode::BAD_REQUEST, Some(error)) + } +} + +impl From for Error { + fn from(error: pkarr::Error) -> Self { + Self::new(StatusCode::BAD_REQUEST, Some(error)) + } +} + +// === INTERNAL_SERVER_ERROR === + impl From for Error { fn from(error: std::io::Error) -> Self { Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.into()) @@ -100,16 +115,8 @@ impl From for Error { } } -// === Pubky specific errors === - -impl From for Error { - fn from(error: AuthnSignatureError) -> Self { - Self::new(StatusCode::BAD_REQUEST, Some(error)) - } -} - -impl From for Error { - fn from(error: pkarr::Error) -> Self { - Self::new(StatusCode::BAD_REQUEST, Some(error)) +impl From> for Error { + fn from(error: flume::SendError) -> Self { + Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.into()) } } diff --git a/pubky-homeserver/src/extractors.rs b/pubky-homeserver/src/extractors.rs index b89911c..567ca6b 100644 --- a/pubky-homeserver/src/extractors.rs +++ b/pubky-homeserver/src/extractors.rs @@ -52,10 +52,6 @@ impl EntryPath { pub fn as_str(&self) -> &str { self.0.as_str() } - - pub fn as_bytes(&self) -> &[u8] { - self.0.as_bytes() - } } #[async_trait] diff --git a/pubky-homeserver/src/lib.rs b/pubky-homeserver/src/lib.rs index 51852ec..4a1253b 100644 --- a/pubky-homeserver/src/lib.rs +++ b/pubky-homeserver/src/lib.rs @@ -1,5 +1,3 @@ -#![allow(unused)] - pub mod config; mod database; mod error; diff --git a/pubky-homeserver/src/routes.rs b/pubky-homeserver/src/routes.rs index 4d057a3..35615fd 100644 --- a/pubky-homeserver/src/routes.rs +++ b/pubky-homeserver/src/routes.rs @@ -1,16 +1,10 @@ -use std::sync::Arc; - use axum::{ extract::DefaultBodyLimit, - http::Method, routing::{delete, get, post, put}, Router, }; use tower_cookies::CookieManagerLayer; -use tower_http::{ - cors::{self, CorsLayer}, - trace::TraceLayer, -}; +use tower_http::{cors::CorsLayer, trace::TraceLayer}; use crate::server::AppState; diff --git a/pubky-homeserver/src/routes/auth.rs b/pubky-homeserver/src/routes/auth.rs index 7feafbb..72246f9 100644 --- a/pubky-homeserver/src/routes/auth.rs +++ b/pubky-homeserver/src/routes/auth.rs @@ -1,21 +1,14 @@ use axum::{ debug_handler, - extract::{Request, State}, - http::{uri::Scheme, HeaderMap, StatusCode, Uri}, + extract::State, + http::{uri::Scheme, StatusCode, Uri}, response::IntoResponse, - Router, }; use axum_extra::{headers::UserAgent, TypedHeader}; use bytes::Bytes; -use heed::BytesEncode; -use postcard::to_allocvec; use tower_cookies::{cookie::SameSite, Cookie, Cookies}; -use pubky_common::{ - crypto::{random_bytes, random_hash}, - session::Session, - timestamp::Timestamp, -}; +use pubky_common::{crypto::random_bytes, session::Session, timestamp::Timestamp}; use crate::{ database::tables::{ @@ -43,7 +36,6 @@ pub async fn signup( pub async fn session( State(state): State, - TypedHeader(user_agent): TypedHeader, cookies: Cookies, pubky: Pubky, ) -> Result { diff --git a/pubky-homeserver/src/routes/pkarr.rs b/pubky-homeserver/src/routes/pkarr.rs index 977a129..9e40230 100644 --- a/pubky-homeserver/src/routes/pkarr.rs +++ b/pubky-homeserver/src/routes/pkarr.rs @@ -1,5 +1,3 @@ -use std::{collections::HashMap, sync::RwLock}; - use axum::{ body::{Body, Bytes}, extract::State, @@ -10,8 +8,7 @@ use axum::{ }; use futures_util::stream::StreamExt; -use pkarr::{PublicKey, SignedPacket}; -use tracing::debug; +use pkarr::SignedPacket; use crate::{ error::{Error, Result}, @@ -31,7 +28,7 @@ pub fn pkarr_router(state: AppState) -> Router { } pub async fn pkarr_put( - State(mut state): State, + State(state): State, pubky: Pubky, body: Body, ) -> Result { diff --git a/pubky-homeserver/src/routes/public.rs b/pubky-homeserver/src/routes/public.rs index 33a6da3..4dc1bf6 100644 --- a/pubky-homeserver/src/routes/public.rs +++ b/pubky-homeserver/src/routes/public.rs @@ -2,27 +2,15 @@ use std::collections::HashMap; use axum::{ body::{Body, Bytes}, - debug_handler, - extract::{Path, Query, State}, + extract::{Query, State}, http::{header, Response, StatusCode}, response::IntoResponse, - RequestExt, Router, }; -use axum_extra::body::AsyncReadBody; use futures_util::stream::StreamExt; use pkarr::PublicKey; -use serde::Deserialize; use tower_cookies::Cookies; -use tracing::debug; - -use pubky_common::crypto::Hasher; - use crate::{ - database::tables::{ - blobs::{BlobsTable, BLOBS_TABLE}, - entries::{EntriesTable, Entry, ENTRIES_TABLE}, - }, error::{Error, Result}, extractors::{EntryPath, Pubky}, server::AppState, @@ -33,7 +21,7 @@ pub async fn put( pubky: Pubky, path: EntryPath, cookies: Cookies, - mut body: Body, + body: Body, ) -> Result { let public_key = pubky.public_key().clone(); let path = path.as_str(); @@ -54,7 +42,7 @@ pub async fn put( // to stream this to filesystem, and keep track of any failed // writes to GC these files later. - state.db.put_entry(&public_key, &path, rx); + state.db.put_entry(&public_key, &path, rx)?; Ok(()) }); @@ -62,7 +50,7 @@ pub async fn put( while let Some(next) = stream.next().await { let chunk = next?; - tx.send(chunk); + tx.send(chunk)?; } drop(tx); @@ -79,7 +67,7 @@ pub async fn get( path: EntryPath, Query(params): Query>, ) -> Result { - verify(path.as_str()); + verify(path.as_str())?; let public_key = pubky.public_key(); let path = path.as_str(); @@ -127,7 +115,6 @@ pub async fn delete( pubky: Pubky, path: EntryPath, cookies: Cookies, - mut body: Body, ) -> Result { let public_key = pubky.public_key().clone(); let path = path.as_str(); @@ -151,13 +138,13 @@ fn authorize( state: &mut AppState, cookies: Cookies, public_key: &PublicKey, - path: &str, + _: &str, ) -> Result<()> { // TODO: can we move this logic to the extractor or a layer // to perform this validation? - let session = state + let _ = state .db - .get_session(cookies, public_key, path)? + .get_session(cookies, public_key)? .ok_or(Error::with_status(StatusCode::UNAUTHORIZED))?; Ok(()) diff --git a/pubky-homeserver/src/server.rs b/pubky-homeserver/src/server.rs index 2863317..3db7441 100644 --- a/pubky-homeserver/src/server.rs +++ b/pubky-homeserver/src/server.rs @@ -1,15 +1,13 @@ -use std::{ - collections::HashMap, future::IntoFuture, net::SocketAddr, num::NonZeroUsize, sync::Arc, -}; +use std::{future::IntoFuture, net::SocketAddr}; use anyhow::{Error, Result}; use pubky_common::auth::AuthnVerifier; -use tokio::{net::TcpListener, signal, sync::Mutex, task::JoinSet}; +use tokio::{net::TcpListener, signal, task::JoinSet}; use tracing::{debug, info, warn}; use pkarr::{ mainline::dht::{DhtSettings, Testnet}, - PkarrClient, PkarrClientAsync, PublicKey, Settings, SignedPacket, + PkarrClient, PkarrClientAsync, PublicKey, Settings, }; use crate::{config::Config, database::DB, pkarr::publish_server_packet}; diff --git a/pubky/src/lib.rs b/pubky/src/lib.rs index 241e3ed..2b6cf42 100644 --- a/pubky/src/lib.rs +++ b/pubky/src/lib.rs @@ -1,5 +1,3 @@ -#![allow(unused)] - mod error; mod shared; diff --git a/pubky/src/native.rs b/pubky/src/native.rs index 6f9f676..0ca2d7c 100644 --- a/pubky/src/native.rs +++ b/pubky/src/native.rs @@ -7,7 +7,7 @@ use ::pkarr::{ use bytes::Bytes; use pkarr::Keypair; use pubky_common::session::Session; -use reqwest::{Method, RequestBuilder, Response}; +use reqwest::{RequestBuilder, Response}; use url::Url; use crate::{ @@ -150,6 +150,6 @@ impl PubkyClient { self.http.request(method, url) } - pub(crate) fn store_session(&self, response: Response) {} - pub(crate) fn remove_session(&self, pubky: &PublicKey) {} + pub(crate) fn store_session(&self, _: Response) {} + pub(crate) fn remove_session(&self, _: &PublicKey) {} } diff --git a/pubky/src/shared/auth.rs b/pubky/src/shared/auth.rs index 0ee15a6..d4a6436 100644 --- a/pubky/src/shared/auth.rs +++ b/pubky/src/shared/auth.rs @@ -2,12 +2,8 @@ use reqwest::{Method, StatusCode}; use pkarr::{Keypair, PublicKey}; use pubky_common::{auth::AuthnSignature, session::Session}; -use url::Url; -use crate::{ - error::{Error, Result}, - PubkyClient, -}; +use crate::{error::Result, PubkyClient}; use super::pkarr::Endpoint; @@ -75,10 +71,7 @@ impl PubkyClient { /// Signout from a homeserver. pub(crate) async fn inner_signout(&self, pubky: &PublicKey) -> Result<()> { - let Endpoint { - public_key, - mut url, - } = self.resolve_pubky_homeserver(pubky).await?; + let Endpoint { mut url, .. } = self.resolve_pubky_homeserver(pubky).await?; url.set_path(&format!("/{}/session", pubky)); @@ -115,14 +108,11 @@ impl PubkyClient { #[cfg(test)] mod tests { - use std::time::Duration; - use crate::*; use pkarr::{mainline::Testnet, Keypair}; use pubky_common::session::Session; use pubky_homeserver::Homeserver; - use tokio::time::sleep; #[tokio::test] async fn basic_authn() { diff --git a/pubky/src/shared/list_builder.rs b/pubky/src/shared/list_builder.rs index 601f71d..0eaec77 100644 --- a/pubky/src/shared/list_builder.rs +++ b/pubky/src/shared/list_builder.rs @@ -1,4 +1,4 @@ -use reqwest::{Method, Response, StatusCode}; +use reqwest::Method; use url::Url; use crate::{error::Result, PubkyClient}; diff --git a/pubky/src/shared/pkarr.rs b/pubky/src/shared/pkarr.rs index f615de0..01cd0fb 100644 --- a/pubky/src/shared/pkarr.rs +++ b/pubky/src/shared/pkarr.rs @@ -1,4 +1,4 @@ -use url::{Origin, Url}; +use url::Url; use pkarr::{ dns::{rdata::SVCB, Packet}, @@ -128,7 +128,7 @@ impl PubkyClient { } if let Some(public_key) = homeserver_public_key { - let mut url = Url::parse(&format!( + let url = Url::parse(&format!( "{}://{}", if origin.starts_with("localhost") { "http" @@ -156,7 +156,7 @@ mod tests { use pkarr::{ dns::{rdata::SVCB, Packet}, - mainline::{dht::DhtSettings, Dht, Testnet}, + mainline::{dht::DhtSettings, Testnet}, Keypair, PkarrClient, Settings, SignedPacket, }; use pubky_homeserver::Homeserver; @@ -183,7 +183,7 @@ mod tests { let server_tld = server.public_key().to_string(); - let mut svcb = SVCB::new(0, server_tld.as_str().try_into().unwrap()); + let svcb = SVCB::new(0, server_tld.as_str().try_into().unwrap()); packet.answers.push(pkarr::dns::ResourceRecord::new( "pubky".try_into().unwrap(), diff --git a/pubky/src/shared/public.rs b/pubky/src/shared/public.rs index 82bfbd8..7dbb18e 100644 --- a/pubky/src/shared/public.rs +++ b/pubky/src/shared/public.rs @@ -1,7 +1,7 @@ use bytes::Bytes; use pkarr::PublicKey; -use reqwest::{Method, Response, StatusCode}; +use reqwest::{Method, StatusCode}; use url::Url; use crate::{ @@ -61,7 +61,7 @@ impl PubkyClient { } pub(crate) async fn pubky_to_http>(&self, url: T) -> Result { - let mut original_url: Url = url.try_into().map_err(|_| Error::InvalidUrl)?; + let original_url: Url = url.try_into().map_err(|_| Error::InvalidUrl)?; if original_url.scheme() != "pubky" { return Ok(original_url); @@ -92,21 +92,6 @@ impl PubkyClient { } } -fn normalize_path(path: &str) -> Result { - let mut path = path.to_string(); - - if path.starts_with('/') { - path = path[1..].to_string() - } - - // TODO: should we return error instead? - if path.ends_with('/') { - path = path[..path.len()].to_string() - } - - Ok(path) -} - #[cfg(test)] mod tests { @@ -177,7 +162,7 @@ mod tests { Err(Error::Reqwest(error)) => { assert!(error.status() == Some(StatusCode::UNAUTHORIZED)) } - error => { + _ => { panic!("expected error StatusCode::UNAUTHORIZED") } } @@ -200,7 +185,7 @@ mod tests { Err(Error::Reqwest(error)) => { assert!(error.status() == Some(StatusCode::UNAUTHORIZED)) } - error => { + _ => { panic!("expected error StatusCode::UNAUTHORIZED") } } diff --git a/pubky/src/shared/recovery_file.rs b/pubky/src/shared/recovery_file.rs index 5f500ff..9fff885 100644 --- a/pubky/src/shared/recovery_file.rs +++ b/pubky/src/shared/recovery_file.rs @@ -2,10 +2,7 @@ use argon2::Argon2; use pkarr::Keypair; use pubky_common::crypto::{decrypt, encrypt}; -use crate::{ - error::{Error, Result}, - PubkyClient, -}; +use crate::error::{Error, Result}; static SPEC_NAME: &str = "recovery"; static SPEC_LINE: &str = "pubky.org/recovery"; @@ -68,6 +65,8 @@ fn recovery_file_encryption_key_from_passphrase(passphrase: &str) -> Result<[u8; mod tests { use super::*; + use crate::PubkyClient; + #[test] fn encrypt_decrypt_recovery_file() { let passphrase = "very secure password";