From c03321bc059ed733970d8a696acb461428b1d284 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sat, 10 Apr 2021 14:44:40 +0300 Subject: dunno --- src/auth.rs | 25 +++++++++++++++++++------ src/handlers.rs | 4 +++- src/routes.rs | 12 ++++-------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index e22262c..95f2378 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,12 +1,14 @@ use crate::error::Error; use crate::schema::{Db, Transaction}; +use anyhow::{anyhow, Context, Result}; use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; use serde::{Deserialize, Serialize}; +use thiserror::Error; use warp::header::headers_cloned; use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION}; -use warp::{reject, Filter, Rejection}; -use thiserror::Error; -use anyhow::*; +use warp::reject; +use warp::reject::Reject; +use warp::{Filter, Rejection}; const BEARER: &str = "Bearer "; const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY----- @@ -65,6 +67,18 @@ struct Claims { // found: String, // }, // } +// impl warp::reject::Reject for Nope {} + +#[derive(Debug)] +struct LessThanTenError {} + +impl Reject for LessThanTenError {} + +// impl From for Rejection { +// fn from(other: LessThanTenError) -> Self { +// warp::reject::custom(other) +// } +// } pub fn with_auth( db: Db, @@ -75,8 +89,6 @@ pub fn with_auth( .and_then(authorize) } -impl warp::reject::Reject for Nope {} - async fn authorize((db, headers): (Db, HeaderMap)) -> Result { match jwt_from_header(&headers) { Ok(jwt) => { @@ -92,7 +104,8 @@ async fn authorize((db, headers): (Db, HeaderMap)) -> Result return Err(anyhow!("missing!")); + Err(e) => return Err(anyhow!("missing!")), + // warp error } } diff --git a/src/handlers.rs b/src/handlers.rs index 256e72a..89905a3 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -61,10 +61,12 @@ 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: String, + header: HeaderMap, new_transaction: Transaction, db: Db, ) -> Result { + + // auth logic debug!("new transaction request {:?}", new_transaction); // let mut transactions = db.lock().await; diff --git a/src/routes.rs b/src/routes.rs index 499ba35..b48fdb2 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -47,14 +47,10 @@ pub fn authenticated_transaction_propose( warp::path("transaction") .and(warp::path::end()) .and(warp::post()) - .and(custom_filters::transaction_json_body()) - .map(|t: Transaction| { - with_auth(db.clone(), t) - }) - .untuple_one() - .and(custom_filters::transaction_json_body()) - .and(custom_filters::with_db(db)) - .and_then(handlers::propose_authenticated_transaction) + .and(custom_filters::transaction_json_body()) // returns transaction + .and(custom_filters::transaction_header()) // returns Transaction + .and(custom_filters::with_db(db)) // wraps db + .and_then(handlers::propose_authenticated_transaction) // uses db, transaction and authenticated // .and(custom_filters::transaction_json_body()) // // TODO: you might have to restore this -- cgit v1.2.3-70-g09d2