Merge pull request #26 from pubky/chore/cleanup-unused

Chore/cleanup unused
This commit is contained in:
Nuh
2024-08-20 10:22:48 +03:00
committed by GitHub
23 changed files with 78 additions and 166 deletions

View File

@@ -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};

View File

@@ -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();

View File

@@ -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<Tables> {
let mut wtxn = env.write_txn()?;
m0::run(env, &mut wtxn);
m0::run(env, &mut wtxn)?;
let tables = Tables::new(env, &mut wtxn)?;

View File

@@ -1,4 +1,4 @@
use heed::{types::Str, Database, Env, RwTxn};
use heed::{Env, RwTxn};
use crate::database::tables::{blobs, entries, sessions, users};

View File

@@ -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)
}

View File

@@ -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<u8> {

View File

@@ -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<Option<Session>> {
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<Option<Vec<u8>>> {
if let Some(cookie) = cookies.get(&public_key.to_string()) {
let rtxn = self.env.read_txn()?;

View File

@@ -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<PublicKeyCodec, User>;

View File

@@ -6,7 +6,6 @@ use axum::{
response::IntoResponse,
};
use pubky_common::auth::AuthnSignatureError;
use tracing::debug;
pub type Result<T, E = Error> = core::result::Result<T, E>;
@@ -70,6 +69,22 @@ impl From<PathRejection> for Error {
}
}
// === Pubky specific errors ===
impl From<AuthnSignatureError> for Error {
fn from(error: AuthnSignatureError) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
}
}
impl From<pkarr::Error> for Error {
fn from(error: pkarr::Error) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
}
}
// === INTERNAL_SERVER_ERROR ===
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.into())
@@ -100,16 +115,8 @@ impl From<axum::Error> for Error {
}
}
// === Pubky specific errors ===
impl From<AuthnSignatureError> for Error {
fn from(error: AuthnSignatureError) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
}
}
impl From<pkarr::Error> for Error {
fn from(error: pkarr::Error) -> Self {
Self::new(StatusCode::BAD_REQUEST, Some(error))
impl<T> From<flume::SendError<T>> for Error {
fn from(error: flume::SendError<T>) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.into())
}
}

View File

@@ -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]

View File

@@ -1,5 +1,3 @@
#![allow(unused)]
pub mod config;
mod database;
mod error;

View File

@@ -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;

View File

@@ -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<AppState>,
TypedHeader(user_agent): TypedHeader<UserAgent>,
cookies: Cookies,
pubky: Pubky,
) -> Result<impl IntoResponse> {

View File

@@ -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<AppState>,
State(state): State<AppState>,
pubky: Pubky,
body: Body,
) -> Result<impl IntoResponse> {

View File

@@ -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<impl IntoResponse> {
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<HashMap<String, String>>,
) -> Result<impl IntoResponse> {
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<impl IntoResponse> {
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(())

View File

@@ -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};

View File

@@ -1,5 +1,3 @@
#![allow(unused)]
mod error;
mod shared;

View File

@@ -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) {}
}

View File

@@ -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() {

View File

@@ -1,4 +1,4 @@
use reqwest::{Method, Response, StatusCode};
use reqwest::Method;
use url::Url;
use crate::{error::Result, PubkyClient};

View File

@@ -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(),

View File

@@ -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<T: TryInto<Url>>(&self, url: T) -> Result<Url> {
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<String> {
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")
}
}

View File

@@ -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";