From 001a9ac21c29af53d31e9710d12e37d565880207 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sat, 10 Apr 2021 16:56:00 +0300 Subject: giving up with style --- src/auth.rs | 2 +- src/custom_filters.rs | 2 +- src/handlers.rs | 9 ++++----- src/routes.rs | 11 ++++++----- src/schema.rs | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/auth.rs b/src/auth.rs index 65d639b..51b2e6a 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -90,7 +90,7 @@ async fn authorize( let decoded = decode::( &jwt, // TODO: what key are we using here? pass db/pw store here to get the claimant's public key <10-04-21, yigit> // - &DecodingKey::from_rsa_pem(PUBLIC_KEY_PEM.as_bytes()).unwrap(), + &DecodingKey::from_rsa_pem(db.users.read().get(&source).unwrap().pubkey.as_bytes()).unwrap(), &Validation::new(Algorithm::HS512), ) .map_err(|_| reject::custom(Error::JWTTokenError)) diff --git a/src/custom_filters.rs b/src/custom_filters.rs index 8c36d02..1d65c69 100644 --- a/src/custom_filters.rs +++ b/src/custom_filters.rs @@ -12,7 +12,7 @@ pub fn with_db(db: Db) -> impl Filter + Clo // Accept only json encoded Transaction body and reject big payloads pub fn transaction_json_body() -> impl Filter + Clone { - warp::body::content_length_limit(1024 * 32).and(warp::body::json()) + warp::body::content_length_limit(1024 * 32).and(warp::filters::body::json()) } // Accept only json encoded Transaction body and reject big payloads diff --git a/src/handlers.rs b/src/handlers.rs index 0bcbd49..a9c0315 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,10 +1,11 @@ +use crate::auth::Pubkey; /// API handlers, the ends of each filter chain use log::debug; // this is more useful than debug! learn how to use this use parking_lot::RwLockUpgradableReadGuard; use std::convert::Infallible; -use warp::{http::StatusCode, reply}; +use warp::filters::BoxedFilter; use warp::reject::Rejection; -use crate::auth::Pubkey; +use warp::{http::StatusCode, reply}; use crate::schema::{Block, Db, Transaction}; @@ -63,11 +64,10 @@ pub async fn propose_transaction( /// POST /transaction, authenticated /// The transaction arrived in this method has been authored by the public key in the source pub async fn propose_authenticated_transaction( - pubkey: Pubkey, + pubkey: BoxedFilter<(Pubkey,)>, new_transaction: Transaction, db: Db, ) -> Result { - // auth logic debug!("new transaction request {:?}", new_transaction); @@ -79,7 +79,6 @@ pub async fn propose_authenticated_transaction( Ok(StatusCode::CREATED) } - /// POST /block /// Proposes a new block for the next round /// Can reject the block diff --git a/src/routes.rs b/src/routes.rs index 871fd9c..b389919 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -8,7 +8,8 @@ use crate::schema::{Db, Transaction}; /// Root, all routes combined pub fn consensus_routes(db: Db) -> impl Filter + Clone { transaction_list(db.clone()) - .or(transaction_propose(db.clone())) + // .or(transaction_propose(db.clone())) + .or(authenticated_transaction_propose(db.clone())) .or(block_propose(db.clone())) .or(block_list(db.clone())) } @@ -51,16 +52,16 @@ pub fn authenticated_transaction_propose( // // header/jwt logic to propose_transaction but that doesn't feel right either // // good luck <10-04-21, yigit> // + let db1 = db.clone(); warp::path("transaction") .and(warp::path::end()) .and(warp::post()) .and(custom_filters::transaction_json_body()) // returns transaction - .map(|t: Transaction| { - // what do these do? - with_auth(db.clone(), t) + .map(move |t: Transaction| { + with_auth(db1.clone(), t).boxed() }) .and(custom_filters::transaction_json_body()) // returns transaction - .and(custom_filters::with_db(db)) // wraps db + .and(custom_filters::with_db(db.clone())) // wraps db .and_then(handlers::propose_authenticated_transaction) // uses db, transaction and authenticated } diff --git a/src/schema.rs b/src/schema.rs index f680bbf..46d49cd 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -72,8 +72,8 @@ pub struct Block { #[derive(Serialize, Deserialize, Debug)] pub struct User { - username: String, - token: String + pub username: String, + pub pubkey: String } // TODO: write schema tests using the original repo <09-04-21, yigit> // -- cgit v1.2.3-70-g09d2