chore: rename crates to pubky-*

This commit is contained in:
nazeh
2024-07-15 07:11:33 +03:00
parent 3a0ad9b028
commit b780a57fe9
16 changed files with 77 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
[workspace]
members = [ "common","homeserver"]
members = ["pubky-*"]
# See: https://github.com/rust-lang/rust/issues/90148#issuecomment-949194352
resolver = "2"

View File

@@ -1,7 +0,0 @@
use axum::{routing::get, Router};
pub mod root;
pub fn create_app() -> Router {
Router::new().route("/", get(root::handler))
}

View File

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

View File

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

View File

@@ -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<S> FromRequestParts<S> for Pubky
where
S: Send + Sync,
{
type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
let params: Path<HashMap<String, String>> =
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))
}
}

View File

@@ -1,3 +1,4 @@
mod extractors;
mod routes;
mod server;

View File

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

View File

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

View File

@@ -0,0 +1,11 @@
use axum::response::IntoResponse;
use tracing::debug;
use crate::extractors::Pubky;
pub async fn put(pubky: Pubky) -> Result<impl IntoResponse, String> {
debug!(pubky=?pubky.public_key());
Ok("Pubky drive...".to_string())
}

View File

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