diff --git a/Cargo.toml b/Cargo.toml index ce5c39f..c60c23b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [ "common","homeserver"] +members = ["pubky-*"] # See: https://github.com/rust-lang/rust/issues/90148#issuecomment-949194352 resolver = "2" diff --git a/homeserver/src/routes.rs b/homeserver/src/routes.rs deleted file mode 100644 index 74bc0ee..0000000 --- a/homeserver/src/routes.rs +++ /dev/null @@ -1,7 +0,0 @@ -use axum::{routing::get, Router}; - -pub mod root; - -pub fn create_app() -> Router { - Router::new().route("/", get(root::handler)) -} diff --git a/common/Cargo.toml b/pubky-common/Cargo.toml similarity index 100% rename from common/Cargo.toml rename to pubky-common/Cargo.toml diff --git a/common/src/auth.rs b/pubky-common/src/auth.rs similarity index 95% rename from common/src/auth.rs rename to pubky-common/src/auth.rs index 6469d2e..a344be3 100644 --- a/common/src/auth.rs +++ b/pubky-common/src/auth.rs @@ -20,7 +20,7 @@ impl AuthnSignature { let time: u64 = Timestamp::now().into(); let time_step = time / TIME_INTERVAL; - let token_hash = token.map_or(random_hash(), |t| crate::crypto::hash(t)); + let token_hash = token.map_or(random_hash(), crate::crypto::hash); let signature = signer .sign(&signable( @@ -80,7 +80,7 @@ impl AuthnVerifier { let past = now - TIME_INTERVAL; let future = now + TIME_INTERVAL; - let result = verify_at(now, &self, &signature, signer, &token_hash); + let result = verify_at(now, self, &signature, signer, &token_hash); match result { Ok(_) => return Ok(()), @@ -88,7 +88,7 @@ impl AuthnVerifier { _ => {} } - let result = verify_at(past, &self, &signature, signer, &token_hash); + let result = verify_at(past, self, &signature, signer, &token_hash); match result { Ok(_) => return Ok(()), @@ -96,7 +96,7 @@ impl AuthnVerifier { _ => {} } - verify_at(future, &self, &signature, signer, &token_hash) + verify_at(future, self, &signature, signer, &token_hash) } // === Private Methods === diff --git a/common/src/crypto.rs b/pubky-common/src/crypto.rs similarity index 100% rename from common/src/crypto.rs rename to pubky-common/src/crypto.rs diff --git a/common/src/lib.rs b/pubky-common/src/lib.rs similarity index 100% rename from common/src/lib.rs rename to pubky-common/src/lib.rs diff --git a/common/src/namespaces.rs b/pubky-common/src/namespaces.rs similarity index 100% rename from common/src/namespaces.rs rename to pubky-common/src/namespaces.rs diff --git a/common/src/timestamp.rs b/pubky-common/src/timestamp.rs similarity index 100% rename from common/src/timestamp.rs rename to pubky-common/src/timestamp.rs diff --git a/homeserver/Cargo.toml b/pubky-homeserver/Cargo.toml similarity index 95% rename from homeserver/Cargo.toml rename to pubky-homeserver/Cargo.toml index e4e2bf6..40cc287 100644 --- a/homeserver/Cargo.toml +++ b/pubky-homeserver/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] anyhow = "1.0.82" axum = "0.7.5" +pkarr = "2.0.3" tokio = { version = "1.37.0", features = ["full"] } tower-http = { version = "0.5.2", features = ["cors", "trace"] } tracing = "0.1.40" diff --git a/pubky-homeserver/src/extractors.rs b/pubky-homeserver/src/extractors.rs new file mode 100644 index 0000000..b637b97 --- /dev/null +++ b/pubky-homeserver/src/extractors.rs @@ -0,0 +1,43 @@ +use std::collections::HashMap; + +use axum::{ + async_trait, + extract::{FromRequestParts, Path}, + http::{request::Parts, StatusCode}, + response::{IntoResponse, Response}, + RequestPartsExt, +}; + +use pkarr::PublicKey; + +#[derive(Debug)] +pub struct Pubky(PublicKey); + +impl Pubky { + pub fn public_key(&self) -> &PublicKey { + &self.0 + } +} + +#[async_trait] +impl FromRequestParts for Pubky +where + S: Send + Sync, +{ + type Rejection = Response; + + async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + let params: Path> = + parts.extract().await.map_err(IntoResponse::into_response)?; + + let pubky_id = params + .get("pubky") + .ok_or_else(|| (StatusCode::NOT_FOUND, "Pubky not found").into_response())?; + + let public_key = PublicKey::try_from(pubky_id.to_string()) + // TODO: convert Pkarr errors to app errors, in this case a params validation error + .map_err(|_| (StatusCode::BAD_REQUEST, "Invalid Pubky").into_response())?; + + Ok(Pubky(public_key)) + } +} diff --git a/homeserver/src/lib.rs b/pubky-homeserver/src/lib.rs similarity index 76% rename from homeserver/src/lib.rs rename to pubky-homeserver/src/lib.rs index b4b9f85..726545f 100644 --- a/homeserver/src/lib.rs +++ b/pubky-homeserver/src/lib.rs @@ -1,3 +1,4 @@ +mod extractors; mod routes; mod server; diff --git a/homeserver/src/main.rs b/pubky-homeserver/src/main.rs similarity index 63% rename from homeserver/src/main.rs rename to pubky-homeserver/src/main.rs index ab284a8..7d8a5c1 100644 --- a/homeserver/src/main.rs +++ b/pubky-homeserver/src/main.rs @@ -3,7 +3,9 @@ use pubky_homeserver::Homeserver; #[tokio::main] async fn main() -> Result<()> { - tracing_subscriber::fmt().init(); + tracing_subscriber::fmt() + .with_env_filter("pubky_homeserver=debug,tower_http=debug") + .init(); let server = Homeserver::start().await?; diff --git a/pubky-homeserver/src/routes.rs b/pubky-homeserver/src/routes.rs new file mode 100644 index 0000000..ecd16e4 --- /dev/null +++ b/pubky-homeserver/src/routes.rs @@ -0,0 +1,12 @@ +use axum::{routing::get, Router}; +use tower_http::trace::TraceLayer; + +pub mod drive; +pub mod root; + +pub fn create_app() -> Router { + Router::new() + .route("/", get(root::handler)) + .route("/:pubky/*key", get(drive::put)) + .layer(TraceLayer::new_for_http()) +} diff --git a/pubky-homeserver/src/routes/drive.rs b/pubky-homeserver/src/routes/drive.rs new file mode 100644 index 0000000..3050250 --- /dev/null +++ b/pubky-homeserver/src/routes/drive.rs @@ -0,0 +1,11 @@ +use axum::response::IntoResponse; + +use tracing::debug; + +use crate::extractors::Pubky; + +pub async fn put(pubky: Pubky) -> Result { + debug!(pubky=?pubky.public_key()); + + Ok("Pubky drive...".to_string()) +} diff --git a/homeserver/src/routes/root.rs b/pubky-homeserver/src/routes/root.rs similarity index 100% rename from homeserver/src/routes/root.rs rename to pubky-homeserver/src/routes/root.rs diff --git a/homeserver/src/server.rs b/pubky-homeserver/src/server.rs similarity index 98% rename from homeserver/src/server.rs rename to pubky-homeserver/src/server.rs index 29d8d42..506d5ac 100644 --- a/homeserver/src/server.rs +++ b/pubky-homeserver/src/server.rs @@ -18,7 +18,7 @@ impl Homeserver { let app = app.clone(); let listener = TcpListener::bind(SocketAddr::from(( - [127, 0, 0, 1], + [0, 0, 0, 0], 6287, // config.port() ))) .await?;